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

Global Styles: Use low-level cache for get_user_data_from_wp_global_styles #45634

Merged
merged 10 commits into from
Nov 9, 2022
74 changes: 0 additions & 74 deletions lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,80 +146,6 @@ public static function get_style_variations() {
return $variations;
}

/**
* Returns the custom post type that contains the user's origin config
* for the current theme or a void array if none are found.
*
* This can also create and return a new draft custom post type.
*
* @param WP_Theme $theme The theme object. If empty, it
* defaults to the current theme.
* @param bool $create_post Optional. Whether a new custom post
* type should be created if none are
* found. False by default.
* @param array $post_status_filter Filter Optional. custom post type by
* post status. ['publish'] by default,
* so it only fetches published posts.
* @return array Custom Post Type for the user's origin config.
*/
public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) {
if ( ! $theme instanceof WP_Theme ) {
$theme = wp_get_theme();
}
$user_cpt = array();
$post_type_filter = 'wp_global_styles';
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme->get_stylesheet(),
),
),
);

$cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );
$post_id = wp_cache_get( $cache_key );

if ( (int) $post_id > 0 ) {
return get_post( $post_id, ARRAY_A );
}

// Special case: '-1' is a results not found.
if ( -1 === $post_id && ! $create_post ) {
return $user_cpt;
}

$recent_posts = wp_get_recent_posts( $args );
if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) {
$user_cpt = $recent_posts[0];
} elseif ( $create_post ) {
$cpt_post_id = wp_insert_post(
array(
'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_status' => 'publish',
'post_title' => __( 'Custom Styles', 'default' ),
'post_type' => $post_type_filter,
'post_name' => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ),
'tax_input' => array(
'wp_theme' => array( wp_get_theme()->get_stylesheet() ),
),
),
true
);
$user_cpt = get_post( $cpt_post_id, ARRAY_A );
}
$cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS;
wp_cache_set( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, '', $cache_expiration );

return $user_cpt;
}

/**
* Returns the user's origin config.
*
Expand Down
68 changes: 68 additions & 0 deletions lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,72 @@ public static function get_user_data() {

return static::$user;
}

/**
* Returns the custom post type that contains the user's origin config
* for the active theme or a void array if none are found.
*
* This can also create and return a new draft custom post type.
*
* @since 5.9.0
*
* @param WP_Theme $theme The theme object. If empty, it
* defaults to the active theme.
* @param bool $create_post Optional. Whether a new custom post
* type should be created if none are
* found. Default false.
* @param array $post_status_filter Optional. Filter custom post type by
* post status. Default `array( 'publish' )`,
* so it only fetches published posts.
* @return array Custom Post Type for the user's origin config.
*/
public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) {
if ( ! $theme instanceof WP_Theme ) {
$theme = wp_get_theme();
}
$user_cpt = array();
$post_type_filter = 'wp_global_styles';
$stylesheet = $theme->get_stylesheet();
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $stylesheet,
),
),
);

$global_style_query = new WP_Query();
$recent_posts = $global_style_query->query( $args );
if ( count( $recent_posts ) === 1 ) {
$user_cpt = get_post( $recent_posts[0], ARRAY_A );
} elseif ( $create_post ) {
$cpt_post_id = wp_insert_post(
array(
'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_status' => 'publish',
'post_title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.
'post_type' => $post_type_filter,
'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),
'tax_input' => array(
'wp_theme' => array( $stylesheet ),
),
),
true
);
if ( ! is_wp_error( $cpt_post_id ) ) {
$user_cpt = get_post( $cpt_post_id, ARRAY_A );
}
}

return $user_cpt;
}
}
96 changes: 87 additions & 9 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,60 @@

class WP_Theme_JSON_Resolver_Gutenberg_Test extends WP_UnitTestCase {

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

/**
* WP_Theme_JSON_Resolver::$blocks_cache property.
*
* @var ReflectionProperty
*/
private static $property_blocks_cache;

/**
* Original value of the WP_Theme_JSON_Resolver::$blocks_cache property.
*
* @var array
*/
private static $property_blocks_cache_orig_value;

/**
* WP_Theme_JSON_Resolver::$core property.
*
* @var ReflectionProperty
*/
private static $property_core;

/**
* Original value of the WP_Theme_JSON_Resolver::$core property.
*
* @var WP_Theme_JSON
*/
private static $property_core_orig_value;

public static function set_up_before_class() {
parent::set_up_before_class();

self::$administrator_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_email' => 'administrator@example.com',
)
);

static::$property_blocks_cache = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'blocks_cache' );
static::$property_blocks_cache->setAccessible( true );
static::$property_blocks_cache_orig_value = static::$property_blocks_cache->getValue();

static::$property_core = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'core' );
static::$property_core->setAccessible( true );
static::$property_core_orig_value = static::$property_core->getValue();
}

public function set_up() {
parent::set_up();
$this->theme_root = realpath( __DIR__ . '/data/themedir1' );
Expand Down Expand Up @@ -256,28 +310,52 @@ public function test_merges_child_theme_json_into_parent_theme_json() {
}

public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
// Switch to a theme that does have support.
switch_theme( 'block-theme' );
wp_set_current_user( self::$administrator_id );
$theme = wp_get_theme();
WP_Theme_JSON_Resolver_Gutenberg::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_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme() );
WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
}
$query_count = count( $this->queries ) - $query_count;
$this->assertEquals( 1, $query_count, 'Only one SQL query should be peformed for multiple invocations of WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles()' );
$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_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme() );
$this->assertEmpty( $user_cpt );
$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );

$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true );
$this->assertNotEmpty( $user_cpt );
$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme, true );
$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_Gutenberg::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
$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.' );
}

/**
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
*/
public 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_Gutenberg::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_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme() );
WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
}
$query_count = count( $this->queries ) - $query_count;
$this->assertEquals( 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_Gutenberg::get_user_data_from_wp_global_styles( $theme );
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
}
}