2021-05-24 04:37:55 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2021-05-24 09:25:56 -04:00
|
|
|
* WP_Theme_JSON_Resolver class
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
|
|
|
* @package WordPress
|
2021-05-24 09:25:56 -04:00
|
|
|
* @subpackage Theme
|
|
|
|
* @since 5.8.0
|
2021-05-24 04:37:55 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-05-24 09:25:56 -04:00
|
|
|
* Class that abstracts the processing of the different data sources
|
|
|
|
* for site-level config and offers an API to work with them.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-11-08 14:19:58 -05:00
|
|
|
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
|
|
|
|
* This is a low-level API that may need to do breaking changes. Please,
|
2023-04-27 19:15:17 -04:00
|
|
|
* use get_global_settings(), get_global_styles(), and get_global_stylesheet() instead.
|
2021-11-08 14:19:58 -05:00
|
|
|
*
|
2021-05-24 04:37:55 -04:00
|
|
|
* @access private
|
|
|
|
*/
|
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 11:47:14 -04:00
|
|
|
#[AllowDynamicProperties]
|
2021-05-24 04:37:55 -04:00
|
|
|
class WP_Theme_JSON_Resolver {
|
|
|
|
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
/**
|
|
|
|
* Container for keep track of registered blocks.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $blocks_cache = array(
|
|
|
|
'core' => array(),
|
|
|
|
'blocks' => array(),
|
|
|
|
'theme' => array(),
|
|
|
|
'user' => array(),
|
|
|
|
);
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/**
|
|
|
|
* Container for data coming from core.
|
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
2021-05-24 04:37:55 -04:00
|
|
|
* @var WP_Theme_JSON
|
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static $core = null;
|
2021-05-24 04:37:55 -04:00
|
|
|
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
/**
|
|
|
|
* Container for data coming from the blocks.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
* @var WP_Theme_JSON
|
|
|
|
*/
|
|
|
|
protected static $blocks = null;
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/**
|
|
|
|
* Container for data coming from the theme.
|
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
2021-05-24 04:37:55 -04:00
|
|
|
* @var WP_Theme_JSON
|
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static $theme = null;
|
2021-05-24 04:37:55 -04:00
|
|
|
|
|
|
|
/**
|
2021-11-08 14:19:58 -05:00
|
|
|
* Container for data coming from the user.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-08-11 05:08:01 -04:00
|
|
|
* @since 5.9.0
|
2021-11-08 14:19:58 -05:00
|
|
|
* @var WP_Theme_JSON
|
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static $user = null;
|
2021-11-08 14:19:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the ID of the custom post type
|
|
|
|
* that holds the user data.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
2021-12-04 07:58:01 -05:00
|
|
|
* @var int
|
2021-11-08 14:19:58 -05:00
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static $user_custom_post_type_id = null;
|
2021-11-08 14:19:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container to keep loaded i18n schema for `theme.json`.
|
|
|
|
*
|
2021-12-04 10:57:01 -05:00
|
|
|
* @since 5.8.0 As `$theme_json_i18n`.
|
|
|
|
* @since 5.9.0 Renamed from `$theme_json_i18n` to `$i18n_schema`.
|
2021-05-24 09:25:56 -04:00
|
|
|
* @var array
|
2021-05-24 04:37:55 -04:00
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static $i18n_schema = null;
|
2021-05-24 04:37:55 -04:00
|
|
|
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
/**
|
|
|
|
* `theme.json` file cache.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $theme_json_file_cache = array();
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/**
|
2021-05-24 09:25:56 -04:00
|
|
|
* Processes a file that adheres to the theme.json schema
|
|
|
|
* and returns an array with its contents, or a void array if none found.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
* @since 6.1.0 Added caching.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @param string $file_path Path to file. Empty if no file.
|
2021-05-24 04:37:55 -04:00
|
|
|
* @return array Contents that adhere to the theme.json schema.
|
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static function read_json_file( $file_path ) {
|
2021-05-24 04:37:55 -04:00
|
|
|
if ( $file_path ) {
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
if ( array_key_exists( $file_path, static::$theme_json_file_cache ) ) {
|
|
|
|
return static::$theme_json_file_cache[ $file_path ];
|
|
|
|
}
|
|
|
|
|
2021-08-11 05:08:01 -04:00
|
|
|
$decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
|
2021-05-24 04:37:55 -04:00
|
|
|
if ( is_array( $decoded_file ) ) {
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
static::$theme_json_file_cache[ $file_path ] = $decoded_file;
|
|
|
|
return static::$theme_json_file_cache[ $file_path ];
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
}
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
|
|
|
|
return array();
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a data structure used in theme.json translation.
|
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
2021-08-11 05:08:01 -04:00
|
|
|
* @deprecated 5.9.0
|
2021-05-24 09:25:56 -04:00
|
|
|
*
|
|
|
|
* @return array An array of theme.json fields that are translatable and the keys that are translatable.
|
2021-05-24 04:37:55 -04:00
|
|
|
*/
|
|
|
|
public static function get_fields_to_translate() {
|
2021-08-11 05:08:01 -04:00
|
|
|
_deprecated_function( __METHOD__, '5.9.0' );
|
|
|
|
return array();
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-24 09:25:56 -04:00
|
|
|
* Given a theme.json structure modifies it in place to update certain values
|
|
|
|
* by its translated strings according to the language set by the user.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @param array $theme_json The theme.json to translate.
|
|
|
|
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
|
|
|
|
* Default 'default'.
|
2021-05-24 04:37:55 -04:00
|
|
|
* @return array Returns the modified $theme_json_structure.
|
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static function translate( $theme_json, $domain = 'default' ) {
|
|
|
|
if ( null === static::$i18n_schema ) {
|
|
|
|
$i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' );
|
|
|
|
static::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema;
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
2022-02-17 04:04:05 -05:00
|
|
|
return translate_settings_using_i18n_schema( static::$i18n_schema, $theme_json, $domain );
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-14 02:15:09 -04:00
|
|
|
* Returns core's origin config.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-05-24 04:37:55 -04:00
|
|
|
* @return WP_Theme_JSON Entity that holds core data.
|
|
|
|
*/
|
|
|
|
public static function get_core_data() {
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
if ( null !== static::$core && static::has_same_registered_blocks( 'core' ) ) {
|
|
|
|
return static::$core;
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:31:10 -04:00
|
|
|
$config = static::read_json_file( __DIR__ . '/theme.json' );
|
|
|
|
$config = static::translate( $config );
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
|
2022-09-20 09:13:21 -04:00
|
|
|
/**
|
|
|
|
* Filters the default data provided by WordPress for global styles & settings.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
2023-07-06 20:36:22 -04:00
|
|
|
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
|
2022-09-20 09:13:21 -04:00
|
|
|
*/
|
2024-06-19 12:41:13 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
|
|
|
|
* compatible class that is not a WP_Theme_JSON_Data object.
|
|
|
|
*/
|
|
|
|
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
|
|
|
|
static::$core = $theme_json->get_theme_json();
|
|
|
|
} else {
|
|
|
|
$config = $theme_json->get_data();
|
|
|
|
static::$core = new WP_Theme_JSON( $config, 'default' );
|
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
|
2022-02-17 04:04:05 -05:00
|
|
|
return static::$core;
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
/**
|
|
|
|
* Checks whether the registered blocks were already processed for this origin.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
|
|
|
* @param string $origin Data source for which to cache the blocks.
|
|
|
|
* Valid values are 'core', 'blocks', 'theme', and 'user'.
|
|
|
|
* @return bool True on success, false otherwise.
|
|
|
|
*/
|
|
|
|
protected static function has_same_registered_blocks( $origin ) {
|
|
|
|
// Bail out if the origin is invalid.
|
|
|
|
if ( ! isset( static::$blocks_cache[ $origin ] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$registry = WP_Block_Type_Registry::get_instance();
|
|
|
|
$blocks = $registry->get_all_registered();
|
|
|
|
|
|
|
|
// Is there metadata for all currently registered blocks?
|
|
|
|
$block_diff = array_diff_key( $blocks, static::$blocks_cache[ $origin ] );
|
|
|
|
if ( empty( $block_diff ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $blocks as $block_name => $block_type ) {
|
|
|
|
static::$blocks_cache[ $origin ][ $block_name ] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/**
|
|
|
|
* Returns the theme's data.
|
|
|
|
*
|
2021-11-08 14:19:58 -05:00
|
|
|
* Data from theme.json will be backfilled from existing
|
|
|
|
* theme supports, if any. Note that if the same data
|
|
|
|
* is present in theme.json and in theme supports,
|
2022-01-18 15:29:06 -05:00
|
|
|
* the theme.json takes precedence.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* @since 5.8.0
|
2021-12-04 07:58:01 -05:00
|
|
|
* @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed.
|
2022-04-28 05:59:13 -04:00
|
|
|
* @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
|
2024-06-04 07:55:14 -04:00
|
|
|
* @since 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports.
|
2024-06-24 04:51:16 -04:00
|
|
|
* Added registration and merging of block style variations from partial theme.json files and the block styles registry.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-12-04 07:58:01 -05:00
|
|
|
* @param array $deprecated Deprecated. Not used.
|
2022-04-28 05:59:13 -04:00
|
|
|
* @param array $options {
|
|
|
|
* Options arguments.
|
|
|
|
*
|
|
|
|
* @type bool $with_supports Whether to include theme supports in the data. Default true.
|
|
|
|
* }
|
2021-05-24 04:37:55 -04:00
|
|
|
* @return WP_Theme_JSON Entity that holds theme data.
|
|
|
|
*/
|
2022-04-11 06:38:00 -04:00
|
|
|
public static function get_theme_data( $deprecated = array(), $options = array() ) {
|
2021-11-08 14:19:58 -05:00
|
|
|
if ( ! empty( $deprecated ) ) {
|
2021-12-04 07:58:01 -05:00
|
|
|
_deprecated_argument( __METHOD__, '5.9.0' );
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
2022-04-11 06:38:00 -04:00
|
|
|
|
2022-11-11 12:51:12 -05:00
|
|
|
$options = wp_parse_args( $options, array( 'with_supports' => true ) );
|
2022-04-11 06:38:00 -04:00
|
|
|
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
|
2022-11-11 11:26:12 -05:00
|
|
|
$wp_theme = wp_get_theme();
|
Themes: Use `get_theme_file_path()` in `wp_theme_has_theme_json()`.
Ensure that all places where `theme.json` is included, use `get_theme_file_path` or `WP_Theme->get_file_path`, so that the path is run through `theme_file_path` filter. This change also means that the method `get_file_path_from_theme` can be deprecated, as it is no longer used in core.
Props flixos90, spacedmonkey, costdev, johnbillion, oglekler, hellofromtonya, mukesh27, audrasjb, oandregal.
Fixes #57629.
Built from https://develop.svn.wordpress.org/trunk@56073
git-svn-id: http://core.svn.wordpress.org/trunk@55585 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-27 13:12:26 -04:00
|
|
|
$theme_json_file = $wp_theme->get_file_path( 'theme.json' );
|
|
|
|
if ( is_readable( $theme_json_file ) ) {
|
2022-11-10 17:16:15 -05:00
|
|
|
$theme_json_data = static::read_json_file( $theme_json_file );
|
2022-11-11 11:26:12 -05:00
|
|
|
$theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) );
|
2022-11-10 17:16:15 -05:00
|
|
|
} else {
|
2024-06-04 07:55:14 -04:00
|
|
|
$theme_json_data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
|
2022-11-10 17:16:15 -05:00
|
|
|
}
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
|
2024-06-24 04:51:16 -04:00
|
|
|
/*
|
|
|
|
* Register variations defined by theme partials (theme.json files in the styles directory).
|
|
|
|
* This is required so the variations pass sanitization of theme.json data.
|
|
|
|
*/
|
2024-06-18 03:09:06 -04:00
|
|
|
$variations = static::get_style_variations( 'block' );
|
2024-06-24 04:51:16 -04:00
|
|
|
wp_register_block_style_variations_from_theme_json_partials( $variations );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source variations from the block registry and block style variation files. Then, merge them into the existing theme.json data.
|
|
|
|
*
|
|
|
|
* In case the same style properties are defined in several sources, this is how we should resolve the values,
|
|
|
|
* from higher to lower priority:
|
|
|
|
*
|
|
|
|
* - styles.blocks.blockType.variations from theme.json
|
|
|
|
* - styles.variations from theme.json
|
|
|
|
* - variations from block style variation files
|
|
|
|
* - variations from block styles registry
|
|
|
|
*
|
|
|
|
* See test_add_registered_block_styles_to_theme_data and test_unwraps_block_style_variations.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
$theme_json_data = static::inject_variations_from_block_style_variation_files( $theme_json_data, $variations );
|
|
|
|
$theme_json_data = static::inject_variations_from_block_styles_registry( $theme_json_data );
|
2024-06-18 03:09:06 -04:00
|
|
|
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
/**
|
|
|
|
* Filters the data provided by the theme for global styles and settings.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
2023-07-06 20:36:22 -04:00
|
|
|
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
*/
|
2024-06-19 12:41:13 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
|
|
|
|
* compatible class that is not a WP_Theme_JSON_Data object.
|
|
|
|
*/
|
|
|
|
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
|
|
|
|
static::$theme = $theme_json->get_theme_json();
|
|
|
|
} else {
|
|
|
|
$config = $theme_json->get_data();
|
|
|
|
static::$theme = new WP_Theme_JSON( $config );
|
|
|
|
}
|
Editor: Ensure block styles in `theme.json` are rendered.
This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.
Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.
Follow-up to [54385].
Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399
git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-06 14:02:10 -04:00
|
|
|
|
2022-11-11 11:26:12 -05:00
|
|
|
if ( $wp_theme->parent() ) {
|
2022-11-10 17:16:15 -05:00
|
|
|
// Get parent theme.json.
|
Themes: Use `get_theme_file_path()` in `wp_theme_has_theme_json()`.
Ensure that all places where `theme.json` is included, use `get_theme_file_path` or `WP_Theme->get_file_path`, so that the path is run through `theme_file_path` filter. This change also means that the method `get_file_path_from_theme` can be deprecated, as it is no longer used in core.
Props flixos90, spacedmonkey, costdev, johnbillion, oglekler, hellofromtonya, mukesh27, audrasjb, oandregal.
Fixes #57629.
Built from https://develop.svn.wordpress.org/trunk@56073
git-svn-id: http://core.svn.wordpress.org/trunk@55585 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-27 13:12:26 -04:00
|
|
|
$parent_theme_json_file = $wp_theme->parent()->get_file_path( 'theme.json' );
|
|
|
|
if ( $theme_json_file !== $parent_theme_json_file && is_readable( $parent_theme_json_file ) ) {
|
2022-11-10 17:16:15 -05:00
|
|
|
$parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
|
2022-11-11 11:26:12 -05:00
|
|
|
$parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) );
|
2022-11-10 17:16:15 -05:00
|
|
|
$parent_theme = new WP_Theme_JSON( $parent_theme_json_data );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merge the child theme.json into the parent theme.json.
|
|
|
|
* The child theme takes precedence over the parent.
|
|
|
|
*/
|
|
|
|
$parent_theme->merge( static::$theme );
|
|
|
|
static::$theme = $parent_theme;
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:38:00 -04:00
|
|
|
if ( ! $options['with_supports'] ) {
|
|
|
|
return static::$theme;
|
|
|
|
}
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/*
|
2021-12-05 13:02:08 -05:00
|
|
|
* We want the presets and settings declared in theme.json
|
|
|
|
* to override the ones declared via theme supports.
|
|
|
|
* So we take theme supports, transform it to theme.json shape
|
2022-02-17 04:04:05 -05:00
|
|
|
* and merge the static::$theme upon that.
|
2021-12-05 13:02:08 -05:00
|
|
|
*/
|
2023-01-26 13:39:13 -05:00
|
|
|
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
if ( ! wp_theme_has_theme_json() ) {
|
2024-06-05 04:13:15 -04:00
|
|
|
/*
|
|
|
|
* Unlike block themes, classic themes without a theme.json disable
|
|
|
|
* default presets when custom preset theme support is added. This
|
|
|
|
* behavior can be overridden by using the corresponding default
|
|
|
|
* preset theme support.
|
|
|
|
*/
|
|
|
|
$theme_support_data['settings']['color']['defaultPalette'] =
|
|
|
|
! isset( $theme_support_data['settings']['color']['palette'] ) ||
|
|
|
|
current_theme_supports( 'default-color-palette' );
|
|
|
|
$theme_support_data['settings']['color']['defaultGradients'] =
|
|
|
|
! isset( $theme_support_data['settings']['color']['gradients'] ) ||
|
|
|
|
current_theme_supports( 'default-gradient-presets' );
|
|
|
|
$theme_support_data['settings']['typography']['defaultFontSizes'] =
|
|
|
|
! isset( $theme_support_data['settings']['typography']['fontSizes'] ) ||
|
|
|
|
current_theme_supports( 'default-font-sizes' );
|
|
|
|
$theme_support_data['settings']['spacing']['defaultSpacingSizes'] =
|
|
|
|
! isset( $theme_support_data['settings']['spacing']['spacingSizes'] ) ||
|
|
|
|
current_theme_supports( 'default-spacing-sizes' );
|
2024-06-04 07:55:14 -04:00
|
|
|
|
2024-03-27 12:00:12 -04:00
|
|
|
/*
|
|
|
|
* Shadow presets are explicitly disabled for classic themes until a
|
|
|
|
* decision is made for whether the default presets should match the
|
|
|
|
* other presets or if they should be disabled by default in classic
|
|
|
|
* themes. See https://github.com/WordPress/gutenberg/issues/59989.
|
|
|
|
*/
|
|
|
|
$theme_support_data['settings']['shadow']['defaultPresets'] = false;
|
|
|
|
|
2023-06-23 09:50:20 -04:00
|
|
|
// Allow themes to enable link color setting via theme_support.
|
|
|
|
if ( current_theme_supports( 'link-color' ) ) {
|
|
|
|
$theme_support_data['settings']['color']['link'] = true;
|
|
|
|
}
|
|
|
|
|
2023-06-23 09:37:26 -04:00
|
|
|
// Allow themes to enable all border settings via theme_support.
|
|
|
|
if ( current_theme_supports( 'border' ) ) {
|
|
|
|
$theme_support_data['settings']['border']['color'] = true;
|
|
|
|
$theme_support_data['settings']['border']['radius'] = true;
|
|
|
|
$theme_support_data['settings']['border']['style'] = true;
|
|
|
|
$theme_support_data['settings']['border']['width'] = true;
|
|
|
|
}
|
2024-01-09 01:26:16 -05:00
|
|
|
|
|
|
|
// Allow themes to enable appearance tools via theme_support.
|
|
|
|
if ( current_theme_supports( 'appearance-tools' ) ) {
|
|
|
|
$theme_support_data['settings']['appearanceTools'] = true;
|
|
|
|
}
|
2021-11-23 00:40:38 -05:00
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
|
2022-02-17 04:04:05 -05:00
|
|
|
$with_theme_supports->merge( static::$theme );
|
2021-05-24 04:37:55 -04:00
|
|
|
return $with_theme_supports;
|
|
|
|
}
|
|
|
|
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
/**
|
|
|
|
* Gets the styles for blocks from the block.json file.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
|
|
|
* @return WP_Theme_JSON
|
|
|
|
*/
|
|
|
|
public static function get_block_data() {
|
|
|
|
$registry = WP_Block_Type_Registry::get_instance();
|
|
|
|
$blocks = $registry->get_all_registered();
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
|
|
|
|
if ( null !== static::$blocks && static::has_same_registered_blocks( 'blocks' ) ) {
|
|
|
|
return static::$blocks;
|
|
|
|
}
|
|
|
|
|
2024-06-04 07:55:14 -04:00
|
|
|
$config = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
foreach ( $blocks as $block_name => $block_type ) {
|
|
|
|
if ( isset( $block_type->supports['__experimentalStyle'] ) ) {
|
|
|
|
$config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ) &&
|
Editor: Reduce the use of the `_wp_array_get()` function to improve performance.
`_wp_array_get()` is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.
This commit aims to further optimize its usage:
* In many cases, `_wp_array_get()` can be replaced with a much simpler and faster `isset()` check.
* The `isset()` function is capable of checking nested arrays, so `isset( $foo['a']['b']['c'] )` will return false even if `$foo['a']` is unset, without throwing any errors or warnings.
* When `_wp_array_get()` cannot be directly replaced with `isset()`, it would be good practice to wrap it in an `isset()` function so that `_wp_array_get()` only runs when it needs to.
Original PR from Gutenberg repository:
* [https://github.com/WordPress/gutenberg/pull/51116 #51116 Performance improvement: Reduce the use of the _wp_array_get() function]
Follow-up to [55851], [56382].
Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.
Built from https://develop.svn.wordpress.org/trunk@56709
git-svn-id: http://core.svn.wordpress.org/trunk@56221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 09:47:20 -04:00
|
|
|
! isset( $config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] )
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
) {
|
2023-07-09 17:40:23 -04:00
|
|
|
/*
|
|
|
|
* Ensure an empty placeholder value exists for the block, if it provides a default blockGap value.
|
|
|
|
* The real blockGap value to be used will be determined when the styles are rendered for output.
|
|
|
|
*/
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
$config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:13:21 -04:00
|
|
|
/**
|
|
|
|
* Filters the data provided by the blocks for global styles & settings.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
2023-07-06 20:36:22 -04:00
|
|
|
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
|
2022-09-20 09:13:21 -04:00
|
|
|
*/
|
2024-06-19 12:41:13 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
|
|
|
|
* compatible class that is not a WP_Theme_JSON_Data object.
|
|
|
|
*/
|
|
|
|
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
|
|
|
|
static::$blocks = $theme_json->get_theme_json();
|
|
|
|
} else {
|
|
|
|
$config = $theme_json->get_data();
|
|
|
|
static::$blocks = new WP_Theme_JSON( $config, 'blocks' );
|
|
|
|
}
|
|
|
|
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
return static::$blocks;
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When given an array, this will remove any keys with the name `//`.
|
|
|
|
*
|
2023-01-24 10:41:15 -05:00
|
|
|
* @since 6.1.0
|
|
|
|
*
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-theme-json-resolver.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$array` parameter to `$input_array` in `WP_Theme_JSON_Resolver::remove_json_comments()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015], [55016], [55017], [55020], [55021], [55023], [55027], [55028], [55034], [55036], [55037], [55038], [55039], [55049], [55050], [55060], [55062], [55064], [55065], [55076], [55077], [55078], [55081], [55090], [55100], [55104], [55112], [55115], [55116], [55117], [55119], [55120], [55126].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@55127
git-svn-id: http://core.svn.wordpress.org/trunk@54660 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 10:37:12 -05:00
|
|
|
* @param array $input_array The array to filter.
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
* @return array The filtered array.
|
|
|
|
*/
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-theme-json-resolver.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$array` parameter to `$input_array` in `WP_Theme_JSON_Resolver::remove_json_comments()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015], [55016], [55017], [55020], [55021], [55023], [55027], [55028], [55034], [55036], [55037], [55038], [55039], [55049], [55050], [55060], [55062], [55064], [55065], [55076], [55077], [55078], [55081], [55090], [55100], [55104], [55112], [55115], [55116], [55117], [55119], [55120], [55126].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@55127
git-svn-id: http://core.svn.wordpress.org/trunk@54660 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 10:37:12 -05:00
|
|
|
private static function remove_json_comments( $input_array ) {
|
|
|
|
unset( $input_array['//'] );
|
|
|
|
foreach ( $input_array as $k => $v ) {
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
if ( is_array( $v ) ) {
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-theme-json-resolver.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$array` parameter to `$input_array` in `WP_Theme_JSON_Resolver::remove_json_comments()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015], [55016], [55017], [55020], [55021], [55023], [55027], [55028], [55034], [55036], [55037], [55038], [55039], [55049], [55050], [55060], [55062], [55064], [55065], [55076], [55077], [55078], [55081], [55090], [55100], [55104], [55112], [55115], [55116], [55117], [55119], [55120], [55126].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@55127
git-svn-id: http://core.svn.wordpress.org/trunk@54660 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 10:37:12 -05:00
|
|
|
$input_array[ $k ] = static::remove_json_comments( $v );
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-theme-json-resolver.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$array` parameter to `$input_array` in `WP_Theme_JSON_Resolver::remove_json_comments()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015], [55016], [55017], [55020], [55021], [55023], [55027], [55028], [55034], [55036], [55037], [55038], [55039], [55049], [55050], [55060], [55062], [55064], [55065], [55076], [55077], [55078], [55081], [55090], [55100], [55104], [55112], [55115], [55116], [55117], [55119], [55120], [55126].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@55127
git-svn-id: http://core.svn.wordpress.org/trunk@54660 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 10:37:12 -05:00
|
|
|
return $input_array;
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
}
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/**
|
2021-11-10 09:18:06 -05:00
|
|
|
* Returns the custom post type that contains the user's origin config
|
2023-04-27 19:15:17 -04:00
|
|
|
* for the active theme or an empty array if none are found.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-11-10 09:18:06 -05:00
|
|
|
* This can also create and return a new draft custom post type.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-11-08 14:19:58 -05:00
|
|
|
* @since 5.9.0
|
|
|
|
*
|
2021-12-04 10:57:01 -05:00
|
|
|
* @param WP_Theme $theme The theme object. If empty, it
|
2022-01-20 18:53:05 -05:00
|
|
|
* defaults to the active theme.
|
Themes: Rename public static functions in `WP_Theme_JSON_Resolver` to remove `custom_post_type` references.
WordPress Core is not really custom and does not reference "custom post type" in its function naming. This commit renames 2 public static methods:
* `WP_Theme_JSON_Resolver::get_user_custom_post_type_id()` renamed to `WP_Theme_JSON_Resolver::get_user_global_styles_post_id()`.
* `WP_Theme_JSON_Resolver::get_user_data_from_custom_post_type()` renamed to `WP_Theme_JSON_Resolver:: get_user_data_from_wp_global_styles()`.
Follow-up to [52049], [52051], [52069], [52232], [52275], [52364].
Props antonvlasenko, bernhard-reiter, costdev, desrosj, hellofromTonya, noisysocks, oandregal, SergeyBiryukov.
Fixes #54517.
Built from https://develop.svn.wordpress.org/trunk@52372
git-svn-id: http://core.svn.wordpress.org/trunk@51964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-14 11:14:01 -05:00
|
|
|
* @param bool $create_post Optional. Whether a new custom post
|
2021-11-29 19:24:27 -05:00
|
|
|
* type should be created if none are
|
2021-12-04 10:57:01 -05:00
|
|
|
* found. Default false.
|
|
|
|
* @param array $post_status_filter Optional. Filter custom post type by
|
|
|
|
* post status. Default `array( 'publish' )`,
|
2021-11-29 19:24:27 -05:00
|
|
|
* so it only fetches published posts.
|
2021-11-08 14:19:58 -05:00
|
|
|
* @return array Custom Post Type for the user's origin config.
|
|
|
|
*/
|
Themes: Rename public static functions in `WP_Theme_JSON_Resolver` to remove `custom_post_type` references.
WordPress Core is not really custom and does not reference "custom post type" in its function naming. This commit renames 2 public static methods:
* `WP_Theme_JSON_Resolver::get_user_custom_post_type_id()` renamed to `WP_Theme_JSON_Resolver::get_user_global_styles_post_id()`.
* `WP_Theme_JSON_Resolver::get_user_data_from_custom_post_type()` renamed to `WP_Theme_JSON_Resolver:: get_user_data_from_wp_global_styles()`.
Follow-up to [52049], [52051], [52069], [52232], [52275], [52364].
Props antonvlasenko, bernhard-reiter, costdev, desrosj, hellofromTonya, noisysocks, oandregal, SergeyBiryukov.
Fixes #54517.
Built from https://develop.svn.wordpress.org/trunk@52372
git-svn-id: http://core.svn.wordpress.org/trunk@51964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-14 11:14:01 -05:00
|
|
|
public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) {
|
2021-11-29 19:24:27 -05:00
|
|
|
if ( ! $theme instanceof WP_Theme ) {
|
|
|
|
$theme = wp_get_theme();
|
|
|
|
}
|
2022-11-10 17:16:15 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bail early if the theme does not support a theme.json.
|
|
|
|
*
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
* Since wp_theme_has_theme_json() only supports the active
|
2022-11-10 17:16:15 -05:00
|
|
|
* theme, the extra condition for whether $theme is the active theme is
|
|
|
|
* present here.
|
|
|
|
*/
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) {
|
2022-11-10 17:16:15 -05:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:19:58 -05:00
|
|
|
$user_cpt = array();
|
|
|
|
$post_type_filter = 'wp_global_styles';
|
2022-09-16 06:57:14 -04:00
|
|
|
$stylesheet = $theme->get_stylesheet();
|
2021-11-29 19:24:27 -05:00
|
|
|
$args = array(
|
2023-02-06 10:33:19 -05:00
|
|
|
'posts_per_page' => 1,
|
|
|
|
'orderby' => 'date',
|
|
|
|
'order' => 'desc',
|
|
|
|
'post_type' => $post_type_filter,
|
|
|
|
'post_status' => $post_status_filter,
|
|
|
|
'ignore_sticky_posts' => true,
|
|
|
|
'no_found_rows' => true,
|
|
|
|
'update_post_meta_cache' => false,
|
|
|
|
'update_post_term_cache' => false,
|
|
|
|
'tax_query' => array(
|
2021-11-29 19:24:27 -05:00
|
|
|
array(
|
|
|
|
'taxonomy' => 'wp_theme',
|
|
|
|
'field' => 'name',
|
2022-09-16 06:57:14 -04:00
|
|
|
'terms' => $stylesheet,
|
2021-11-08 14:19:58 -05:00
|
|
|
),
|
2021-11-29 19:24:27 -05:00
|
|
|
),
|
2021-11-08 14:19:58 -05:00
|
|
|
);
|
|
|
|
|
2022-09-16 06:57:14 -04:00
|
|
|
$global_style_query = new WP_Query();
|
|
|
|
$recent_posts = $global_style_query->query( $args );
|
|
|
|
if ( count( $recent_posts ) === 1 ) {
|
2023-02-06 10:33:19 -05:00
|
|
|
$user_cpt = get_object_vars( $recent_posts[0] );
|
Themes: Rename public static functions in `WP_Theme_JSON_Resolver` to remove `custom_post_type` references.
WordPress Core is not really custom and does not reference "custom post type" in its function naming. This commit renames 2 public static methods:
* `WP_Theme_JSON_Resolver::get_user_custom_post_type_id()` renamed to `WP_Theme_JSON_Resolver::get_user_global_styles_post_id()`.
* `WP_Theme_JSON_Resolver::get_user_data_from_custom_post_type()` renamed to `WP_Theme_JSON_Resolver:: get_user_data_from_wp_global_styles()`.
Follow-up to [52049], [52051], [52069], [52232], [52275], [52364].
Props antonvlasenko, bernhard-reiter, costdev, desrosj, hellofromTonya, noisysocks, oandregal, SergeyBiryukov.
Fixes #54517.
Built from https://develop.svn.wordpress.org/trunk@52372
git-svn-id: http://core.svn.wordpress.org/trunk@51964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-14 11:14:01 -05:00
|
|
|
} elseif ( $create_post ) {
|
2021-11-08 14:19:58 -05:00
|
|
|
$cpt_post_id = wp_insert_post(
|
|
|
|
array(
|
|
|
|
'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
|
|
|
|
'post_status' => 'publish',
|
2022-09-20 05:37:09 -04:00
|
|
|
'post_title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.
|
2021-11-08 14:19:58 -05:00
|
|
|
'post_type' => $post_type_filter,
|
2022-09-16 06:57:14 -04:00
|
|
|
'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),
|
2021-11-08 14:19:58 -05:00
|
|
|
'tax_input' => array(
|
2022-09-16 06:57:14 -04:00
|
|
|
'wp_theme' => array( $stylesheet ),
|
2021-11-08 14:19:58 -05:00
|
|
|
),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2022-09-16 06:57:14 -04:00
|
|
|
if ( ! is_wp_error( $cpt_post_id ) ) {
|
2023-02-06 10:33:19 -05:00
|
|
|
$user_cpt = get_object_vars( get_post( $cpt_post_id ) );
|
2022-09-16 06:57:14 -04:00
|
|
|
}
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $user_cpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user's origin config.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
2024-06-04 07:55:14 -04:00
|
|
|
* @since 6.6.0 The 'isGlobalStylesUserThemeJSON' flag is left on the user data.
|
2024-06-18 03:09:06 -04:00
|
|
|
* Register the block style variations coming from the user data.
|
2021-11-08 14:19:58 -05:00
|
|
|
*
|
Themes: Rename public static functions in `WP_Theme_JSON_Resolver` to remove `custom_post_type` references.
WordPress Core is not really custom and does not reference "custom post type" in its function naming. This commit renames 2 public static methods:
* `WP_Theme_JSON_Resolver::get_user_custom_post_type_id()` renamed to `WP_Theme_JSON_Resolver::get_user_global_styles_post_id()`.
* `WP_Theme_JSON_Resolver::get_user_data_from_custom_post_type()` renamed to `WP_Theme_JSON_Resolver:: get_user_data_from_wp_global_styles()`.
Follow-up to [52049], [52051], [52069], [52232], [52275], [52364].
Props antonvlasenko, bernhard-reiter, costdev, desrosj, hellofromTonya, noisysocks, oandregal, SergeyBiryukov.
Fixes #54517.
Built from https://develop.svn.wordpress.org/trunk@52372
git-svn-id: http://core.svn.wordpress.org/trunk@51964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-14 11:14:01 -05:00
|
|
|
* @return WP_Theme_JSON Entity that holds styles for user data.
|
2021-11-08 14:19:58 -05:00
|
|
|
*/
|
|
|
|
public static function get_user_data() {
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
if ( null !== static::$user && static::has_same_registered_blocks( 'user' ) ) {
|
|
|
|
return static::$user;
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:19:58 -05:00
|
|
|
$config = array();
|
2022-02-17 04:04:05 -05:00
|
|
|
$user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() );
|
2021-11-08 14:19:58 -05:00
|
|
|
|
|
|
|
if ( array_key_exists( 'post_content', $user_cpt ) ) {
|
|
|
|
$decoded_data = json_decode( $user_cpt['post_content'], true );
|
|
|
|
|
|
|
|
$json_decoding_error = json_last_error();
|
|
|
|
if ( JSON_ERROR_NONE !== $json_decoding_error ) {
|
Code Modernization: Use wp_trigger_error() instead of trigger_error().
Replaces `trigger_error()` with `wp_trigger_error()`.
The usage of `wp_trigger_error()` avoids generating `E_USER` family errors unless `WP_DEBUG` is on. In doing so, users should not see these messages in normal production.
Notes:
* Removes `E_USER_NOTICE` when passed as an argumnent, as it's the default error level.
* An empty string is passed for the function name when its name is already in the error message or does not add value to the error message.
* Externally maintained libraries are not included.
Follow-up to [55204], [25956], [29630], [38883], [52062], [52049], [54272], [38883], [55245], [51599], [14452], [38883], [24976].
Props prasadkarmalkar, rajinsharwar, thelovekesh, hellofromTonya, swissspidy.
Fixes #59652.
Built from https://develop.svn.wordpress.org/trunk@58409
git-svn-id: http://core.svn.wordpress.org/trunk@57858 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-13 16:50:14 -04:00
|
|
|
wp_trigger_error( __METHOD__, 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() );
|
2022-09-20 09:13:21 -04:00
|
|
|
/**
|
|
|
|
* Filters the data provided by the user for global styles & settings.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
2023-07-06 20:36:22 -04:00
|
|
|
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
|
2022-09-20 09:13:21 -04:00
|
|
|
*/
|
2022-10-11 16:08:12 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
|
2024-06-19 12:41:13 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
|
|
|
|
* compatible class that is not a WP_Theme_JSON_Data object.
|
|
|
|
*/
|
|
|
|
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
|
|
|
|
return $theme_json->get_theme_json();
|
|
|
|
} else {
|
|
|
|
$config = $theme_json->get_data();
|
|
|
|
return new WP_Theme_JSON( $config, 'custom' );
|
|
|
|
}
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
2023-07-09 17:40:23 -04:00
|
|
|
/*
|
|
|
|
* Very important to verify that the flag isGlobalStylesUserThemeJSON is true.
|
|
|
|
* If it's not true then the content was not escaped and is not safe.
|
|
|
|
*/
|
2021-11-08 14:19:58 -05:00
|
|
|
if (
|
|
|
|
is_array( $decoded_data ) &&
|
|
|
|
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
|
|
|
|
$decoded_data['isGlobalStylesUserThemeJSON']
|
|
|
|
) {
|
2024-06-06 04:02:16 -04:00
|
|
|
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
|
2021-11-08 14:19:58 -05:00
|
|
|
$config = $decoded_data;
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 09:13:21 -04:00
|
|
|
|
2022-09-21 16:24:12 -04:00
|
|
|
/** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
|
2024-06-19 12:41:13 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
|
|
|
|
* compatible class that is not a WP_Theme_JSON_Data object.
|
|
|
|
*/
|
|
|
|
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
|
|
|
|
static::$user = $theme_json->get_theme_json();
|
|
|
|
} else {
|
|
|
|
$config = $theme_json->get_data();
|
|
|
|
static::$user = new WP_Theme_JSON( $config, 'custom' );
|
|
|
|
}
|
2021-11-08 14:19:58 -05:00
|
|
|
|
2022-02-17 04:04:05 -05:00
|
|
|
return static::$user;
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-05 13:02:08 -05:00
|
|
|
* Returns the data merged from multiple origins.
|
|
|
|
*
|
2023-02-06 10:33:19 -05:00
|
|
|
* There are four sources of data (origins) for a site:
|
|
|
|
*
|
|
|
|
* - default => WordPress
|
|
|
|
* - blocks => each one of the blocks provides data for itself
|
|
|
|
* - theme => the active theme
|
|
|
|
* - custom => data provided by the user
|
|
|
|
*
|
|
|
|
* The custom's has higher priority than the theme's, the theme's higher than blocks',
|
|
|
|
* and block's higher than default's.
|
2021-11-08 14:19:58 -05:00
|
|
|
*
|
2022-04-14 02:05:11 -04:00
|
|
|
* Unlike the getters
|
|
|
|
* {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_core_data/ get_core_data},
|
|
|
|
* {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_theme_data/ get_theme_data},
|
|
|
|
* and {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_user_data/ get_user_data},
|
|
|
|
* this method returns data after it has been merged with the previous origins.
|
|
|
|
* This means that if the same piece of data is declared in different origins
|
2023-02-06 10:33:19 -05:00
|
|
|
* (default, blocks, theme, custom), the last origin overrides the previous.
|
2021-11-08 14:19:58 -05:00
|
|
|
*
|
|
|
|
* For example, if the user has set a background color
|
|
|
|
* for the paragraph block, and the theme has done it as well,
|
|
|
|
* the user preference wins.
|
2021-05-24 09:25:56 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2021-12-04 07:58:01 -05:00
|
|
|
* @since 5.9.0 Added user data, removed the `$settings` parameter,
|
|
|
|
* added the `$origin` parameter.
|
2022-09-21 07:43:13 -04:00
|
|
|
* @since 6.1.0 Added block data and generation of spacingSizes array.
|
2023-02-06 10:33:19 -05:00
|
|
|
* @since 6.2.0 Changed ' $origin' parameter values to 'default', 'blocks', 'theme' or 'custom'.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2023-02-06 10:33:19 -05:00
|
|
|
* @param string $origin Optional. To what level should we merge data: 'default', 'blocks', 'theme' or 'custom'.
|
|
|
|
* 'custom' is used as default value as well as fallback value if the origin is unknown.
|
2021-05-24 04:37:55 -04:00
|
|
|
* @return WP_Theme_JSON
|
|
|
|
*/
|
2021-11-29 19:24:27 -05:00
|
|
|
public static function get_merged_data( $origin = 'custom' ) {
|
2021-11-08 14:19:58 -05:00
|
|
|
if ( is_array( $origin ) ) {
|
2021-12-04 07:58:01 -05:00
|
|
|
_deprecated_argument( __FUNCTION__, '5.9.0' );
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
|
2023-03-01 11:17:18 -05:00
|
|
|
$result = new WP_Theme_JSON();
|
|
|
|
$result->merge( static::get_core_data() );
|
2023-02-06 10:33:19 -05:00
|
|
|
if ( 'default' === $origin ) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository:
* [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087
* [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792
* [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544
* [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665
* [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085
Notes:
* It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.
* [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details.
* Giving additional props for those who did a deep dive investigation into the failed tests.
Follow-up to [54160], [54159].
Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54162
git-svn-id: http://core.svn.wordpress.org/trunk@53721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 14:44:09 -04:00
|
|
|
$result->merge( static::get_block_data() );
|
2023-02-06 10:33:19 -05:00
|
|
|
if ( 'blocks' === $origin ) {
|
|
|
|
return $result;
|
|
|
|
}
|
2021-11-08 14:19:58 -05:00
|
|
|
|
2023-02-06 10:33:19 -05:00
|
|
|
$result->merge( static::get_theme_data() );
|
|
|
|
if ( 'theme' === $origin ) {
|
|
|
|
return $result;
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
|
2023-02-06 10:33:19 -05:00
|
|
|
$result->merge( static::get_user_data() );
|
2022-09-21 07:43:13 -04:00
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:19:58 -05:00
|
|
|
/**
|
|
|
|
* Returns the ID of the custom post type
|
|
|
|
* that stores user data.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
|
|
|
*
|
|
|
|
* @return integer|null
|
|
|
|
*/
|
Themes: Rename public static functions in `WP_Theme_JSON_Resolver` to remove `custom_post_type` references.
WordPress Core is not really custom and does not reference "custom post type" in its function naming. This commit renames 2 public static methods:
* `WP_Theme_JSON_Resolver::get_user_custom_post_type_id()` renamed to `WP_Theme_JSON_Resolver::get_user_global_styles_post_id()`.
* `WP_Theme_JSON_Resolver::get_user_data_from_custom_post_type()` renamed to `WP_Theme_JSON_Resolver:: get_user_data_from_wp_global_styles()`.
Follow-up to [52049], [52051], [52069], [52232], [52275], [52364].
Props antonvlasenko, bernhard-reiter, costdev, desrosj, hellofromTonya, noisysocks, oandregal, SergeyBiryukov.
Fixes #54517.
Built from https://develop.svn.wordpress.org/trunk@52372
git-svn-id: http://core.svn.wordpress.org/trunk@51964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-14 11:14:01 -05:00
|
|
|
public static function get_user_global_styles_post_id() {
|
2022-02-17 04:04:05 -05:00
|
|
|
if ( null !== static::$user_custom_post_type_id ) {
|
|
|
|
return static::$user_custom_post_type_id;
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 04:04:05 -05:00
|
|
|
$user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme(), true );
|
2021-11-08 14:19:58 -05:00
|
|
|
|
|
|
|
if ( array_key_exists( 'ID', $user_cpt ) ) {
|
2022-02-17 04:04:05 -05:00
|
|
|
static::$user_custom_post_type_id = $user_cpt['ID'];
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 04:04:05 -05:00
|
|
|
return static::$user_custom_post_type_id;
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
2021-05-24 04:37:55 -04:00
|
|
|
/**
|
2022-04-14 02:15:09 -04:00
|
|
|
* Determines whether the active theme has a theme.json file.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-06-23 15:05:57 -04:00
|
|
|
* @since 5.8.0
|
2021-12-04 07:58:01 -05:00
|
|
|
* @since 5.9.0 Added a check in the parent theme.
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
* @deprecated 6.2.0 Use wp_theme_has_theme_json() instead.
|
2021-06-23 15:05:57 -04:00
|
|
|
*
|
|
|
|
* @return bool
|
2021-05-24 04:37:55 -04:00
|
|
|
*/
|
|
|
|
public static function theme_has_support() {
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
_deprecated_function( __METHOD__, '6.2.0', 'wp_theme_has_theme_json()' );
|
2021-05-24 04:37:55 -04:00
|
|
|
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
return wp_theme_has_theme_json();
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-24 09:25:56 -04:00
|
|
|
* Builds the path to the given file and checks that it is readable.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
2021-05-24 09:25:56 -04:00
|
|
|
* If it isn't, returns an empty string, otherwise returns the whole file path.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2021-12-04 07:58:01 -05:00
|
|
|
* @since 5.9.0 Adapted to work with child themes, added the `$template` argument.
|
2021-05-24 04:37:55 -04:00
|
|
|
*
|
|
|
|
* @param string $file_name Name of the file.
|
2021-11-08 14:19:58 -05:00
|
|
|
* @param bool $template Optional. Use template theme directory. Default false.
|
2021-05-24 04:37:55 -04:00
|
|
|
* @return string The whole file path or empty if the file doesn't exist.
|
|
|
|
*/
|
2022-02-17 04:04:05 -05:00
|
|
|
protected static function get_file_path_from_theme( $file_name, $template = false ) {
|
2021-11-08 14:19:58 -05:00
|
|
|
$path = $template ? get_template_directory() : get_stylesheet_directory();
|
|
|
|
$candidate = $path . '/' . $file_name;
|
|
|
|
|
|
|
|
return is_readable( $candidate ) ? $candidate : '';
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans the cached data so it can be recalculated.
|
2021-05-24 09:25:56 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2021-12-04 07:58:01 -05:00
|
|
|
* @since 5.9.0 Added the `$user`, `$user_custom_post_type_id`,
|
|
|
|
* and `$i18n_schema` variables to reset.
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
* @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables
|
|
|
|
* to reset.
|
2021-05-24 04:37:55 -04:00
|
|
|
*/
|
|
|
|
public static function clean_cached_data() {
|
2022-02-17 04:04:05 -05:00
|
|
|
static::$core = null;
|
Editor: Fix performance regression in WP_Theme_JSON_Resolver.
A significant performance regression was added late in WP 6.1 beta cycle when some of the existing caching for `theme.json` processing was removed. The rationale for removing the caching was this code was now used before all the blocks are registered (aka get template data, etc.) and resulted in stale cache that created issues (see [https://github.com/WordPress/gutenberg/issues/44434 Gutenberg Issue 44434] and [https://github.com/WordPress/gutenberg/issues/44619 Gutenberg Issue 44619]). The changes were limited to only reads from the file system. However, it introduced a big impact in performance.
This commit adds caching and checks for blocks with different origins. How? It add caching for the calculated data for core, theme, and user based on the blocks that are registered. If the blocks haven't changed since the last time they were calculated for the origin, the cached data is returned. Otherwise, the data is recalculated and cached.
Essentially, this brings back the previous cache, but refreshing it when the blocks change.
It partially adds unit tests for these changes. Additional tests will be added.
References:
* [https://github.com/WordPress/gutenberg/issues/44772 Performance regression in WP 6.1 for theme.json processing]
Follow-up to [54251], [54399].
Props aristath, oandregal, bernhard-reiter, spacedmonkey, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54493
git-svn-id: http://core.svn.wordpress.org/trunk@54052 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 13:17:13 -04:00
|
|
|
static::$blocks = null;
|
|
|
|
static::$blocks_cache = array(
|
|
|
|
'core' => array(),
|
|
|
|
'blocks' => array(),
|
|
|
|
'theme' => array(),
|
|
|
|
'user' => array(),
|
|
|
|
);
|
2022-02-17 04:04:05 -05:00
|
|
|
static::$theme = null;
|
|
|
|
static::$user = null;
|
|
|
|
static::$user_custom_post_type_id = null;
|
|
|
|
static::$i18n_schema = null;
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|
|
|
|
|
2023-02-06 10:33:19 -05:00
|
|
|
/**
|
|
|
|
* Returns an array of all nested JSON files within a given directory.
|
|
|
|
*
|
|
|
|
* @since 6.2.0
|
|
|
|
*
|
|
|
|
* @param string $dir The directory to recursively iterate and list files of.
|
|
|
|
* @return array The merged array.
|
|
|
|
*/
|
|
|
|
private static function recursively_iterate_json( $dir ) {
|
|
|
|
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
|
|
|
|
$nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
|
|
|
|
return $nested_json_files;
|
|
|
|
}
|
|
|
|
|
2024-05-31 01:49:14 -04:00
|
|
|
/**
|
|
|
|
* Determines if a supplied style variation matches the provided scope.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private static function style_variation_has_scope( $variation, $scope ) {
|
|
|
|
if ( 'block' === $scope ) {
|
|
|
|
return isset( $variation['blockTypes'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'theme' === $scope ) {
|
|
|
|
return ! isset( $variation['blockTypes'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-06 10:33:19 -05:00
|
|
|
|
2022-04-05 05:52:02 -04:00
|
|
|
/**
|
|
|
|
* Returns the style variations defined by the theme.
|
|
|
|
*
|
|
|
|
* @since 6.0.0
|
2023-02-06 10:33:19 -05:00
|
|
|
* @since 6.2.0 Returns parent theme variations if theme is a child.
|
2024-05-31 01:49:14 -04:00
|
|
|
* @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.
|
2024-06-18 02:02:14 -04:00
|
|
|
* Added basic caching for read theme.json partial files.
|
2024-05-31 01:49:14 -04:00
|
|
|
*
|
|
|
|
* @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc.
|
2022-04-05 05:52:02 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2024-05-31 01:49:14 -04:00
|
|
|
public static function get_style_variations( $scope = 'theme' ) {
|
2023-02-06 10:33:19 -05:00
|
|
|
$variation_files = array();
|
|
|
|
$variations = array();
|
|
|
|
$base_directory = get_stylesheet_directory() . '/styles';
|
|
|
|
$template_directory = get_template_directory() . '/styles';
|
2022-04-05 05:52:02 -04:00
|
|
|
if ( is_dir( $base_directory ) ) {
|
2023-02-06 10:33:19 -05:00
|
|
|
$variation_files = static::recursively_iterate_json( $base_directory );
|
|
|
|
}
|
|
|
|
if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) {
|
|
|
|
$variation_files_parent = static::recursively_iterate_json( $template_directory );
|
|
|
|
// If the child and parent variation file basename are the same, only include the child theme's.
|
|
|
|
foreach ( $variation_files_parent as $parent_path => $parent ) {
|
|
|
|
foreach ( $variation_files as $child_path => $child ) {
|
|
|
|
if ( basename( $parent_path ) === basename( $child_path ) ) {
|
|
|
|
unset( $variation_files_parent[ $parent_path ] );
|
2022-04-05 05:52:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 10:33:19 -05:00
|
|
|
$variation_files = array_merge( $variation_files, $variation_files_parent );
|
|
|
|
}
|
|
|
|
ksort( $variation_files );
|
|
|
|
foreach ( $variation_files as $path => $file ) {
|
2024-06-18 02:02:14 -04:00
|
|
|
$decoded_file = self::read_json_file( $path );
|
2024-05-31 01:49:14 -04:00
|
|
|
if ( is_array( $decoded_file ) && static::style_variation_has_scope( $decoded_file, $scope ) ) {
|
2023-02-06 10:33:19 -05:00
|
|
|
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
|
|
|
|
$variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
|
|
|
|
if ( empty( $variation['title'] ) ) {
|
|
|
|
$variation['title'] = basename( $path, '.json' );
|
|
|
|
}
|
|
|
|
$variations[] = $variation;
|
|
|
|
}
|
2022-04-05 05:52:02 -04:00
|
|
|
}
|
|
|
|
return $variations;
|
|
|
|
}
|
2024-05-30 21:19:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves relative paths in theme.json styles to theme absolute paths
|
|
|
|
* and returns them in an array that can be embedded
|
|
|
|
* as the value of `_link` object in REST API responses.
|
|
|
|
*
|
|
|
|
* @since 6.6.0
|
2024-07-23 20:26:14 -04:00
|
|
|
* @since 6.7.0 Resolve relative paths in block styles.
|
2024-05-30 21:19:14 -04:00
|
|
|
*
|
2024-05-30 23:48:14 -04:00
|
|
|
* @param WP_Theme_JSON $theme_json A theme json instance.
|
2024-05-30 21:19:14 -04:00
|
|
|
* @return array An array of resolved paths.
|
|
|
|
*/
|
|
|
|
public static function get_resolved_theme_uris( $theme_json ) {
|
|
|
|
$resolved_theme_uris = array();
|
|
|
|
|
|
|
|
if ( ! $theme_json instanceof WP_Theme_JSON ) {
|
|
|
|
return $resolved_theme_uris;
|
|
|
|
}
|
|
|
|
|
|
|
|
$theme_json_data = $theme_json->get_raw_data();
|
|
|
|
/*
|
|
|
|
* The same file convention when registering web fonts.
|
2024-05-30 23:48:14 -04:00
|
|
|
* See: WP_Font_Face_Resolver::to_theme_file_uri.
|
2024-05-30 21:19:14 -04:00
|
|
|
*/
|
|
|
|
$placeholder = 'file:./';
|
2024-07-23 20:26:14 -04:00
|
|
|
|
|
|
|
// Top level styles.
|
|
|
|
$background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null;
|
2024-05-30 21:19:14 -04:00
|
|
|
if (
|
|
|
|
isset( $background_image_url ) &&
|
|
|
|
is_string( $background_image_url ) &&
|
|
|
|
// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
|
|
|
|
str_starts_with( $background_image_url, $placeholder )
|
|
|
|
) {
|
|
|
|
$file_type = wp_check_filetype( $background_image_url );
|
|
|
|
$src_url = str_replace( $placeholder, '', $background_image_url );
|
|
|
|
$resolved_theme_uri = array(
|
|
|
|
'name' => $background_image_url,
|
|
|
|
'href' => sanitize_url( get_theme_file_uri( $src_url ) ),
|
|
|
|
'target' => 'styles.background.backgroundImage.url',
|
|
|
|
);
|
|
|
|
if ( isset( $file_type['type'] ) ) {
|
|
|
|
$resolved_theme_uri['type'] = $file_type['type'];
|
|
|
|
}
|
|
|
|
$resolved_theme_uris[] = $resolved_theme_uri;
|
|
|
|
}
|
|
|
|
|
2024-07-23 20:26:14 -04:00
|
|
|
// Block styles.
|
|
|
|
if ( ! empty( $theme_json_data['styles']['blocks'] ) ) {
|
|
|
|
foreach ( $theme_json_data['styles']['blocks'] as $block_name => $block_styles ) {
|
|
|
|
if ( ! isset( $block_styles['background']['backgroundImage']['url'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$background_image_url = $block_styles['background']['backgroundImage']['url'];
|
|
|
|
if (
|
|
|
|
is_string( $background_image_url ) &&
|
|
|
|
// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
|
|
|
|
str_starts_with( $background_image_url, $placeholder )
|
|
|
|
) {
|
|
|
|
$file_type = wp_check_filetype( $background_image_url );
|
|
|
|
$src_url = str_replace( $placeholder, '', $background_image_url );
|
|
|
|
$resolved_theme_uri = array(
|
|
|
|
'name' => $background_image_url,
|
|
|
|
'href' => sanitize_url( get_theme_file_uri( $src_url ) ),
|
|
|
|
'target' => "styles.blocks.{$block_name}.background.backgroundImage.url",
|
|
|
|
);
|
|
|
|
if ( isset( $file_type['type'] ) ) {
|
|
|
|
$resolved_theme_uri['type'] = $file_type['type'];
|
|
|
|
}
|
|
|
|
$resolved_theme_uris[] = $resolved_theme_uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 21:19:14 -04:00
|
|
|
return $resolved_theme_uris;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves relative paths in theme.json styles to theme absolute paths
|
|
|
|
* and merges them with incoming theme JSON.
|
|
|
|
*
|
|
|
|
* @since 6.6.0
|
|
|
|
*
|
2024-06-24 04:51:16 -04:00
|
|
|
* @param WP_Theme_JSON $theme_json A theme json instance.
|
2024-05-30 21:19:14 -04:00
|
|
|
* @return WP_Theme_JSON Theme merged with resolved paths, if any found.
|
|
|
|
*/
|
|
|
|
public static function resolve_theme_file_uris( $theme_json ) {
|
|
|
|
$resolved_urls = static::get_resolved_theme_uris( $theme_json );
|
|
|
|
if ( empty( $resolved_urls ) ) {
|
|
|
|
return $theme_json;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolved_theme_json_data = array(
|
|
|
|
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $resolved_urls as $resolved_url ) {
|
|
|
|
$path = explode( '.', $resolved_url['target'] );
|
|
|
|
_wp_array_set( $resolved_theme_json_data, $path, $resolved_url['href'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$theme_json->merge( new WP_Theme_JSON( $resolved_theme_json_data ) );
|
|
|
|
|
|
|
|
return $theme_json;
|
|
|
|
}
|
2024-06-24 04:51:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds variations sourced from block style variations files to the supplied theme.json data.
|
|
|
|
*
|
|
|
|
* @since 6.6.0
|
|
|
|
*
|
2024-06-24 06:33:15 -04:00
|
|
|
* @param array $data Array following the theme.json specification.
|
2024-06-24 04:51:16 -04:00
|
|
|
* @param array $variations Shared block style variations.
|
|
|
|
* @return array Theme json data including shared block style variation definitions.
|
|
|
|
*/
|
|
|
|
private static function inject_variations_from_block_style_variation_files( $data, $variations ) {
|
|
|
|
if ( empty( $variations ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $variations as $variation ) {
|
|
|
|
if ( empty( $variation['styles'] ) || empty( $variation['blockTypes'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$variation_name = $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] );
|
|
|
|
|
|
|
|
foreach ( $variation['blockTypes'] as $block_type ) {
|
|
|
|
// First, override partial styles with any top-level styles.
|
|
|
|
$top_level_data = $data['styles']['variations'][ $variation_name ] ?? array();
|
|
|
|
if ( ! empty( $top_level_data ) ) {
|
|
|
|
$variation['styles'] = array_replace_recursive( $variation['styles'], $top_level_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, override styles so far with any block-level styles.
|
|
|
|
$block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
|
|
|
|
if ( ! empty( $block_level_data ) ) {
|
|
|
|
$variation['styles'] = array_replace_recursive( $variation['styles'], $block_level_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name );
|
|
|
|
_wp_array_set( $data, $path, $variation['styles'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds variations sourced from the block styles registry to the supplied theme.json data.
|
|
|
|
*
|
|
|
|
* @since 6.6.0
|
|
|
|
*
|
2024-06-24 06:33:15 -04:00
|
|
|
* @param array $data Array following the theme.json specification.
|
2024-06-24 04:51:16 -04:00
|
|
|
* @return array Theme json data including shared block style variation definitions.
|
|
|
|
*/
|
|
|
|
private static function inject_variations_from_block_styles_registry( $data ) {
|
|
|
|
$registry = WP_Block_Styles_Registry::get_instance();
|
|
|
|
$styles = $registry->get_all_registered();
|
|
|
|
|
|
|
|
foreach ( $styles as $block_type => $variations ) {
|
|
|
|
foreach ( $variations as $variation_name => $variation ) {
|
|
|
|
if ( empty( $variation['style_data'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, override registry styles with any top-level styles.
|
|
|
|
$top_level_data = $data['styles']['variations'][ $variation_name ] ?? array();
|
|
|
|
if ( ! empty( $top_level_data ) ) {
|
|
|
|
$variation['style_data'] = array_replace_recursive( $variation['style_data'], $top_level_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, override styles so far with any block-level styles.
|
|
|
|
$block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
|
|
|
|
if ( ! empty( $block_level_data ) ) {
|
|
|
|
$variation['style_data'] = array_replace_recursive( $variation['style_data'], $block_level_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name );
|
|
|
|
_wp_array_set( $data, $path, $variation['style_data'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|