Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 96 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| wpcom_tag_cloud_replace_core | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| WPCOM_Tag_Cloud_Widget | |
0.00% |
0 / 93 |
|
0.00% |
0 / 5 |
462 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| widget | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| update | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| form | |
0.00% |
0 / 49 |
|
0.00% |
0 / 1 |
72 | |||
| _get_current_taxonomy | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php // phpcs:ignore Squiz.Commenting.FileComment.Missing |
| 2 | |
| 3 | /** |
| 4 | * A WordPress.com Tag Cloud widget. |
| 5 | */ |
| 6 | class WPCOM_Tag_Cloud_Widget extends WP_Widget { |
| 7 | /** |
| 8 | * Sets up a new Tag Cloud widget instance. |
| 9 | * |
| 10 | * @since 2.8.0 |
| 11 | */ |
| 12 | public function __construct() { |
| 13 | $widget_ops = array( |
| 14 | 'description' => __( 'A cloud of your most used tags.', 'wpcomsh' ), |
| 15 | 'customize_selective_refresh' => true, |
| 16 | ); |
| 17 | parent::__construct( 'tag_cloud', __( 'Tag Cloud', 'wpcomsh' ), $widget_ops ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Outputs the content for the current Tag Cloud widget instance. |
| 22 | * |
| 23 | * @param array $args Display arguments including 'before_title', 'after_title', |
| 24 | * 'before_widget', and 'after_widget'. |
| 25 | * @param array $instance Settings for the current Tag Cloud widget instance. |
| 26 | * |
| 27 | * @since 2.8.0 |
| 28 | */ |
| 29 | public function widget( $args, $instance ) { |
| 30 | $current_taxonomy = $this->_get_current_taxonomy( $instance ); |
| 31 | |
| 32 | if ( ! empty( $instance['title'] ) ) { |
| 33 | $title = $instance['title']; |
| 34 | } elseif ( 'post_tag' === $current_taxonomy ) { |
| 35 | $title = __( 'Tags', 'wpcomsh' ); |
| 36 | } else { |
| 37 | $tax = get_taxonomy( $current_taxonomy ); |
| 38 | $title = $tax->labels->name; |
| 39 | } |
| 40 | |
| 41 | $show_count = ! empty( $instance['count'] ); |
| 42 | |
| 43 | $tag_cloud = wp_tag_cloud( |
| 44 | /** |
| 45 | * Filters the taxonomy used in the Tag Cloud widget. |
| 46 | * |
| 47 | * @param array $args Args used for the tag cloud widget. |
| 48 | * @param array $instance Array of settings for the current widget. |
| 49 | * |
| 50 | * @since 4.9.0 Added the `$instance` parameter. |
| 51 | * |
| 52 | * @see wp_tag_cloud() |
| 53 | * |
| 54 | * @since 2.8.0 |
| 55 | * @since 3.0.0 Added taxonomy drop-down. |
| 56 | */ |
| 57 | apply_filters( |
| 58 | 'widget_tag_cloud_args', |
| 59 | array( |
| 60 | 'taxonomy' => $current_taxonomy, |
| 61 | 'echo' => false, |
| 62 | 'number' => empty( $instance['max_tags'] ) ? 0 : $instance['max_tags'], |
| 63 | 'show_count' => $show_count, |
| 64 | ), |
| 65 | $instance |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | if ( empty( $tag_cloud ) ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
| 74 | $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 75 | |
| 76 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 77 | if ( $title ) { |
| 78 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 79 | } |
| 80 | |
| 81 | echo '<div class="tagcloud">'; |
| 82 | |
| 83 | echo $tag_cloud; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- as this is HTML generated by core. |
| 84 | |
| 85 | echo "</div>\n"; |
| 86 | echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Handles updating settings for the current Tag Cloud widget instance. |
| 91 | * |
| 92 | * @param array $new_instance New settings for this instance as input by the user via |
| 93 | * WP_Widget::form(). |
| 94 | * @param array $old_instance Old settings for this instance. |
| 95 | * |
| 96 | * @return array Settings to save or bool false to cancel saving. |
| 97 | * @since 2.8.0 |
| 98 | */ |
| 99 | public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 100 | $instance = array(); |
| 101 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 102 | $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0; |
| 103 | $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] ); |
| 104 | $instance['max_tags'] = empty( $new_instance['max_tags'] ) ? 0 : (int) $new_instance['max_tags']; |
| 105 | |
| 106 | return $instance; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Outputs the Tag Cloud widget settings form. |
| 111 | * |
| 112 | * @param array $instance Current settings. |
| 113 | * @return never |
| 114 | * |
| 115 | * @since 2.8.0 |
| 116 | */ |
| 117 | public function form( $instance ) { |
| 118 | $current_taxonomy = $this->_get_current_taxonomy( $instance ); |
| 119 | $title_id = $this->get_field_id( 'title' ); |
| 120 | $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false; |
| 121 | $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
| 122 | $max_tags = empty( $instance['max_tags'] ) ? 0 : $instance['max_tags']; |
| 123 | |
| 124 | echo '<p><label for="' . esc_attr( $title_id ) . '">' . esc_html__( 'Title:', 'wpcomsh' ) . '</label> |
| 125 | <input type="text" class="widefat" id="' . esc_attr( $title_id ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" value="' . esc_attr( $instance['title'] ) . '" /> |
| 126 | </p>'; |
| 127 | |
| 128 | $max_tags_id = $this->get_field_id( 'max_tags' ); |
| 129 | echo '<p><label for="' . esc_attr( $max_tags_id ) . '">' . esc_html__( 'Number of Tags:', 'wpcomsh' ) . '</label> |
| 130 | <input type="number" class="widefat" id="' . esc_attr( $max_tags_id ) . '" name="' . esc_attr( $this->get_field_name( 'max_tags' ) ) . '" value="' . esc_attr( $max_tags ) . '" /> |
| 131 | <small>' . esc_html__( 'Maximum number of tags displayed', 'wpcomsh' ) . '</small> |
| 132 | </p>'; |
| 133 | |
| 134 | $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' ); |
| 135 | $id = $this->get_field_id( 'taxonomy' ); |
| 136 | $name = $this->get_field_name( 'taxonomy' ); |
| 137 | |
| 138 | $count_checkbox = sprintf( |
| 139 | '<p><input type="checkbox" class="checkbox" id="%1$s" name="%2$s"%3$s /> <label for="%1$s">%4$s</label></p>', |
| 140 | esc_attr( $this->get_field_id( 'count' ) ), |
| 141 | esc_attr( $this->get_field_name( 'count' ) ), |
| 142 | checked( $count, true, false ), |
| 143 | esc_html__( 'Show tag counts', 'wpcomsh' ) |
| 144 | ); |
| 145 | |
| 146 | switch ( count( $taxonomies ) ) { |
| 147 | |
| 148 | // No tag cloud supporting taxonomies found, display error message. |
| 149 | case 0: |
| 150 | echo '<p>' . esc_html__( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.', 'wpcomsh' ) . '</p>'; |
| 151 | echo '<input type="hidden" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" value="" />'; |
| 152 | break; |
| 153 | |
| 154 | // Just a single tag cloud supporting taxonomy found, no need to display a select. |
| 155 | case 1: |
| 156 | $keys = array_keys( $taxonomies ); |
| 157 | $taxonomy = reset( $keys ); |
| 158 | echo '<input type="hidden" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" value="' . esc_attr( $taxonomy ) . '" />'; |
| 159 | echo $count_checkbox; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML is already escaped |
| 160 | break; |
| 161 | |
| 162 | // More than one tag cloud supporting taxonomy found, display a select. |
| 163 | default: |
| 164 | printf( |
| 165 | '<p><label for="%1$s">%2$s</label>' . |
| 166 | '<select class="widefat" id="%1$s" name="%3$s">', |
| 167 | esc_attr( $id ), |
| 168 | esc_html__( 'Taxonomy:', 'wpcomsh' ), |
| 169 | esc_attr( $name ) |
| 170 | ); |
| 171 | |
| 172 | foreach ( $taxonomies as $taxonomy => $tax ) { |
| 173 | printf( |
| 174 | '<option value="%s"%s>%s</option>', |
| 175 | esc_attr( $taxonomy ), |
| 176 | selected( $taxonomy, $current_taxonomy, false ), |
| 177 | esc_html( $tax->labels->name ) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | echo '</select></p>' . $count_checkbox; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML is already escaped |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Retrieves the taxonomy for the current Tag cloud widget instance. |
| 187 | * |
| 188 | * @param array $instance Current settings. |
| 189 | * |
| 190 | * @return string Name of the current taxonomy if set, otherwise 'post_tag'. |
| 191 | * @since 4.4.0 |
| 192 | */ |
| 193 | public function _get_current_taxonomy( $instance ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
| 194 | if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) { |
| 195 | return $instance['taxonomy']; |
| 196 | } |
| 197 | |
| 198 | return 'post_tag'; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Replace core Tag Cloud widget. |
| 204 | */ |
| 205 | function wpcom_tag_cloud_replace_core() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed |
| 206 | unregister_widget( 'WP_Widget_Tag_Cloud' ); |
| 207 | register_widget( 'WPCOM_Tag_Cloud_Widget' ); |
| 208 | } |
| 209 | add_action( 'widgets_init', 'wpcom_tag_cloud_replace_core', 11 ); |