Skip to content

Commit b3f903c

Browse files
committed
Taxonomy: Fix tag cloud handling of float font sizes.
The `smallest` and `largest` arguments of `wp_generate_tag_cloud()` were documented as integers, but they are only used in arithmetic, and the Tag Cloud block always passes them as floats so that its font size settings can be fractional. They are now documented as `int|float`. Accepting floats exposed a bug. The `aria-label` on each tag is meant to be omitted when all tags share one font size, which was checked by comparing the font spread against the integer `0`. With equal float sizes the spread is `0.0`, which is not identical to `0`, so the `aria-label` was output anyway, including for any Tag Cloud block whose smallest and largest sizes matched. The spread is now checked for being greater than zero; a negative spread was already clamped to `1`. Developed in WordPress#15306. Follow-up to r40816. Props huzaifaalmesbah, westonruter. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65161 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0d4a0ee commit b3f903c

2 files changed

Lines changed: 107 additions & 34 deletions

File tree

‎src/wp-includes/category-template.php‎

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -819,38 +819,38 @@ function default_topic_count_scale( $count ) {
819819
* @param string|array $args {
820820
* Optional. Array or string of arguments for generating a tag cloud.
821821
*
822-
* @type int $smallest Smallest font size used to display tags. Paired
823-
* with the value of `$unit`, to determine CSS text
824-
* size unit. Default 8 (pt).
825-
* @type int $largest Largest font size used to display tags. Paired
826-
* with the value of `$unit`, to determine CSS text
827-
* size unit. Default 22 (pt).
828-
* @type string $unit CSS text size unit to use with the `$smallest`
829-
* and `$largest` values. Accepts any valid CSS text
830-
* size unit. Default 'pt'.
831-
* @type int $number The number of tags to return. Accepts any
832-
* positive integer or zero to return all.
833-
* Default 0.
834-
* @type string $format Format to display the tag cloud in. Accepts 'flat'
835-
* (tags separated with spaces), 'list' (tags displayed
836-
* in an unordered list), or 'array' (returns an array).
837-
* Default 'flat'.
838-
* @type string $separator HTML or text to separate the tags. Default "\n" (newline).
839-
* @type string $orderby Value to order tags by. Accepts 'name' or 'count'.
840-
* Default 'name'. The {@see 'tag_cloud_sort'} filter
841-
* can also affect how tags are sorted.
842-
* @type string $order How to order the tags. Accepts 'ASC' (ascending),
843-
* 'DESC' (descending), or 'RAND' (random). Default 'ASC'.
844-
* @type int|bool $filter Whether to enable filtering of the final output
845-
* via {@see 'wp_generate_tag_cloud'}. Default 1.
846-
* @type array $topic_count_text Nooped plural text from _n_noop() to supply to
847-
* tag counts. Default null.
848-
* @type callable $topic_count_text_callback Callback used to generate nooped plural text for
849-
* tag counts based on the count. Default null.
850-
* @type callable $topic_count_scale_callback Callback used to determine the tag count scaling
851-
* value. Default default_topic_count_scale().
852-
* @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts
853-
* 0, 1, or their bool equivalents.
822+
* @type int|float $smallest Smallest font size used to display tags. Paired
823+
* with the value of `$unit`, to determine CSS text
824+
* size unit. Default 8 (pt).
825+
* @type int|float $largest Largest font size used to display tags. Paired
826+
* with the value of `$unit`, to determine CSS text
827+
* size unit. Default 22 (pt).
828+
* @type string $unit CSS text size unit to use with the `$smallest`
829+
* and `$largest` values. Accepts any valid CSS text
830+
* size unit. Default 'pt'.
831+
* @type int $number The number of tags to return. Accepts any
832+
* positive integer or zero to return all.
833+
* Default 0.
834+
* @type string $format Format to display the tag cloud in. Accepts 'flat'
835+
* (tags separated with spaces), 'list' (tags displayed
836+
* in an unordered list), or 'array' (returns an array).
837+
* Default 'flat'.
838+
* @type string $separator HTML or text to separate the tags. Default "\n" (newline).
839+
* @type string $orderby Value to order tags by. Accepts 'name' or 'count'.
840+
* Default 'name'. The {@see 'tag_cloud_sort'} filter
841+
* can also affect how tags are sorted.
842+
* @type string $order How to order the tags. Accepts 'ASC' (ascending),
843+
* 'DESC' (descending), or 'RAND' (random). Default 'ASC'.
844+
* @type int|bool $filter Whether to enable filtering of the final output
845+
* via {@see 'wp_generate_tag_cloud'}. Default 1.
846+
* @type array $topic_count_text Nooped plural text from _n_noop() to supply to
847+
* tag counts. Default null.
848+
* @type callable $topic_count_text_callback Callback used to generate nooped plural text for
849+
* tag counts based on the count. Default null.
850+
* @type callable $topic_count_scale_callback Callback used to determine the tag count scaling
851+
* value. Default default_topic_count_scale().
852+
* @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts
853+
* 0, 1, or their bool equivalents.
854854
* }
855855
* @return string|string[] Tag cloud as a string or an array, depending on 'format' argument.
856856
*
@@ -970,7 +970,7 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
970970
* - when the tag count is displayed (for example when users check the checkbox in the
971971
* Tag Cloud widget), regardless of the tags font size
972972
*/
973-
if ( $args['show_count'] || 0 !== $font_spread ) {
973+
if ( $args['show_count'] || $font_spread > 0 ) {
974974
$aria_label = true;
975975
}
976976

‎tests/phpunit/tests/term/wpGenerateTagCloud.php‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,84 @@ public function test_should_include_tag_link_position_class() {
302302
$this->assertSame( array( 1, 2, 3 ), array_map( 'intval', $matches[1] ) );
303303
}
304304

305+
/**
306+
* Tests that the aria-label is only output when font sizes differ or counts are shown.
307+
*
308+
* @ticket 65817
309+
*
310+
* @covers ::wp_generate_tag_cloud
311+
*
312+
* @dataProvider data_aria_label_font_sizes
313+
*
314+
* @param int|float $smallest Smallest font size.
315+
* @param int|float $largest Largest font size.
316+
* @param int $show_count Whether to show tag counts.
317+
* @param bool $expect_aria_label Whether an aria-label is expected.
318+
*/
319+
public function test_aria_label_font_sizes( $smallest, $largest, int $show_count, bool $expect_aria_label ) {
320+
register_taxonomy( 'wptests_tax', 'post' );
321+
$term_ids = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
322+
$posts = self::factory()->post->create_many( 2 );
323+
324+
wp_set_post_terms( $posts[0], $term_ids, 'wptests_tax' );
325+
wp_set_post_terms( $posts[1], array( $term_ids[1] ), 'wptests_tax' );
326+
327+
$term_objects = $this->retrieve_terms(
328+
array(
329+
'include' => $term_ids,
330+
),
331+
'wptests_tax'
332+
);
333+
334+
$actual = wp_generate_tag_cloud(
335+
$term_objects,
336+
array(
337+
'format' => 'array',
338+
'smallest' => $smallest,
339+
'largest' => $largest,
340+
'show_count' => $show_count,
341+
)
342+
);
343+
344+
$this->assertCount( 2, $actual );
345+
346+
$expected_aria_labels = array(
347+
$term_objects[0]->name . ' (1 item)',
348+
$term_objects[1]->name . ' (2 items)',
349+
);
350+
foreach ( $actual as $i => $link ) {
351+
$processor = new WP_HTML_Tag_Processor( $link );
352+
$this->assertTrue( $processor->next_tag( 'A' ), 'Expected the tag cloud item to be a link.' );
353+
$this->assertSame(
354+
$expect_aria_label ? $expected_aria_labels[ $i ] : null,
355+
$processor->get_attribute( 'aria-label' ),
356+
'Expected the aria-label attribute to be ' . ( $expect_aria_label ? 'present' : 'absent' ) . '.'
357+
);
358+
}
359+
}
360+
361+
/**
362+
* Data provider for {@see self::test_aria_label_font_sizes()}.
363+
*
364+
* @return array<non-falsy-string, array{ 0: int|float, 1: int|float, 2: int, 3: bool }>
365+
*/
366+
public static function data_aria_label_font_sizes(): array {
367+
return array(
368+
'equal int sizes' => array( 12, 12, 0, false ),
369+
'equal float sizes' => array( 1.2, 1.2, 0, false ),
370+
'equal integral float sizes' => array( 12.0, 12.0, 0, false ),
371+
'different int sizes' => array( 8, 22, 0, true ),
372+
'different float sizes' => array( 0.8, 1.5, 0, true ),
373+
'equal float sizes with count' => array( 1.2, 1.2, 1, true ),
374+
'smallest larger than largest' => array( 22.5, 8.5, 0, true ),
375+
);
376+
}
377+
305378
/**
306379
* Helper method retrieve the created terms.
307380
*
308381
* @param array $get_terms_args Options passed to get_terms()
309-
* @return array
382+
* @return WP_Term[]
310383
*/
311384
protected function retrieve_terms( $get_terms_args, $taxonomy = 'post_tag' ) {
312385
$terms = get_terms( array( $taxonomy ), $get_terms_args );

0 commit comments

Comments
 (0)