From 514f23e6aed039976294eabf737a79877f1faae9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Sep 2022 20:40:29 +0200 Subject: [PATCH 01/11] WP_Theme_JSON: Invalidate blocks metadata cache when needed --- src/wp-includes/class-wp-theme-json.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 77b77388af23c..4a618af3be37a 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -729,14 +729,16 @@ protected static function append_to_selector( $selector, $to_append, $position = * @return array Block metadata. */ protected static function get_blocks_metadata() { + $registry = WP_Block_Type_Registry::get_instance(); + $blocks = $registry->get_all_registered(); + if ( null !== static::$blocks_metadata ) { - return static::$blocks_metadata; + $missing_blocks = array_diff_key( $blocks, static::$blocks_metadata ); + if ( count( $missing_blocks ) === 0 ) { + return static::$blocks_metadata; + } } - static::$blocks_metadata = array(); - - $registry = WP_Block_Type_Registry::get_instance(); - $blocks = $registry->get_all_registered(); foreach ( $blocks as $block_name => $block_type ) { if ( isset( $block_type->supports['__experimentalSelector'] ) && From 2072c0de928392bfa394192bbe8c4e94989dca1f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Sep 2022 20:43:48 +0200 Subject: [PATCH 02/11] Add back block_metadata init --- src/wp-includes/class-wp-theme-json.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 4a618af3be37a..46da4e41a37e8 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -739,6 +739,7 @@ protected static function get_blocks_metadata() { } } + static::$blocks_metadata = array(); foreach ( $blocks as $block_name => $block_type ) { if ( isset( $block_type->supports['__experimentalSelector'] ) && From 163b21c4d751630d012b23622dc7ccaff67a114a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Sep 2022 20:44:34 +0200 Subject: [PATCH 03/11] Change variable name and add comment --- src/wp-includes/class-wp-theme-json.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 46da4e41a37e8..964cec1f3e1d0 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -733,8 +733,9 @@ protected static function get_blocks_metadata() { $blocks = $registry->get_all_registered(); if ( null !== static::$blocks_metadata ) { - $missing_blocks = array_diff_key( $blocks, static::$blocks_metadata ); - if ( count( $missing_blocks ) === 0 ) { + // Do we have metadata for all currently registered blocks? + $blocks = array_diff_key( $blocks, static::$blocks_metadata ); + if ( count( $blocks ) === 0 ) { return static::$blocks_metadata; } } From e9897ffa76fd9cc5d7afe4996f32d70b55f9904b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Sep 2022 20:47:00 +0200 Subject: [PATCH 04/11] Oops --- src/wp-includes/class-wp-theme-json.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 964cec1f3e1d0..20a0d9dc90d62 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -740,7 +740,6 @@ protected static function get_blocks_metadata() { } } - static::$blocks_metadata = array(); foreach ( $blocks as $block_name => $block_type ) { if ( isset( $block_type->supports['__experimentalSelector'] ) && From d7dcc6a404859999094dadbd3af35efe702630bd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Sep 2022 20:48:44 +0200 Subject: [PATCH 05/11] That's more like it --- src/wp-includes/class-wp-theme-json.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 20a0d9dc90d62..594ad4c61c7a0 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -732,7 +732,9 @@ protected static function get_blocks_metadata() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - if ( null !== static::$blocks_metadata ) { + if ( null === static::$blocks_metadata ) { + static::$blocks_metadata = array(); + } else { // Do we have metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); if ( count( $blocks ) === 0 ) { From b746d248438c6f403e7f483908ef8f758bafb6dd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 29 Sep 2022 21:56:40 +0200 Subject: [PATCH 06/11] Caching is overrated :yolo: --- .../class-wp-theme-json-resolver.php | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index a92f5c1e09f4f..2cbe4a35ddb16 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -178,31 +178,29 @@ public static function get_theme_data( $deprecated = array(), $options = array() $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - if ( null === static::$theme ) { - $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); - $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); - /** - * Filters the data provided by the theme for global styles & settings. - * - * @since 6.1.0 - * - * @param WP_Theme_JSON_Data Class to access and update the underlying data. - */ - $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); - $theme_json_data = $theme_json->get_data(); - static::$theme = new WP_Theme_JSON( $theme_json_data ); - - if ( wp_get_theme()->parent() ) { - // Get parent theme.json. - $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); - $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); - $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); - - // Merge the child theme.json into the parent theme.json. - // The child theme takes precedence over the parent. - $parent_theme->merge( static::$theme ); - static::$theme = $parent_theme; - } + $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); + $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + /** + * Filters the data provided by the theme for global styles & settings. + * + * @since 6.1.0 + * + * @param WP_Theme_JSON_Data Class to access and update the underlying data. + */ + $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + $theme_json_data = $theme_json->get_data(); + static::$theme = new WP_Theme_JSON( $theme_json_data ); + + if ( wp_get_theme()->parent() ) { + // Get parent theme.json. + $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); + $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); + $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); + + // Merge the child theme.json into the parent theme.json. + // The child theme takes precedence over the parent. + $parent_theme->merge( static::$theme ); + static::$theme = $parent_theme; } if ( ! $options['with_supports'] ) { From 8d46f0ac1e29e7263b50f321c5b4e77b13a84a0b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 4 Oct 2022 12:33:13 +0200 Subject: [PATCH 07/11] Revert "Caching is overrated :yolo:" This reverts commit b746d248438c6f403e7f483908ef8f758bafb6dd. --- .../class-wp-theme-json-resolver.php | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 2cbe4a35ddb16..a92f5c1e09f4f 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -178,29 +178,31 @@ public static function get_theme_data( $deprecated = array(), $options = array() $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); - $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); - /** - * Filters the data provided by the theme for global styles & settings. - * - * @since 6.1.0 - * - * @param WP_Theme_JSON_Data Class to access and update the underlying data. - */ - $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); - $theme_json_data = $theme_json->get_data(); - static::$theme = new WP_Theme_JSON( $theme_json_data ); - - if ( wp_get_theme()->parent() ) { - // Get parent theme.json. - $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); - $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); - $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); - - // Merge the child theme.json into the parent theme.json. - // The child theme takes precedence over the parent. - $parent_theme->merge( static::$theme ); - static::$theme = $parent_theme; + if ( null === static::$theme ) { + $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); + $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + /** + * Filters the data provided by the theme for global styles & settings. + * + * @since 6.1.0 + * + * @param WP_Theme_JSON_Data Class to access and update the underlying data. + */ + $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + $theme_json_data = $theme_json->get_data(); + static::$theme = new WP_Theme_JSON( $theme_json_data ); + + if ( wp_get_theme()->parent() ) { + // Get parent theme.json. + $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); + $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); + $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); + + // Merge the child theme.json into the parent theme.json. + // The child theme takes precedence over the parent. + $parent_theme->merge( static::$theme ); + static::$theme = $parent_theme; + } } if ( ! $options['with_supports'] ) { From 103bc47e1d19cb1adce1cd36eea231c5b0097bb5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 4 Oct 2022 14:03:42 +0200 Subject: [PATCH 08/11] Initialize blocks_metadata as empty array props @oandregal --- src/wp-includes/class-wp-theme-json.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 594ad4c61c7a0..6d7b467dd3efa 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -33,9 +33,10 @@ class WP_Theme_JSON { * process it twice. * * @since 5.8.0 + * @since 6.1.0 Initialize as empty array. * @var array */ - protected static $blocks_metadata = null; + protected static $blocks_metadata = array(); /** * The CSS selector for the top-level styles. @@ -732,14 +733,10 @@ protected static function get_blocks_metadata() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - if ( null === static::$blocks_metadata ) { - static::$blocks_metadata = array(); - } else { - // Do we have metadata for all currently registered blocks? - $blocks = array_diff_key( $blocks, static::$blocks_metadata ); - if ( count( $blocks ) === 0 ) { - return static::$blocks_metadata; - } + // Do we have metadata for all currently registered blocks? + $blocks = array_diff_key( $blocks, static::$blocks_metadata ); + if ( count( $blocks ) === 0 ) { + return static::$blocks_metadata; } foreach ( $blocks as $block_name => $block_type ) { From bdc092303b1d0b6a1b576b40987b1318079efe76 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 4 Oct 2022 14:59:40 +0200 Subject: [PATCH 09/11] Use `empty()` instead of `count(...) === 0` Co-authored-by: Tonya Mork --- src/wp-includes/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 6d7b467dd3efa..cb25496a8c5ad 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -735,7 +735,7 @@ protected static function get_blocks_metadata() { // Do we have metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); - if ( count( $blocks ) === 0 ) { + if ( empty( $blocks ) ) { return static::$blocks_metadata; } From a9787c0ae84cb4456ac20564b89026635afe7236 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 4 Oct 2022 14:59:51 +0200 Subject: [PATCH 10/11] Avoid "we" in comment Co-authored-by: Tonya Mork --- src/wp-includes/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index cb25496a8c5ad..6c72fde3f3dbf 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -733,7 +733,7 @@ protected static function get_blocks_metadata() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - // Do we have metadata for all currently registered blocks? + // Is there metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); if ( empty( $blocks ) ) { return static::$blocks_metadata; From 25b560aa45551b424bad9bd1e39793807300e4ee Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 4 Oct 2022 16:09:14 +0200 Subject: [PATCH 11/11] Fix PHPDoc comment Co-authored-by: David B --- src/wp-includes/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 6c72fde3f3dbf..483c68447bad6 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -33,7 +33,7 @@ class WP_Theme_JSON { * process it twice. * * @since 5.8.0 - * @since 6.1.0 Initialize as empty array. + * @since 6.1.0 Initialize as an empty array. * @var array */ protected static $blocks_metadata = array();