diff --git a/src/wp-admin/includes/class-wp-terms-list-table.php b/src/wp-admin/includes/class-wp-terms-list-table.php index cb48cbc875..24df0daf6b 100644 --- a/src/wp-admin/includes/class-wp-terms-list-table.php +++ b/src/wp-admin/includes/class-wp-terms-list-table.php @@ -129,7 +129,12 @@ class WP_Terms_List_Table extends WP_List_Table { $this->set_pagination_args( array( - 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ), + 'total_items' => wp_count_terms( + array( + 'taxonomy' => $this->screen->taxonomy, + 'search' => $search, + ) + ), 'per_page' => $tags_per_page, ) ); diff --git a/src/wp-admin/includes/nav-menu.php b/src/wp-admin/includes/nav-menu.php index 218e25d587..42d3607d23 100644 --- a/src/wp-admin/includes/nav-menu.php +++ b/src/wp-admin/includes/nav-menu.php @@ -725,7 +725,6 @@ function wp_nav_menu_item_taxonomy_meta_box( $object, $box ) { $num_pages = ceil( wp_count_terms( - $taxonomy_name, array_merge( $args, array( diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 7ee484e649..6c979c2ff6 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -263,7 +263,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller { unset( $count_args['number'], $count_args['offset'] ); - $total_terms = wp_count_terms( $this->taxonomy, $count_args ); + $total_terms = wp_count_terms( $count_args ); // wp_count_terms() can return a falsey value when the term has no children. if ( ! $total_terms ) { diff --git a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php index 34ff5212e4..31515f806a 100644 --- a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php +++ b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php @@ -150,7 +150,7 @@ class WP_Sitemaps_Taxonomies extends WP_Sitemaps_Provider { return $max_num_pages; } - $term_count = wp_count_terms( $taxonomy, $this->get_taxonomies_query_args( $taxonomy ) ); + $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) ); return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) ); } diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index af5a20a443..c61964bdcf 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1730,18 +1730,24 @@ function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true). * * @since 2.3.0 + * @since 5.6.0 Changed the function signature so that the `$args` array can be provided as the first parameter. * - * @param string $taxonomy Taxonomy name. - * @param array|string $args Optional. Array of arguments that get passed to get_terms(). - * Default empty array. + * @param array|string $args Optional. Array of arguments that get passed to get_terms(). + * Default empty array. + * @param array|string $deprecated Taxonomy name. * @return array|int|WP_Error Number of terms in that taxonomy or WP_Error if the taxonomy does not exist. */ -function wp_count_terms( $taxonomy, $args = array() ) { - $defaults = array( - 'taxonomy' => $taxonomy, - 'hide_empty' => false, - ); - $args = wp_parse_args( $args, $defaults ); +function wp_count_terms( $args = array(), $deprecated = array() ) { + // Check whether function is used with legacy signature `$taxonomy` and `$args`. + $use_legacy_args = $args && ( is_string( $args ) && taxonomy_exists( $args ) || is_array( $args ) && wp_is_numeric_array( $args ) ); + + $defaults = array( 'hide_empty' => false ); + if ( $use_legacy_args ) { + $defaults['taxonomy'] = $args; + $args = $deprecated; + } + + $args = wp_parse_args( $args, $defaults ); // Backward compatibility. if ( isset( $args['ignore_empty'] ) ) { diff --git a/tests/phpunit/tests/import/import.php b/tests/phpunit/tests/import/import.php index fbf8828e7d..1454bed8be 100644 --- a/tests/phpunit/tests/import/import.php +++ b/tests/phpunit/tests/import/import.php @@ -65,8 +65,8 @@ class Tests_Import_Import extends WP_Import_UnitTestCase { $this->assertEquals( 'author@example.org', $author->user_email ); // Check that terms were imported correctly. - $this->assertEquals( 30, wp_count_terms( 'category' ) ); - $this->assertEquals( 3, wp_count_terms( 'post_tag' ) ); + $this->assertEquals( 30, wp_count_terms( array( 'taxonomy' => 'category' ) ) ); + $this->assertEquals( 3, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) ); $foo = get_term_by( 'slug', 'foo', 'category' ); $this->assertEquals( 0, $foo->parent ); $bar = get_term_by( 'slug', 'bar', 'category' ); @@ -230,8 +230,8 @@ class Tests_Import_Import extends WP_Import_UnitTestCase { $this->assertEquals( 'author', $author->user_login ); $this->assertEquals( 'author@example.org', $author->user_email ); - $this->assertEquals( 30, wp_count_terms( 'category' ) ); - $this->assertEquals( 3, wp_count_terms( 'post_tag' ) ); + $this->assertEquals( 30, wp_count_terms( array( 'taxonomy' => 'category' ) ) ); + $this->assertEquals( 3, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) ); $foo = get_term_by( 'slug', 'foo', 'category' ); $this->assertEquals( 0, $foo->parent ); $bar = get_term_by( 'slug', 'bar', 'category' ); diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 08c08f8a7c..38979c1739 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -65,11 +65,34 @@ class Tests_Term extends WP_UnitTestCase { * @ticket 15919 */ function test_wp_count_terms() { - $count = wp_count_terms( 'category', array( 'hide_empty' => true ) ); + $count = wp_count_terms( + array( + 'hide_empty' => true, + 'taxonomy' => 'category', + ) + ); // There are 5 posts, all Uncategorized. $this->assertEquals( 1, $count ); } + /** + * @ticket 36399 + */ + function test_wp_count_terms_legacy_interoperability() { + self::factory()->tag->create_many( 5 ); + + // Counts all terms (1 default category, 5 tags). + $count = wp_count_terms(); + $this->assertEquals( 6, $count ); + + // Counts only tags (5), with both current and legacy signature. + // Legacy usage should not trigger deprecated notice. + $count = wp_count_terms( array( 'taxonomy' => 'post_tag' ) ); + $legacy_count = wp_count_terms( 'post_tag' ); + $this->assertEquals( 5, $count ); + $this->assertEquals( $count, $legacy_count ); + } + /** * @ticket 15475 */ @@ -127,13 +150,13 @@ class Tests_Term extends WP_UnitTestCase { $term = rand_str(); $this->assertNull( category_exists( $term ) ); - $initial_count = wp_count_terms( 'category' ); + $initial_count = wp_count_terms( array( 'taxonomy' => 'category' ) ); $t = wp_insert_category( array( 'cat_name' => $term ) ); $this->assertTrue( is_numeric( $t ) ); $this->assertNotWPError( $t ); $this->assertTrue( $t > 0 ); - $this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) ); + $this->assertEquals( $initial_count + 1, wp_count_terms( array( 'taxonomy' => 'category' ) ) ); // Make sure the term exists. $this->assertTrue( term_exists( $term ) > 0 ); @@ -143,7 +166,7 @@ class Tests_Term extends WP_UnitTestCase { $this->assertTrue( wp_delete_category( $t ) ); $this->assertNull( term_exists( $term ) ); $this->assertNull( term_exists( $t ) ); - $this->assertEquals( $initial_count, wp_count_terms( 'category' ) ); + $this->assertEquals( $initial_count, wp_count_terms( array( 'taxonomy' => 'category' ) ) ); } /** diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index fb2ea1399e..8299dca198 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -24,14 +24,14 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase { $term = 'term'; $this->assertNull( term_exists( $term ) ); - $initial_count = wp_count_terms( $taxonomy ); + $initial_count = wp_count_terms( array( 'taxonomy' => $taxonomy ) ); $t = wp_insert_term( $term, $taxonomy ); $this->assertInternalType( 'array', $t ); $this->assertNotWPError( $t ); $this->assertTrue( $t['term_id'] > 0 ); $this->assertTrue( $t['term_taxonomy_id'] > 0 ); - $this->assertEquals( $initial_count + 1, wp_count_terms( $taxonomy ) ); + $this->assertEquals( $initial_count + 1, wp_count_terms( array( 'taxonomy' => $taxonomy ) ) ); // Make sure the term exists. $this->assertTrue( term_exists( $term ) > 0 ); @@ -43,7 +43,7 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase { remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 ); $this->assertNull( term_exists( $term ) ); $this->assertNull( term_exists( $t['term_id'] ) ); - $this->assertEquals( $initial_count, wp_count_terms( $taxonomy ) ); + $this->assertEquals( $initial_count, wp_count_terms( array( 'taxonomy' => $taxonomy ) ) ); } public function test_wp_insert_term_taxonomy_does_not_exist() {