- Timestamp:
- 10/27/2022 04:32:42 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json-resolver.php
r54517 r54706 423 423 ), 424 424 ); 425 426 $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );427 $post_id = (int) get_transient( $cache_key );428 // Special case: '-1' is a results not found.429 if ( -1 === $post_id && ! $create_post ) {430 return $user_cpt;431 }432 433 if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) {434 return get_post( $post_id, ARRAY_A );435 }436 425 437 426 $global_style_query = new WP_Query(); … … 457 446 } 458 447 } 459 $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS;460 set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration );461 448 462 449 return $user_cpt; -
trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php
r54631 r54706 14 14 15 15 /** 16 * Administrator ID. 17 * 18 * @var int 19 */ 20 protected static $administrator_id; 21 22 /** 16 23 * Theme root directory. 17 24 * … … 64 71 public static function set_up_before_class() { 65 72 parent::set_up_before_class(); 73 74 self::$administrator_id = self::factory()->user->create( 75 array( 76 'role' => 'administrator', 77 'user_email' => 'administrator@example.com', 78 ) 79 ); 66 80 67 81 static::$property_blocks_cache = new ReflectionProperty( WP_Theme_JSON_Resolver::class, 'blocks_cache' ); … … 621 635 */ 622 636 function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() { 637 wp_set_current_user( self::$administrator_id ); 623 638 $theme = wp_get_theme(); 624 639 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); … … 630 645 } 631 646 $query_count = count( $this->queries ) - $query_count; 632 $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type ' );647 $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); 633 648 634 649 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 635 $this->assertEmpty( $user_cpt );650 $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); 636 651 637 652 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); 638 $this->assertNotEmpty( $user_cpt );653 $this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' ); 639 654 640 655 $query_count = count( $this->queries ); … … 642 657 $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 643 658 WP_Theme_JSON_Resolver::clean_cached_data(); 644 $this->assertSameSets( $user_cpt, $new_user_cpt );659 $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); 645 660 } 646 661 $query_count = count( $this->queries ) - $query_count; 647 $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); 648 remove_filter( 'query', array( $this, 'filter_db_query' ) ); 662 $this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); 663 } 664 665 /** 666 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles 667 */ 668 function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() { 669 $theme = wp_get_theme(); 670 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 671 add_filter( 'query', array( $this, 'filter_db_query' ) ); 672 $query_count = count( $this->queries ); 673 for ( $i = 0; $i < 3; $i++ ) { 674 WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 675 WP_Theme_JSON_Resolver::clean_cached_data(); 676 } 677 $query_count = count( $this->queries ) - $query_count; 678 $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); 679 680 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); 681 $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); 649 682 } 650 683
Note: See TracChangeset
for help on using the changeset viewer.