Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use low-level cache for get_user_data_from_wp_global_styles(). #3517

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,17 +423,6 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
),
);

$cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );
$post_id = (int) get_transient( $cache_key );
// Special case: '-1' is a results not found.
if ( -1 === $post_id && ! $create_post ) {
return $user_cpt;
}

if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) {
return get_post( $post_id, ARRAY_A );
}

$global_style_query = new WP_Query();
$recent_posts = $global_style_query->query( $args );
if ( count( $recent_posts ) === 1 ) {
Expand All @@ -456,8 +445,6 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
$user_cpt = get_post( $cpt_post_id, ARRAY_A );
}
}
$cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS;
set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration );
Copy link
Contributor Author

@peterwilsoncc peterwilsoncc Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching to low level caching in wp-query per discussion on ticket.


return $user_cpt;
}
Expand Down
45 changes: 39 additions & 6 deletions tests/phpunit/tests/theme/wpThemeJsonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {

/**
* Administrator ID.
*
* @var int
*/
protected static $administrator_id;

/**
* Theme root directory.
*
Expand Down Expand Up @@ -64,6 +71,13 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
public static function set_up_before_class() {
parent::set_up_before_class();

self::$administrator_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_email' => '[email protected]',
)
);

static::$property_blocks_cache = new ReflectionProperty( WP_Theme_JSON_Resolver::class, 'blocks_cache' );
static::$property_blocks_cache->setAccessible( true );
static::$property_blocks_cache_orig_value = static::$property_blocks_cache->getValue();
Expand Down Expand Up @@ -620,6 +634,7 @@ function test_merges_child_theme_json_into_parent_theme_json() {
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
*/
function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
wp_set_current_user( self::$administrator_id );
$theme = wp_get_theme();
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
add_filter( 'query', array( $this, 'filter_db_query' ) );
Expand All @@ -629,23 +644,41 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries(
WP_Theme_JSON_Resolver::clean_cached_data();
}
$query_count = count( $this->queries ) - $query_count;
$this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
$this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );

$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
$this->assertEmpty( $user_cpt );
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );

$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
$this->assertNotEmpty( $user_cpt );
$this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' );

$query_count = count( $this->queries );
for ( $i = 0; $i < 3; $i ++ ) {
$new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver::clean_cached_data();
$this->assertSameSets( $user_cpt, $new_user_cpt );
$this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." );
}
$query_count = count( $this->queries ) - $query_count;
$this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has increased from zero to one.

As the transient has been removed, it's no longer set on post creation so a single query is made on the following call (in run 0), in subsequent runs, it is cached.

}

/**
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
*/
function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() {
$theme = wp_get_theme();
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
add_filter( 'query', array( $this, 'filter_db_query' ) );
$query_count = count( $this->queries );
for ( $i = 0; $i < 3; $i++ ) {
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver::clean_cached_data();
}
$query_count = count( $this->queries ) - $query_count;
$this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
remove_filter( 'query', array( $this, 'filter_db_query' ) );
$this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );

$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
}

/**
Expand Down