-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Apply the custom global styles CSS variables on the server #21420
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,3 +339,82 @@ function gutenberg_experimental_global_styles_register_cpt() { | |
add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); | ||
add_action( 'admin_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets_editor' ); | ||
} | ||
|
||
/** | ||
* Flatten a nested Global styles config and generates the corresponding | ||
* flattened CSS variables. | ||
* | ||
* @param array $config Styles configuration. | ||
* @return array Flattened CSS variables declaration. | ||
*/ | ||
function gutenberg_get_nested_css_variables( $config ) { | ||
$result = []; | ||
$token = '--'; | ||
foreach ( $config as $key => $value ) { | ||
if ( ! is_array( $value ) ) { | ||
$result[ sanitize_title_with_dashes( $key ) ] = $value; | ||
} else { | ||
$nested_css_variables = gutenberg_get_nested_css_variables( $value ); | ||
foreach ( $nested_css_variables as $subkey => $subvalue ) { | ||
$result[ sanitize_title_with_dashes( $key ) . $token . $subkey ] = $subvalue; | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
}; | ||
|
||
/** | ||
* Flatten a nested Global styles config and generates the corresponding | ||
* flattened CSS variables. | ||
* | ||
* @param array $styles Styles configuration. | ||
* @return array Flattened CSS variables declaration. | ||
*/ | ||
function gutenberg_get_css_variables( $styles = [] ) { | ||
$prefix = '--wp'; | ||
$token = '--'; | ||
|
||
$result = []; | ||
foreach ( gutenberg_get_nested_css_variables( $styles ) as $key => $value ) { | ||
$result[] = $prefix . $token . $key . ':' . $value; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Renders the CSS variables for blocks | ||
* | ||
* @param string $block_content Output of the current block. | ||
* @param array $block Block Object. | ||
* @return string New block output. | ||
*/ | ||
function gutenberg_apply_style_attribute( $block_content, $block ) { | ||
if ( isset( $block['attrs'] ) && isset( $block['attrs']['style'] ) ) { | ||
$extra_styles = implode(';',gutenberg_get_css_variables($block['attrs']['style'])); | ||
|
||
// Try replacing an existing style | ||
$style_regex = '/((^\s*<[a-zA-Z]+\b[^><]*)style="([^"]*)")([^><]*>.*)/'; | ||
$updated_content = preg_replace_callback($style_regex, function($matches) use($extra_styles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the |
||
return ( | ||
$matches[2] . | ||
'style="' . $matches[3] . ';' . $extra_styles .'"' . | ||
$matches[4] | ||
); | ||
}, $block_content); | ||
|
||
// If no changes, add a new style | ||
if ( $updated_content === $block_content ) { | ||
$no_style_regex = '/(^\s*<[a-zA-Z]+\b[^><]*)(>.*)/'; | ||
$updated_content = preg_replace_callback($no_style_regex, function($matches) use($extra_styles) { | ||
return $matches[1] . ' style="'. $extra_styles .'"' . $matches[2]; | ||
}, $block_content); | ||
} | ||
|
||
return $updated_content; | ||
} | ||
|
||
return $block_content; | ||
} | ||
|
||
add_filter( 'render_block', 'gutenberg_apply_style_attribute', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this doesn't rely on the attributes presence but on the "support flag". We need server-side awareness of support flags. cc @gziolo did you work on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet, the plan was to come up with something better than
supports
:)Anyway,
supports
works on the server and can be exposed when registered there to the client. So we should be good in that context. You just need to say we want it and we can implement everything. @aduth has some custom code that shimsclassname
oralign
flags on the server. We can add the same logic for everything else.Let me know whether we should update https://github.com/WordPress/gutenberg/blob/master/docs/rfc/block-registration.md#backward-compatibility and the part that states that
supports
is client-side only and exists only for backward compatibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm thinking more and more that supports is actually a good thing if done right. Don't know what you'll think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably fine to keep it as is but make it more configurable. We still need to use data that can be serialized but we shouldn't limit ourselves to
boolean
.