Skip to content

Commit 994c202

Browse files
Micro-optimizations in ::get_styles_for_block()
1 parent 86ac232 commit 994c202

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/wp-includes/class-wp-theme-json.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,33 +2168,35 @@ public function get_styles_for_block( $block_metadata ) {
21682168

21692169
// If the block has feature selectors, generate the declarations for them within the current style variation.
21702170
if ( ! empty( $block_metadata['features'] ) ) {
2171+
$clean_style_variation_selector = trim( $style_variation_selector );
21712172
foreach ( $block_metadata['features'] as $feature_name => $feature_selector ) {
2172-
if ( ! empty( $style_variation_node[ $feature_name ] ) ) {
2173-
// Prepend the variation selector to the feature selector.
2174-
$split_feature_selectors = explode( ',', $feature_selector );
2175-
$feature_selectors = array_map(
2176-
function( $split_feature_selector ) use ( $style_variation_selector ) {
2177-
return trim( $style_variation_selector ) . trim( $split_feature_selector );
2178-
},
2179-
$split_feature_selectors
2180-
);
2181-
$combined_feature_selectors = implode( ',', $feature_selectors );
2182-
2183-
// Compute declarations for the feature.
2184-
$new_feature_declarations = static::compute_style_properties( array( $feature_name => $style_variation_node[ $feature_name ] ), $settings, null, $this->theme_json );
2185-
2186-
// Merge new declarations with any that already exist for
2187-
// the feature selector. This may occur when multiple block
2188-
// support features use the same custom selector.
2189-
if ( isset( $style_variation_declarations[ $combined_feature_selectors ] ) ) {
2190-
$style_variation_declarations[ $combined_feature_selectors ] = array_merge( $style_variation_declarations[ $combined_feature_selectors ], $new_feature_declarations );
2191-
} else {
2192-
$style_variation_declarations[ $combined_feature_selectors ] = $new_feature_declarations;
2193-
}
2194-
// Remove the feature from the variation's node now the
2195-
// styles will be included under the feature level selector.
2196-
unset( $style_variation_node[ $feature_name ] );
2173+
if ( empty( $style_variation_node[ $feature_name ] ) ) {
2174+
continue;
2175+
}
2176+
// Prepend the variation selector to the feature selector.
2177+
$split_feature_selectors = explode( ',', $feature_selector );
2178+
$feature_selectors = array_map(
2179+
static function( $split_feature_selector ) use ( $clean_style_variation_selector ) {
2180+
return $clean_style_variation_selector . trim( $split_feature_selector );
2181+
},
2182+
$split_feature_selectors
2183+
);
2184+
$combined_feature_selectors = implode( ',', $feature_selectors );
2185+
2186+
// Compute declarations for the feature.
2187+
$new_feature_declarations = static::compute_style_properties( array( $feature_name => $style_variation_node[ $feature_name ] ), $settings, null, $this->theme_json );
2188+
2189+
// Merge new declarations with any that already exist for
2190+
// the feature selector. This may occur when multiple block
2191+
// support features use the same custom selector.
2192+
if ( isset( $style_variation_declarations[ $combined_feature_selectors ] ) ) {
2193+
$style_variation_declarations[ $combined_feature_selectors ] = array_merge( $style_variation_declarations[ $combined_feature_selectors ], $new_feature_declarations );
2194+
} else {
2195+
$style_variation_declarations[ $combined_feature_selectors ] = $new_feature_declarations;
21972196
}
2197+
// Remove the feature from the variation's node now the
2198+
// styles will be included under the feature level selector.
2199+
unset( $style_variation_node[ $feature_name ] );
21982200
}
21992201
}
22002202
// Compute declarations for remaining styles not covered by feature level selectors.

0 commit comments

Comments
 (0)