Make WordPress Core

Changeset 60676


Ignore:
Timestamp:
08/26/2025 10:22:12 PM (3 months ago)
Author:
davidbaumwald
Message:

Taxonomy: Ensure term_exists respects $parent_term when $term is an integer.

This change updates term_exists by ensuring that any numeric value passed as the $parent_term argument is passed to the subsequent get_terms call when the $term argument is an integer. This change includes unit tests to validate the fix.

Props spacedmonkey, lopo, lgadzhev, hugod, thehercules, nickbrazilian, audrasjb, vijendrajat, sachinrajcp123, bobbyleenoblestudios.
Fixes #55358.

Location:
trunk
Files:
2 edited

Legend:

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

    r60445 r60676  
    16311631    $defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term );
    16321632
     1633    if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
     1634        $defaults['parent'] = (int) $parent_term;
     1635    }
     1636
    16331637    if ( is_int( $term ) ) {
    16341638        if ( 0 === $term ) {
     
    16411645        if ( '' === $term ) {
    16421646            return null;
    1643         }
    1644 
    1645         if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
    1646             $defaults['parent'] = (int) $parent_term;
    16471647        }
    16481648
  • trunk/tests/phpunit/tests/term/termExists.php

    r55745 r60676  
    402402        $this->assertNull( term_exists( null ) );
    403403    }
     404
     405    /**
     406     * @ticket 55358
     407     * @covers ::term_exists()
     408     */
     409    public function test_term_exists_with_numeric_parent_term() {
     410        register_taxonomy(
     411            'foo',
     412            'post',
     413            array(
     414                'hierarchical' => true,
     415            )
     416        );
     417
     418        $parent_term = self::factory()->term->create(
     419            array(
     420                'taxonomy' => 'foo',
     421            )
     422        );
     423
     424        $child_term = self::factory()->term->create(
     425            array(
     426                'taxonomy' => 'foo',
     427                'parent'   => $parent_term,
     428                'slug'     => 'child-term',
     429            )
     430        );
     431
     432        // Test with numeric parent_term as integer
     433        $found = term_exists( 'child-term', 'foo', $parent_term );
     434        $this->assertIsArray( $found );
     435        $this->assertEquals( $child_term, $found['term_id'] );
     436
     437        // Test with numeric parent_term as string
     438        $found = term_exists( 'child-term', 'foo', (string) $parent_term );
     439        $this->assertIsArray( $found );
     440        $this->assertEquals( $child_term, $found['term_id'] );
     441
     442        _unregister_taxonomy( 'foo' );
     443    }
     444
     445    /**
     446     * @ticket 55358
     447     * @covers ::term_exists()
     448     */
     449    public function test_term_exists_with_non_numeric_parent_term() {
     450        register_taxonomy(
     451            'foo',
     452            'post',
     453            array(
     454                'hierarchical' => true,
     455            )
     456        );
     457
     458        $parent_term = self::factory()->term->create(
     459            array(
     460                'taxonomy' => 'foo',
     461            )
     462        );
     463
     464        $child_term = self::factory()->term->create(
     465            array(
     466                'taxonomy' => 'foo',
     467                'parent'   => $parent_term,
     468                'slug'     => 'child-term',
     469            )
     470        );
     471
     472        // Test with non-numeric parent_term (should not set parent filter)
     473        $found = term_exists( 'child-term', 'foo', 'not-numeric' );
     474        $this->assertIsArray( $found );
     475        $this->assertEquals( $child_term, $found['term_id'] );
     476
     477        // Test with null parent_term (should not set parent filter)
     478        $found = term_exists( 'child-term', 'foo', null );
     479        $this->assertIsArray( $found );
     480        $this->assertEquals( $child_term, $found['term_id'] );
     481
     482        _unregister_taxonomy( 'foo' );
     483    }
     484
     485    /**
     486     * @ticket 55358
     487     * @covers ::term_exists()
     488     */
     489    public function test_term_exists_with_empty_taxonomy_and_numeric_parent() {
     490        register_taxonomy(
     491            'foo',
     492            'post',
     493            array(
     494                'hierarchical' => true,
     495            )
     496        );
     497
     498        $parent_term = self::factory()->term->create(
     499            array(
     500                'taxonomy' => 'foo',
     501            )
     502        );
     503
     504        $child_term = self::factory()->term->create(
     505            array(
     506                'taxonomy' => 'foo',
     507                'parent'   => $parent_term,
     508                'slug'     => 'child-term',
     509            )
     510        );
     511
     512        // Test with empty taxonomy and numeric parent_term (should not set parent filter)
     513        $found = term_exists( 'child-term', '', $parent_term );
     514        $this->assertIsString( $found );
     515        $this->assertEquals( $child_term, $found );
     516
     517        _unregister_taxonomy( 'foo' );
     518    }
     519
     520    /**
     521     * @ticket 55358
     522     * @covers ::term_exists()
     523     */
     524    public function test_term_exists_with_wordpress_categories() {
     525        // Create a parent category
     526        $parent_cat = self::factory()->term->create(
     527            array(
     528                'taxonomy' => 'category',
     529                'name'     => 'Parent Category',
     530                'slug'     => 'parent-category',
     531            )
     532        );
     533
     534        // Create a child category
     535        $child_cat = self::factory()->term->create(
     536            array(
     537                'taxonomy' => 'category',
     538                'name'     => 'Child Category',
     539                'slug'     => 'child-category',
     540                'parent'   => $parent_cat,
     541            )
     542        );
     543
     544        // Test finding child category with numeric parent
     545        $found = term_exists( 'child-category', 'category', $parent_cat );
     546        $this->assertIsArray( $found );
     547        $this->assertEquals( $child_cat, $found['term_id'] );
     548
     549        // Test finding child category with wrong parent
     550        $found = term_exists( 'child-category', 'category', 999 );
     551        $this->assertNull( $found );
     552    }
    404553}
Note: See TracChangeset for help on using the changeset viewer.