Make WordPress Core

Changeset 61048


Ignore:
Timestamp:
10/22/2025 04:28:33 PM (4 months ago)
Author:
johnjamesjacoby
Message:

Query: Prevent querying for all terms in WP_Term_Query when include is set to [0].

This change brings the include parameter of WP_Term_Query up-to-speed with the include and __in parameters of other _Query classes, so that sending a value of [0] will not unintentionally perform an unbound query that returns all Terms.

It also introduces 3 new unit tests (for the Post, Term, and User query classes) to compare this behavior between them and ensure they remain consistent going forward.

Props audrasjb, hareesh-pillai, hellofromTonya, johnjamesjacoby, mukesh27.

Fixes #47719.

Location:
trunk
Files:
4 edited

Legend:

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

    r60697 r61048  
    471471        $include      = $args['include'];
    472472
    473         $inclusions = '';
    474473        if ( ! empty( $include ) ) {
    475474            $exclude      = '';
    476475            $exclude_tree = '';
    477476            $inclusions   = implode( ',', wp_parse_id_list( $include ) );
    478         }
    479 
    480         if ( ! empty( $inclusions ) ) {
     477
    481478            $this->sql_clauses['where']['inclusions'] = 't.term_id IN ( ' . $inclusions . ' )';
    482479        }
     
    816813
    817814        if ( empty( $terms ) ) {
     815            $this->terms = array();
     816
    818817            if ( $args['cache_results'] ) {
    819                 wp_cache_set_salted( $cache_key, array(), 'term-queries', $last_changed );
    820             }
    821             return array();
     818                wp_cache_set_salted( $cache_key, $this->terms, 'term-queries', $last_changed );
     819            }
     820
     821            return $this->terms;
    822822        }
    823823
  • trunk/tests/phpunit/tests/post/query.php

    r60729 r61048  
    780780
    781781    /**
     782     * @ticket 47719
     783     */
     784    public function test_post__in_should_return_no_posts_when_0() {
     785        self::factory()->post->create_many( 4 );
     786
     787        $query = new WP_Query(
     788            array(
     789                'post_type' => 'post',
     790                'post__in'  => array( 0 ),
     791            )
     792        );
     793
     794        $this->assertSame( array(), $query->posts );
     795        $this->assertSame( 0, $query->found_posts );
     796    }
     797
     798    /**
    782799     * @ticket 57296
    783800     * @covers WP_Query::get_posts
  • trunk/tests/phpunit/tests/term/query.php

    r60729 r61048  
    10501050
    10511051        $this->assertContains( $t1, $q->terms );
     1052    }
     1053
     1054    /**
     1055     * @ticket 47719
     1056     */
     1057    public function test_include_should_return_no_terms_when_0() {
     1058        register_taxonomy( 'wptests_tax', 'post' );
     1059
     1060        self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     1061
     1062        $query = new WP_Term_Query(
     1063            array(
     1064                'taxonomy' => 'wptests_tax',
     1065                'include'  => array( 0 ),
     1066            )
     1067        );
     1068
     1069        $expected = array();
     1070        $this->assertSame( $expected, $query->terms );
     1071        $this->assertSame( $expected, $query->get_terms() );
    10521072    }
    10531073
  • trunk/tests/phpunit/tests/user/query.php

    r61037 r61048  
    17391739        // Make sure manually setting total_users doesn't get overwritten.
    17401740        $this->assertSame( 1, $q->total_users );
     1741    }
     1742
     1743    /**
     1744     * @ticket 47719
     1745     */
     1746    public function test_include_should_return_no_users_when_0() {
     1747        $query = new WP_User_Query(
     1748            array(
     1749                'role'    => '',
     1750                'include' => array( 0 ),
     1751            )
     1752        );
     1753
     1754        $this->assertSame( array(), $query->get_results() );
    17411755    }
    17421756
Note: See TracChangeset for help on using the changeset viewer.