Make WordPress Core

Changeset 48840


Ignore:
Timestamp:
08/21/2020 10:30:06 PM (4 years ago)
Author:
flixos90
Message:

Taxonomy: Allow for wp_count_terms( $args ) signature, making passing a taxonomy optional.

This brings wp_count_terms() in line with other taxonomy functions such as get_terms() which technically no longer require a taxonomy. Similar to the previously modified functions, no deprecation warning is triggered when using the legacy signature.

Fixes #36399.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-terms-list-table.php

    r48586 r48840  
    130130        $this->set_pagination_args(
    131131            array(
    132                 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
     132                'total_items' => wp_count_terms(
     133                    array(
     134                        'taxonomy' => $this->screen->taxonomy,
     135                        'search'   => $search,
     136                    )
     137                ),
    133138                'per_page'    => $tags_per_page,
    134139            )
  • trunk/src/wp-admin/includes/nav-menu.php

    r48586 r48840  
    726726    $num_pages = ceil(
    727727        wp_count_terms(
    728             $taxonomy_name,
    729728            array_merge(
    730729                $args,
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r48782 r48840  
    264264        unset( $count_args['number'], $count_args['offset'] );
    265265
    266         $total_terms = wp_count_terms( $this->taxonomy, $count_args );
     266        $total_terms = wp_count_terms( $count_args );
    267267
    268268        // wp_count_terms() can return a falsey value when the term has no children.
  • trunk/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php

    r48782 r48840  
    151151        }
    152152
    153         $term_count = wp_count_terms( $taxonomy, $this->get_taxonomies_query_args( $taxonomy ) );
     153        $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );
    154154
    155155        return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
  • trunk/src/wp-includes/taxonomy.php

    r48782 r48840  
    17311731 *
    17321732 * @since 2.3.0
    1733  *
    1734  * @param string       $taxonomy Taxonomy name.
    1735  * @param array|string $args     Optional. Array of arguments that get passed to get_terms().
    1736  *                               Default empty array.
     1733 * @since 5.6.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
     1734 *
     1735 * @param array|string $args       Optional. Array of arguments that get passed to get_terms().
     1736 *                                 Default empty array.
     1737 * @param array|string $deprecated Taxonomy name.
    17371738 * @return array|int|WP_Error Number of terms in that taxonomy or WP_Error if the taxonomy does not exist.
    17381739 */
    1739 function wp_count_terms( $taxonomy, $args = array() ) {
    1740     $defaults = array(
    1741         'taxonomy'   => $taxonomy,
    1742         'hide_empty' => false,
    1743     );
    1744     $args     = wp_parse_args( $args, $defaults );
     1740function wp_count_terms( $args = array(), $deprecated = array() ) {
     1741    // Check whether function is used with legacy signature `$taxonomy` and `$args`.
     1742    $use_legacy_args = $args && ( is_string( $args ) && taxonomy_exists( $args ) || is_array( $args ) && wp_is_numeric_array( $args ) );
     1743
     1744    $defaults = array( 'hide_empty' => false );
     1745    if ( $use_legacy_args ) {
     1746        $defaults['taxonomy'] = $args;
     1747        $args                 = $deprecated;
     1748    }
     1749
     1750    $args = wp_parse_args( $args, $defaults );
    17451751
    17461752    // Backward compatibility.
  • trunk/tests/phpunit/tests/import/import.php

    r47526 r48840  
    6666
    6767        // Check that terms were imported correctly.
    68         $this->assertEquals( 30, wp_count_terms( 'category' ) );
    69         $this->assertEquals( 3, wp_count_terms( 'post_tag' ) );
     68        $this->assertEquals( 30, wp_count_terms( array( 'taxonomy' => 'category' ) ) );
     69        $this->assertEquals( 3, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) );
    7070        $foo = get_term_by( 'slug', 'foo', 'category' );
    7171        $this->assertEquals( 0, $foo->parent );
     
    231231        $this->assertEquals( 'author@example.org', $author->user_email );
    232232
    233         $this->assertEquals( 30, wp_count_terms( 'category' ) );
    234         $this->assertEquals( 3, wp_count_terms( 'post_tag' ) );
     233        $this->assertEquals( 30, wp_count_terms( array( 'taxonomy' => 'category' ) ) );
     234        $this->assertEquals( 3, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) );
    235235        $foo = get_term_by( 'slug', 'foo', 'category' );
    236236        $this->assertEquals( 0, $foo->parent );
  • trunk/tests/phpunit/tests/term.php

    r48043 r48840  
    6666     */
    6767    function test_wp_count_terms() {
    68         $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );
     68        $count = wp_count_terms(
     69            array(
     70                'hide_empty' => true,
     71                'taxonomy'   => 'category',
     72            )
     73        );
    6974        // There are 5 posts, all Uncategorized.
    7075        $this->assertEquals( 1, $count );
     76    }
     77
     78    /**
     79     * @ticket 36399
     80     */
     81    function test_wp_count_terms_legacy_interoperability() {
     82        self::factory()->tag->create_many( 5 );
     83
     84        // Counts all terms (1 default category, 5 tags).
     85        $count = wp_count_terms();
     86        $this->assertEquals( 6, $count );
     87
     88        // Counts only tags (5), with both current and legacy signature.
     89        // Legacy usage should not trigger deprecated notice.
     90        $count        = wp_count_terms( array( 'taxonomy' => 'post_tag' ) );
     91        $legacy_count = wp_count_terms( 'post_tag' );
     92        $this->assertEquals( 5, $count );
     93        $this->assertEquals( $count, $legacy_count );
    7194    }
    7295
     
    128151        $this->assertNull( category_exists( $term ) );
    129152
    130         $initial_count = wp_count_terms( 'category' );
     153        $initial_count = wp_count_terms( array( 'taxonomy' => 'category' ) );
    131154
    132155        $t = wp_insert_category( array( 'cat_name' => $term ) );
     
    134157        $this->assertNotWPError( $t );
    135158        $this->assertTrue( $t > 0 );
    136         $this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) );
     159        $this->assertEquals( $initial_count + 1, wp_count_terms( array( 'taxonomy' => 'category' ) ) );
    137160
    138161        // Make sure the term exists.
     
    144167        $this->assertNull( term_exists( $term ) );
    145168        $this->assertNull( term_exists( $t ) );
    146         $this->assertEquals( $initial_count, wp_count_terms( 'category' ) );
     169        $this->assertEquals( $initial_count, wp_count_terms( array( 'taxonomy' => 'category' ) ) );
    147170    }
    148171
  • trunk/tests/phpunit/tests/term/wpInsertTerm.php

    r47122 r48840  
    2525        $this->assertNull( term_exists( $term ) );
    2626
    27         $initial_count = wp_count_terms( $taxonomy );
     27        $initial_count = wp_count_terms( array( 'taxonomy' => $taxonomy ) );
    2828
    2929        $t = wp_insert_term( $term, $taxonomy );
     
    3232        $this->assertTrue( $t['term_id'] > 0 );
    3333        $this->assertTrue( $t['term_taxonomy_id'] > 0 );
    34         $this->assertEquals( $initial_count + 1, wp_count_terms( $taxonomy ) );
     34        $this->assertEquals( $initial_count + 1, wp_count_terms( array( 'taxonomy' => $taxonomy ) ) );
    3535
    3636        // Make sure the term exists.
     
    4444        $this->assertNull( term_exists( $term ) );
    4545        $this->assertNull( term_exists( $t['term_id'] ) );
    46         $this->assertEquals( $initial_count, wp_count_terms( $taxonomy ) );
     46        $this->assertEquals( $initial_count, wp_count_terms( array( 'taxonomy' => $taxonomy ) ) );
    4747    }
    4848
Note: See TracChangeset for help on using the changeset viewer.