-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Tweaks for #42452 #42691
Tweaks for #42452 #42691
Conversation
* | ||
* @return WP_Style_Engine_CSS_Rules_Store[] | ||
*/ | ||
public static function get_stores() { |
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.
A get_stores
in the Store class makes sense in order to get all registered/available stores. In all the other PRs I made, this one was always a requirement so I'm adding it here too as it makes it easier to abstract the logic for $stores
in the WP_Style_Engine
object as well.
/** | ||
* Instance of WP_Style_Engine_CSS_Rules_Store to hold block supports CSS rules. | ||
* | ||
* @var array<string, WP_Style_Engine_CSS_Rules_Store|null> | ||
*/ | ||
private static $stores = array( | ||
'layout-block-supports' => null, | ||
'block-supports' => null, | ||
); | ||
|
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.
We should not have a hardcoded array of stores. Instead, we can use the WP_Style_Engine_CSS_Rules_Store::get_stores()
method to get all available/registered stores. This will make it easier to extend the class and it will work for all future iterations regardless of the store's name.
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.
Makes total sense. 🙇
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.
Just to leave a note on my thinking in relation to these hardcoded stores:
- I was trying to find a way to control the number and the name of stores registered in Gutenberg
- By having a hard coded list somewhere we can then control the order in which we print each store's styles onto the page. This is important for inheritance and also, later, for CSS layers.
I agree defining them here is not the right place, and we don't need to do right now anyway.
Maybe later we can think about how to tell the style engine/store class to restrict stores and the order in which we store them for the purposes of rendering to the page. 🤔
cc @andrewserong just for a FYI
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.
cc @andrewserong just for a FYI
Thanks for the heads-up, and great points! Yes, it could be a good idea for us to keep an allow-list of stores (somewhere?) to catch accidental typos / ensure that Gutenberg's styles are being added to the right place. We could even use a _doing_it_wrong
call to flag when it's being used incorrectly?
and we don't need to do right now anyway. Maybe later we can think about how to tell the style engine/store class to restrict stores and the order in which we store them for the purposes of rendering to the page.
Agreed.
foreach ( static::$stores as $store_key => $store_instance ) { | ||
static::$stores[ $store_key ] = WP_Style_Engine_CSS_Rules_Store::get_store( $store_key ); | ||
} |
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.
No longer needed since the $stores
was removed. Instead of this, we'll be getting a store directly when needed.
$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 ); | ||
static::get_store( $store_key )->add_rule( $css_selector )->add_declarations( $css_declarations ); |
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.
Simplifies the implementation by using the methods other objects in the engine already have
return null; | ||
} | ||
return static::$stores[ $store_key ]; | ||
return WP_Style_Engine_CSS_Rules_Store::get_store( $store_key ); |
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.
Always gets a store. Previously we only allowed 2 store names, this one will get the store if it existed, otherwise it will create a new store and return it.
$stores = WP_Style_Engine_CSS_Rules_Store::get_stores(); | ||
|
||
foreach ( $stores as $key => $store ) { | ||
$styles = static::compile_stylesheet_from_store( $key ); | ||
|
||
if ( ! empty( $styles ) ) { | ||
wp_register_style( $key, false, array(), true, true ); | ||
wp_add_inline_style( $key, $styles ); | ||
wp_enqueue_style( $key ); | ||
} | ||
} |
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.
Gets an array of all stores and enqueued them each one separately.
* | ||
* @return string A string of classnames separate by a space. | ||
*/ | ||
public function compile_classnames( $classnames ) { |
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.
The compile_classnames
method was only used in 1 spot, and it just does an implode( ' ', array_unique( $classnames ) );
so nothing complicated that would warrant a new method. The implode & array_unique were simply added to the place where this method was being called.
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.
Good call
@@ -594,40 +578,21 @@ public function compile_css( $css_declarations, $css_selector ) { | |||
if ( $css_selector ) { | |||
$css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations ); | |||
return $css_rule->get_css(); | |||
} else { |
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.
else
here was not needed since the line above is a `return.
$store = static::get_store( $store_key ); | ||
if ( $store ) { | ||
$processor = new WP_Style_Engine_Processor( $store ); | ||
return $processor->get_css(); | ||
} | ||
return ''; | ||
$processor = new WP_Style_Engine_Processor( static::get_store( $store_key ) ); | ||
return $processor->get_css(); |
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.
Since the $store
now always exists, this method was simplified.
@@ -678,7 +643,7 @@ function wp_style_engine_get_block_supports_styles( $block_styles, $options = ar | |||
} | |||
|
|||
if ( ! empty( $parsed_styles['classnames'] ) ) { | |||
$styles_output['classnames'] = $style_engine->compile_classnames( $parsed_styles['classnames'] ); | |||
$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) ); |
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.
The compile_classnames
method was removed.
* @return WP_Style_Engine_CSS_Rules_Store|null The store, if found, otherwise `null`. | ||
* @return WP_Style_Engine_CSS_Rules_Store. | ||
*/ | ||
function wp_style_engine_add_to_store( $store_key, $css_rules = array() ) { | ||
if ( empty( $store_key ) || empty( $css_rules ) ) { | ||
return null; | ||
$style_engine = WP_Style_Engine::get_instance(); | ||
if ( empty( $css_rules ) ) { | ||
return $style_engine::get_store( $store_key ); | ||
} | ||
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 ); | ||
|
||
foreach ( $css_rules as $selector => $css_declarations ) { | ||
if ( empty( $selector ) || empty( $css_declarations ) ) { | ||
continue; | ||
} | ||
return $style_engine::get_store( $store_key ); | ||
$style_engine::store_css_rule( $selector, $css_declarations, $store_key ); | ||
} | ||
return $style_engine::get_store( $store_key ); |
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.
Tweaked the wp_style_engine_add_to_store
function to always return a store instead of store|null.
This will make the API more robust.
Added test for new method.
* | ||
* @return void | ||
*/ | ||
public static function remove_all_stores() { |
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.
Hope you don't mind, I added this to make testing easier.
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.
Thanks a lot for this @aristath 🙇
I merged the latest change from the base branch and added a couple of tests. Hope that's okay! If you think the remove_all_stores
is a bit off or needs renaming, go for it. I mainly added it for the tests.
…y/style-engine-enqueue-block-supports-styles-tweaks
* abstract stores * "else" not needed * compile_classnames method not needed * we have a method to get the store here * Make the wp_style_engine_add_to_store function always return a store * wp_style_engine_get_stylesheet - always return string * Merged with base branch. Added test for new method. Co-authored-by: ramonjd <ramonjd@gmail.com> Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
* abstract stores * "else" not needed * compile_classnames method not needed * we have a method to get the store here * Make the wp_style_engine_add_to_store function always return a store * wp_style_engine_get_stylesheet - always return string * Merged with base branch. Added test for new method. Co-authored-by: ramonjd <ramonjd@gmail.com> Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
* Enqueuing block support styles version 1. * Linter, this one's for you. * Post trunk merge cleanup and update tests. * Removed spacing around curly braces in CSS rules. Updated tests. We could maybe add a prettify option down the road. Juggling methods around to cater for adding styles to (any) store. Also making return values consistent. * Splitting `wp_style_engine_enqueue_block_supports_styles` and `wp_style_engine_get_block_supports_styles` so we can enqueue styles that don't need parsing, e.g., layout * Integrate the processor class * Migrate layout styles to style engine store. * Update packages/style-engine/class-wp-style-engine.php Co-authored-by: Ari Stathopoulos <aristath@gmail.com> * Tweaks for #42452 (#42691) * abstract stores * "else" not needed * compile_classnames method not needed * we have a method to get the store here * Make the wp_style_engine_add_to_store function always return a store * wp_style_engine_get_stylesheet - always return string * Merged with base branch. Added test for new method. Co-authored-by: ramonjd <ramonjd@gmail.com> Co-authored-by: Ramon <ramonjd@users.noreply.github.com> * Adding check for the context argument. Adding tests. * Updating the processor so that it's ignorant of stores. Why? So that it can be used to process any CSS and not just stored CSS. Updating layout for backwards compatibility in gutenberg_get_layout_style (returning the styles that are collected in the function body only) Created a new mode for incoming $css_rules to be a collection of selector + css_declaration keys. Removed wp_style_engine_get_stylesheet (from store) since we don't use it yet Added a new function wp_style_engine_get_stylesheet_from_css_rules() that will process and compile a collection of CSS rules, and not store them. * dump var_dump() * Improve the processor * remove trailing commas - compatibility with PHP < 7.2 * rename css_declarations to declarations * remove unused variable Removing unused function wp_style_engine_get_stylesheet_from_store Updating tests to check for merging and deduping * Switch parse_block_styles from public to protected static * Now that all methods are static, there's no need to call `get_instance()` * Revert get_instance() in wp_style_engine_add_to_store because we want to ensure the hook is enqueued here. * Adding a test for the 'enqueue' flag. * Update lib/block-supports/layout.php Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com> * Adding a test for the 'enqueue' flag. * Add named stores to the processor * avoid setting var for something that only gets used once * Only use "else" if absolutely necessary * Add a set_name method * combine & simplify conditions * use empty() instead of isset() checks here * shorten it Co-authored-by: Ari Stathopoulos <aristath@gmail.com> Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
No description provided.