-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Take block data for Global Styles from block.json #22698
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
41480b1
Take block data from block.json
oandregal 1312f9f
Enable background
oandregal 5fb5a35
Make selector optional and move it under support as experimental
oandregal 40a45e9
Enable background-color
oandregal c9db087
Update how object selectors are processed
oandregal 6582c3e
Add background to supported features
oandregal 96f71dd
Add docs
oandregal 12f2de8
Document core/heading/*
oandregal 9de210b
Improve docs
oandregal 7457a1b
Inform about JSON errors
oandregal 04f410b
Update docs
oandregal df3bac3
Remove general selector
oandregal b923780
Update comments
oandregal 7f94cf9
Use str_replace instead of preg_replace
oandregal 41d378b
Check error code instead of string
oandregal 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -80,6 +80,13 @@ function gutenberg_experimental_global_styles_get_from_file( $file_path ) { | |
| file_get_contents( $file_path ), | ||
| true | ||
| ); | ||
|
|
||
| $json_decoding_error = json_last_error(); | ||
| if ( JSON_ERROR_NONE !== $json_decoding_error ) { | ||
| error_log( 'Error when decoding file schema: ' . json_last_error_msg() ); | ||
| return $config; | ||
| } | ||
|
|
||
| if ( is_array( $decoded_file ) ) { | ||
| $config = $decoded_file; | ||
| } | ||
|
|
@@ -97,6 +104,13 @@ function gutenberg_experimental_global_styles_get_user() { | |
| $user_cpt = gutenberg_experimental_global_styles_get_user_cpt( array( 'publish' ) ); | ||
| if ( array_key_exists( 'post_content', $user_cpt ) ) { | ||
| $decoded_data = json_decode( $user_cpt['post_content'], true ); | ||
|
|
||
| $json_decoding_error = json_last_error(); | ||
| if ( JSON_ERROR_NONE !== $json_decoding_error ) { | ||
| error_log( 'Error when decoding user schema: ' . json_last_error_msg() ); | ||
| return $config; | ||
| } | ||
|
|
||
| if ( is_array( $decoded_data ) ) { | ||
| $config = $decoded_data; | ||
| } | ||
|
|
@@ -253,64 +267,101 @@ function gutenberg_experimental_global_styles_get_theme() { | |
| return $theme_config; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the style features a particular block supports. | ||
| * | ||
| * @param array $supports The block supports array. | ||
| * | ||
| * @return array Style features supported by the block. | ||
| */ | ||
| function gutenberg_experimental_global_styles_get_supported_styles( $supports ) { | ||
| $style_features = array( | ||
| 'color' => array( '__experimentalColor' ), | ||
| 'background-color' => array( '__experimentalColor' ), | ||
| 'background' => array( '__experimentalColor', 'gradients' ), | ||
| 'line-height' => array( '__experimentalLineHeight' ), | ||
| 'font-size' => array( '__experimentalFontSize' ), | ||
| ); | ||
|
|
||
| $supported_features = array(); | ||
| foreach ( $style_features as $style_feature => $path ) { | ||
| if ( gutenberg_experimental_get( $supports, $path ) ) { | ||
| $supported_features[] = $style_feature; | ||
| } | ||
| } | ||
|
|
||
| return $supported_features; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the block data (selector/supports). | ||
| * | ||
| * @return array | ||
| */ | ||
| function gutenberg_experimental_global_styles_get_block_data() { | ||
| // TODO: this data should be taken from the block registry. | ||
| // | ||
| // At the moment this array replicates the current capabilities | ||
| // declared by blocks via __experimentalLineHeight, | ||
| // __experimentalColor, and __experimentalFontSize. | ||
| $block_data = array( | ||
| 'global' => array( | ||
| 'global' => array( | ||
| 'selector' => ':root', | ||
| 'supports' => array(), // By being blank, the 'global' section won't output any style yet. | ||
| ), | ||
| 'core/paragraph' => array( | ||
| 'selector' => 'p', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/heading/h1' => array( | ||
| 'selector' => 'h1', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/heading/h2' => array( | ||
| 'selector' => 'h2', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/heading/h3' => array( | ||
| 'selector' => 'h3', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/heading/h4' => array( | ||
| 'selector' => 'h4', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/heading/h5' => array( | ||
| 'selector' => 'h5', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/heading/h6' => array( | ||
| 'selector' => 'h6', | ||
| 'supports' => array( 'line-height', 'font-size', 'color' ), | ||
| ), | ||
| 'core/columns' => array( | ||
| 'selector' => '.wp-block-columns', | ||
| 'supports' => array( 'color' ), | ||
| ), | ||
| 'core/group' => array( | ||
| 'selector' => '.wp-block-group', | ||
| 'supports' => array( 'color' ), | ||
| ), | ||
| 'core/media-text' => array( | ||
| 'selector' => '.wp-block-media-text', | ||
| 'supports' => array( 'color' ), | ||
| 'supports' => array( 'background-color' ), | ||
|
Member
Author
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. This one-liner allows the global context to generate CSS for background-color. Adding it in this PR (which is a refactor to take data from the block.json) because it's so small a change. |
||
| ), | ||
| ); | ||
|
|
||
| $registry = WP_Block_Type_Registry::get_instance(); | ||
| foreach ( $registry->get_all_registered() as $block_name => $block_type ) { | ||
| if ( ! is_array( $block_type->supports ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); | ||
| if ( empty( $supports ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| /* | ||
| * Assign the selector for the block. | ||
| * | ||
| * Some blocks can declare multiple selectors: | ||
| * | ||
| * - core/heading represents the H1-H6 HTML elements | ||
| * - core/list represents the UL and OL HTML elements | ||
| * - core/group is meant to represent DIV and other HTML elements | ||
| * | ||
| * Some other blocks don't provide a selector, | ||
| * so we generate a class for them based on their name: | ||
| * | ||
| * - 'core/group' => '.wp-block-group' | ||
| * - 'my-custom-library/block-name' => '.wp-block-my-custom-library-block-name' | ||
| * | ||
| * Note that, for core blocks, we don't add the `core/` prefix to its class name. | ||
| * This is for historical reasons, as they come with a class without that infix. | ||
| * | ||
| */ | ||
| if ( | ||
| isset( $block_type->supports['__experimentalSelector'] ) && | ||
| is_string( $block_type->supports['__experimentalSelector'] ) | ||
| ) { | ||
| $block_data[ $block_name ] = array( | ||
| 'selector' => $block_type->supports['__experimentalSelector'], | ||
| 'supports' => $supports, | ||
| ); | ||
| } elseif ( | ||
| isset( $block_type->supports['__experimentalSelector'] ) && | ||
| is_array( $block_type->supports['__experimentalSelector'] ) | ||
| ) { | ||
| foreach ( $block_type->supports['__experimentalSelector'] as $key => $selector ) { | ||
| $block_data[ $key ] = array( | ||
| 'selector' => $selector, | ||
| 'supports' => $supports, | ||
| ); | ||
| } | ||
| } else { | ||
| $block_data[ $block_name ] = array( | ||
| 'selector' => '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) ), | ||
| 'supports' => $supports, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return $block_data; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.