Skip to content

Commit

Permalink
Add add_store() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Jul 25, 2022
1 parent a777954 commit 326aca1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
33 changes: 8 additions & 25 deletions lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ protected function get_preset_classes_store( $setting_nodes, $origins ) {

$selector = $metadata['selector'];
$node = _wp_array_get( $this->theme_json, $metadata['path'], array() );
$store = $this->combine_style_engine_stores( $store, static::compute_preset_classes_store( $node, $selector, $origins ) );
$store->add_store( static::compute_preset_classes_store( $node, $selector, $origins ) );
}
return $store;
}
Expand Down Expand Up @@ -723,10 +723,10 @@ public function get_stylesheet_store( $types = array( 'variables', 'styles', 'pr
$setting_nodes = static::get_setting_nodes( $this->theme_json, $blocks_metadata );

if ( in_array( 'variables', $types, true ) ) {
$store = $this->combine_style_engine_stores( $store, $this->get_css_variables_store( $setting_nodes, $origins ) );
$store->add_store( $this->get_css_variables_store( $setting_nodes, $origins ) );
}
if ( in_array( 'styles', $types, true ) ) {
$store = $this->combine_style_engine_stores( $store, $this->get_block_classes_store( $style_nodes ) );
$store->add_store( $this->get_block_classes_store( $style_nodes ) );
} elseif ( in_array( 'base-layout-styles', $types, true ) ) {
// Base layout styles are provided as part of `styles`, so only output separately if explicitly requested.
// For backwards compatibility, the Columns block is explicitly included, to support a different default gap value.
Expand All @@ -743,12 +743,12 @@ public function get_stylesheet_store( $types = array( 'variables', 'styles', 'pr
);

foreach ( $base_styles_nodes as $base_style_node ) {
$store = $this->combine_style_engine_stores( $store, $this->get_layout_styles_store( $base_style_node ) );
$store->add_store( $this->get_layout_styles_store( $base_style_node ) );
}
}

if ( in_array( 'presets', $types, true ) ) {
$store = $this->combine_style_engine_stores( $store, $this->get_preset_classes_store( $setting_nodes, $origins ) );
$store->add_store( $this->get_preset_classes_store( $setting_nodes, $origins ) );
}

return $store;
Expand Down Expand Up @@ -882,7 +882,7 @@ function( $pseudo_selector ) use ( $selector ) {
static::ROOT_BLOCK_SELECTOR !== $selector &&
! empty( $block_metadata['name'] )
) {
$store = $this->combine_style_engine_stores( $store, $this->get_layout_styles_store( $block_metadata ) );
$store->add_store( $this->get_layout_styles_store( $block_metadata ) );
}

// 5. Generate and append the feature level rulesets.
Expand Down Expand Up @@ -952,7 +952,7 @@ function( $pseudo_selector ) use ( $selector ) {
// For backwards compatibility, ensure the legacy block gap CSS variable is still available.
$store->add_rule( $selector )->add_declarations( array( '--wp--style--block-gap' => $block_gap_value ) );
}
$store = $this->combine_style_engine_stores( $store, $this->get_layout_styles_store( $block_metadata ) );
$store->add_store( $this->get_layout_styles_store( $block_metadata ) );
}

return $store;
Expand Down Expand Up @@ -982,7 +982,7 @@ protected function get_block_classes_store( $style_nodes ) {
if ( null === $metadata['selector'] ) {
continue;
}
$store = $this->combine_style_engine_stores( $store, $this->get_block_styles_store( $metadata ) );
$store->add_store( $this->get_block_styles_store( $metadata ) );
}

return $store;
Expand Down Expand Up @@ -1587,23 +1587,6 @@ protected function get_layout_styles( $block_metadata ) {
return ( new WP_Style_Engine_Processor_Gutenberg( $this->get_layout_styles_store( $block_metadata ) ) )->get_css( true );
}

/**
* Combine style-engine stores.
* Accepts 2 store-engine stores and returns the 1st, with the rules of the 2nd one added to it.
*
* @param WP_Style_Engine_CSS_Rules_Store_Gutenberg $store_1 The first store.
* @param WP_Style_Engine_CSS_Rules_Store_Gutenberg $store_2 The second store.
*
* @return WP_Style_Engine_CSS_Rules_Store_Gutenberg The combined store.
*/
public static function combine_style_engine_stores( $store_1, $store_2 ) {
$store_2_rules = $store_2->get_all_rules();
foreach ( $store_2_rules as $store_2_rule ) {
$store_1->add_rule( $store_2_rule->get_selector() )->add_declarations( $store_2_rule->get_declarations() );
}
return $store_1;
}

/**
* Converts a declarations array with name/value keys to a plain array with property => value pairs.
*
Expand Down
16 changes: 16 additions & 0 deletions packages/style-engine/class-wp-style-engine-css-rules-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,20 @@ public function add_rule( $selector ) {
public function remove_rule( $selector ) {
unset( $this->rules[ $selector ] );
}

/**
* Add a store. This method effectively merges the rules of the defined store into the current store,
* and removes them from the original store to avoid duplication.
*
* @param WP_Style_Engine_CSS_Rules_Store $store The store to merge.
*
* @return WP_Style_Engine_CSS_Rules_Store Returns the merged store.
*/
public function add_store( WP_Style_Engine_CSS_Rules_Store $store ) {
$incoming_rules = $store->get_all_rules();
foreach ( $incoming_rules as $rule ) {
$this->add_rule( $rule->get_selector() )->add_declarations( $rule->get_declarations() );
}
return $this;
}
}

0 comments on commit 326aca1

Please sign in to comment.