~canonical-sysadmins/wordpress/5.1

« back to all changes in this revision

Viewing changes to wp-includes/widgets/class-wp-widget-tag-cloud.php

  • Committer: Barry Price
  • Date: 2019-02-22 03:51:26 UTC
  • mfrom: (1.2.12 upstream)
  • Revision ID: barry.price@canonical.com-20190222035126-o28k38qs8jfyjsxt
Merge WP5.1 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
         */
24
24
        public function __construct() {
25
25
                $widget_ops = array(
26
 
                        'description' => __( 'A cloud of your most used tags.' ),
 
26
                        'description'                 => __( 'A cloud of your most used tags.' ),
27
27
                        'customize_selective_refresh' => true,
28
28
                );
29
29
                parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
47
47
                        if ( 'post_tag' === $current_taxonomy ) {
48
48
                                $title = __( 'Tags' );
49
49
                        } else {
50
 
                                $tax = get_taxonomy( $current_taxonomy );
 
50
                                $tax   = get_taxonomy( $current_taxonomy );
51
51
                                $title = $tax->labels->name;
52
52
                        }
53
53
                }
66
66
                 * @param array $args     Args used for the tag cloud widget.
67
67
                 * @param array $instance Array of settings for the current widget.
68
68
                 */
69
 
                $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
70
 
                        'taxonomy'   => $current_taxonomy,
71
 
                        'echo'       => false,
72
 
                        'show_count' => $show_count,
73
 
                ), $instance ) );
 
69
                $tag_cloud = wp_tag_cloud(
 
70
                        apply_filters(
 
71
                                'widget_tag_cloud_args',
 
72
                                array(
 
73
                                        'taxonomy'   => $current_taxonomy,
 
74
                                        'echo'       => false,
 
75
                                        'show_count' => $show_count,
 
76
                                ),
 
77
                                $instance
 
78
                        )
 
79
                );
74
80
 
75
81
                if ( empty( $tag_cloud ) ) {
76
82
                        return;
103
109
         * @return array Settings to save or bool false to cancel saving.
104
110
         */
105
111
        public function update( $new_instance, $old_instance ) {
106
 
                $instance = array();
107
 
                $instance['title'] = sanitize_text_field( $new_instance['title'] );
108
 
                $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
109
 
                $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
 
112
                $instance             = array();
 
113
                $instance['title']    = sanitize_text_field( $new_instance['title'] );
 
114
                $instance['count']    = ! empty( $new_instance['count'] ) ? 1 : 0;
 
115
                $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
110
116
                return $instance;
111
117
        }
112
118
 
118
124
         * @param array $instance Current settings.
119
125
         */
120
126
        public function form( $instance ) {
121
 
                $current_taxonomy = $this->_get_current_taxonomy($instance);
122
 
                $title_id = $this->get_field_id( 'title' );
123
 
                $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 
127
                $current_taxonomy  = $this->_get_current_taxonomy( $instance );
 
128
                $title_id          = $this->get_field_id( 'title' );
 
129
                $count             = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
124
130
                $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
125
131
 
126
 
                echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label>
127
 
                        <input type="text" class="widefat" id="' . $title_id .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" />
 
132
                echo '<p><label for="' . $title_id . '">' . __( 'Title:' ) . '</label>
 
133
                        <input type="text" class="widefat" id="' . $title_id . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $instance['title'] . '" />
128
134
                </p>';
129
135
 
130
136
                $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
131
 
                $id = $this->get_field_id( 'taxonomy' );
132
 
                $name = $this->get_field_name( 'taxonomy' );
133
 
                $input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
 
137
                $id         = $this->get_field_id( 'taxonomy' );
 
138
                $name       = $this->get_field_name( 'taxonomy' );
 
139
                $input      = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
134
140
 
135
141
                $count_checkbox = sprintf(
136
142
                        '<p><input type="checkbox" class="checkbox" id="%1$s" name="%2$s"%3$s /> <label for="%1$s">%4$s</label></p>',
142
148
 
143
149
                switch ( count( $taxonomies ) ) {
144
150
 
145
 
                // No tag cloud supporting taxonomies found, display error message
146
 
                case 0:
147
 
                        echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
148
 
                        printf( $input, '' );
149
 
                        break;
150
 
 
151
 
                // Just a single tag cloud supporting taxonomy found, no need to display a select.
152
 
                case 1:
153
 
                        $keys = array_keys( $taxonomies );
154
 
                        $taxonomy = reset( $keys );
155
 
                        printf( $input, esc_attr( $taxonomy ) );
156
 
                        echo $count_checkbox;
157
 
                        break;
158
 
 
159
 
                // More than one tag cloud supporting taxonomy found, display a select.
160
 
                default:
161
 
                        printf(
162
 
                                '<p><label for="%1$s">%2$s</label>' .
163
 
                                '<select class="widefat" id="%1$s" name="%3$s">',
164
 
                                $id,
165
 
                                __( 'Taxonomy:' ),
166
 
                                $name
167
 
                        );
168
 
 
169
 
                        foreach ( $taxonomies as $taxonomy => $tax ) {
 
151
                        // No tag cloud supporting taxonomies found, display error message
 
152
                        case 0:
 
153
                                echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
 
154
                                printf( $input, '' );
 
155
                                break;
 
156
 
 
157
                        // Just a single tag cloud supporting taxonomy found, no need to display a select.
 
158
                        case 1:
 
159
                                $keys     = array_keys( $taxonomies );
 
160
                                $taxonomy = reset( $keys );
 
161
                                printf( $input, esc_attr( $taxonomy ) );
 
162
                                echo $count_checkbox;
 
163
                                break;
 
164
 
 
165
                        // More than one tag cloud supporting taxonomy found, display a select.
 
166
                        default:
170
167
                                printf(
171
 
                                        '<option value="%s"%s>%s</option>',
172
 
                                        esc_attr( $taxonomy ),
173
 
                                        selected( $taxonomy, $current_taxonomy, false ),
174
 
                                        $tax->labels->name
 
168
                                        '<p><label for="%1$s">%2$s</label>' .
 
169
                                        '<select class="widefat" id="%1$s" name="%3$s">',
 
170
                                        $id,
 
171
                                        __( 'Taxonomy:' ),
 
172
                                        $name
175
173
                                );
176
 
                        }
177
 
 
178
 
                        echo '</select></p>' . $count_checkbox;
 
174
 
 
175
                                foreach ( $taxonomies as $taxonomy => $tax ) {
 
176
                                        printf(
 
177
                                                '<option value="%s"%s>%s</option>',
 
178
                                                esc_attr( $taxonomy ),
 
179
                                                selected( $taxonomy, $current_taxonomy, false ),
 
180
                                                $tax->labels->name
 
181
                                        );
 
182
                                }
 
183
 
 
184
                                echo '</select></p>' . $count_checkbox;
179
185
                }
180
186
        }
181
187
 
187
193
         * @param array $instance Current settings.
188
194
         * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
189
195
         */
190
 
        public function _get_current_taxonomy($instance) {
191
 
                if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
 
196
        public function _get_current_taxonomy( $instance ) {
 
197
                if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
192
198
                        return $instance['taxonomy'];
 
199
                }
193
200
 
194
201
                return 'post_tag';
195
202
        }