diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index dd8bf832174e67..5939c11d722fc7 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -612,17 +612,20 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { $origin = 'theme'; } - $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); - $registry = WP_Block_Type_Registry::get_instance(); - $valid_block_names = array_keys( $registry->get_all_registered() ); + $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); + + // TODO: this needs to be validated at a later stage. $valid_element_names = array_keys( static::ELEMENTS ); - $valid_variations = array(); - foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) { - if ( ! isset( $block_meta['styleVariations'] ) ) { - continue; - } - $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] ); + $valid_block_names = array(); + if ( isset( $theme_json['styles']['blocks'] ) ) { + $valid_block_names = array_keys( $theme_json['styles']['blocks'] ); + } + if ( isset( $theme_json['settings']['blocks'] ) ) { + $valid_block_names = array_unique( array_merge( $valid_block_names, array_keys( $theme_json['settings']['blocks'] ) ) ); } + $valid_variations = array(); + // END OF TODO + $theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations ); $this->theme_json = static::maybe_opt_in_into_settings( $theme_json ); @@ -2849,15 +2852,17 @@ public static function remove_insecure_properties( $theme_json ) { $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); - $valid_block_names = array_keys( static::get_blocks_metadata() ); + // TODO: this needs to be validated at a later stage. $valid_element_names = array_keys( static::ELEMENTS ); - $valid_variations = array(); - foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) { - if ( ! isset( $block_meta['styleVariations'] ) ) { - continue; - } - $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] ); + $valid_block_names = array(); + if ( isset( $theme_json['styles']['blocks'] ) ) { + $valid_block_names = array_keys( $theme_json['styles']['blocks'] ); + } + if ( isset( $theme_json['settings']['blocks'] ) ) { + $valid_block_names = array_unique( array_merge( $valid_block_names, array_keys( $theme_json['settings']['blocks'] ) ) ); } + $valid_variations = array(); + // END OF TODO $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations ); diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 39721742946cd1..cdda9dd5879fa4 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -23,19 +23,6 @@ #[AllowDynamicProperties] class WP_Theme_JSON_Resolver_Gutenberg { - /** - * 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(), - ); - /** * Container for data coming from core. * @@ -161,7 +148,7 @@ protected static function translate( $theme_json, $domain = 'default' ) { * @return WP_Theme_JSON Entity that holds core data. */ public static function get_core_data() { - if ( null !== static::$core && static::has_same_registered_blocks( 'core' ) ) { + if ( null !== static::$core ) { return static::$core; } @@ -182,37 +169,6 @@ public static function get_core_data() { return static::$core; } - /** - * 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; - } - /** * Returns the theme's data. * @@ -240,7 +196,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { + if ( null === static::$theme ) { $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); $wp_theme = wp_get_theme(); if ( '' !== $theme_json_file ) { @@ -371,7 +327,7 @@ public static function get_block_data() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - if ( null !== static::$blocks && static::has_same_registered_blocks( 'blocks' ) ) { + if ( null !== static::$blocks ) { return static::$blocks; } @@ -512,7 +468,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post * @return WP_Theme_JSON Entity that holds styles for user data. */ public static function get_user_data() { - if ( null !== static::$user && static::has_same_registered_blocks( 'user' ) ) { + if ( null !== static::$user ) { return static::$user; } @@ -688,12 +644,6 @@ protected static function get_file_path_from_theme( $file_name, $template = fals public static function clean_cached_data() { 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;