-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Global Styles: Add style variation partial cache #62610
Open
aaronrobertshaw
wants to merge
3
commits into
trunk
Choose a base branch
from
add/style-variation-cache
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/6857 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/62610 |
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -91,6 +91,14 @@ class WP_Theme_JSON_Resolver_Gutenberg { | |||||||||||
*/ | ||||||||||||
protected static $theme_json_file_cache = array(); | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Cache of parsed and translated style variation theme.json partials. | ||||||||||||
* | ||||||||||||
* @since 6.6.0 | ||||||||||||
* @var array | ||||||||||||
*/ | ||||||||||||
protected static $style_variations_cache = array(); | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Processes a file that adheres to the theme.json schema | ||||||||||||
* and returns an array with its contents, or a void array if none found. | ||||||||||||
|
@@ -715,45 +723,33 @@ private static function recursively_iterate_json( $dir ) { | |||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Determines if a supplied style variation matches the provided scope. | ||||||||||||
* Determines the scope of a style variation. | ||||||||||||
* | ||||||||||||
* For backwards compatibility, if a variation does not define any scope | ||||||||||||
* related property, e.g. `blockTypes`, it is assumed to be a theme style | ||||||||||||
* variation. | ||||||||||||
* | ||||||||||||
* @since 6.6.0 | ||||||||||||
* | ||||||||||||
* @param array $variation Theme.json shaped style variation object. | ||||||||||||
* @param string $scope Scope to check e.g. theme, block etc. | ||||||||||||
* @param array $variation Theme.json shaped style variation object. | ||||||||||||
* | ||||||||||||
* @return boolean | ||||||||||||
* @return string | ||||||||||||
*/ | ||||||||||||
private static function style_variation_has_scope( $variation, $scope ) { | ||||||||||||
if ( 'block' === $scope ) { | ||||||||||||
return isset( $variation['blockTypes'] ); | ||||||||||||
} | ||||||||||||
|
||||||||||||
if ( 'theme' === $scope ) { | ||||||||||||
return ! isset( $variation['blockTypes'] ); | ||||||||||||
protected static function get_style_variation_scope( $variation ) { | ||||||||||||
if ( isset( $variation['blockTypes'] ) ) { | ||||||||||||
return 'block'; | ||||||||||||
} | ||||||||||||
|
||||||||||||
return false; | ||||||||||||
return 'theme'; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns the style variations defined by the theme (parent and child). | ||||||||||||
* Retrieves all style variation partials defined by the theme (parent and child). | ||||||||||||
* | ||||||||||||
* @since 6.2.0 Returns parent theme variations if theme is a child. | ||||||||||||
* @since 6.6.0 Added configurable scope parameter to allow filtering | ||||||||||||
* theme.json partial files by the scope to which they | ||||||||||||
* can be applied e.g. theme vs block etc. | ||||||||||||
* | ||||||||||||
* @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. | ||||||||||||
* @return array | ||||||||||||
* @since 6.6.0 | ||||||||||||
*/ | ||||||||||||
public static function get_style_variations( $scope = 'theme' ) { | ||||||||||||
protected static function get_style_variation_files() { | ||||||||||||
$variation_files = array(); | ||||||||||||
$variations = array(); | ||||||||||||
$base_directory = get_stylesheet_directory() . '/styles'; | ||||||||||||
$template_directory = get_template_directory() . '/styles'; | ||||||||||||
if ( is_dir( $base_directory ) ) { | ||||||||||||
|
@@ -772,18 +768,60 @@ public static function get_style_variations( $scope = 'theme' ) { | |||||||||||
$variation_files = array_merge( $variation_files, $variation_files_parent ); | ||||||||||||
} | ||||||||||||
ksort( $variation_files ); | ||||||||||||
|
||||||||||||
return $variation_files; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns the style variations defined by the theme (parent and child). | ||||||||||||
* | ||||||||||||
* @since 6.2.0 Returns parent theme variations if theme is a child. | ||||||||||||
* @since 6.6.0 Added cache and configurable scope parameter to allow | ||||||||||||
* filtering theme.json partial files by the scope to | ||||||||||||
* which they can be applied e.g. theme vs block etc. | ||||||||||||
* | ||||||||||||
* @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. | ||||||||||||
* @return array | ||||||||||||
*/ | ||||||||||||
public static function get_style_variations( $scope = 'theme' ) { | ||||||||||||
$theme_dir = get_stylesheet_directory(); | ||||||||||||
$locale = get_locale(); | ||||||||||||
|
||||||||||||
if ( | ||||||||||||
isset( static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ] ) && | ||||||||||||
// Variations depend on registered blocks. | ||||||||||||
null !== static::$blocks && | ||||||||||||
static::has_same_registered_blocks( 'theme' ) | ||||||||||||
) { | ||||||||||||
return static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ]; | ||||||||||||
} | ||||||||||||
|
||||||||||||
$variation_files = static::get_style_variation_files(); | ||||||||||||
$variations = array( | ||||||||||||
'theme' => array(), | ||||||||||||
'block' => array(), | ||||||||||||
); | ||||||||||||
|
||||||||||||
foreach ( $variation_files as $path => $file ) { | ||||||||||||
$decoded_file = self::read_json_file( $path ); | ||||||||||||
if ( is_array( $decoded_file ) && static::style_variation_has_scope( $decoded_file, $scope ) ) { | ||||||||||||
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); | ||||||||||||
$variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data(); | ||||||||||||
if ( empty( $variation['title'] ) ) { | ||||||||||||
$variation['title'] = basename( $path, '.json' ); | ||||||||||||
if ( is_array( $decoded_file ) ) { | ||||||||||||
$variation_scope = static::get_style_variation_scope( $decoded_file ); | ||||||||||||
|
||||||||||||
if ( $variation_scope ) { | ||||||||||||
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); | ||||||||||||
$variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data(); | ||||||||||||
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. From what I'm seeing, many of Rust's block styles aren't registered and are therefore failing the test in gutenberg/lib/class-wp-theme-json-gutenberg.php Lines 1080 to 1084 in d66f4d1
|
||||||||||||
|
||||||||||||
if ( empty( $variation['title'] ) ) { | ||||||||||||
$variation['title'] = basename( $path, '.json' ); | ||||||||||||
} | ||||||||||||
|
||||||||||||
$variations[ $variation_scope ][] = $variation; | ||||||||||||
} | ||||||||||||
$variations[] = $variation; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
return $variations; | ||||||||||||
static::$style_variations_cache[ $theme_dir ][ $locale ] = $variations; | ||||||||||||
|
||||||||||||
return static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ]; | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
||||||||||||
|
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.
Actually, I withdraw my comment.
Many of the Rust variation's block styles seems to be missing, e.g.,"core/calendar"
when the cache is returned here.It seems to be missing in the return value of
$variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data();
below.