Make WordPress Core

Changeset 31024 for trunk


Ignore:
Timestamp:
01/02/2015 09:33:33 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce 'name' parameter for get_terms().

This enhancement requires a modification in the way that wp_dropdown_categories()
prepares its arguments for get_terms(), so that its unrelated 'name' param is
not mistaken for the new 'name' argument in get_terms().

Props danielbachhuber.
Fixes #30611.

Location:
trunk
Files:
3 edited

Legend:

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

    r31006 r31024  
    377377        $tab_index_attribute = " tabindex=\"$tab_index\"";
    378378    }
    379     $categories = get_terms( $r['taxonomy'], $r );
     379
     380    // Avoid clashes with the 'name' param of get_terms().
     381    $get_terms_args = $r;
     382    unset( $get_terms_args['name'] );
     383    $categories = get_terms( $r['taxonomy'], $get_terms_args );
     384
    380385    $name = esc_attr( $r['name'] );
    381386    $class = esc_attr( $r['class'] );
  • trunk/src/wp-includes/taxonomy.php

    r31012 r31024  
    15491549 *
    15501550 * @since 2.3.0
     1551 * @since 4.2.0 Introduced 'name' parameter.
    15511552 *
    15521553 * @global wpdb $wpdb WordPress database abstraction object.
     
    15791580 *                                           term objects), 'ids' or 'names' (returns an array of integers
    15801581 *                                           or strings, respectively. Default 'all'.
     1582 *     @type string|array $name              Optional. Name or array of names to return term(s) for. Default empty.
    15811583 *     @type string|array $slug              Optional. Slug or array of slugs to return term(s) for. Default empty.
    15821584 *     @type bool         $hierarchical      Whether to include terms that have non-empty descendants (even
     
    16191621    $defaults = array('orderby' => 'name', 'order' => 'ASC',
    16201622        'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
    1621         'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
     1623        'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '',
    16221624        'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
    16231625        'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
     
    17941796    if ( ! empty( $exclusions ) ) {
    17951797        $where .= $exclusions;
     1798    }
     1799
     1800    if ( ! empty( $args['name'] ) ) {
     1801        if ( is_array( $args['name'] ) ) {
     1802            $name = array_map( 'sanitize_text_field', $args['name'] );
     1803            $where .= " AND t.name IN ('" . implode( "', '", $name ) . "')";
     1804        } else {
     1805            $name = sanitize_text_field( $args['name'] );
     1806            $where .= $wpdb->prepare( " AND t.name = %s", $name );
     1807        }
    17961808    }
    17971809
  • trunk/tests/phpunit/tests/term/getTerms.php

    r30688 r31024  
    423423
    424424        $this->assertEquals( array( $t1, $t3 ), $found );
     425    }
     426
     427    /**
     428     * @ticket 30611
     429     */
     430    public function test_get_terms_by_name() {
     431        $t1 = $this->factory->tag->create( array( 'name' => 'Foo' ) );
     432        $t2 = $this->factory->tag->create( array( 'name' => 'Bar' ) );
     433
     434        $found = get_terms( 'post_tag', array(
     435            'hide_empty' => false,
     436            'fields' => 'ids',
     437            'name' => 'Foo',
     438        ) );
     439
     440        $this->assertEquals( array( $t1 ), $found );
     441    }
     442
     443    /**
     444     * @ticket 30611
     445     */
     446    public function test_get_terms_by_multiple_names() {
     447        $t1 = $this->factory->tag->create( array( 'name' => 'Foo' ) );
     448        $t2 = $this->factory->tag->create( array( 'name' => 'Bar' ) );
     449        $t3 = $this->factory->tag->create( array( 'name' => 'Barry' ) );
     450
     451        $found = get_terms( 'post_tag', array(
     452            'hide_empty' => false,
     453            'fields' => 'ids',
     454            'name' => array( 'Foo', 'Barry' )
     455        ) );
     456
     457        $this->assertEqualSets( array( $t3, $t1 ), $found );
    425458    }
    426459
Note: See TracChangeset for help on using the changeset viewer.