diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php index 73e012f33d1c7..52da6ac058f03 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php @@ -25,6 +25,19 @@ class WP_Theme_JSON_Resolver_6_1 extends WP_Theme_JSON_Resolver_6_0 { */ protected static $core = null; + /** + * Container for keep track of registered blocks. + * + * @since 6.1.0 + * @var array + */ + protected static $blocks_cache = array( + 'core' => array(), + 'blocks' => array(), + 'theme' => array(), + 'user' => array(), + ); + /** * Given a theme.json structure modifies it in place * to update certain values by its translated strings @@ -50,7 +63,7 @@ protected static function translate( $theme_json, $domain = 'default' ) { * @return WP_Theme_JSON_Gutenberg Entity that holds core data. */ public static function get_core_data() { - if ( null !== static::$core ) { + if ( null !== static::$core && static::has_same_registered_blocks( 'core' ) ) { return static::$core; } @@ -74,7 +87,7 @@ public static function get_core_data() { * @return WP_Theme_JSON_Gutenberg Entity that holds styles for user data. */ public static function get_user_data() { - if ( null !== static::$user ) { + if ( null !== static::$user && static::has_same_registered_blocks( 'user' ) ) { return static::$user; } @@ -188,4 +201,35 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post return $user_cpt; } + + /** + * Checks whether the registered blocks were already processed for this origin. + * + * @since 6.1.0 + * + * @param string $origin Data source for which to cache the blocks. + * Valid values are 'core', 'blocks', 'theme', and 'user'. + * @return bool True on success, false otherwise. + */ + protected static function has_same_registered_blocks( $origin ) { + // Bail out if the origin is invalid. + if ( ! isset( static::$blocks_cache[ $origin ] ) ) { + return false; + } + + $registry = WP_Block_Type_Registry::get_instance(); + $blocks = $registry->get_all_registered(); + + // Is there metadata for all currently registered blocks? + $block_diff = array_diff_key( $blocks, static::$blocks_cache[ $origin ] ); + if ( empty( $block_diff ) ) { + return true; + } + + foreach ( $blocks as $block_name => $block_type ) { + static::$blocks_cache[ $origin ][ $block_name ] = true; + } + + return false; + } }