mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Improve performance of WP_Theme_JSON::merge
when merging background styles
#7486
Closed
mukeshpanchal27
wants to merge
14
commits into
WordPress:trunk
from
mukeshpanchal27:fix/61858-performance-regression
Closed
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c13a398
Improve performance
mukeshpanchal27 2b7be60
Merge branch 'WordPress:trunk' into fix/61858-performance-regression
mukeshpanchal27 46640a7
Introduce get_block_node_paths()
mukeshpanchal27 76e0140
Add options that include node path and block elements
mukeshpanchal27 82dc69a
Apply suggestions from code review
mukeshpanchal27 867ae4a
Remove "include_block_elements" parameter
mukeshpanchal27 dbbd010
Address review feedbacks
mukeshpanchal27 bdf769c
Merge branch 'WordPress:trunk' into fix/61858-performance-regression
mukeshpanchal27 2adb26a
Remove variable
mukeshpanchal27 58dfa83
Update new variable description
mukeshpanchal27 35e4ed4
Unit tests for new changes
mukeshpanchal27 f1237f8
Merge branch 'trunk' into fix/61858-performance-regression
felixarntz 115dc4e
Add since annotation for the method change.
felixarntz 0eb736b
Move option variable assignments before the loops.
felixarntz 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 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 |
---|---|---|
|
@@ -2696,7 +2696,8 @@ private static function update_separator_declarations( $declarations ) { | |
* @param array $options { | ||
* Optional. An array of options for now used for internal purposes only (may change without notice). | ||
* | ||
* @type bool $include_block_style_variations Includes nodes for block style variations. Default false. | ||
* @type bool $include_block_style_variations Includes nodes for block style variations. Default false. | ||
* @type bool $include_node_paths_only Return only block nodes node paths. Default false. | ||
* } | ||
* @return array The block nodes in theme.json. | ||
*/ | ||
|
@@ -2713,56 +2714,81 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt | |
} | ||
|
||
foreach ( $theme_json['styles']['blocks'] as $name => $node ) { | ||
$selector = null; | ||
if ( isset( $selectors[ $name ]['selector'] ) ) { | ||
$selector = $selectors[ $name ]['selector']; | ||
} | ||
|
||
$duotone_selector = null; | ||
if ( isset( $selectors[ $name ]['duotone'] ) ) { | ||
$duotone_selector = $selectors[ $name ]['duotone']; | ||
} | ||
$include_node_paths_only = $options['include_node_paths_only'] ?? false; | ||
$node_path = array( 'styles', 'blocks', $name ); | ||
if ( $include_node_paths_only ) { | ||
mukeshpanchal27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$nodes[] = array( | ||
'path' => $node_path, | ||
); | ||
} else { | ||
$selector = null; | ||
if ( isset( $selectors[ $name ]['selector'] ) ) { | ||
$selector = $selectors[ $name ]['selector']; | ||
} | ||
|
||
$feature_selectors = null; | ||
if ( isset( $selectors[ $name ]['selectors'] ) ) { | ||
$feature_selectors = $selectors[ $name ]['selectors']; | ||
} | ||
$duotone_selector = null; | ||
if ( isset( $selectors[ $name ]['duotone'] ) ) { | ||
$duotone_selector = $selectors[ $name ]['duotone']; | ||
} | ||
|
||
$variation_selectors = array(); | ||
$include_variations = $options['include_block_style_variations'] ?? false; | ||
if ( $include_variations && isset( $node['variations'] ) ) { | ||
foreach ( $node['variations'] as $variation => $node ) { | ||
$variation_selectors[] = array( | ||
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ), | ||
'selector' => $selectors[ $name ]['styleVariations'][ $variation ], | ||
); | ||
$feature_selectors = null; | ||
if ( isset( $selectors[ $name ]['selectors'] ) ) { | ||
$feature_selectors = $selectors[ $name ]['selectors']; | ||
} | ||
} | ||
|
||
$nodes[] = array( | ||
'name' => $name, | ||
'path' => array( 'styles', 'blocks', $name ), | ||
'selector' => $selector, | ||
'selectors' => $feature_selectors, | ||
'duotone' => $duotone_selector, | ||
'features' => $feature_selectors, | ||
'variations' => $variation_selectors, | ||
'css' => $selector, | ||
); | ||
$variation_selectors = array(); | ||
$include_variations = $options['include_block_style_variations'] ?? false; | ||
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. Similarly, this could also be set outside the loop |
||
if ( $include_variations && isset( $node['variations'] ) ) { | ||
foreach ( $node['variations'] as $variation => $node ) { | ||
$variation_selectors[] = array( | ||
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ), | ||
'selector' => $selectors[ $name ]['styleVariations'][ $variation ], | ||
); | ||
} | ||
} | ||
|
||
$nodes[] = array( | ||
'name' => $name, | ||
'path' => $node_path, | ||
'selector' => $selector, | ||
'selectors' => $feature_selectors, | ||
'duotone' => $duotone_selector, | ||
'features' => $feature_selectors, | ||
'variations' => $variation_selectors, | ||
'css' => $selector, | ||
); | ||
} | ||
|
||
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) { | ||
foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) { | ||
$node_path = array( 'styles', 'blocks', $name, 'elements', $element ); | ||
if ( $include_node_paths_only ) { | ||
$nodes[] = array( | ||
'path' => $node_path, | ||
); | ||
continue; | ||
} | ||
|
||
$nodes[] = array( | ||
'path' => array( 'styles', 'blocks', $name, 'elements', $element ), | ||
'path' => $node_path, | ||
'selector' => $selectors[ $name ]['elements'][ $element ], | ||
); | ||
|
||
// Handle any pseudo selectors for the element. | ||
if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] ) ) { | ||
foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) { | ||
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) { | ||
$node_path = array( 'styles', 'blocks', $name, 'elements', $element ); | ||
if ( $include_node_paths_only ) { | ||
$nodes[] = array( | ||
'path' => $node_path, | ||
); | ||
continue; | ||
} | ||
|
||
$nodes[] = array( | ||
'path' => array( 'styles', 'blocks', $name, 'elements', $element ), | ||
'path' => $node_path, | ||
'selector' => static::append_to_selector( $selectors[ $name ]['elements'][ $element ], $pseudo_selector ), | ||
); | ||
} | ||
|
@@ -3236,7 +3262,11 @@ public function merge( $incoming ) { | |
* some values provide exceptions, namely style values that are | ||
* objects and represent unique definitions for the style. | ||
*/ | ||
$style_nodes = static::get_styles_block_nodes(); | ||
$style_nodes = static::get_block_nodes( | ||
$this->theme_json, | ||
array(), | ||
array( 'include_node_paths_only' => true ) | ||
); | ||
foreach ( $style_nodes as $style_node ) { | ||
$path = $style_node['path']; | ||
mukeshpanchal27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* | ||
|
This file contains 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
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.
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.
This could be moved outside of the
foreach
loop since that option doesn't change