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: Check for null values to cater for blockGap #59258

Merged
merged 3 commits into from
Feb 22, 2024

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Feb 22, 2024

What?

I noticed this PHP error while trying to extract block styles, whose values have a CSS var, using wp|gutenberg_get_global_styles().

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in lib/class-wp-theme-json-gutenberg.php on line 3827

Deprecated: preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated in lib/class-wp-theme-json-gutenberg.php on line 3830

It's complaining about passing a null value to strpos() in WP_Theme_JSON_Gutenberg::convert_variables_to_value

So I added a check to make sure the value is not empty before we run it through strpos().

Why

When using wp|gutenberg_get_global_styles() with the resolve-variables flag like this —

`gutenberg_get_global_styles( array( 'typography', 'fontSize' ), array( 'block_name' => 'core/search', 'transforms' => array( 'resolve-variables' ) ) );`

— it will grab merged Theme JSON data from WP_Theme_JSON_Resolver_Gutenberg::get_merged_data() and run it through WP_Theme_JSON_Gutenberg::resolve_variables, which calls self::convert_variables_to_value.

The snag is that:

  1. WP_Theme_JSON_Resolver_Gutenberg::get_merged_data() calls static::get_block_data()
  2. static::get_block_data() sets blockGap for supported blocks to null if the value is not defined.

Because WP_Theme_JSON_Gutenberg::convert_variables_to_value is expecting strings as values, it will trigger the error.

Testing Instructions

Run the tests: npm run test:unit:php:base -- --filter WP_Theme_JSON_Gutenberg_Test

If you like, try to call the function from inside a block render/block supports function, or anywhere you're sure that block styles have already been registered

$test = gutenberg_get_global_styles( array( 'spacing', 'blockGap' ), array( 'origin' => 'base', 'block_name' => 'core/post-template', 'transforms' => array( 'resolve-variables' ) ) );
var_dump($test);

You should see NULL and no errors.

@ramonjd ramonjd self-assigned this Feb 22, 2024
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Feb 22, 2024
Copy link

github-actions bot commented Feb 22, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/class-wp-theme-json-gutenberg.php
❔ phpunit/class-wp-theme-json-test.php

* WP_Theme_JSON_Gutenberg::resolve_variables may be called with merged data from WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()
* WP_Theme_JSON_Resolver_Gutenberg::get_block_data() sets blockGap for supported blocks to `null` if the value is not defined.
*/
$this->assertNull( $styles['blocks']['core/post-template']['spacing']['blockGap'], 'core/post-template block: blockGap' );
Copy link
Member Author

Choose a reason for hiding this comment

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

i added this test to check the happy path, e.g., resolution of spacing presets when those presets are applied to blockGap values.

Copy link

github-actions bot commented Feb 22, 2024

Flaky tests detected in ece1a4d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7999294851
📝 Reported issues:

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this up!

I didn't see the warning at first because my local env was running an older PHP version (PHP 8.0.22), but after updating to 8.2.10, I could see the PHP Deprecated warnings:

image

With this PR applied, no warnings 👍

LGTM! ✨

@ramonjd ramonjd force-pushed the fix/theme-json-block-gap-variables-resolution branch from 98c4d02 to ece1a4d Compare February 22, 2024 04:05
@ramonjd ramonjd enabled auto-merge (squash) February 22, 2024 04:06
@ramonjd ramonjd merged commit 255ed23 into trunk Feb 22, 2024
56 checks passed
@ramonjd ramonjd deleted the fix/theme-json-block-gap-variables-resolution branch February 22, 2024 04:38
@github-actions github-actions bot added this to the Gutenberg 17.9 milestone Feb 22, 2024
@getdave
Copy link
Contributor

getdave commented Feb 22, 2024

@ramonjd Do you want to backport this to WP 6.5? If so please can you add the requisite labels? Many thanks.

@ramonjd
Copy link
Member Author

ramonjd commented Feb 22, 2024

Do you want to backport this to WP 6.5? If so please can you add the requisite labels? Many thanks.

This will require a PHP back port.

I was going to prep one today.

I'm not sure it'll make 6.5, but I'll label the trac ticket to try 😄

@getdave
Copy link
Contributor

getdave commented Feb 27, 2024

I noticed the backport isn't committed. I'm going to try and wrangle this in time for Beta 3 today.

Perhaps @swissspidy or @youknowriad can commit as I don't have permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json Needs PHP backport Needs PHP backport to Core [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants