Make WordPress Core

Changeset 30205


Ignore:
Timestamp:
11/03/2014 02:24:23 PM (10 years ago)
Author:
boonebgorges
Message:

In in_object_in_term(), only check numeric string values against term_id.

The previous in_array() check was playing too loose with mixed types, such
that a string like '10_term_name' would incorrectly match a term_id 10.

Props nobinobi, realloc.
Fixes #29467.

Location:
trunk
Files:
2 edited

Legend:

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

    r30184 r30205  
    42634263
    42644264    foreach ( $object_terms as $object_term ) {
    4265         if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id
     4265        // If term is an int, check against term_ids only.
     4266        if ( $ints && in_array( $object_term->term_id, $ints ) ) {
     4267            return true;
     4268        }
     4269
    42664270        if ( $strs ) {
    4267             if ( in_array( $object_term->term_id, $strs ) ) return true;
    4268             if ( in_array( $object_term->name, $strs ) )    return true;
    4269             if ( in_array( $object_term->slug, $strs ) )    return true;
     4271            // Only check numeric strings against term_id, to avoid false matches due to type juggling.
     4272            $numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
     4273            if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
     4274                return true;
     4275            }
     4276
     4277            if ( in_array( $object_term->name, $strs ) ) return true;
     4278            if ( in_array( $object_term->slug, $strs ) ) return true;
    42704279        }
    42714280    }
  • trunk/tests/phpunit/tests/term/isObjectInTerm.php

    r30204 r30205  
    8282        _unregister_taxonomy( 'wptests_tax', 'post' );
    8383    }
     84
     85    /**
     86     * @ticket 29467
     87     */
     88    public function test_should_not_return_true_if_term_name_begins_with_existing_term_id() {
     89        register_taxonomy( 'wptests_tax', 'post' );
     90        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     91
     92        $post_ID  = $this->factory->post->create();
     93        wp_set_object_terms( $post_ID, $t, 'wptests_tax' );
     94
     95        $int_tax_name = $t . '_term_name';
     96
     97        $this->assertFalse( is_object_in_term( $post_ID, 'wptests_tax', $int_tax_name ) );
     98
     99        // Verify it works properly when the post is actually in the term.
     100        wp_set_object_terms( $post_ID, array( $int_tax_name ), 'wptests_tax' );
     101        $this->assertTrue( is_object_in_term( $post_ID, 'wptests_tax', $int_tax_name ) );
     102    }
    84103}
Note: See TracChangeset for help on using the changeset viewer.