WordPress.org

Make WordPress Core

Changeset 25161


Ignore:
Timestamp:
08/29/2013 03:18:08 PM (8 years ago)
Author:
wonderboymusic
Message:

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

Props mikeschinkel.

Fixes #13661.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r25142 r25161  
    13841384    switch ( $fields ) {
    13851385        case 'all':
    1386             $selects = array('t.*', 'tt.*');
     1386            $selects = array( 't.*', 'tt.*' );
    13871387            break;
    13881388        case 'ids':
    13891389        case 'id=>parent':
    1390             $selects = array('t.term_id', 'tt.parent', 'tt.count');
     1390            $selects = array( 't.term_id', 'tt.parent', 'tt.count' );
    13911391            break;
    13921392        case 'names':
    1393             $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
     1393            $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
    13941394            break;
    13951395        case 'count':
    13961396            $orderby = '';
    13971397            $order = '';
    1398             $selects = array('COUNT(*)');
     1398            $selects = array( 'COUNT(*)' );
     1399            break;
     1400        case 'id=>name':
     1401            $selects = array( 't.term_id', 't.name' );
     1402            break;
     1403        case 'id=>slug':
     1404            $selects = array( 't.term_id', 't.slug' );
     1405            break;
    13991406    }
    14001407
     
    14551462        }
    14561463    }
    1457     reset ( $terms );
     1464    reset( $terms );
    14581465
    14591466    $_terms = array();
    14601467    if ( 'id=>parent' == $fields ) {
    1461         while ( $term = array_shift($terms) )
     1468        while ( $term = array_shift( $terms ) )
    14621469            $_terms[$term->term_id] = $term->parent;
     1470    } elseif ( 'ids' == $fields ) {
     1471        while ( $term = array_shift( $terms ) )
     1472            $_terms[] = $term->term_id;
     1473    } elseif ( 'names' == $fields ) {
     1474        while ( $term = array_shift( $terms ) )
     1475            $_terms[] = $term->name;
     1476    } elseif ( 'id=>name' == $fields ) {
     1477        while ( $term = array_shift( $terms ) )
     1478            $_terms[$term->term_id] = $term->name;
     1479    } elseif ( 'id=>slug' == $fields ) {
     1480        while ( $term = array_shift( $terms ) )
     1481            $_terms[$term->term_id] = $term->slug;
     1482    }
     1483
     1484    if ( ! empty( $_terms ) )
    14631485        $terms = $_terms;
    1464     } elseif ( 'ids' == $fields ) {
    1465         while ( $term = array_shift($terms) )
    1466             $_terms[] = $term->term_id;
    1467         $terms = $_terms;
    1468     } elseif ( 'names' == $fields ) {
    1469         while ( $term = array_shift($terms) )
    1470             $_terms[] = $term->name;
    1471         $terms = $_terms;
    1472     }
    14731486
    14741487    if ( $number && is_array( $terms ) && count( $terms ) > $number )
     
    14771490    wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
    14781491
    1479     $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
     1492    $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
    14801493    return $terms;
    14811494}
  • trunk/tests/data

    • Property svn:ignore set to
      .trac-ticket-cache.core.trac.wordpress.org
      .trac-ticket-cache.unit-tests.trac.wordpress.org
  • trunk/tests/tests/term/getTerms.php

    r25002 r25161  
    8888        $this->assertEquals( $term_id, $terms[0]->term_id );
    8989    }
     90
     91    /**
     92     * @ticket 13661
     93     */
     94    function test_get_terms_fields() {
     95        $term_id1 = $this->factory->tag->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
     96        $term_id2 = $this->factory->tag->create( array( 'slug' => 'hoo', 'name' => 'HOO!', 'parent' => $term_id1 ) );
     97
     98        $terms_id_parent = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
     99        $this->assertEquals( array(
     100            $term_id1 => 0,
     101            $term_id2 => $term_id1
     102        ), $terms_id_parent );
     103
     104        $terms_ids = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) );
     105        $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms_ids );
     106
     107        $terms_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'names' ) );
     108        $this->assertEqualSets( array( 'WOO!', 'HOO!' ), $terms_name );
     109
     110        $terms_id_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>name' ) );
     111        $this->assertEquals( array(
     112            $term_id1 => 'WOO!',
     113            $term_id2 => 'HOO!',
     114        ), $terms_id_name );
     115
     116        $terms_id_slug = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>slug' ) );
     117        $this->assertEquals( array(
     118            $term_id1 => 'woo',
     119            $term_id2 => 'hoo'
     120        ), $terms_id_slug );
     121    }
    90122}
Note: See TracChangeset for help on using the changeset viewer.