Make WordPress Core

Changeset 29863


Ignore:
Timestamp:
10/09/2014 02:48:47 AM (9 years ago)
Author:
boonebgorges
Message:

Fix term_exists() for parent = 0.

Passing a 0 (or '0') as the 'parent' param of term_exists() should limit
results to terms with no parent.

Adds unit test.

Fixes #29851.

Location:
trunk
Files:
2 edited

Legend:

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

    r29862 r29863  
    16561656 * @param int|string $term The term to check
    16571657 * @param string $taxonomy The taxonomy name to use
    1658  * @param int $parent ID of parent term under which to confine the exists search.
     1658 * @param int $parent Optional. ID of parent term under which to confine the exists search.
    16591659 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
    16601660 *               and the term ID exists. Returns an array of the term ID and the term taxonomy ID
    16611661 *               if the taxonomy is specified and the pairing exists.
    16621662 */
    1663 function term_exists($term, $taxonomy = '', $parent = 0) {
     1663function term_exists( $term, $taxonomy = '', $parent = null ) {
    16641664    global $wpdb;
    16651665
     
    16871687    $else_where_fields = array($term);
    16881688    if ( !empty($taxonomy) ) {
    1689         $parent = (int) $parent;
    1690         if ( $parent > 0 ) {
     1689        if ( is_numeric( $parent ) ) {
     1690            $parent = (int) $parent;
    16911691            $where_fields[] = $parent;
    16921692            $else_where_fields[] = $parent;
  • trunk/tests/phpunit/tests/term.php

    r29862 r29863  
    137137        $this->assertInternalType( 'array', $found );
    138138        $this->assertEquals( $t, $found['term_id'] );
     139    }
     140
     141    /**
     142     * @ticket 29851
     143     */
     144    public function test_term_exists_taxonomy_nonempty_parent_0_should_return_false_for_child_term() {
     145        register_taxonomy( 'foo', 'post', array(
     146            'hierarchical' => true,
     147        ) );
     148
     149        $parent_term = $this->factory->term->create( array(
     150            'taxonomy' => 'foo',
     151        ) );
     152
     153        $t = $this->factory->term->create( array(
     154            'taxonomy' => 'foo',
     155            'parent' => $parent_term,
     156            'slug' => 'child-term',
     157        ) );
     158
     159        $found = term_exists( 'child-term', 'foo', 0 );
     160
     161        _unregister_taxonomy( 'foo' );
     162
     163        $this->assertSame( null, $found['term_id'] );
    139164    }
    140165
Note: See TracChangeset for help on using the changeset viewer.