Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: reduce number of WP_Theme_JSON instances #53357

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );

Expand Down
58 changes: 4 additions & 54 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down