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

Accept transforms in gutenberg_get_global_styles context params #50484

Merged
merged 8 commits into from
Jun 19, 2023

Conversation

samnajian
Copy link
Contributor

@samnajian samnajian commented May 9, 2023

What?

This PR addresses the changes explained in #49712 by introducing the transforms to the $context param as form of an array, so that

gutenberg_get_global_styles( array(), array( 
    'block_name' => 'core/post-terms',
    'transforms' => array( 'resolve-variables' ) 
    )
 );

returns:

"core/post-terms": {
    "typography": { "fontSize": "12px" }
}

and not the following:

"core/post-terms": {
    "typography": { "fontSize": "var(--wp--preset--font-size--small)" }
}

for a theme.json with the following content:

"core/post-terms": {
    "typography": { "fontSize": "var(--wp--preset--font-size--small)" }
}

Why?

There are some usages of the gutenberg_get_global_styles where the user is interested in the values of the css rules and not the variables.

How?

Currently the acceptable value is only resolve-variables as in

$context = array( 'transforms' => array( 'resolve-variables' ) ) 

and in future PRs other transforms can be added such as

$context =  array( 'transforms' => array( 'resolve-references' ) )

Testing Instructions

Paste the following in functions.php of the theme:

add_action( 'init', function(){
        error_log( print_r( wp_get_global_styles( array(), array('block_name'=>'core/post-terms') ), true ) );
} );

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label May 9, 2023
@github-actions
Copy link

github-actions bot commented May 9, 2023

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @samnajian! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@samnajian samnajian force-pushed the add/wp_get_global_styles_values branch 6 times, most recently from 5977de8 to 1b1cb69 Compare May 10, 2023 12:39
@samnajian samnajian changed the title Add/wp get global styles values Add wp_get_global_styles_values May 11, 2023
@samnajian samnajian force-pushed the add/wp_get_global_styles_values branch 2 times, most recently from f5a641c to 6b90136 Compare May 24, 2023 14:03
@samnajian samnajian changed the title Add wp_get_global_styles_values Accept transforms in gutenberg_get_global_styles context params May 24, 2023
@samnajian samnajian force-pushed the add/wp_get_global_styles_values branch 5 times, most recently from 9853971 to 5b201fc Compare May 26, 2023 09:06
@samnajian samnajian marked this pull request as ready for review May 26, 2023 09:36
@oandregal oandregal added [Type] Enhancement A suggestion for improvement. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels May 26, 2023
@oandregal oandregal requested a review from geriux May 26, 2023 11:51
@geriux
Copy link
Member

geriux commented May 26, 2023

Hey there! I just did a quick test of these changes to see what the response would be like and this would benefit the mobile editor a lot since we won't need to go through the data and manually parse it (which on mid-end devices could take a toll on performance).

I can't add too much value to the code review though since my PHP knowledge is limited 😅 but I like that it still uses gutenberg_get_global_styles and it just sets a context to get the style values. Once we implement this we would need to keep support for the default response so this works perfectly.

To test it I updated the mobile endpoint to pass the transforms:

$context = array(
	'transforms' => array(
		'resolve-variables',
	),
);			
$settings['__experimentalStyles'] = gutenberg_get_global_styles(array(), $context);

Thank you for working on this @samnajian 👏

@samnajian samnajian force-pushed the add/wp_get_global_styles_values branch 2 times, most recently from 3e03386 to 14e536c Compare June 4, 2023 19:41
Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

This is brilliant work, Sam! Thanks for all your effort here. There's only one use case we need to fix, but otherwise it's working as expected.

@samnajian samnajian force-pushed the add/wp_get_global_styles_values branch 5 times, most recently from 2f7dbb0 to ae2ca1f Compare June 19, 2023 08:51
Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

This is ready to be merged. Thanks for pushing until it was feature complete, Sam! It was harder than anticipated, and you did great.

I see some lint issues that need fixing before merging, though.

@samnajian samnajian force-pushed the add/wp_get_global_styles_values branch from ae2ca1f to bcb6c73 Compare June 19, 2023 09:18
@oandregal oandregal merged commit cfaecf2 into WordPress:trunk Jun 19, 2023
@github-actions github-actions bot added this to the Gutenberg 16.1 milestone Jun 19, 2023
@ramonjd ramonjd added the Needs PHP backport Needs PHP backport to Core label Jun 20, 2023
@ramonjd
Copy link
Member

ramonjd commented Jun 20, 2023

Thanks everyone for working on this. I'll add it to the backport list for WP 6.3 🙇

@oandregal
Copy link
Member

Backporting at WordPress/wordpress-develop#4656

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants