Index: trunk/src/wp-includes/category-template.php =================================================================== --- trunk/src/wp-includes/category-template.php (revision 40043) +++ trunk/src/wp-includes/category-template.php (revision 40816) @@ -660,8 +660,8 @@ * * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the - * text for the tooltip of the tag link. + * text for the tag link count. * * The 'topic_count_text_callback' argument is a function, which given the count - * of the posts with that tag returns a text for the tooltip of the tag link. + * of the posts with that tag returns a text for the tag link count. * * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type @@ -672,4 +672,5 @@ * * @since 2.3.0 + * @since 4.8.0 Added the `show_count` argument. * * @param array|string|null $args Optional. Override default arguments. @@ -681,5 +682,6 @@ 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', - 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true + 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true, + 'show_count' => 0, ); $args = wp_parse_args( $args, $defaults ); @@ -737,4 +739,5 @@ * @todo Complete functionality. * @since 2.3.0 + * @since 4.8.0 Added the `show_count` argument. * * @param array $tags List of tags. @@ -767,9 +770,11 @@ * via {@see 'wp_generate_tag_cloud'}. Default 1|true. * @type string $topic_count_text Nooped plural text from _n_noop() to supply to - * tag tooltips. Default null. + * tag counts. Default null. * @type callable $topic_count_text_callback Callback used to generate nooped plural text for - * tag tooltips based on the count. Default null. + * tag counts based on the count. Default null. * @type callable $topic_count_scale_callback Callback used to determine the tag count scaling * value. Default default_topic_count_scale(). + * @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts + * 0, 1, or their bool equivalents. * } * @return string|array Tag cloud as a string or an array, depending on 'format' argument. @@ -781,4 +786,5 @@ 'topic_count_text' => null, 'topic_count_text_callback' => null, 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, + 'show_count' => 0, ); @@ -791,5 +797,5 @@ } - // Juggle topic count tooltips: + // Juggle topic counts. if ( isset( $args['topic_count_text'] ) ) { // First look for nooped plural support via topic_count_text. @@ -798,5 +804,5 @@ // Look for the alternative callback style. Ignore the previous default. if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { - $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); + $translate_nooped_plural = _n_noop( '%s item', '%s items' ); } else { $translate_nooped_plural = false; @@ -807,5 +813,5 @@ } else { // This is the default for when no callback, plural, or argument is passed in. - $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); + $translate_nooped_plural = _n_noop( '%s item', '%s items' ); } @@ -862,4 +868,20 @@ $font_step = $font_spread / $spread; + $aria_label = false; + /* + * Determine whether to output an 'aria-label' attribute with the tag name and count. + * When tags have a different font size, they visually convey an important information + * that should be available to assistive technologies too. On the other hand, sometimes + * themes set up the Tag Cloud to display all tags with the same font size (setting + * the 'smallest' and 'largest' arguments to the same value). + * In order to always serve the same content to all users, the 'aria-label' gets printed out: + * - when tags have a different size + * - when the tag count is displayed (for example when users check the checkbox in the + * Tag Cloud widget), regardless of the tags font size + */ + if ( $args['show_count'] || 0 !== $font_spread ) { + $aria_label = true; + } + // Assemble the data that will be used to generate the tag cloud markup. $tags_data = array(); @@ -871,19 +893,21 @@ if ( $translate_nooped_plural ) { - $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); + $formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); } else { - $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); + $formatted_count = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); } $tags_data[] = array( - 'id' => $tag_id, - 'url' => '#' != $tag->link ? $tag->link : '#', - 'role' => '#' != $tag->link ? '' : ' role="button"', - 'name' => $tag->name, - 'title' => $title, - 'slug' => $tag->slug, - 'real_count' => $real_count, - 'class' => 'tag-link-' . $tag_id, - 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, + 'id' => $tag_id, + 'url' => '#' != $tag->link ? $tag->link : '#', + 'role' => '#' != $tag->link ? '' : ' role="button"', + 'name' => $tag->name, + 'formatted_count' => $formatted_count, + 'slug' => $tag->slug, + 'real_count' => $real_count, + 'class' => 'tag-cloud-link tag-link-' . $tag_id, + 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, + 'aria_label' => $aria_label ? sprintf( ' aria-label="%1$s (%2$s)"', esc_attr( $tag->name ), esc_attr( $formatted_count ) ) : '', + 'show_count' => $args['show_count'] ? ' (' . $real_count . ')' : '', ); } @@ -900,8 +924,17 @@ $a = array(); - // generate the output links array + // Generate the output links array. foreach ( $tags_data as $key => $tag_data ) { $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); - $a[] = "" . esc_html( $tag_data['name'] ) . ""; + $a[] = sprintf( + '%6$s%7$s', + esc_url( $tag_data['url'] ), + $tag_data['role'], + esc_attr( $class ), + esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), + $tag_data['aria_label'], + esc_html( $tag_data['name'] ), + $tag_data['show_count'] + ); } @@ -911,5 +944,10 @@ break; case 'list' : - $return = "