Make WordPress Core


Ignore:
Timestamp:
08/21/2020 10:30:06 PM (5 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.