Make WordPress Core

Changeset 65161


Ignore:
Timestamp:
11/01/2026 05:58:25 AM (less than one hour ago)
Author:
westonruter
Message:

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 ​https://github.com/WordPress/wordpress-develop/pull/15306.
Follow-up to r40816.

Props huzaifaalmesbah, westonruter.
See #65817.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r64234 r65161  
    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.
    … …  
    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        }
  • trunk/tests/phpunit/tests/term/wpGenerateTagCloud.php

    r65018 r65161  
    304304
    305305        /**
     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
     378        /**
    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' ) {
Note: See TracChangeset for help on using the changeset viewer.