Skip to content

Commit

Permalink
Update behavior based on #46150.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Dec 26, 2022
1 parent a45fc65 commit 787aa97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 56 deletions.
25 changes: 16 additions & 9 deletions lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

if ( ! function_exists( 'wp_theme_use_persistent_cache' ) ) {
/**
* Returns whether theme file-based logic should use caching where applicable.
* Returns whether theme file-based logic should use persistent caching where applicable.
*
* Using a cache for theme file-based logic is typically a good approach to improve performance, particularly in
* production environments. However, when developing a theme, using a cache can be detrimental as changes to the
Expand All @@ -22,12 +22,13 @@
* @return bool True when a cache should be used for theme file-based logic, false otherwise.
*/
function wp_theme_use_persistent_cache() {
// By default, use a cache unless in a local or development environment.
// This is not reliable though, so a filter is available for more granular handling.
$use_cache = ! in_array( wp_get_environment_type(), array( 'local', 'development' ), true );
// By default, do not use a persistent cache.
// Potentially we can use a more reasonable default later, once persistent caching around the theme file-based
// logic has been improved.
$use_cache = false;

/**
* Filters whether theme file-based logic should use caching where applicable.
* Filters whether theme file-based logic should use persistent caching where applicable.
*
* Using a cache for theme file-based logic is typically a good approach to improve performance, particularly
* in production environments. However, when developing a theme, using a cache can be detrimental as changes to
Expand All @@ -53,7 +54,6 @@ function wp_theme_use_persistent_cache() {
* Whether a theme or its parent have a theme.json file.
*
* The result would be cached via the WP_Object_Cache.
* It can be cleared by calling wp_theme_has_theme_json_clean_cache().
*
* @return boolean
*/
Expand All @@ -69,7 +69,7 @@ function wp_theme_has_theme_json() {
* with the $found parameter which apparently had some issues in some implementations
* https://developer.wordpress.org/reference/functions/wp_cache_get/
*/
if ( wp_theme_use_persistent_cache() && is_int( $theme_has_support ) ) {
if ( is_int( $theme_has_support ) ) {
return (bool) $theme_has_support;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ function wp_theme_has_theme_json_clean_cache() {
* @return string Stylesheet.
*/
function gutenberg_get_global_stylesheet( $types = array() ) {
$can_use_cached = empty( $types ) && wp_theme_use_persistent_cache();
$can_use_cached = empty( $types );
$cache_key = 'gutenberg_get_global_stylesheet';
$cache_group = 'theme_json';
if ( $can_use_cached ) {
Expand Down Expand Up @@ -215,7 +215,7 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) {
$cache_key = 'gutenberg_get_global_settings_' . $origin;
$settings = wp_cache_get( $cache_key, $cache_group );

if ( false === $settings || ! wp_theme_use_persistent_cache() ) {
if ( false === $settings ) {
$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings();
wp_cache_set( $cache_key, $settings, $cache_group );
}
Expand Down Expand Up @@ -257,8 +257,15 @@ function _gutenberg_clean_theme_json_caches() {
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* See https://github.com/WordPress/gutenberg/pull/45372
*
* Persistent caching of theme file-based logic can be enabled on demand through the
* {@see 'wp_theme_use_persistent_cache'} filter, however at this point doing so is
* discouraged except for experiments.
*/
function _gutenberg_add_non_persistent_theme_json_cache_group() {
if ( wp_theme_use_persistent_cache() ) {
return;
}
wp_cache_add_non_persistent_groups( 'theme_json' );
}
add_action( 'plugins_loaded', '_gutenberg_add_non_persistent_theme_json_cache_group' );
48 changes: 1 addition & 47 deletions phpunit/get-global-styles-and-settings-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,13 @@

class WP_Get_Global_Styles_And_Settings_Test extends WP_UnitTestCase {

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

public static function wpSetUpBeforeClass( $factory ) {
self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
}

public static function wpTearDownAfterClass() {
self::delete_user( self::$administrator_id );
}

public function test_wp_theme_use_persistent_cache() {
$expected_default = ! in_array( wp_get_environment_type(), array( 'local', 'development' ), true );

$this->assertSame( $expected_default, wp_theme_use_persistent_cache() );
$this->assertFalse( wp_theme_use_persistent_cache() );

add_filter( 'wp_theme_use_persistent_cache', '__return_true' );
$this->assertTrue( wp_theme_use_persistent_cache() );

add_filter( 'wp_theme_use_persistent_cache', '__return_false' );
$this->assertFalse( wp_theme_use_persistent_cache() );
}

public function test_global_styles_user_cpt_change_invalidates_cached_stylesheet() {
$this->override_theme_root( realpath( DIR_TESTDATA . '/themedir1' ) );
add_filter( 'wp_theme_use_persistent_cache', '__return_true' );
switch_theme( 'block-theme' );
wp_set_current_user( self::$administrator_id );

$styles = gutenberg_get_global_stylesheet();
$this->assertStringNotContainsString( 'background-color: hotpink;', $styles );

$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true );
$config = json_decode( $user_cpt['post_content'], true );
$config['styles']['color']['background'] = 'hotpink';
$user_cpt['post_content'] = wp_json_encode( $config );

wp_update_post( $user_cpt, true, false );

$styles = gutenberg_get_global_stylesheet();
$this->assertStringContainsString( 'background-color: hotpink;', $styles );
}

public function override_theme_root( $theme_root ) {
$override = function() use ( $theme_root ) {
return $theme_root;
};
add_filter( 'theme_root', $override );
add_filter( 'stylesheet_root', $override );
add_filter( 'template_root', $override );
}
}

1 comment on commit 787aa97

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3783388820
📝 Reported issues:

Please sign in to comment.