Changeset 63358
- Timestamp:
- 08/27/2026 09:28:56 AM (7 weeks ago)
- Location:
- trunk
- Files:
-
- 6 edited
-
src/wp-includes/class-wp-query.php (modified) (3 diffs)
-
src/wp-includes/class-wp-term-query.php (modified) (5 diffs)
-
src/wp-includes/post.php (modified) (1 diff)
-
tests/phpunit/tests/post/getPosts.php (modified) (1 diff)
-
tests/phpunit/tests/query/fieldsClause.php (modified) (1 diff)
-
tests/phpunit/tests/term/query.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r63357 r63358 3296 3296 } 3297 3297 3298 /** @var int[] */ 3299 $post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' ); 3300 3301 foreach ( $post_parents as $cache_key => $post_parent ) { 3298 /** @var array<non-falsy-string, int|false> $cached_post_parents */ 3299 $cached_post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' ); 3300 3301 $post_parents = array(); 3302 $this->posts = array(); 3303 3304 foreach ( $cached_post_parents as $cache_key => $post_parent ) { 3302 3305 $obj = new stdClass(); 3303 3306 $obj->ID = (int) str_replace( 'post_parent:', '', $cache_key ); … … 3305 3308 3306 3309 $this->posts[] = $obj; 3310 3311 $post_parents[ $obj->ID ] = $obj->post_parent; 3307 3312 } 3308 3313 … … 3969 3974 * 3970 3975 * @phpstan-return ( 3971 * $query is array{ fields: 'ids', ... } ? int[] : WP_Post[] 3976 * $query is array{ fields: 'ids', ... } 3977 * ? int[] 3978 * : ( $query is array{ fields: 'id=>parent', ... } 3979 * ? array<int, int> 3980 * : WP_Post[] ) 3972 3981 * ) 3973 3982 */ -
trunk/src/wp-includes/class-wp-term-query.php
r63320 r63358 301 301 * 302 302 * @param string|array $query Array or URL query string of parameters. 303 * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string 304 * when 'count' is passed to `$args['fields']`. 303 * @return WP_Term[]|int[]|string[]|int|string Array of terms, or number of terms as numeric string 304 * when 'count' is passed to `$args['fields']`, or the 305 * integer 0 when the queried parent term is not in the 306 * taxonomy hierarchy. 307 * 308 * @phpstan-return ( 309 * $query is array{ fields: 'count', ... } 310 * ? 0|numeric-string 311 * : ( $query is array{ fields: 'ids'|'tt_ids', ... } 312 * ? int[] 313 * : ( $query is array{ fields: 'id=>parent', ... } 314 * ? array<int, int> 315 * : ( $query is array{ fields: 'names'|'slugs', ... } 316 * ? string[] 317 * : ( $query is array{ fields: 'id=>name'|'id=>slug', ... } 318 * ? array<int, string> 319 * : WP_Term[] ) ) ) ) 320 * ) 305 321 */ 306 322 public function query( $query ) { … … 319 335 * - 'all_with_object_id' 320 336 * 321 * The following will result in a numeric string being returned: 337 * The following will result in a numeric string being returned, or the integer 0 338 * when the queried parent term is not in the taxonomy hierarchy: 322 339 * 323 340 * - 'count' … … 330 347 * - 'slugs' 331 348 * 332 * The following will result in an array of numeric strings being returned:349 * The following will result in an array of integers being returned: 333 350 * 334 351 * - 'id=>parent' 335 *336 * The following will result in an array of integers being returned:337 *338 352 * - 'ids' 339 353 * - 'tt_ids' … … 343 357 * @global wpdb $wpdb WordPress database abstraction object. 344 358 * 345 * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string 346 * when 'count' is passed to `$args['fields']`. 359 * @return WP_Term[]|int[]|string[]|int|string Array of terms, or number of terms as numeric string 360 * when 'count' is passed to `$args['fields']`, or the 361 * integer 0 when the queried parent term is not in the 362 * taxonomy hierarchy. 363 * 364 * @phpstan-return 0|numeric-string|int[]|array<int, int>|string[]|array<int, string>|WP_Term[] 347 365 */ 348 366 public function get_terms() { … … 803 821 804 822 if ( 'count' === $_fields ) { 823 // The request selects a single COUNT column, so it only returns null on a database error. 824 /** @var numeric-string $count */ 805 825 $count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared 806 826 if ( $args['cache_results'] ) { -
trunk/src/wp-includes/post.php
r63002 r63358 2620 2620 * 2621 2621 * @phpstan-return ( 2622 * $args is array{ fields: 'ids', ... } ? int[] : WP_Post[] 2622 * $args is array{ fields: 'ids', ... } 2623 * ? int[] 2624 * : ( $args is array{ fields: 'id=>parent', ... } 2625 * ? array<int, int> 2626 * : WP_Post[] ) 2623 2627 * ) 2624 2628 */ -
trunk/tests/phpunit/tests/post/getPosts.php
r62041 r63358 177 177 $this->assertSame( array( $second_post_id ), $found_post_ids ); 178 178 } 179 180 /** 181 * Verifies what get_posts() returns for the `id=>parent` fields value. 182 * 183 * The second call is served from the `post-queries` cache, which previously returned 184 * the parent IDs keyed by the `post_parent:{$post_id}` cache keys they are stored 185 * under rather than by post ID. 186 * 187 * The cache hit is not specific to an external object cache being in use: the second 188 * call is served from the cache with the default in-memory implementation as well. 189 * 190 * @ticket 65817 191 */ 192 public function test_should_return_parent_ids_keyed_by_post_id_when_cached(): void { 193 $parent_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); 194 $this->assertIsInt( $parent_id, 'The parent page was not created.' ); 195 196 $child_id = self::factory()->post->create( 197 array( 198 'post_type' => 'page', 199 'post_parent' => $parent_id, 200 ) 201 ); 202 $this->assertIsInt( $child_id, 'The child page was not created.' ); 203 204 $args = array( 205 'post_type' => 'page', 206 'fields' => 'id=>parent', 207 'numberposts' => -1, 208 'orderby' => 'ID', 209 'order' => 'ASC', 210 ); 211 212 $expected = array( 213 $parent_id => 0, 214 $child_id => $parent_id, 215 ); 216 217 $this->assertSame( $expected, get_posts( $args ), 'The uncached call is not of the expected form.' ); 218 $this->assertSame( $expected, get_posts( $args ), 'The cached call is not of the expected form.' ); 219 } 179 220 } -
trunk/tests/phpunit/tests/query/fieldsClause.php
r57653 r63358 213 213 214 214 /** 215 * Tests the value returned when the fields are limited to the ID and parent sub-set. 216 * 217 * The second query is served from the `post-queries` cache, which previously returned 218 * the parent IDs keyed by the `post_parent:{$post_id}` cache keys they are stored 219 * under rather than by post ID. 220 * 221 * The cache hit is not specific to an external object cache being in use: the second 222 * query is served from the cache with the default in-memory implementation as well. 223 * 224 * @ticket 65817 225 * 226 * @covers WP_Query::query 227 */ 228 public function test_id_and_parent_subset_should_return_parent_ids_keyed_by_post_id_when_cached() { 229 $parent_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); 230 $this->assertIsInt( $parent_id, 'The parent page was not created.' ); 231 232 $child_id = self::factory()->post->create( 233 array( 234 'post_type' => 'page', 235 'post_parent' => $parent_id, 236 ) 237 ); 238 $this->assertIsInt( $child_id, 'The child page was not created.' ); 239 240 $query_args = array( 241 'post_type' => 'page', 242 'fields' => 'id=>parent', 243 'posts_per_page' => -1, 244 'orderby' => 'ID', 245 'order' => 'ASC', 246 ); 247 248 $expected = array( 249 $parent_id => 0, 250 $child_id => $parent_id, 251 ); 252 253 $q1 = new WP_Query(); 254 $this->assertSame( $expected, $q1->query( $query_args ), 'The uncached query is not of the expected form.' ); 255 256 $q2 = new WP_Query(); 257 $this->assertSame( $expected, $q2->query( $query_args ), 'The cached query is not of the expected form.' ); 258 } 259 260 /** 261 * Tests the value returned when the fields are limited to the ID and parent sub-set 262 * and the query results are not cached. 263 * 264 * With caching disabled every query runs against the database, so the `post-queries` 265 * cache is never consulted. 266 * 267 * @ticket 65817 268 * 269 * @covers WP_Query::query 270 */ 271 public function test_id_and_parent_subset_should_return_parent_ids_keyed_by_post_id_when_not_caching_results() { 272 $parent_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); 273 $this->assertIsInt( $parent_id, 'The parent page was not created.' ); 274 275 $child_id = self::factory()->post->create( 276 array( 277 'post_type' => 'page', 278 'post_parent' => $parent_id, 279 ) 280 ); 281 $this->assertIsInt( $child_id, 'The child page was not created.' ); 282 283 $query_args = array( 284 'post_type' => 'page', 285 'fields' => 'id=>parent', 286 'posts_per_page' => -1, 287 'orderby' => 'ID', 288 'order' => 'ASC', 289 'cache_results' => false, 290 ); 291 292 $expected = array( 293 $parent_id => 0, 294 $child_id => $parent_id, 295 ); 296 297 $q1 = new WP_Query(); 298 $this->assertSame( $expected, $q1->query( $query_args ), 'First query is not of the expected form.' ); 299 300 $q2 = new WP_Query(); 301 $this->assertSame( $expected, $q2->query( $query_args ), 'Second query is not of the expected form.' ); 302 } 303 304 /** 305 * Tests the posts property when the fields are limited to the ID and parent sub-set 306 * and the query matches nothing. 307 * 308 * An empty result set is cached like any other, and the cached branch only ever 309 * appended to the posts property, so a second identical query left it as null where 310 * the first left it an empty array. 311 * 312 * @ticket 65817 313 */ 314 public function test_id_and_parent_subset_should_populate_posts_property_when_cached_query_has_no_results() { 315 $query_args = array( 316 'post_type' => 'wptests_pt', 317 'fields' => 'id=>parent', 318 'name' => 'this-slug-does-not-exist', 319 ); 320 321 $q1 = new WP_Query(); 322 $this->assertSame( array(), $q1->query( $query_args ), 'The uncached query did not return an empty array.' ); 323 $this->assertSame( array(), $q1->posts, 'The posts property is not an empty array after the uncached query.' ); 324 325 $q2 = new WP_Query(); 326 $this->assertSame( array(), $q2->query( $query_args ), 'The cached query did not return an empty array.' ); 327 $this->assertSame( array(), $q2->posts, 'The posts property is not an empty array after the cached query.' ); 328 } 329 330 /** 215 331 * Filters the posts fields. 216 332 * -
trunk/tests/phpunit/tests/term/query.php
r61048 r63358 1208 1208 $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' ); 1209 1209 } 1210 1211 /** 1212 * Verifies what WP_Term_Query::query() returns for the `id=>parent` fields value. 1213 * 1214 * Unlike WP_Query, both the uncached and the cached path run the results through 1215 * WP_Term_Query::format_terms(), so the parent IDs are keyed by term ID either way. 1216 * 1217 * The assertion is repeated for a second query so that the cached path is covered 1218 * both when an external object cache is in use and when it is not. 1219 * 1220 * @ticket 65817 1221 * 1222 * @covers WP_Term_Query::query 1223 */ 1224 public function test_id_and_parent_fields_should_return_parent_ids_keyed_by_term_id() { 1225 register_taxonomy( 'wptests_tax_hierarchical', 'post', array( 'hierarchical' => true ) ); 1226 1227 $parent_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_hierarchical' ) ); 1228 $this->assertIsInt( $parent_id, 'The parent term was not created.' ); 1229 1230 $child_id = self::factory()->term->create( 1231 array( 1232 'taxonomy' => 'wptests_tax_hierarchical', 1233 'parent' => $parent_id, 1234 ) 1235 ); 1236 $this->assertIsInt( $child_id, 'The child term was not created.' ); 1237 1238 $query_args = array( 1239 'taxonomy' => 'wptests_tax_hierarchical', 1240 'fields' => 'id=>parent', 1241 'hide_empty' => false, 1242 'orderby' => 'term_id', 1243 'order' => 'ASC', 1244 ); 1245 1246 $expected = array( 1247 $parent_id => 0, 1248 $child_id => $parent_id, 1249 ); 1250 1251 $q1 = new WP_Term_Query(); 1252 $this->assertSame( $expected, $q1->query( $query_args ), 'First query is not of the expected form.' ); 1253 1254 $q2 = new WP_Term_Query(); 1255 $this->assertSame( $expected, $q2->query( $query_args ), 'Second query is not of the expected form.' ); 1256 } 1210 1257 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)