From 752ce7db9e8aff33c578c931e2c462fa59bbef4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Dec 2022 16:14:23 +0100 Subject: [PATCH 01/15] Add new public method wp_theme_has_theme_json --- .../global-styles-and-settings.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index b6c8c91b464c0..96fac51ce9e61 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -255,3 +255,43 @@ function ( $item ) { } } } + +/** + * Whether a theme or its parent have a theme.json file. + * + * @since 6.2.0 + * + * @return boolean + */ +function wp_theme_has_theme_json() { + $cache_group = 'theme_json'; + $cache_key = 'wp_theme_has_theme_json'; + $theme_has_support = wp_cache_get( $cache_key, $cache_group ); + + /** + * $theme_has_support is stored as an int in the cache. + * + * The reason not to store it as a boolean is to avoid working + * with the $found parameter which apparently had some issues in some implementations + * https://developer.wordpress.org/reference/functions/wp_cache_get/ + * + * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. + */ + if ( ! WP_DEBUG && is_int( $theme_has_support ) ) { + return (bool) $theme_has_support; + } + + // Has the own theme a theme.json? + $theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' ); + + // Look up the parent if the child does not have a theme.json. + if ( ! $theme_has_support ) { + $theme_has_support = is_readable( get_template_directory() . '/theme.json' ); + } + + $theme_has_support = $theme_has_support ? 1 : 0; + + wp_cache_set( $cache_key, $theme_has_support, $cache_group ); + + return (bool) $theme_has_support; +} From bb9d154eb08dbc3fad2ab4852a18af9e1a45940d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Dec 2022 16:18:51 +0100 Subject: [PATCH 02/15] Substitute WP_Theme_JSON_Resolver::theme_has_support by wp_theme_has_theme_json --- src/wp-admin/edit-form-blocks.php | 2 +- src/wp-admin/site-editor.php | 2 +- src/wp-includes/block-editor.php | 2 +- src/wp-includes/block-patterns.php | 2 +- src/wp-includes/block-supports/layout.php | 4 +-- src/wp-includes/block-template-utils.php | 4 +-- .../class-wp-theme-json-resolver.php | 25 +++++-------------- .../global-styles-and-settings.php | 4 +-- src/wp-includes/script-loader.php | 6 ++--- src/wp-includes/theme-templates.php | 2 +- .../tests/theme/wpThemeJsonResolver.php | 2 +- 11 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index f43c835168c1d..e324a4e1b7bd5 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -203,7 +203,7 @@ static function( $classes ) { 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), ), - 'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(), + 'supportsLayout' => wp_theme_has_theme_json(), 'supportsTemplateMode' => current_theme_supports( 'block-templates' ), // Whether or not to load the 'postcustom' meta box is stored as a user meta diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index a8b43b52af768..deb556a695768 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -74,7 +74,7 @@ static function( $classes ) { 'styles' => get_block_editor_theme_styles(), 'defaultTemplateTypes' => $indexed_template_types, 'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), - 'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(), + 'supportsLayout' => wp_theme_has_theme_json(), 'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ), '__unstableHomeTemplate' => $home_template, ); diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 7af9f2a90062a..4227c674c02ff 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -417,7 +417,7 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex } } - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( wp_theme_has_theme_json() ) { $block_classes = array( 'css' => 'styles', '__unstableType' => 'theme', diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index 074ed33c84b44..63398513d48dc 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -139,7 +139,7 @@ function _register_remote_theme_patterns() { return; } - if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { return; } diff --git a/src/wp-includes/block-supports/layout.php b/src/wp-includes/block-supports/layout.php index 7a882a9bab386..93c7364a323b6 100644 --- a/src/wp-includes/block-supports/layout.php +++ b/src/wp-includes/block-supports/layout.php @@ -464,7 +464,7 @@ function wp_restore_group_inner_container( $block_content, $block ) { ); if ( - WP_Theme_JSON_Resolver::theme_has_support() || + wp_theme_has_theme_json() || 1 === preg_match( $group_with_inner_container_regex, $block_content ) || ( isset( $block['attrs']['layout']['type'] ) && 'flex' === $block['attrs']['layout']['type'] ) ) { @@ -527,7 +527,7 @@ function wp_restore_image_outer_container( $block_content, $block ) { )/iUx"; if ( - WP_Theme_JSON_Resolver::theme_has_support() || + wp_theme_has_theme_json() || 0 === preg_match( $image_with_align, $block_content, $matches ) ) { return $block_content; diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 826237aa18946..66c48db7f1305 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -347,7 +347,7 @@ function _get_block_templates_files( $template_type ) { * @return array Template item. */ function _add_block_template_info( $template_item ) { - if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { return $template_item; } @@ -370,7 +370,7 @@ function _add_block_template_info( $template_item ) { * @return array Template info. */ function _add_block_template_part_area_info( $template_info ) { - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( wp_theme_has_theme_json() ) { $theme_data = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts(); } diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index b1f15897b1af5..dda55ff00153c 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -57,14 +57,6 @@ class WP_Theme_JSON_Resolver { */ protected static $theme = null; - /** - * Whether or not the theme supports theme.json. - * - * @since 5.8.0 - * @var bool - */ - protected static $theme_has_support = null; - /** * Container for data coming from the user. * @@ -295,7 +287,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() * and merge the static::$theme upon that. */ $theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_default_block_editor_settings() ); - if ( ! static::theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); } @@ -416,11 +408,11 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post /* * Bail early if the theme does not support a theme.json. * - * Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active + * Since wp_theme_has_theme_json() only supports the active * theme, the extra condition for whether $theme is the active theme is * present here. */ - if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) { + if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) { return array(); } @@ -597,18 +589,14 @@ public static function get_user_global_styles_post_id() { * * @since 5.8.0 * @since 5.9.0 Added a check in the parent theme. + * @deprecated 6.2.0 Use wp_theme_has_theme_json() instead. * * @return bool */ public static function theme_has_support() { - if ( ! isset( static::$theme_has_support ) ) { - static::$theme_has_support = ( - static::get_file_path_from_theme( 'theme.json' ) !== '' || - static::get_file_path_from_theme( 'theme.json', true ) !== '' - ); - } + _deprecated_function( __METHOD__, '6.2.0', 'wp_theme_has_theme_json()' ); - return static::$theme_has_support; + return wp_theme_has_theme_json(); } /** @@ -651,7 +639,6 @@ public static function clean_cached_data() { static::$theme = null; static::$user = null; static::$user_custom_post_type_id = null; - static::$theme_has_support = null; static::$i18n_schema = null; } diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 96fac51ce9e61..8492e6395a74a 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -102,7 +102,7 @@ function wp_get_global_stylesheet( $types = array() ) { $tree = WP_Theme_JSON_Resolver::get_merged_data(); - $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support(); + $supports_theme_json = wp_theme_has_theme_json(); if ( empty( $types ) && ! $supports_theme_json ) { $types = array( 'variables', 'presets', 'base-layout-styles' ); } elseif ( empty( $types ) ) { @@ -184,7 +184,7 @@ function wp_get_global_styles_svg_filters() { } } - $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support(); + $supports_theme_json = wp_theme_has_theme_json(); $origins = array( 'default', 'theme', 'custom' ); if ( ! $supports_theme_json ) { diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 27defd0659460..ab693a3f22b13 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1618,7 +1618,7 @@ function wp_default_styles( $styles ) { ); // Only load the default layout and margin styles for themes without theme.json file. - if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles'; } @@ -3665,7 +3665,7 @@ function _wp_theme_json_webfonts_handler() { * @since 6.1.0 */ function wp_enqueue_classic_theme_styles() { - if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { $suffix = wp_scripts_get_suffix(); wp_register_style( 'classic-theme-styles', '/' . WPINC . "/css/classic-themes$suffix.css", array(), true ); wp_enqueue_style( 'classic-theme-styles' ); @@ -3683,7 +3683,7 @@ function wp_enqueue_classic_theme_styles() { * @return array A filtered array of editor settings. */ function wp_add_editor_classic_theme_styles( $editor_settings ) { - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( wp_theme_has_theme_json() ) { return $editor_settings; } diff --git a/src/wp-includes/theme-templates.php b/src/wp-includes/theme-templates.php index 7ef010eea5ffd..ae71852a370e0 100644 --- a/src/wp-includes/theme-templates.php +++ b/src/wp-includes/theme-templates.php @@ -209,7 +209,7 @@ function the_block_template_skip_link() { * @since 5.8.0 */ function wp_enable_block_templates() { - if ( wp_is_block_theme() || WP_Theme_JSON_Resolver::theme_has_support() ) { + if ( wp_is_block_theme() || wp_theme_has_theme_json() ) { add_theme_support( 'block-templates' ); } } diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index cd71b889e1c97..830704c07ba07 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -477,7 +477,7 @@ public function test_add_theme_supports_are_loaded_for_themes_without_theme_json remove_theme_support( 'custom-line-height' ); remove_theme_support( 'editor-color-palette' ); - $this->assertFalse( WP_Theme_JSON_Resolver::theme_has_support() ); + $this->assertFalse( wp_theme_has_theme_json() ); $this->assertTrue( $settings['typography']['lineHeight'] ); $this->assertSame( $color_palette, $settings['color']['palette']['theme'] ); } From 5b09d4dcdd155c93bd862ad8af638462c3f79cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Dec 2022 17:46:25 +0100 Subject: [PATCH 03/15] Add tests --- .../block-theme-child-no-theme-json/style.css | 8 ++ .../default-child-no-theme-json/style.css | 8 ++ tests/phpunit/tests/theme/themeDir.php | 2 + .../tests/theme/wpThemeHasThemeJson.php | 86 +++++++++++++++++++ .../tests/theme/wpThemeJsonResolver.php | 16 ---- 5 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 tests/phpunit/data/themedir1/block-theme-child-no-theme-json/style.css create mode 100644 tests/phpunit/data/themedir1/default-child-no-theme-json/style.css create mode 100644 tests/phpunit/tests/theme/wpThemeHasThemeJson.php diff --git a/tests/phpunit/data/themedir1/block-theme-child-no-theme-json/style.css b/tests/phpunit/data/themedir1/block-theme-child-no-theme-json/style.css new file mode 100644 index 0000000000000..344441288258e --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme-child-no-theme-json/style.css @@ -0,0 +1,8 @@ +/* +Theme Name: Block Theme Child with no theme.json +Theme URI: https://wordpress.org/ +Description: For testing purposes only. +Template: block-theme +Version: 1.0.0 +Text Domain: block-theme-child-no-theme-json +*/ diff --git a/tests/phpunit/data/themedir1/default-child-no-theme-json/style.css b/tests/phpunit/data/themedir1/default-child-no-theme-json/style.css new file mode 100644 index 0000000000000..8f971990d9fec --- /dev/null +++ b/tests/phpunit/data/themedir1/default-child-no-theme-json/style.css @@ -0,0 +1,8 @@ +/* +Theme Name: Default Child Theme with no theme.json +Theme URI: https://wordpress.org/ +Description: For testing purposes only. +Template: default +Version: 1.0.0 +Text Domain: default-child-no-theme-json +*/ diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index 29fbbd392f05a..270895c4426bd 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -163,6 +163,7 @@ public function test_theme_list() { $theme_names = array_keys( $themes ); $expected = array( 'WordPress Default', + 'Default Child Theme with no theme.json', 'Sandbox', 'Stylesheet Only', 'My Theme', @@ -177,6 +178,7 @@ public function test_theme_list() { 'REST Theme', 'Block Theme', 'Block Theme Child Theme', + 'Block Theme Child with no theme.json', 'Block Theme Child Theme With Fluid Typography', 'Block Theme [0.4.0]', 'Block Theme [1.0.0] in subdirectory', diff --git a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php new file mode 100644 index 0000000000000..a538562b8183f --- /dev/null +++ b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php @@ -0,0 +1,86 @@ +assertTrue( wp_theme_has_theme_json() ); + } + + /** + * Test that it reports correctly themes that do not have a theme.json. + * + * @covers wp_theme_has_theme_json + */ + public function test_theme_has_no_theme_json() { + switch_theme( 'default' ); + $this->assertFalse( wp_theme_has_theme_json() ); + } + + /** + * Test it reports correctly child themes that have a theme.json. + * + * @covers wp_theme_has_theme_json + */ + public function test_child_theme_has_theme_json() { + switch_theme( 'block-theme-child' ); + $this->assertTrue( wp_theme_has_theme_json() ); + } + + /** + * Test that it reports correctly child themes that do not have a theme.json + * and the parent does. + * + * @covers wp_theme_has_theme_json + */ + public function test_child_theme_has_not_theme_json_but_parent_has() { + switch_theme( 'block-theme-child-no-theme-json' ); + $this->assertTrue( wp_theme_has_theme_json() ); + } + + /** + * Test that it reports correctly child themes that do not have a theme.json + * and the parent does not either. + * + * @covers wp_theme_has_theme_json + */ + public function test_neither_child_or_parent_themes_have_theme_json() { + switch_theme( 'default-child-no-theme-json' ); + $this->assertFalse( wp_theme_has_theme_json() ); + } + + /** + * Test that switching themes recalculates theme support. + * + * @ticket 52991 + * + * @covers wp_theme_has_theme_json + */ + public function test_switching_themes_recalculates_support() { + // The "default" theme doesn't have theme.json support. + switch_theme( 'default' ); + $default = wp_theme_has_theme_json(); + + // Switch to a theme that does have support. + switch_theme( 'block-theme' ); + $block_theme = wp_theme_has_theme_json(); + + $this->assertFalse( $default ); + $this->assertTrue( $block_theme ); + } + +} diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 830704c07ba07..8ae0548c4accb 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -431,22 +431,6 @@ public function data_get_core_data() { ); } - /** - * @ticket 52991 - */ - public function test_switching_themes_recalculates_data() { - // The "default" theme doesn't have theme.json support. - switch_theme( 'default' ); - $default = WP_Theme_JSON_Resolver::theme_has_support(); - - // Switch to a theme that does have support. - switch_theme( 'block-theme' ); - $has_theme_json_support = WP_Theme_JSON_Resolver::theme_has_support(); - - $this->assertFalse( $default ); - $this->assertTrue( $has_theme_json_support ); - } - /** * @ticket 54336 */ From 3dfbad3a4d41f69dbbac15c10397ce504715546b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Dec 2022 18:04:27 +0100 Subject: [PATCH 04/15] Port cache group management from PR 3712 --- src/wp-includes/default-filters.php | 4 +-- .../global-styles-and-settings.php | 27 +++++++++++++++++++ src/wp-includes/load.php | 2 +- src/wp-includes/ms-blogs.php | 4 +-- tests/phpunit/includes/abstract-testcase.php | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 5c623e371b195..2155300e3b7aa 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -346,8 +346,8 @@ add_action( 'init', '_register_core_block_patterns_and_categories' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); -add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); -add_action( 'start_previewing_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); +add_action( 'switch_theme', '_wp_clean_theme_json_caches' ); +add_action( 'start_previewing_theme', '_wp_clean_theme_json_caches' ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 8492e6395a74a..643f030a0c2fe 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -264,6 +264,22 @@ function ( $item ) { * @return boolean */ function wp_theme_has_theme_json() { + /** + * By using the 'theme_json' group, this data is marked to be non-persistent across requests. + * See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places. + * + * The rationale for this is to make sure derived data from theme.json + * is always fresh from the potential modifications done via hooks + * that can use dynamic data (modify the stylesheet depending on some option, + * settings depending on user permissions, etc.). + * See some of the existing hooks to modify theme.json behaviour: + * https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ + * + * A different alternative considered was to invalidate the cache upon certain + * 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 + */ $cache_group = 'theme_json'; $cache_key = 'wp_theme_has_theme_json'; $theme_has_support = wp_cache_get( $cache_key, $cache_group ); @@ -295,3 +311,14 @@ function wp_theme_has_theme_json() { return (bool) $theme_has_support; } + +/** + * Private function to clean the caches under the theme_json group. + * + * @since 6.1.2 + * @access private + */ +function _wp_clean_theme_json_caches() { + wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); + WP_Theme_JSON_Resolver::clean_cached_data(); +} \ No newline at end of file diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 4c212086df34e..c2450e582f958 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -753,7 +753,7 @@ function wp_start_object_cache() { ) ); - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } $first_init = false; diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 0ea6f85627dcb..7ff830e930863 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -575,7 +575,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { ); } - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } } @@ -666,7 +666,7 @@ function restore_current_blog() { ); } - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } } diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 6cfeaa307cc26..33df37ad44331 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -401,7 +401,7 @@ public static function flush_cache() { ) ); - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } /** From 04d72eeddfe3eb4b68d0329066a6ae326f722103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Dec 2022 18:13:53 +0100 Subject: [PATCH 05/15] Substitute call to clean_cached_data by _wp_clean_theme_json_caches --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 10 +++++----- .../tests/webfonts/wpThemeJsonWebfontsHandler.php | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 8ae0548c4accb..200088f85c55a 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -110,7 +110,7 @@ public function tear_down() { unset( $GLOBALS['wp_themes'] ); // Reset data between tests. - WP_Theme_JSON_Resolver::clean_cached_data(); + _wp_clean_theme_json_caches(); parent::tear_down(); } @@ -376,7 +376,7 @@ public function data_has_same_registered_blocks_when_all_blocks_are_cached() { * @ticket 56467 */ public function test_get_core_data( $should_fire_filter, $core_is_cached, $blocks_are_cached ) { - WP_Theme_JSON_Resolver::clean_cached_data(); + _wp_clean_theme_json_caches(); // If should cache core, then fire the method to cache it before running the tests. if ( $core_is_cached ) { @@ -621,7 +621,7 @@ function( $query ) use ( &$global_styles_query_count ) { ); 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(); + _wp_clean_theme_json_caches(); } $this->assertSame( 0, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); @@ -634,7 +634,7 @@ function( $query ) use ( &$global_styles_query_count ) { $global_styles_query_count = 0; 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(); + _wp_clean_theme_json_caches(); $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); } $this->assertSame( 1, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); @@ -651,7 +651,7 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q $query_count = get_num_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(); + _wp_clean_theme_json_caches(); } $query_count = get_num_queries() - $query_count; $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); diff --git a/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php b/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php index 5c609923d7b39..30df269803d2d 100644 --- a/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php +++ b/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php @@ -129,7 +129,7 @@ public function data_font_face_not_generated() { private function setup_theme_and_test( $theme_name ) { switch_theme( $theme_name ); do_action( 'after_setup_theme' ); - WP_Theme_JSON_Resolver::clean_cached_data(); + _wp_clean_theme_json_caches(); do_action( 'wp_loaded' ); do_action( 'wp_enqueue_scripts' ); } From 771a347095ed1d2cadeb38749415dfaa0c0b0eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Dec 2022 18:23:55 +0100 Subject: [PATCH 06/15] Fix lint issue --- src/wp-includes/global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 643f030a0c2fe..1fe17ec0a9ec5 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -321,4 +321,4 @@ function wp_theme_has_theme_json() { function _wp_clean_theme_json_caches() { wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); -} \ No newline at end of file +} From 5d5c69fa57376414cabff06b44cc3dd29a1ae91d Mon Sep 17 00:00:00 2001 From: costdev Date: Wed, 14 Dec 2022 10:14:58 +0100 Subject: [PATCH 07/15] Fix lint issues --- src/wp-includes/global-styles-and-settings.php | 8 ++++---- tests/phpunit/tests/theme/wpThemeHasThemeJson.php | 11 ----------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 1fe17ec0a9ec5..e881403d45c27 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -261,10 +261,10 @@ function ( $item ) { * * @since 6.2.0 * - * @return boolean + * @return bool */ function wp_theme_has_theme_json() { - /** + /* * By using the 'theme_json' group, this data is marked to be non-persistent across requests. * See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places. * @@ -284,7 +284,7 @@ function wp_theme_has_theme_json() { $cache_key = 'wp_theme_has_theme_json'; $theme_has_support = wp_cache_get( $cache_key, $cache_group ); - /** + /* * $theme_has_support is stored as an int in the cache. * * The reason not to store it as a boolean is to avoid working @@ -297,7 +297,7 @@ function wp_theme_has_theme_json() { return (bool) $theme_has_support; } - // Has the own theme a theme.json? + // Does the theme have its own theme.json? $theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' ); // Look up the parent if the child does not have a theme.json. diff --git a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php index a538562b8183f..fcbe4e3f3d05b 100644 --- a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php @@ -13,8 +13,6 @@ class Tests_Theme_WpThemeHasThemeJson extends WP_Theme_UnitTestCase { /** * Test that it reports correctly themes that have a theme.json. - * - * @covers wp_theme_has_theme_json */ public function test_theme_has_theme_json() { switch_theme( 'block-theme' ); @@ -23,8 +21,6 @@ public function test_theme_has_theme_json() { /** * Test that it reports correctly themes that do not have a theme.json. - * - * @covers wp_theme_has_theme_json */ public function test_theme_has_no_theme_json() { switch_theme( 'default' ); @@ -33,8 +29,6 @@ public function test_theme_has_no_theme_json() { /** * Test it reports correctly child themes that have a theme.json. - * - * @covers wp_theme_has_theme_json */ public function test_child_theme_has_theme_json() { switch_theme( 'block-theme-child' ); @@ -44,8 +38,6 @@ public function test_child_theme_has_theme_json() { /** * Test that it reports correctly child themes that do not have a theme.json * and the parent does. - * - * @covers wp_theme_has_theme_json */ public function test_child_theme_has_not_theme_json_but_parent_has() { switch_theme( 'block-theme-child-no-theme-json' ); @@ -55,8 +47,6 @@ public function test_child_theme_has_not_theme_json_but_parent_has() { /** * Test that it reports correctly child themes that do not have a theme.json * and the parent does not either. - * - * @covers wp_theme_has_theme_json */ public function test_neither_child_or_parent_themes_have_theme_json() { switch_theme( 'default-child-no-theme-json' ); @@ -68,7 +58,6 @@ public function test_neither_child_or_parent_themes_have_theme_json() { * * @ticket 52991 * - * @covers wp_theme_has_theme_json */ public function test_switching_themes_recalculates_support() { // The "default" theme doesn't have theme.json support. From 16b5d6ce738c12e20012bee0a61726b4ca7df147 Mon Sep 17 00:00:00 2001 From: costdev Date: Wed, 14 Dec 2022 10:23:38 +0100 Subject: [PATCH 08/15] Improve tests --- tests/phpunit/tests/theme/wpThemeHasThemeJson.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php index fcbe4e3f3d05b..77691de1cc40f 100644 --- a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php @@ -68,8 +68,8 @@ public function test_switching_themes_recalculates_support() { switch_theme( 'block-theme' ); $block_theme = wp_theme_has_theme_json(); - $this->assertFalse( $default ); - $this->assertTrue( $block_theme ); + $this->assertFalse( $default, 'The "default" theme should not report theme.json support.' ); + $this->assertTrue ( $block_theme, 'The block theme should report theme.json support.' ); } } From 88418422b0320b23ed36317089ccbb0341051d97 Mon Sep 17 00:00:00 2001 From: costdev Date: Wed, 14 Dec 2022 10:37:13 +0100 Subject: [PATCH 09/15] Use data provider in tests --- .../tests/theme/wpThemeHasThemeJson.php | 73 ++++++++++--------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php index 77691de1cc40f..b74403124dbf5 100644 --- a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php @@ -12,49 +12,50 @@ class Tests_Theme_WpThemeHasThemeJson extends WP_Theme_UnitTestCase { /** - * Test that it reports correctly themes that have a theme.json. - */ - public function test_theme_has_theme_json() { - switch_theme( 'block-theme' ); - $this->assertTrue( wp_theme_has_theme_json() ); - } - - /** - * Test that it reports correctly themes that do not have a theme.json. - */ - public function test_theme_has_no_theme_json() { - switch_theme( 'default' ); - $this->assertFalse( wp_theme_has_theme_json() ); - } - - /** - * Test it reports correctly child themes that have a theme.json. - */ - public function test_child_theme_has_theme_json() { - switch_theme( 'block-theme-child' ); - $this->assertTrue( wp_theme_has_theme_json() ); - } - - /** - * Test that it reports correctly child themes that do not have a theme.json - * and the parent does. + * Tests that wp_theme_has_theme_json() correctly reports whether a theme has a theme.json file. + * + * @dataProvider data_theme_has_theme_json_reports_correctly + * + * @param string $theme The slug of the theme to switch to. + * @param bool $expected The expected result. */ - public function test_child_theme_has_not_theme_json_but_parent_has() { - switch_theme( 'block-theme-child-no-theme-json' ); - $this->assertTrue( wp_theme_has_theme_json() ); + public function test_theme_has_theme_json_reports_correctly( $theme, $expected ) { + switch_theme( $theme ); + $this->assertSame( $expected, wp_theme_has_theme_json() ); } /** - * Test that it reports correctly child themes that do not have a theme.json - * and the parent does not either. + * Data provider for test_theme_has_theme_json_reports_correctly(). + * + * @return array[] */ - public function test_neither_child_or_parent_themes_have_theme_json() { - switch_theme( 'default-child-no-theme-json' ); - $this->assertFalse( wp_theme_has_theme_json() ); + public function data_theme_has_theme_json_reports_correctly() { + return array( + 'a theme with theme.json' => array( + 'theme' => 'block-theme', + 'expected' => true, + ), + 'a theme without theme.json' => array( + 'theme' => 'default', + 'expected' => false, + ), + 'a child theme with theme.json' => array( + 'theme' => 'block-theme-child', + 'expected' => true, + ), + 'a child theme without theme.json and parent theme with theme.json' => array( + 'theme' => 'block-theme-child-no-theme-json', + 'expected' => true, + ), + 'a child theme without theme.json and parent theme without theme.json' => array( + 'theme' => 'default-child-no-theme-json', + 'expected' => false, + ), + ); } /** - * Test that switching themes recalculates theme support. + * Tests that switching themes recalculates theme support. * * @ticket 52991 * @@ -69,7 +70,7 @@ public function test_switching_themes_recalculates_support() { $block_theme = wp_theme_has_theme_json(); $this->assertFalse( $default, 'The "default" theme should not report theme.json support.' ); - $this->assertTrue ( $block_theme, 'The block theme should report theme.json support.' ); + $this->assertTrue( $block_theme, 'The block theme should report theme.json support.' ); } } From 860418295f2765a0649d42bc15a3e5d397dcc3a2 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 16 Jan 2023 15:00:52 -0600 Subject: [PATCH 10/15] Make wp_clean_theme_json_caches() public --- src/wp-includes/default-filters.php | 4 ++-- src/wp-includes/global-styles-and-settings.php | 7 +++---- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 10 +++++----- .../tests/webfonts/wpThemeJsonWebfontsHandler.php | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 2155300e3b7aa..bfb146c5a9424 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -346,8 +346,8 @@ add_action( 'init', '_register_core_block_patterns_and_categories' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); -add_action( 'switch_theme', '_wp_clean_theme_json_caches' ); -add_action( 'start_previewing_theme', '_wp_clean_theme_json_caches' ); +add_action( 'switch_theme', 'wp_clean_theme_json_caches' ); +add_action( 'start_previewing_theme', 'wp_clean_theme_json_caches' ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index e881403d45c27..7a546bb78fa2d 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -313,12 +313,11 @@ function wp_theme_has_theme_json() { } /** - * Private function to clean the caches under the theme_json group. + * Cleans the caches under the theme_json group. * - * @since 6.1.2 - * @access private + * @since 6.2.0 */ -function _wp_clean_theme_json_caches() { +function wp_clean_theme_json_caches() { wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); } diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 200088f85c55a..9dd72ee929c50 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -110,7 +110,7 @@ public function tear_down() { unset( $GLOBALS['wp_themes'] ); // Reset data between tests. - _wp_clean_theme_json_caches(); + wp_clean_theme_json_caches(); parent::tear_down(); } @@ -376,7 +376,7 @@ public function data_has_same_registered_blocks_when_all_blocks_are_cached() { * @ticket 56467 */ public function test_get_core_data( $should_fire_filter, $core_is_cached, $blocks_are_cached ) { - _wp_clean_theme_json_caches(); + wp_clean_theme_json_caches(); // If should cache core, then fire the method to cache it before running the tests. if ( $core_is_cached ) { @@ -621,7 +621,7 @@ function( $query ) use ( &$global_styles_query_count ) { ); for ( $i = 0; $i < 3; $i++ ) { WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - _wp_clean_theme_json_caches(); + wp_clean_theme_json_caches(); } $this->assertSame( 0, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); @@ -634,7 +634,7 @@ function( $query ) use ( &$global_styles_query_count ) { $global_styles_query_count = 0; for ( $i = 0; $i < 3; $i++ ) { $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - _wp_clean_theme_json_caches(); + wp_clean_theme_json_caches(); $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); } $this->assertSame( 1, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); @@ -651,7 +651,7 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q $query_count = get_num_queries(); for ( $i = 0; $i < 3; $i++ ) { WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - _wp_clean_theme_json_caches(); + wp_clean_theme_json_caches(); } $query_count = get_num_queries() - $query_count; $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); diff --git a/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php b/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php index 30df269803d2d..be8709cb71f22 100644 --- a/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php +++ b/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php @@ -129,7 +129,7 @@ public function data_font_face_not_generated() { private function setup_theme_and_test( $theme_name ) { switch_theme( $theme_name ); do_action( 'after_setup_theme' ); - _wp_clean_theme_json_caches(); + wp_clean_theme_json_caches(); do_action( 'wp_loaded' ); do_action( 'wp_enqueue_scripts' ); } From 901e98a5e600d8ee0938e2115a21e2d29173eb28 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 16 Jan 2023 15:34:57 -0600 Subject: [PATCH 11/15] Comment improvements --- src/wp-includes/global-styles-and-settings.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 7a546bb78fa2d..0eebe07cb1afe 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -257,28 +257,28 @@ function ( $item ) { } /** - * Whether a theme or its parent have a theme.json file. + * Checks whether a theme or its parent has a theme.json file. * * @since 6.2.0 * - * @return bool + * @return bool Returns true if theme or its parent has a theme.json file, false otherwise. */ function wp_theme_has_theme_json() { /* * By using the 'theme_json' group, this data is marked to be non-persistent across requests. - * See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places. + * @see `wp_cache_add_non_persistent_groups()`. * * The rationale for this is to make sure derived data from theme.json * is always fresh from the potential modifications done via hooks * that can use dynamic data (modify the stylesheet depending on some option, * settings depending on user permissions, etc.). - * See some of the existing hooks to modify theme.json behaviour: - * https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ + * For some of the existing hooks to modify theme.json behavior: + * @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ * * A different alternative considered was to invalidate the cache upon certain * 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 + * @see https://github.com/WordPress/gutenberg/pull/45372 */ $cache_group = 'theme_json'; $cache_key = 'wp_theme_has_theme_json'; @@ -289,9 +289,10 @@ function wp_theme_has_theme_json() { * * The reason not to store it as a boolean is to avoid working * with the $found parameter which apparently had some issues in some implementations - * https://developer.wordpress.org/reference/functions/wp_cache_get/ + * @see https://developer.wordpress.org/reference/functions/wp_cache_get/ * - * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. + * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme + * developers workflow. */ if ( ! WP_DEBUG && is_int( $theme_has_support ) ) { return (bool) $theme_has_support; From cbcd7d29a9d1e68d62d6b3bda8a4f44473991564 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 16 Jan 2023 15:49:24 -0600 Subject: [PATCH 12/15] Test comment and annotation improvements --- tests/phpunit/tests/theme/wpThemeHasThemeJson.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php index b74403124dbf5..636886c03bbcc 100644 --- a/tests/phpunit/tests/theme/wpThemeHasThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeHasThemeJson.php @@ -12,7 +12,7 @@ class Tests_Theme_WpThemeHasThemeJson extends WP_Theme_UnitTestCase { /** - * Tests that wp_theme_has_theme_json() correctly reports whether a theme has a theme.json file. + * @ticket 56975 * * @dataProvider data_theme_has_theme_json_reports_correctly * @@ -25,7 +25,7 @@ public function test_theme_has_theme_json_reports_correctly( $theme, $expected ) } /** - * Data provider for test_theme_has_theme_json_reports_correctly(). + * Data provider. * * @return array[] */ @@ -55,10 +55,7 @@ public function data_theme_has_theme_json_reports_correctly() { } /** - * Tests that switching themes recalculates theme support. - * * @ticket 52991 - * */ public function test_switching_themes_recalculates_support() { // The "default" theme doesn't have theme.json support. @@ -72,5 +69,4 @@ public function test_switching_themes_recalculates_support() { $this->assertFalse( $default, 'The "default" theme should not report theme.json support.' ); $this->assertTrue( $block_theme, 'The block theme should report theme.json support.' ); } - } From 91d6ec67796ae8854de9e8978e7285de950166a5 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 17 Jan 2023 14:53:43 -0600 Subject: [PATCH 13/15] Add TODO note --- src/wp-includes/global-styles-and-settings.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 0eebe07cb1afe..14a11ff578646 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -292,7 +292,9 @@ function wp_theme_has_theme_json() { * @see https://developer.wordpress.org/reference/functions/wp_cache_get/ * * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme - * developers workflow. + * developer's workflow. + * + * TODO Replace `WP_DEBUG` once an "in development mode" check is available in Core. */ if ( ! WP_DEBUG && is_int( $theme_has_support ) ) { return (bool) $theme_has_support; From c4a20376f3c8146a5273fba3e6105d054bc3b426 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 17 Jan 2023 14:55:43 -0600 Subject: [PATCH 14/15] Use todo annotation --- src/wp-includes/global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 14a11ff578646..be9b15dab6074 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -294,7 +294,7 @@ function wp_theme_has_theme_json() { * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme * developer's workflow. * - * TODO Replace `WP_DEBUG` once an "in development mode" check is available in Core. + * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core. */ if ( ! WP_DEBUG && is_int( $theme_has_support ) ) { return (bool) $theme_has_support; From 3a5b3a13591e7e8f141d88f0752de91c0192096d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 18 Jan 2023 04:01:38 -0600 Subject: [PATCH 15/15] Rename wp_clean_theme_json_caches --- src/wp-includes/default-filters.php | 4 ++-- src/wp-includes/global-styles-and-settings.php | 2 +- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 10 +++++----- .../tests/webfonts/wpThemeJsonWebfontsHandler.php | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index bfb146c5a9424..4e6340700cc15 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -346,8 +346,8 @@ add_action( 'init', '_register_core_block_patterns_and_categories' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); -add_action( 'switch_theme', 'wp_clean_theme_json_caches' ); -add_action( 'start_previewing_theme', 'wp_clean_theme_json_caches' ); +add_action( 'switch_theme', 'wp_clean_theme_json_cache' ); +add_action( 'start_previewing_theme', 'wp_clean_theme_json_cache' ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index be9b15dab6074..d93792668752e 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -320,7 +320,7 @@ function wp_theme_has_theme_json() { * * @since 6.2.0 */ -function wp_clean_theme_json_caches() { +function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); } diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 9dd72ee929c50..a25de5e3b4b8b 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -110,7 +110,7 @@ public function tear_down() { unset( $GLOBALS['wp_themes'] ); // Reset data between tests. - wp_clean_theme_json_caches(); + wp_clean_theme_json_cache(); parent::tear_down(); } @@ -376,7 +376,7 @@ public function data_has_same_registered_blocks_when_all_blocks_are_cached() { * @ticket 56467 */ public function test_get_core_data( $should_fire_filter, $core_is_cached, $blocks_are_cached ) { - wp_clean_theme_json_caches(); + wp_clean_theme_json_cache(); // If should cache core, then fire the method to cache it before running the tests. if ( $core_is_cached ) { @@ -621,7 +621,7 @@ function( $query ) use ( &$global_styles_query_count ) { ); for ( $i = 0; $i < 3; $i++ ) { WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - wp_clean_theme_json_caches(); + wp_clean_theme_json_cache(); } $this->assertSame( 0, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); @@ -634,7 +634,7 @@ function( $query ) use ( &$global_styles_query_count ) { $global_styles_query_count = 0; for ( $i = 0; $i < 3; $i++ ) { $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - wp_clean_theme_json_caches(); + wp_clean_theme_json_cache(); $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); } $this->assertSame( 1, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); @@ -651,7 +651,7 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q $query_count = get_num_queries(); for ( $i = 0; $i < 3; $i++ ) { WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - wp_clean_theme_json_caches(); + wp_clean_theme_json_cache(); } $query_count = get_num_queries() - $query_count; $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); diff --git a/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php b/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php index be8709cb71f22..ae7deacc01977 100644 --- a/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php +++ b/tests/phpunit/tests/webfonts/wpThemeJsonWebfontsHandler.php @@ -129,7 +129,7 @@ public function data_font_face_not_generated() { private function setup_theme_and_test( $theme_name ) { switch_theme( $theme_name ); do_action( 'after_setup_theme' ); - wp_clean_theme_json_caches(); + wp_clean_theme_json_cache(); do_action( 'wp_loaded' ); do_action( 'wp_enqueue_scripts' ); }