-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Style engine: enqueue block support styles #42452
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
ramonjd
merged 30 commits into
trunk
from
try/style-engine-enqueue-block-supports-styles
Aug 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0840074
Enqueuing block support styles version 1.
ramonjd 6a42821
Linter, this one's for you.
ramonjd 1f2b4a8
Post trunk merge cleanup and update tests.
ramonjd 5a51073
Removed spacing around curly braces in CSS rules. Updated tests.
ramonjd 55576b9
Splitting `wp_style_engine_enqueue_block_supports_styles` and `wp_sty…
ramonjd acc6209
Integrate the processor class
ramonjd 7a22c5b
Migrate layout styles to style engine store.
ramonjd 7a6aa60
Update packages/style-engine/class-wp-style-engine.php
ramonjd 49b1433
Tweaks for #42452 (#42691)
aristath d7893a6
Adding check for the context argument.
ramonjd bc6e39a
Updating the processor so that it's ignorant of stores. Why? So that …
ramonjd e8a621d
dump var_dump()
ramonjd 9ba5232
Improve the processor
aristath b41af7d
remove trailing commas - compatibility with PHP < 7.2
aristath 4ccf650
rename css_declarations to declarations
aristath e30e4fb
remove unused variable
aristath 062b920
Switch parse_block_styles from public to protected static
ramonjd 5e4164a
Now that all methods are static, there's no need to call `get_instanc…
ramonjd fcebf93
Revert get_instance() in wp_style_engine_add_to_store because we want…
ramonjd d0d5fe5
Adding a test for the 'enqueue' flag.
ramonjd e59eb10
Update lib/block-supports/layout.php
ramonjd 5d7b827
Adding a test for the 'enqueue' flag.
ramonjd 4d9f6c8
Merge branch 'try/style-engine-enqueue-block-supports-styles' of gith…
ramonjd 8d021ee
Add named stores to the processor
aristath 5f60026
avoid setting var for something that only gets used once
aristath a63f9cb
Only use "else" if absolutely necessary
aristath 0eaef08
Add a set_name method
aristath e4c791b
combine & simplify conditions
aristath 787f793
use empty() instead of isset() checks here
aristath 7dd5773
shorten it
aristath 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
Migrate layout styles to style engine store.
- Loading branch information
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 |
|---|---|---|
|
|
@@ -36,7 +36,8 @@ class WP_Style_Engine { | |
| * @var array<string, WP_Style_Engine_CSS_Rules_Store|null> | ||
| */ | ||
| private static $stores = array( | ||
| 'block-supports' => null, | ||
| 'layout-block-supports' => null, | ||
| 'block-supports' => null, | ||
| ); | ||
|
|
||
| /** | ||
|
|
@@ -320,16 +321,29 @@ public static function get_instance() { | |
| * @param array $css_declarations An array of parsed CSS property => CSS value pairs. | ||
| * @param string $store_key A valid key corresponding to an existing store in static::$stores. | ||
| * | ||
| * @return string A compiled CSS string. | ||
| * @return void. | ||
| */ | ||
| public function store_css_rule( $css_selector, $css_declarations, $store_key ) { | ||
| public static function store_css_rule( $css_selector, $css_declarations, $store_key ) { | ||
| if ( ! $css_selector || ! isset( static::$stores[ $store_key ] ) ) { | ||
| return false; | ||
| return; | ||
| } | ||
| $css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations ); | ||
| $stored_css_rule = static::$stores[ $store_key ]->add_rule( $css_selector ); | ||
| $stored_css_rule->add_declarations( $css_declarations ); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a store by store key. | ||
| * | ||
| * @param string $store_key A valid key corresponding to an existing store in static::$stores. | ||
| * | ||
| * @return WP_Style_Engine_CSS_Rules_Store|null The store, if found, otherwise `null`. | ||
| */ | ||
| public static function get_store( $store_key ) { | ||
| if ( ! isset( static::$stores[ $store_key ] ) ) { | ||
| return null; | ||
| } | ||
| return static::$stores[ $store_key ]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -368,19 +382,14 @@ protected static function render_styles( $callable, $priority = 10 ) { | |
| * Fetches, processes and compiles stored styles, then renders them to the page. | ||
| */ | ||
| public static function process_and_enqueue_stored_styles() { | ||
|
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. The enqueue and WP hook logic could well live in Gutenberg itself, e.g., |
||
| foreach ( static::$stores as $store_key => $store_instance ) { | ||
| if ( ! $store_instance ) { | ||
| continue; | ||
| } | ||
|
|
||
| $processor = new WP_Style_Engine_Processor( $store_instance ); | ||
| $styles_output = $processor->get_css(); | ||
|
|
||
| if ( ! empty( $styles_output ) ) { | ||
| wp_register_style( $store_key, false, array(), true, true ); | ||
| wp_add_inline_style( $store_key, $styles_output ); | ||
| wp_enqueue_style( $store_key ); | ||
| } | ||
| // 1. Block supports | ||
| // @TODO we could loop through static::$stores to enqueue and get the key. | ||
| $styles_output = static::compile_stylesheet_from_store( 'block-supports' ) . static::compile_stylesheet_from_store( 'layout-block-supports' ); | ||
|
|
||
| if ( ! empty( $styles_output ) ) { | ||
| wp_register_style( 'block-supports', false, array(), true, true ); | ||
| wp_add_inline_style( 'block-supports', $styles_output ); | ||
| wp_enqueue_style( 'block-supports' ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -522,42 +531,6 @@ protected static function get_css_declarations( $style_value, $style_definition, | |
| return $css_declarations; | ||
| } | ||
|
|
||
| /** | ||
| * Returns compiled CSS from parsed css_declarations. | ||
| * | ||
| * @param array $css_declarations An array of parsed CSS property => CSS value pairs. | ||
| * @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values. | ||
| * | ||
| * @return string A compiled CSS string. | ||
| */ | ||
| public function compile_css( $css_declarations, $css_selector ) { | ||
| if ( empty( $css_declarations ) || ! is_array( $css_declarations ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| // Return an entire rule if there is a selector. | ||
| if ( $css_selector ) { | ||
| $css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations ); | ||
| return $css_rule->get_css(); | ||
| } else { | ||
| $css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations ); | ||
| return $css_declarations->get_declarations_string(); | ||
| } | ||
| } | ||
| /** | ||
| * Returns a string of classnames, | ||
| * | ||
| * @param string $classnames A flat array of classnames. | ||
| * | ||
| * @return string A string of classnames separate by a space. | ||
| */ | ||
| public function compile_classnames( $classnames ) { | ||
| if ( empty( $classnames ) || ! is_array( $classnames ) ) { | ||
| return null; | ||
| } | ||
| return implode( ' ', array_unique( $classnames ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Style value parser that returns a CSS definition array comprising style properties | ||
| * that have keys representing individual style properties, otherwise known as longhand CSS properties. | ||
|
|
@@ -604,6 +577,59 @@ protected static function get_individual_property_css_declarations( $style_value | |
| } | ||
| return $css_declarations; | ||
| } | ||
|
|
||
| /** | ||
| * Returns compiled CSS from parsed css_declarations. | ||
| * | ||
| * @param array $css_declarations An array of parsed CSS property => CSS value pairs. | ||
| * @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values. | ||
| * | ||
| * @return string A compiled CSS string. | ||
| */ | ||
| public function compile_css( $css_declarations, $css_selector ) { | ||
| if ( empty( $css_declarations ) || ! is_array( $css_declarations ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| // Return an entire rule if there is a selector. | ||
| if ( $css_selector ) { | ||
| $css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations ); | ||
| return $css_rule->get_css(); | ||
| } else { | ||
| $css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations ); | ||
| return $css_declarations->get_declarations_string(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a string of classnames, | ||
| * | ||
| * @param string $classnames A flat array of classnames. | ||
| * | ||
| * @return string A string of classnames separate by a space. | ||
| */ | ||
| public function compile_classnames( $classnames ) { | ||
| if ( empty( $classnames ) || ! is_array( $classnames ) ) { | ||
| return null; | ||
| } | ||
| return implode( ' ', array_unique( $classnames ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a compiled stylesheet from stored CSS rules. | ||
| * | ||
| * @param string $store_key A valid key corresponding to an existing store in static::$stores. | ||
| * | ||
| * @return string A compiled stylesheet from stored CSS rules. | ||
| */ | ||
| public static function compile_stylesheet_from_store( $store_key ) { | ||
| $store = static::get_store( $store_key ); | ||
| if ( $store ) { | ||
| $processor = new WP_Style_Engine_Processor( $store ); | ||
| return $processor->get_css(); | ||
| } | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -650,7 +676,7 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) { | |
| $styles_output['css'] = $style_engine->compile_css( $parsed_styles['css_declarations'], $options['selector'] ); | ||
| $styles_output['declarations'] = $parsed_styles['css_declarations']; | ||
| if ( true === $options['enqueue'] ) { | ||
| $style_engine->store_css_rule( $options['selector'], $parsed_styles['css_declarations'], 'block-supports' ); | ||
| $style_engine::store_css_rule( $options['selector'], $parsed_styles['css_declarations'], 'block-supports' ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -664,22 +690,47 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) { | |
| } | ||
|
|
||
| /** | ||
| * Global public interface method to parse block styles from a single block style object | ||
| * and then, via the `enqueue` flag, will enqueue them for rendering on the frontend in a block-supports inline style tag. | ||
| * Global public interface method to register styles to be enqueued and rendered. | ||
| * | ||
| * @access public | ||
| * | ||
| * @param string $store_key A valid store key. | ||
| * @param array $css_rules array( | ||
| * 'selector' => (string) A CSS selector. | ||
| * 'css_declarations' => (boolean) An array of CSS definitions, e.g., array( "$property" => "$value" ). | ||
| * );. | ||
| * | ||
| * @return WP_Style_Engine_CSS_Rules_Store|null The store, if found, otherwise `null`. | ||
| */ | ||
| function wp_style_engine_add_to_store( $store_key, $css_rules = array() ) { | ||
| if ( empty( $store_key ) || empty( $css_rules ) ) { | ||
| return null; | ||
| } | ||
| if ( class_exists( 'WP_Style_Engine' ) ) { | ||
| $style_engine = WP_Style_Engine::get_instance(); | ||
| foreach ( $css_rules as $selector => $css_declarations ) { | ||
| $style_engine::store_css_rule( $selector, $css_declarations, $store_key ); | ||
| } | ||
| return $style_engine::get_store( $store_key ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a compiled stylesheet from stored CSS rules. | ||
| * | ||
| * @access public | ||
| * | ||
| * @param string $selector A CSS selector. | ||
| * @param array $css_declarations An array of CSS definitions, e.g., array( "$property" => "$value" ). | ||
| * @param string $store_key A valid store key. | ||
| * | ||
| * @return boolean Whether the storage process was successful. | ||
| * @return string A compiled stylesheet from stored CSS rules. | ||
| */ | ||
| function wp_style_engine_enqueue_block_supports_styles( $selector, $css_declarations ) { | ||
| if ( empty( $selector ) || empty( $css_declarations ) ) { | ||
| return false; | ||
| function wp_style_engine_get_stylesheet( $store_key ) { | ||
| if ( empty( $store_key ) ) { | ||
| return null; | ||
| } | ||
| if ( class_exists( 'WP_Style_Engine' ) ) { | ||
| return WP_Style_Engine::get_instance()->store_css_rule( $selector, $css_declarations, 'block-supports' ); | ||
| return WP_Style_Engine::get_instance()::compile_stylesheet_from_store( $store_key ); | ||
| } | ||
| return false; | ||
| return null; | ||
| } | ||
|
|
||
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.