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
|
|
|
|
*
|
|
|
|
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
|
|
|
|
*/
|
2022-10-11 16:08:12 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
|
2022-09-20 09:13:21 -04:00
|
|
|
$config = $theme_json->get_data();
|
2022-02-17 04:04:05 -05:00
|
|
|
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.
|
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-10 17:16:15 -05:00
|
|
|
$theme_json_file = static::get_file_path_from_theme( 'theme.json' );
|
2022-11-11 11:26:12 -05:00
|
|
|
$wp_theme = wp_get_theme();
|
2022-11-10 17:16:15 -05:00
|
|
|
if ( '' !== $theme_json_file ) {
|
|
|
|
$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 {
|
|
|
|
$theme_json_data = array();
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
|
|
|
|
*/
|
2022-10-11 16:08:12 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
|
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
|
|
|
$theme_json_data = $theme_json->get_data();
|
|
|
|
static::$theme = new WP_Theme_JSON( $theme_json_data );
|
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.
|
|
|
|
$parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true );
|
|
|
|
if ( '' !== $parent_theme_json_file ) {
|
|
|
|
$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() ) {
|
2021-11-23 00:40:38 -05:00
|
|
|
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
|
|
|
|
$theme_support_data['settings']['color'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$default_palette = false;
|
|
|
|
if ( current_theme_supports( 'default-color-palette' ) ) {
|
|
|
|
$default_palette = true;
|
|
|
|
}
|
|
|
|
if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) {
|
|
|
|
// If the theme does not have any palette, we still want to show the core one.
|
|
|
|
$default_palette = true;
|
|
|
|
}
|
|
|
|
$theme_support_data['settings']['color']['defaultPalette'] = $default_palette;
|
|
|
|
|
|
|
|
$default_gradients = false;
|
|
|
|
if ( current_theme_supports( 'default-gradient-presets' ) ) {
|
|
|
|
$default_gradients = true;
|
|
|
|
}
|
|
|
|
if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) {
|
|
|
|
// If the theme does not have any gradients, we still want to show the core ones.
|
|
|
|
$default_gradients = true;
|
|
|
|
}
|
|
|
|
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;
|
2022-04-11 06:38:00 -04:00
|
|
|
|
|
|
|
// Classic themes without a theme.json don't support global duotone.
|
|
|
|
$theme_support_data['settings']['color']['defaultDuotone'] = false;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = array( 'version' => 2 );
|
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'] ) &&
|
|
|
|
null === _wp_array_get( $config, array( 'styles', 'blocks', $block_name, 'spacing', 'blockGap' ), null )
|
|
|
|
) {
|
|
|
|
// 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.
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
|
|
|
|
*/
|
2022-10-11 16:08:12 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
|
2022-09-20 09:13:21 -04:00
|
|
|
$config = $theme_json->get_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
|
|
|
static::$blocks = new WP_Theme_JSON( $config, 'blocks' );
|
|
|
|
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
|
|
|
|
*
|
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 ) {
|
|
|
|
trigger_error( '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
|
|
|
|
*
|
|
|
|
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
|
|
|
|
*/
|
2022-10-11 16:08:12 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
|
2022-09-20 09:13:21 -04:00
|
|
|
$config = $theme_json->get_data();
|
2021-11-29 19:24:27 -05:00
|
|
|
return new WP_Theme_JSON( $config, 'custom' );
|
2021-11-08 14:19:58 -05:00
|
|
|
}
|
|
|
|
|
2021-12-05 13:02:08 -05: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']
|
|
|
|
) {
|
|
|
|
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
|
|
|
|
$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 */
|
2022-10-11 16:08:12 -04:00
|
|
|
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
|
2022-09-20 09:13:21 -04:00
|
|
|
$config = $theme_json->get_data();
|
2022-02-17 04:04:05 -05:00
|
|
|
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 ) {
|
|
|
|
$result->set_spacing_sizes();
|
|
|
|
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 ) {
|
|
|
|
$result->set_spacing_sizes();
|
|
|
|
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
|
|
|
$result->set_spacing_sizes();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2022-04-05 05:52:02 -04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_style_variations() {
|
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 ) {
|
|
|
|
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
|
|
|
|
if ( is_array( $decoded_file ) ) {
|
|
|
|
$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;
|
|
|
|
}
|
2021-05-24 04:37:55 -04:00
|
|
|
}
|