WordPress.org

Make WordPress Core

Changeset 40293


Ignore:
Timestamp:
03/16/2017 02:03:53 AM (2 years ago)
Author:
boonebgorges
Message:

Improve querying for terms with falsey names and slugs.

Prior to [38677], get_term_by() would always return false if
an empty string were passed as the queried 'name' or 'slug'. The
refactor to use get_terms() broke this behavior; inappropriately
imprecise empty() checks caused the 'name' or 'slug' clause to be
discarded altogether when fetching terms, resulting in an incorrect
term being returned from the function.

We fix the regression by special-casing truly empty values passed
to get_term_by(), and ensuring that WP_Term_Query is properly
able to handle 0 and '0' term queries.

Props sstoqnov.
Fixes #21760.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r40147 r40293  
    475475        }
    476476
    477         if ( ! empty( $args['name'] ) ) {
     477        if (
     478            ( ! empty( $args['name'] ) ) ||
     479            ( is_string( $args['name'] ) && 0 !== strlen( $args['name'] ) )
     480        ) {
    478481            $names = (array) $args['name'];
    479482            foreach ( $names as &$_name ) {
     
    485488        }
    486489
    487         if ( ! empty( $args['slug'] ) ) {
     490        if (
     491            ( ! empty( $args['slug'] ) ) ||
     492            ( is_string( $args['slug'] ) && 0 !== strlen( $args['slug'] ) )
     493        ) {
    488494            if ( is_array( $args['slug'] ) ) {
    489495                $slug = array_map( 'sanitize_title', $args['slug'] );
  • trunk/src/wp-includes/taxonomy.php

    r40292 r40293  
    836836    }
    837837
     838    // No need to perform a query for empty 'slug' or 'name'.
     839    if ( 'slug' === $field || 'name' === $field ) {
     840        $value = (string) $value;
     841
     842        if ( 0 === strlen( $value ) ) {
     843            return false;
     844        }
     845    }
     846
    838847    if ( 'id' === $field || 'term_id' === $field ) {
    839848        $term = get_term( (int) $value, $taxonomy, $output, $filter );
  • trunk/tests/phpunit/tests/term/getTermBy.php

    r40275 r40293  
    206206        $this->assertEquals( 0, $action->get_call_count() );
    207207    }
     208
     209    /**
     210     * @ticket 21760
     211     */
     212    public function test_get_term_by_name_with_string_0() {
     213        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     214
     215        $term_id = $this->factory->term->create( array(
     216            'name'     => '0',
     217            'taxonomy' => 'wptests_tax',
     218        ) );
     219
     220        $found = get_term_by( 'name', '0', 'wptests_tax' );
     221        $this->assertSame( $term_id, $found->term_id );
     222    }
     223
     224    /**
     225     * @ticket 21760
     226     */
     227    public function test_get_term_by_slug_with_string_0() {
     228        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     229
     230        $term_id = $this->factory->term->create( array(
     231            'taxonomy' => 'wptests_tax',
     232            'name'     => '0',
     233            'slug'     => '0',
     234        ) );
     235
     236        $found = get_term_by( 'slug', '0', 'wptests_tax' );
     237        $this->assertSame( $term_id, $found->term_id );
     238    }
     239
     240    /**
     241     * @ticket 21760
     242     */
     243    public function test_get_term_by_with_empty_string() {
     244        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     245
     246        $found_by_slug = get_term_by( 'slug', '', 'wptests_tax' );
     247        $found_by_name = get_term_by( 'name', '', 'wptests_tax' );
     248
     249        $this->assertFalse( $found_by_slug );
     250        $this->assertFalse( $found_by_name );
     251    }
    208252}
  • trunk/tests/phpunit/tests/term/getTerms.php

    r38382 r40293  
    22242224    }
    22252225
     2226    /**
     2227     * @ticket 21760
     2228     */
     2229    public function test_with_term_slug_equal_to_string_0() {
     2230        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     2231
     2232        $term_id = self::factory()->term->create( array(
     2233            'name' => '0',
     2234            'slug' => '0',
     2235            'taxonomy' => 'wptests_tax',
     2236        ) );
     2237
     2238        $found = get_terms( array(
     2239            'taxonomy'   => 'wptests_tax',
     2240            'hide_empty' => 0,
     2241            'slug'       => '0',
     2242        ) );
     2243
     2244        $this->assertEqualSets( array( $term_id ), wp_list_pluck( $found, 'term_id' ) );
     2245    }
     2246
     2247    /**
     2248     * @ticket 21760
     2249     */
     2250    public function test_with_multiple_term_slugs_one_equal_to_string_0() {
     2251        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     2252
     2253        $term_id1 = self::factory()->term->create( array(
     2254            'name'     => '0',
     2255            'slug'     => '0',
     2256            'taxonomy' => 'wptests_tax',
     2257        ) );
     2258
     2259        $term_id2 = self::factory()->term->create( array(
     2260            'name'     => 'Test',
     2261            'slug'     => 'test',
     2262            'taxonomy' => 'wptests_tax',
     2263        ) );
     2264
     2265        $found = get_terms( array(
     2266            'taxonomy'   => 'wptests_tax',
     2267            'hide_empty' => 0,
     2268            'slug'       => array(
     2269                '0',
     2270                'test',
     2271            ),
     2272        ) );
     2273
     2274        $this->assertEqualSets( array( $term_id1, $term_id2 ), wp_list_pluck( $found, 'term_id' ) );
     2275    }
     2276
    22262277    protected function create_hierarchical_terms_and_posts() {
    22272278        $terms = array();
Note: See TracChangeset for help on using the changeset viewer.