Support `id=>name` and `id=>slug` values for `fields` arg in `get_terms()`. Adds unit tests.

Props mikeschinkel.

Fixes #13661.



git-svn-id: https://develop.svn.wordpress.org/trunk@25161 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-08-29 15:18:08 +00:00
parent 744a4a80a4
commit 69785902db
2 changed files with 57 additions and 12 deletions

View File

@ -1383,19 +1383,26 @@ function get_terms($taxonomies, $args = '') {
$selects = array(); $selects = array();
switch ( $fields ) { switch ( $fields ) {
case 'all': case 'all':
$selects = array('t.*', 'tt.*'); $selects = array( 't.*', 'tt.*' );
break; break;
case 'ids': case 'ids':
case 'id=>parent': case 'id=>parent':
$selects = array('t.term_id', 'tt.parent', 'tt.count'); $selects = array( 't.term_id', 'tt.parent', 'tt.count' );
break; break;
case 'names': case 'names':
$selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
break; break;
case 'count': case 'count':
$orderby = ''; $orderby = '';
$order = ''; $order = '';
$selects = array('COUNT(*)'); $selects = array( 'COUNT(*)' );
break;
case 'id=>name':
$selects = array( 't.term_id', 't.name' );
break;
case 'id=>slug':
$selects = array( 't.term_id', 't.slug' );
break;
} }
$_fields = $fields; $_fields = $fields;
@ -1454,29 +1461,35 @@ function get_terms($taxonomies, $args = '') {
} }
} }
} }
reset ( $terms ); reset( $terms );
$_terms = array(); $_terms = array();
if ( 'id=>parent' == $fields ) { if ( 'id=>parent' == $fields ) {
while ( $term = array_shift($terms) ) while ( $term = array_shift( $terms ) )
$_terms[$term->term_id] = $term->parent; $_terms[$term->term_id] = $term->parent;
$terms = $_terms;
} elseif ( 'ids' == $fields ) { } elseif ( 'ids' == $fields ) {
while ( $term = array_shift($terms) ) while ( $term = array_shift( $terms ) )
$_terms[] = $term->term_id; $_terms[] = $term->term_id;
$terms = $_terms;
} elseif ( 'names' == $fields ) { } elseif ( 'names' == $fields ) {
while ( $term = array_shift($terms) ) while ( $term = array_shift( $terms ) )
$_terms[] = $term->name; $_terms[] = $term->name;
$terms = $_terms; } elseif ( 'id=>name' == $fields ) {
while ( $term = array_shift( $terms ) )
$_terms[$term->term_id] = $term->name;
} elseif ( 'id=>slug' == $fields ) {
while ( $term = array_shift( $terms ) )
$_terms[$term->term_id] = $term->slug;
} }
if ( ! empty( $_terms ) )
$terms = $_terms;
if ( $number && is_array( $terms ) && count( $terms ) > $number ) if ( $number && is_array( $terms ) && count( $terms ) > $number )
$terms = array_slice( $terms, $offset, $number ); $terms = array_slice( $terms, $offset, $number );
wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
$terms = apply_filters('get_terms', $terms, $taxonomies, $args); $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
return $terms; return $terms;
} }

View File

@ -87,4 +87,36 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$terms = get_terms( array( '111' => 'post_tag' ), array( 'hide_empty' => false ) ); $terms = get_terms( array( '111' => 'post_tag' ), array( 'hide_empty' => false ) );
$this->assertEquals( $term_id, $terms[0]->term_id ); $this->assertEquals( $term_id, $terms[0]->term_id );
} }
/**
* @ticket 13661
*/
function test_get_terms_fields() {
$term_id1 = $this->factory->tag->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
$term_id2 = $this->factory->tag->create( array( 'slug' => 'hoo', 'name' => 'HOO!', 'parent' => $term_id1 ) );
$terms_id_parent = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
$this->assertEquals( array(
$term_id1 => 0,
$term_id2 => $term_id1
), $terms_id_parent );
$terms_ids = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms_ids );
$terms_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'names' ) );
$this->assertEqualSets( array( 'WOO!', 'HOO!' ), $terms_name );
$terms_id_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>name' ) );
$this->assertEquals( array(
$term_id1 => 'WOO!',
$term_id2 => 'HOO!',
), $terms_id_name );
$terms_id_slug = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>slug' ) );
$this->assertEquals( array(
$term_id1 => 'woo',
$term_id2 => 'hoo'
), $terms_id_slug );
}
} }