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

PHP: backport from 6.1 again. #46363

Closed
wants to merge 5 commits into from
Closed
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
48 changes: 46 additions & 2 deletions lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Comment on lines +28 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* 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(),
);

Not needed, as this class will inherit it from Core's WP_Theme_JSON_Resolver class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After talking with Jonny, this code is needed for sites that are running < WP 6.1 with the plugin.

Also need:

protected static $blocks = null;

this too.

Sorry for the confusion.

/**
* Given a theme.json structure modifies it in place
* to update certain values by its translated strings
Expand All @@ -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;
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to add the changes for WP_Theme_JSON_Resolver::get_theme_data(). Why? Gutenberg's class extends from WP_Theme_JSON_Resolver_6_0 which will overload Core's get_theme_data() method, meaning Core's won't be used. That can cause unexpected differences in the way the plugin works vs Core.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
/**
* Returns the theme's data.
*
* Data from theme.json will be backfilled from existing
* theme supports, if any. Note that if the same data
* is present in theme.json and in theme supports,
* the theme.json takes precedence.
*
* @since 5.8.0
* @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed.
* @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
*
* @param array $deprecated Deprecated. Not used.
* @param array $options {
* Options arguments.
*
* @type bool $with_supports Whether to include theme supports in the data. Default true.
* }
* @return WP_Theme_JSON Entity that holds theme data.
*/
public static function get_theme_data( $deprecated = array(), $options = array() ) {
return WP_Theme_JSON_Resolver::get_theme_data( $deprecated, $options );
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above suggested code will bypass WP_Theme_JSON_Resolver_6_0::get_theme_data() implementation, to call the version in Core 6.1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But wait a minute. This won't work if the site is running < WP 6.1 with the plugin. So the entire WP_Theme_JSON_Resolver::get_theme_data() code needs to be copied here it seems.