Changeset 54186
- Timestamp:
- 09/16/2022 10:55:58 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json-resolver.php
r54162 r54186 305 305 $user_cpt = array(); 306 306 $post_type_filter = 'wp_global_styles'; 307 $stylesheet = $theme->get_stylesheet(); 307 308 $args = array( 308 'numberposts' => 1, 309 'orderby' => 'date', 310 'order' => 'desc', 311 'post_type' => $post_type_filter, 312 'post_status' => $post_status_filter, 313 'tax_query' => array( 309 'posts_per_page' => 1, 310 'orderby' => 'post_date', 311 'order' => 'desc', 312 'post_type' => $post_type_filter, 313 'post_status' => $post_status_filter, 314 'ignore_sticky_posts' => true, 315 'no_found_rows' => true, 316 'tax_query' => array( 314 317 array( 315 318 'taxonomy' => 'wp_theme', 316 319 'field' => 'name', 317 'terms' => $ theme->get_stylesheet(),320 'terms' => $stylesheet, 318 321 ), 319 322 ), … … 321 324 322 325 $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) ); 323 $post_id = wp_cache_get( $cache_key ); 324 325 if ( (int) $post_id > 0 ) { 326 return get_post( $post_id, ARRAY_A ); 327 } 328 326 $post_id = (int) get_transient( $cache_key ); 329 327 // Special case: '-1' is a results not found. 330 328 if ( -1 === $post_id && ! $create_post ) { … … 332 330 } 333 331 334 $recent_posts = wp_get_recent_posts( $args ); 335 if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) { 336 $user_cpt = $recent_posts[0]; 332 if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) { 333 return get_post( $post_id, ARRAY_A ); 334 } 335 336 $global_style_query = new WP_Query(); 337 $recent_posts = $global_style_query->query( $args ); 338 if ( count( $recent_posts ) === 1 ) { 339 $user_cpt = get_post( $recent_posts[0], ARRAY_A ); 337 340 } elseif ( $create_post ) { 338 341 $cpt_post_id = wp_insert_post( … … 340 343 'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }', 341 344 'post_status' => 'publish', 342 'post_title' => 'Custom Styles',345 'post_title' => __( 'Custom Styles' ), 343 346 'post_type' => $post_type_filter, 344 'post_name' => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ),347 'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ), 345 348 'tax_input' => array( 346 'wp_theme' => array( wp_get_theme()->get_stylesheet()),349 'wp_theme' => array( $stylesheet ), 347 350 ), 348 351 ), 349 352 true 350 353 ); 351 $user_cpt = get_post( $cpt_post_id, ARRAY_A ); 354 if ( ! is_wp_error( $cpt_post_id ) ) { 355 $user_cpt = get_post( $cpt_post_id, ARRAY_A ); 356 } 352 357 } 353 358 $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS; 354 wp_cache_set( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, '', $cache_expiration );359 set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration ); 355 360 356 361 return $user_cpt; -
trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php
r54184 r54186 354 354 } 355 355 356 /** 357 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles 358 */ 356 359 function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() { 360 $theme = wp_get_theme(); 361 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 357 362 add_filter( 'query', array( $this, 'filter_db_query' ) ); 358 363 $query_count = count( $this->queries ); 359 364 for ( $i = 0; $i < 3; $i++ ) { 360 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ); 361 WP_Theme_JSON_Resolver::clean_cached_data(); 362 } 363 $query_count = count( $this->queries ) - $query_count; 364 $this->assertEquals( 1, $query_count, 'Only one SQL query should be peformed for multiple invocations of WP_Theme_JSON_Resolver::get_global_styles_from_post()' ); 365 366 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ); 367 $this->assertEmpty( $user_cpt ); 368 369 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ); 370 $this->assertNotEmpty( $user_cpt ); 371 372 $query_count = count( $this->queries ); 373 for ( $i = 0; $i < 3; $i++ ) { 374 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ); 365 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 375 366 WP_Theme_JSON_Resolver::clean_cached_data(); 376 367 } 377 368 $query_count = count( $this->queries ) - $query_count; 378 369 $this->assertEquals( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); 370 371 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 372 $this->assertEmpty( $user_cpt ); 373 374 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); 375 $this->assertNotEmpty( $user_cpt ); 376 377 $query_count = count( $this->queries ); 378 for ( $i = 0; $i < 3; $i ++ ) { 379 $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 380 WP_Theme_JSON_Resolver::clean_cached_data(); 381 $this->assertSameSets( $user_cpt, $new_user_cpt ); 382 } 383 $query_count = count( $this->queries ) - $query_count; 384 $this->assertEquals( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); 379 385 remove_filter( 'query', array( $this, 'filter_db_query' ) ); 380 386 } 387 388 /** 389 * @ticket 55392 390 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles 391 */ 392 function test_get_user_data_from_wp_global_styles_does_exist() { 393 $theme = wp_get_theme(); 394 $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); 395 $this->assertIsArray( $post1 ); 396 $this->assertArrayHasKey( 'ID', $post1 ); 397 wp_delete_post( $post1['ID'], true ); 398 $post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); 399 $this->assertIsArray( $post2 ); 400 $this->assertArrayHasKey( 'ID', $post2 ); 401 } 402 403 /** 404 * @ticket 55392 405 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles 406 */ 407 function test_get_user_data_from_wp_global_styles_create_post() { 408 $theme = wp_get_theme( 'testing' ); 409 $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 410 $this->assertIsArray( $post1 ); 411 $this->assertSameSets( array(), $post1 ); 412 $post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 413 $this->assertIsArray( $post2 ); 414 $this->assertSameSets( array(), $post2 ); 415 $post3 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); 416 $this->assertIsArray( $post3 ); 417 $this->assertArrayHasKey( 'ID', $post3 ); 418 } 419 420 /** 421 * @ticket 55392 422 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles 423 */ 424 function test_get_user_data_from_wp_global_styles_filter_state() { 425 $theme = wp_get_theme( 'foo' ); 426 $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true, array( 'publish' ) ); 427 $this->assertIsArray( $post1 ); 428 $this->assertArrayHasKey( 'ID', $post1 ); 429 $post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, false, array( 'draft' ) ); 430 $this->assertIsArray( $post2 ); 431 $this->assertSameSets( array(), $post2 ); 432 } 381 433 }
Note: See TracChangeset
for help on using the changeset viewer.