diff --git a/backport-changelog/6.8/7596.md b/backport-changelog/6.8/7596.md new file mode 100644 index 00000000000000..2644b1ff947183 --- /dev/null +++ b/backport-changelog/6.8/7596.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/7596 + +* https://github.com/WordPress/gutenberg/pull/66155 diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 2231cb0f11538f..5c6c8872bbfe36 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -91,6 +91,14 @@ class WP_Theme_JSON_Resolver_Gutenberg { */ protected static $theme_json_file_cache = array(); + /** + * Cache for resolved files per theme. + * + * @since 6.8.0 + * @var array + */ + protected static $resolved_theme_uris_cache = array(); + /** * Processes a file that adheres to the theme.json schema * and returns an array with its contents, or a void array if none found. @@ -696,20 +704,22 @@ protected static function get_file_path_from_theme( $file_name, $template = fals * and `$i18n_schema` variables to reset. * @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables * to reset. + * @since 6.8.0 Added the `$resolved_theme_uris_cache` variable to reset. */ public static function clean_cached_data() { - static::$core = null; - static::$blocks = null; - static::$blocks_cache = array( + static::$core = null; + static::$blocks = null; + static::$blocks_cache = array( 'core' => array(), 'blocks' => array(), 'theme' => array(), 'user' => array(), ); - static::$theme = null; - static::$user = null; - static::$user_custom_post_type_id = null; - static::$i18n_schema = null; + static::$theme = null; + static::$user = null; + static::$user_custom_post_type_id = null; + static::$i18n_schema = null; + static::$resolved_theme_uris_cache = array(); } /** @@ -839,6 +849,7 @@ public static function get_style_variations_from_directory( $directory, $scope = * * @since 6.6.0 * @since 6.7.0 Added support for resolving block styles. + * @since 6.8.0 Added caching for resolved theme URIs. * * @param WP_Theme_JSON_Gutenberg $theme_json A theme json instance. * @return array An array of resolved paths. @@ -850,7 +861,12 @@ public static function get_resolved_theme_uris( $theme_json ) { return $resolved_theme_uris; } - $theme_json_data = $theme_json->get_raw_data(); + $theme_json_data = $theme_json->get_raw_data(); + $resolved_theme_uris_cache_key = md5( wp_json_encode( $theme_json_data ) ); + + if ( ! empty( static::$resolved_theme_uris_cache[ $resolved_theme_uris_cache_key ] ) ) { + return static::$resolved_theme_uris_cache[ $resolved_theme_uris_cache_key ]; + } // Using the same file convention when registering web fonts. See: WP_Font_Face_Resolver:: to_theme_file_uri. $placeholder = 'file:./'; @@ -901,7 +917,7 @@ public static function get_resolved_theme_uris( $theme_json ) { } } } - + static::$resolved_theme_uris_cache[ $resolved_theme_uris_cache_key ] = $resolved_theme_uris; return $resolved_theme_uris; } diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index d2339f2496290c..bed1a171263f4b 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -31,6 +31,20 @@ class WP_Theme_JSON_Resolver_Gutenberg_Test extends WP_UnitTestCase { */ private static $property_blocks_cache_orig_value; + /** + * WP_Theme_JSON_Resolver_Gutenberg::$resolved_theme_uris_cache property. + * + * @var ReflectionProperty + */ + private static $property_resolved_theme_uris_cache; + + /** + * Original value of the WP_Theme_JSON_Resolver_Gutenberg::$resolved_theme_uris_cache property. + * + * @var array + */ + private static $property_resolved_theme_uris_cache_orig_value; + /** * WP_Theme_JSON_Resolver_Gutenberg::$core property. * @@ -81,11 +95,16 @@ public static function set_up_before_class() { 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(); + + static::$property_resolved_theme_uris_cache = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'resolved_theme_uris_cache' ); + static::$property_resolved_theme_uris_cache->setAccessible( true ); + static::$property_resolved_theme_uris_cache_orig_value = static::$property_resolved_theme_uris_cache->getValue(); } public static function tear_down_after_class() { static::$property_blocks_cache->setValue( null, static::$property_blocks_cache_orig_value ); static::$property_core->setValue( null, static::$property_core_orig_value ); + static::$property_resolved_theme_uris_cache->setValue( null, static::$property_resolved_theme_uris_cache_orig_value ); parent::tear_down_after_class(); } @@ -1347,7 +1366,12 @@ public function test_get_resolved_theme_uris() { $actual = WP_Theme_JSON_Resolver_Gutenberg::get_resolved_theme_uris( $theme_json ); remove_filter( 'theme_file_uri', $filter_theme_file_uri_callback ); - $this->assertSame( $expected_data, $actual ); + $this->assertSame( $expected_data, $actual, 'Resolved theme uris do not match.' ); + + // Test that resolved theme uris are cached. + $cache_key = md5( wp_json_encode( $theme_json->get_raw_data() ) ); + $expected_cache_data = array( "$cache_key" => $actual ); + $this->assertSame( $expected_cache_data, static::$property_resolved_theme_uris_cache->getValue(), 'Resolved theme uris cache data does not match.' ); } /**