From 797ddb60ba193caf16b0cc02095b91ecc460af15 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 10 Oct 2015 02:12:40 +0000 Subject: [PATCH] Return `WP_Term` objects from `get_terms()`. Props boonebgorges, flixos90, DrewAPicture. See #14162. git-svn-id: https://develop.svn.wordpress.org/trunk@34998 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy-functions.php | 9 ++++-- tests/phpunit/tests/term/getTerms.php | 39 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 6ed4cb3e24..53435c0bc6 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -991,7 +991,8 @@ function get_term_to_edit( $id, $taxonomy ) { * @since 2.3.0 * @since 4.2.0 Introduced 'name' and 'childless' parameters. * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter. - * Introduced the 'meta_query' and 'update_term_meta_cache' parameters. + * Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return + * a list of WP_Term objects. * * @global wpdb $wpdb WordPress database abstraction object. * @global array $wp_filter @@ -1052,8 +1053,8 @@ function get_term_to_edit( $id, $taxonomy ) { * @type array $meta_query Meta query clauses to limit retrieved terms by. * See `WP_Meta_Query`. Default empty. * } - * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies - * do not exist. + * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies + * do not exist. */ function get_terms( $taxonomies, $args = '' ) { global $wpdb; @@ -1493,6 +1494,8 @@ function get_terms( $taxonomies, $args = '' ) { foreach ( $terms as $term ) { $_terms[ $term->term_id ] = $term->slug; } + } else { + $_terms = array_map( 'get_term', $terms ); } if ( ! empty( $_terms ) ) { diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 5256b823ba..1c42e49ed7 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -1572,6 +1572,45 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEqualSets( array( $terms[0], $terms[1] ), $found ); } + /** + * @ticket 14162 + */ + public function test_should_return_wp_term_objects() { + register_taxonomy( 'wptests_tax', 'post' ); + $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + + $found = get_terms( 'wptests_tax', array( + 'hide_empty' => false, + 'fields' => 'all', + ) ); + + $this->assertNotEmpty( $found ); + + foreach ( $found as $term ) { + $this->assertInstanceOf( 'WP_Term', $term ); + } + } + + /** + * @ticket 14162 + */ + public function test_should_prime_individual_term_cache_when_fields_is_all() { + global $wpdb; + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + + $found = get_terms( 'wptests_tax', array( + 'hide_empty' => false, + 'fields' => 'all', + ) ); + + $num_queries = $wpdb->num_queries; + $term0 = get_term( $terms[0] ); + $this->assertSame( $num_queries, $wpdb->num_queries ); + + } + protected function create_hierarchical_terms_and_posts() { $terms = array();