Skip to content

Commit d6e1720

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@61048 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 409d03b commit d6e1720

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

src/wp-includes/class-wp-term-query.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,11 @@ public function get_terms() {
470470
$exclude_tree = $args['exclude_tree'];
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-
}
479477

480-
if ( ! empty( $inclusions ) ) {
481478
$this->sql_clauses['where']['inclusions'] = 't.term_id IN ( ' . $inclusions . ' )';
482479
}
483480

@@ -815,10 +812,13 @@ public function get_terms() {
815812
$terms = $wpdb->get_results( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
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 );
818+
wp_cache_set_salted( $cache_key, $this->terms, 'term-queries', $last_changed );
820819
}
821-
return array();
820+
821+
return $this->terms;
822822
}
823823

824824
$term_ids = wp_list_pluck( $terms, 'term_id' );

tests/phpunit/tests/post/query.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,23 @@ public function test_found_posts_should_be_integer_even_if_found_posts_filter_re
778778
$this->assertIsInt( $q->found_posts );
779779
}
780780

781+
/**
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+
781798
/**
782799
* @ticket 57296
783800
* @covers WP_Query::get_posts

tests/phpunit/tests/term/query.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,26 @@ public function test_hide_empty_should_include_empty_parents_of_nonempty_childre
10511051
$this->assertContains( $t1, $q->terms );
10521052
}
10531053

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() );
1072+
}
1073+
10541074
/**
10551075
* Ensure cache keys are generated without WPDB placeholders.
10561076
*

tests/phpunit/tests/user/query.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,20 @@ public function test_users_pre_query_filter_should_bypass_database_query() {
17401740
$this->assertSame( 1, $q->total_users );
17411741
}
17421742

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() );
1755+
}
1756+
17431757
public static function filter_users_pre_query( $posts, $query ) {
17441758
$query->total_users = 1;
17451759

0 commit comments

Comments
 (0)