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

Theme JSON Resolver: Update cache check to also check that the object is an instance of the Gutenberg version #42756

Merged
merged 2 commits into from
Jul 28, 2022

Conversation

andrewserong
Copy link
Contributor

@andrewserong andrewserong commented Jul 28, 2022

What?

Alternative to #42748 to fix the issue of the Theme JSON data being cached from the core version of the resolver class. This issue prevents features that have not yet landed in core, such as fluid typography and blockGap at the theme.json level, from functioning.

Why?

As in #42748 the issue appears to be that by the time the sub-classed Gutenberg version of the Resolver class runs, the core version of the Resolver has already cached the theme data.

How?

Where #42748 attempted to deal with the problem by adding another static $theme variable, this PR updates the cache check so that if doesn't just check whether the $theme variable is null, but it also checks whether or not it's an instance of the WP_Theme_JSON_Gutenberg class, which should help ensure that the cache is invalidated when we expect it to be. Kudos @talldan for the idea, and @scruffian for identifying the original issue.

Testing Instructions

In a blocks-based theme, test that experimental features that have not yet landed in core, work as expected. For example fluid typography, and blockGap at the individual block level in theme.json. For an example of the latter, update your theme.json file to include the following (ensuring that you're running a theme like TwentyTwentyTwo that sets settings.spacing.blockGap to true or opts-in to appearanceTools):

	"styles": {
		"blocks": {
			"core/buttons": {
				"spacing": {
					"blockGap": {
						"top": "3px",
						"left": "15px"
					}
				}
			},

Add some blocks to a post or page, and publish it. On the front-end of your site, you should see the spacing takes effect.

Screenshots or screencast

Before (even though spacing is set at the block level, the styles are not output After (spacing is output correctly)
image image

@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Jul 28, 2022
@@ -33,7 +33,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array(
_deprecated_argument( __METHOD__, '5.9' );
}

if ( null === static::$theme ) {
if ( null === static::$theme || ! static::$theme instanceof WP_Theme_JSON_Gutenberg ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be good to add a comment with some instructions to remove the || ! static::$theme instanceof WP_Theme_JSON_Gutenberg when backporting to core.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added here: 3673bc8

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

I've tested this by adding block styles to my theme.json (tested in block and classic themes).

E.g.,

	"styles": {
		"blocks": {
			"core/social-links": {
				"spacing": {
					"padding": "100px",
					"blockGap": "100px"
				}
			},
			"core/columns": {
				"spacing": {
					"blockGap": "100px"
				}
			}
		}
	},

The blockGap and other styles are being merged into the theme_json object and output in the CSS as expected.

Smoke tested other theme.json API features such as fluid typography and block supports.

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Good stuff, glad it was a relatively easy fix 😄 .

Maybe in the long run there's a better way to extend the theme json resolver, but this works for now!

@andrewserong andrewserong self-assigned this Jul 28, 2022
@andrewserong andrewserong added the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Jul 28, 2022
@glendaviesnz
Copy link
Contributor

Gave it a quick test on BlockBase theme and worked as expected.

@andrewserong
Copy link
Contributor Author

Thanks for the reviews, all! I'll merge this in now, and we can always follow up if we need to tweak the logic further.

@Mamaduka this is another PR that would be great for us to get into 13.8 since it's a pretty visible bug. Do you mind cherry picking this one, too? We were also wondering if it'd be worth putting out an updated RC to include it.

@andrewserong andrewserong merged commit 3f5d5a5 into trunk Jul 28, 2022
@andrewserong andrewserong deleted the try/alternative-fix-for-early-theme-json-creation branch July 28, 2022 05:32
@github-actions github-actions bot added this to the Gutenberg 13.9 milestone Jul 28, 2022
@Mamaduka
Copy link
Member

I will cherry-pick both changes and then can release RC2 👍

@andrewserong
Copy link
Contributor Author

Perfect, thanks so much @Mamaduka! Much appreciated 🙇

Mamaduka pushed a commit that referenced this pull request Jul 29, 2022
… is an instance of the Gutenberg version (#42756)

* Theme JSON Resolver: Update cache check to also check that the object is an instance of the Gutenberg version

* Add comment for when the resolver is backported to core
@Mamaduka
Copy link
Member

Cherry-picked the fixes for 13.8.

@Mamaduka Mamaduka removed the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Jul 29, 2022
@Mamaduka
Copy link
Member

@andrewserong, the RC2 is now available https://github.com/WordPress/gutenberg/releases/tag/v13.8.0-rc.2.

@andrewserong
Copy link
Contributor Author

andrewserong commented Aug 1, 2022

Excellent, thanks again @Mamaduka!

oandregal added a commit that referenced this pull request Dec 22, 2022
Same code as core. There's a check for detecting whether the class
is an instance of WP_Theme_JSON_Gutenberg that was not ported.
This check was introduced to make sure the cache of the core class
didn't interfere with the cache of the Gutenberg class,
so it's no longer necessary.

See #42756
oandregal added a commit that referenced this pull request Dec 22, 2022
… inheriting per WordPress version (#46750)

* Backport WP_Theme_JSON_Resolver from core as it is

* Substitute WP_Theme_JSON by WP_Theme_JSON_Gutenberg

* Substitute WP_Theme_JSON_Data by WP_Theme_JSON_Data_Gutenberg

* Rename WP_Theme_JSON_Resolver to WP_Theme_JSON_Resolver_Base

The goal is to use it as base to inherit from, until we are able
to remove all the children.

* Make WP_Theme_JSON_Resolver_6_1 inherit from WP_Theme_JSON_Resolver_Base

* 6.1: remove field already defined in base class

* 6.1: remove translate, it is equal to base method

* 6.1: remove get_core_data, it does not have base changes

* 6.1: remove get_user_data

Missed core changes and does not do anything differently from core.

* 6.1: remove get_user_data_from_wp_global_styles

It misses core changes and does not do anything differently.

* 6.2: inherit from WP_Theme_JSON_Resolver_Base instead of 6.1

This makes the WP_Theme_JSON_Resover_6_1 class unused.

* 6.1: remove class no longer in use

* 6.2: deprecate theme_has_support

#45380

* 6.2: substitute WP_Theme_JSON_Resolver::theme_has_support

by wp_theme_has_theme_json()

#45168

* 6.2: port changes to get_user_data_from_wp_global_styles

#46043

* 6.2: update get_merged_data

#45969

* 6.2: remove get_user_data

Same code as core. There's a check for detecting whether the class
is an instance of WP_Theme_JSON_Gutenberg that was not ported.
This check was introduced to make sure the cache of the core class
didn't interfere with the cache of the Gutenberg class,
so it's no longer necessary.

See #42756

* experimental: make it inherit from WP_Theme_JSON_Resolver_Base

* 6.2: remove class no longer in use

* experimental: delete remove_json_comments

It's already part of the base class.

* experimental: remove get_block_data

It's already part of the base class and it didn't have
the changes from core.

* experimental: appearanceTools

Port changes to the base class that were meant to be part of 6.1.
See #43337

* experimental: port webfonts (experimental API)

see #37140

* experimental: use gutenberg_get_legacy_theme_supports_for_theme_json

#46112

* experimental: get_theme_data, all code is in the base class

* experimental: remove empty class

* Rename WP_Theme_JSON_Resolver_Base to WP_Theme_JSON_Resolver_Gutenberg

* Fix lint issue: rename class-wp-theme-json-resolver.php to class-wp-theme-json-resolver-gutenberg.php

* Move theme.json to unversioned lib/

* Move theme-i18n.json to unversioned lib/
noahtallen pushed a commit that referenced this pull request Dec 28, 2022
… inheriting per WordPress version (#46750)

* Backport WP_Theme_JSON_Resolver from core as it is

* Substitute WP_Theme_JSON by WP_Theme_JSON_Gutenberg

* Substitute WP_Theme_JSON_Data by WP_Theme_JSON_Data_Gutenberg

* Rename WP_Theme_JSON_Resolver to WP_Theme_JSON_Resolver_Base

The goal is to use it as base to inherit from, until we are able
to remove all the children.

* Make WP_Theme_JSON_Resolver_6_1 inherit from WP_Theme_JSON_Resolver_Base

* 6.1: remove field already defined in base class

* 6.1: remove translate, it is equal to base method

* 6.1: remove get_core_data, it does not have base changes

* 6.1: remove get_user_data

Missed core changes and does not do anything differently from core.

* 6.1: remove get_user_data_from_wp_global_styles

It misses core changes and does not do anything differently.

* 6.2: inherit from WP_Theme_JSON_Resolver_Base instead of 6.1

This makes the WP_Theme_JSON_Resover_6_1 class unused.

* 6.1: remove class no longer in use

* 6.2: deprecate theme_has_support

#45380

* 6.2: substitute WP_Theme_JSON_Resolver::theme_has_support

by wp_theme_has_theme_json()

#45168

* 6.2: port changes to get_user_data_from_wp_global_styles

#46043

* 6.2: update get_merged_data

#45969

* 6.2: remove get_user_data

Same code as core. There's a check for detecting whether the class
is an instance of WP_Theme_JSON_Gutenberg that was not ported.
This check was introduced to make sure the cache of the core class
didn't interfere with the cache of the Gutenberg class,
so it's no longer necessary.

See #42756

* experimental: make it inherit from WP_Theme_JSON_Resolver_Base

* 6.2: remove class no longer in use

* experimental: delete remove_json_comments

It's already part of the base class.

* experimental: remove get_block_data

It's already part of the base class and it didn't have
the changes from core.

* experimental: appearanceTools

Port changes to the base class that were meant to be part of 6.1.
See #43337

* experimental: port webfonts (experimental API)

see #37140

* experimental: use gutenberg_get_legacy_theme_supports_for_theme_json

#46112

* experimental: get_theme_data, all code is in the base class

* experimental: remove empty class

* Rename WP_Theme_JSON_Resolver_Base to WP_Theme_JSON_Resolver_Gutenberg

* Fix lint issue: rename class-wp-theme-json-resolver.php to class-wp-theme-json-resolver-gutenberg.php

* Move theme.json to unversioned lib/

* Move theme-i18n.json to unversioned lib/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Themes Questions or issues with incorporating or styling blocks in a theme. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants