diff --git src/wp-includes/category-template.php src/wp-includes/category-template.php
index b4ae6f447a..ce78471d21 100644
|
|
function wp_list_categories( $args = '' ) { |
659 | 659 | * be to return the top 45 tags in the tag cloud list. |
660 | 660 | * |
661 | 661 | * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the |
662 | | * text for the tooltip of the tag link. |
| 662 | * text for the tag link count. |
663 | 663 | * |
664 | 664 | * The 'topic_count_text_callback' argument is a function, which given the count |
665 | | * of the posts with that tag returns a text for the tooltip of the tag link. |
| 665 | * of the posts with that tag returns a text for the tag link count. |
666 | 666 | * |
667 | 667 | * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type |
668 | 668 | * passed to edit.php for the popular tags edit links. |
… |
… |
function wp_list_categories( $args = '' ) { |
671 | 671 | * should be used, because only one will be used and the other ignored, if they are both set. |
672 | 672 | * |
673 | 673 | * @since 2.3.0 |
| 674 | * @since 4.8.0 Added the `show_count` argument. |
674 | 675 | * |
675 | 676 | * @param array|string|null $args Optional. Override default arguments. |
676 | 677 | * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
… |
… |
function wp_tag_cloud( $args = '' ) { |
680 | 681 | $defaults = array( |
681 | 682 | 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, |
682 | 683 | 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
683 | | 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true |
| 684 | 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true, |
| 685 | 'show_count' => 0, |
684 | 686 | ); |
685 | 687 | $args = wp_parse_args( $args, $defaults ); |
686 | 688 | |
… |
… |
function default_topic_count_scale( $count ) { |
736 | 738 | * |
737 | 739 | * @todo Complete functionality. |
738 | 740 | * @since 2.3.0 |
| 741 | * @since 4.8.0 Added the `show_count` argument. |
739 | 742 | * |
740 | 743 | * @param array $tags List of tags. |
741 | 744 | * @param string|array $args { |
… |
… |
function default_topic_count_scale( $count ) { |
766 | 769 | * @type int|bool $filter Whether to enable filtering of the final output |
767 | 770 | * via {@see 'wp_generate_tag_cloud'}. Default 1|true. |
768 | 771 | * @type string $topic_count_text Nooped plural text from _n_noop() to supply to |
769 | | * tag tooltips. Default null. |
| 772 | * tag counts. Default null. |
770 | 773 | * @type callable $topic_count_text_callback Callback used to generate nooped plural text for |
771 | | * tag tooltips based on the count. Default null. |
| 774 | * tag counts based on the count. Default null. |
772 | 775 | * @type callable $topic_count_scale_callback Callback used to determine the tag count scaling |
773 | 776 | * value. Default default_topic_count_scale(). |
| 777 | * @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts |
| 778 | * 0, 1, or their bool equivalents. |
774 | 779 | * } |
775 | 780 | * @return string|array Tag cloud as a string or an array, depending on 'format' argument. |
776 | 781 | */ |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
780 | 785 | 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
781 | 786 | 'topic_count_text' => null, 'topic_count_text_callback' => null, |
782 | 787 | 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, |
| 788 | 'show_count' => 0, |
783 | 789 | ); |
784 | 790 | |
785 | 791 | $args = wp_parse_args( $args, $defaults ); |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
790 | 796 | return $return; |
791 | 797 | } |
792 | 798 | |
793 | | // Juggle topic count tooltips: |
| 799 | // Juggle topic counts. |
794 | 800 | if ( isset( $args['topic_count_text'] ) ) { |
795 | 801 | // First look for nooped plural support via topic_count_text. |
796 | 802 | $translate_nooped_plural = $args['topic_count_text']; |
797 | 803 | } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { |
798 | 804 | // Look for the alternative callback style. Ignore the previous default. |
799 | 805 | if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { |
800 | | $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); |
| 806 | $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
801 | 807 | } else { |
802 | 808 | $translate_nooped_plural = false; |
803 | 809 | } |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
806 | 812 | $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
807 | 813 | } else { |
808 | 814 | // This is the default for when no callback, plural, or argument is passed in. |
809 | | $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); |
| 815 | $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
810 | 816 | } |
811 | 817 | |
812 | 818 | /** |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
861 | 867 | $font_spread = 1; |
862 | 868 | $font_step = $font_spread / $spread; |
863 | 869 | |
| 870 | $aria_label = false; |
| 871 | /* |
| 872 | * Determine whether to output an 'aria-label' attribute with the tag name and count. |
| 873 | * When tags have a different font size, they visually convey an important information |
| 874 | * that should be available to assistive technologies too. On the other hand, sometimes |
| 875 | * themes set up the Tag Cloud to display all tags with the same font size (setting |
| 876 | * the 'smallest' and 'largest' arguments to the same value). |
| 877 | * In order to always serve the same content to all users, the 'aria-label' gets printed out: |
| 878 | * - when tags have a different size |
| 879 | * - when the tag count is displayed (for example when users check the checkbox in the |
| 880 | * Tag Cloud widget), regardless of the tags font size |
| 881 | */ |
| 882 | if ( $args['show_count'] || 0 !== $font_spread ) { |
| 883 | $aria_label = true; |
| 884 | } |
| 885 | |
864 | 886 | // Assemble the data that will be used to generate the tag cloud markup. |
865 | 887 | $tags_data = array(); |
866 | 888 | foreach ( $tags as $key => $tag ) { |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
870 | 892 | $real_count = $real_counts[ $key ]; |
871 | 893 | |
872 | 894 | if ( $translate_nooped_plural ) { |
873 | | $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
| 895 | $formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
874 | 896 | } else { |
875 | | $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); |
| 897 | $formatted_count = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); |
876 | 898 | } |
877 | 899 | |
878 | 900 | $tags_data[] = array( |
879 | | 'id' => $tag_id, |
880 | | 'url' => '#' != $tag->link ? $tag->link : '#', |
881 | | 'role' => '#' != $tag->link ? '' : ' role="button"', |
882 | | 'name' => $tag->name, |
883 | | 'title' => $title, |
884 | | 'slug' => $tag->slug, |
885 | | 'real_count' => $real_count, |
886 | | 'class' => 'tag-link-' . $tag_id, |
887 | | 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, |
| 901 | 'id' => $tag_id, |
| 902 | 'url' => '#' != $tag->link ? $tag->link : '#', |
| 903 | 'role' => '#' != $tag->link ? '' : ' role="button"', |
| 904 | 'name' => $tag->name, |
| 905 | 'formatted_count' => $formatted_count, |
| 906 | 'slug' => $tag->slug, |
| 907 | 'real_count' => $real_count, |
| 908 | 'class' => 'tag-cloud-link tag-link-' . $tag_id, |
| 909 | 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, |
| 910 | 'aria_label' => $aria_label ? sprintf( ' aria-label="%1$s (%2$s)"', esc_attr( $tag->name ), esc_attr( $formatted_count ) ) : '', |
| 911 | 'show_count' => $args['show_count'] ? '<span class="tag-link-count"> (' . $real_count . ')</span>' : '', |
888 | 912 | ); |
889 | 913 | } |
890 | 914 | |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
899 | 923 | |
900 | 924 | $a = array(); |
901 | 925 | |
902 | | // generate the output links array |
| 926 | // Generate the output links array. |
903 | 927 | foreach ( $tags_data as $key => $tag_data ) { |
904 | 928 | $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); |
905 | | $a[] = "<a href='" . esc_url( $tag_data['url'] ) . "'" . $tag_data['role'] . " class='" . esc_attr( $class ) . "' title='" . esc_attr( $tag_data['title'] ) . "' style='font-size: " . esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ) . ";'>" . esc_html( $tag_data['name'] ) . "</a>"; |
| 929 | $a[] = sprintf( |
| 930 | '<a href="%1$s"%2$s class="%3$s" style="font-size: %4$s;"%5$s>%6$s%7$s</a>', |
| 931 | esc_url( $tag_data['url'] ), |
| 932 | $tag_data['role'], |
| 933 | esc_attr( $class ), |
| 934 | esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), |
| 935 | $tag_data['aria_label'], |
| 936 | esc_html( $tag_data['name'] ), |
| 937 | $tag_data['show_count'] |
| 938 | ); |
906 | 939 | } |
907 | 940 | |
908 | 941 | switch ( $args['format'] ) { |
… |
… |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
910 | 943 | $return =& $a; |
911 | 944 | break; |
912 | 945 | case 'list' : |
913 | | $return = "<ul class='wp-tag-cloud'>\n\t<li>"; |
| 946 | /* |
| 947 | * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
| 948 | * technologies the default role when the list is styled with `list-style: none`. |
| 949 | * Note: this is redundant but doesn't harm. |
| 950 | */ |
| 951 | $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
914 | 952 | $return .= join( "</li>\n\t<li>", $a ); |
915 | 953 | $return .= "</li>\n</ul>\n"; |
916 | 954 | break; |
diff --git src/wp-includes/widgets/class-wp-widget-tag-cloud.php src/wp-includes/widgets/class-wp-widget-tag-cloud.php
index b4adabd039..bb25cd830a 100644
|
|
class WP_Widget_Tag_Cloud extends WP_Widget { |
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
| 56 | $show_count = ! empty( $instance['count'] ); |
| 57 | |
56 | 58 | /** |
57 | 59 | * Filters the taxonomy used in the Tag Cloud widget. |
58 | 60 | * |
… |
… |
class WP_Widget_Tag_Cloud extends WP_Widget { |
64 | 66 | * @param array $args Args used for the tag cloud widget. |
65 | 67 | */ |
66 | 68 | $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array( |
67 | | 'taxonomy' => $current_taxonomy, |
68 | | 'echo' => false |
| 69 | 'taxonomy' => $current_taxonomy, |
| 70 | 'echo' => false, |
| 71 | 'show_count' => $show_count, |
69 | 72 | ) ) ); |
70 | 73 | |
71 | 74 | if ( empty( $tag_cloud ) ) { |
… |
… |
class WP_Widget_Tag_Cloud extends WP_Widget { |
102 | 105 | public function update( $new_instance, $old_instance ) { |
103 | 106 | $instance = array(); |
104 | 107 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 108 | $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0; |
105 | 109 | $instance['taxonomy'] = stripslashes($new_instance['taxonomy']); |
106 | 110 | return $instance; |
107 | 111 | } |
… |
… |
class WP_Widget_Tag_Cloud extends WP_Widget { |
117 | 121 | public function form( $instance ) { |
118 | 122 | $current_taxonomy = $this->_get_current_taxonomy($instance); |
119 | 123 | $title_id = $this->get_field_id( 'title' ); |
| 124 | $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false; |
120 | 125 | $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
121 | 126 | |
122 | 127 | echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label> |
… |
… |
class WP_Widget_Tag_Cloud extends WP_Widget { |
128 | 133 | $name = $this->get_field_name( 'taxonomy' ); |
129 | 134 | $input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />'; |
130 | 135 | |
| 136 | $count_checkbox = sprintf( |
| 137 | '<p><input type="checkbox" class="checkbox" id="%1$s" name="%2$s"%3$s /> <label for="%1$s">%4$s</label></p>', |
| 138 | $this->get_field_id( 'count' ), |
| 139 | $this->get_field_name( 'count' ), |
| 140 | checked( $count, true, false ), |
| 141 | __( 'Show tag counts' ) |
| 142 | ); |
| 143 | |
131 | 144 | switch ( count( $taxonomies ) ) { |
132 | 145 | |
133 | 146 | // No tag cloud supporting taxonomies found, display error message |
… |
… |
class WP_Widget_Tag_Cloud extends WP_Widget { |
136 | 149 | printf( $input, '' ); |
137 | 150 | break; |
138 | 151 | |
139 | | // Just a single tag cloud supporting taxonomy found, no need to display options |
| 152 | // Just a single tag cloud supporting taxonomy found, no need to display a select. |
140 | 153 | case 1: |
141 | 154 | $keys = array_keys( $taxonomies ); |
142 | 155 | $taxonomy = reset( $keys ); |
143 | 156 | printf( $input, esc_attr( $taxonomy ) ); |
| 157 | echo $count_checkbox; |
144 | 158 | break; |
145 | 159 | |
146 | | // More than one tag cloud supporting taxonomy found, display options |
| 160 | // More than one tag cloud supporting taxonomy found, display a select. |
147 | 161 | default: |
148 | 162 | printf( |
149 | 163 | '<p><label for="%1$s">%2$s</label>' . |
… |
… |
class WP_Widget_Tag_Cloud extends WP_Widget { |
162 | 176 | ); |
163 | 177 | } |
164 | 178 | |
165 | | echo '</select></p>'; |
| 179 | echo '</select></p>' . $count_checkbox; |
166 | 180 | } |
167 | 181 | } |
168 | 182 | |
diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
index 6bb5aef83b..132ffda1c3 100644
|
|
class Tests_Post extends WP_UnitTestCase { |
939 | 939 | 'link' => 'edit' |
940 | 940 | ) ); |
941 | 941 | |
942 | | preg_match_all( "|href='([^']+)'|", $wp_tag_cloud, $matches ); |
| 942 | preg_match_all( '|href="([^"]+)"|', $wp_tag_cloud, $matches ); |
943 | 943 | $this->assertSame( 1, count( $matches[1] ) ); |
944 | 944 | |
945 | 945 | $terms = get_terms( $tax ); |
diff --git tests/phpunit/tests/term/wpGenerateTagCloud.php tests/phpunit/tests/term/wpGenerateTagCloud.php
index 4f50ecc1d7..63d0209bc4 100644
|
|
class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { |
119 | 119 | 'format' => 'list', |
120 | 120 | ) ); |
121 | 121 | |
122 | | $this->assertRegExp( "|^<ul class='wp-tag-cloud'>|", $found ); |
| 122 | $this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found ); |
123 | 123 | $this->assertRegExp( "|</ul>\n|", $found ); |
124 | 124 | $this->assertContains( '>' . $tags[0]->name . '<', $found ); |
125 | 125 | } |
… |
… |
class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { |
164 | 164 | 'format' => 'list', |
165 | 165 | ) ); |
166 | 166 | |
167 | | $this->assertRegExp( "|^<ul class='wp-tag-cloud'>|", $found ); |
| 167 | $this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found ); |
168 | 168 | $this->assertRegExp( "|</ul>\n|", $found ); |
169 | 169 | |
170 | 170 | foreach ( $tags as $tag ) { |
… |
… |
class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { |
198 | 198 | ), |
199 | 199 | ) ); |
200 | 200 | |
201 | | $this->assertContains( "title='Term has 1 post'", $actual[0] ); |
202 | | $this->assertContains( "title='Term has 2 posts'", $actual[1] ); |
| 201 | $this->assertContains( 'aria-label="' . $term_objects[0]->name . ' (Term has 1 post)"', $actual[0] ); |
| 202 | $this->assertContains( 'aria-label="' . $term_objects[1]->name . ' (Term has 2 posts)"', $actual[1] ); |
203 | 203 | } |
204 | 204 | |
205 | 205 | public function test_topic_count_text_callback() { |
… |
… |
class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { |
223 | 223 | 'topic_count_text_callback' => array( $this, 'topic_count_text_callback' ), |
224 | 224 | ) ); |
225 | 225 | |
226 | | $this->assertContains( "title='1 foo'", $actual[0] ); |
227 | | $this->assertContains( "title='2 foo'", $actual[1] ); |
| 226 | $this->assertContains( 'aria-label="' . $term_objects[0]->name . ' (1 foo)"', $actual[0] ); |
| 227 | $this->assertContains( 'aria-label="' . $term_objects[1]->name . ' (2 foo)"', $actual[1] ); |
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |