Editor: Backport hooks to filter `theme.json` data from Gutenberg to 6.1.

This changeset ports the work done in Gutenberg (released in 14.1) to add hooks to filter the `theme.json` data. Specifically, it adds the following filters: `theme_json_default`, `theme_json_blocks`, `theme_json_theme`, and `theme_json_user`.

For more details, see the following Gutenberg pull requests:

- [https://github.com/WordPress/gutenberg/pull/44015 gutenberg#44015]: Make global styles data filterable
- [https://github.com/WordPress/gutenberg/pull/44109 gutenberg#44109]: Prepare `WP_Theme_JSON_Data` class for backporting
- [https://github.com/WordPress/gutenberg/pull/44159 gutenberg#44159]: Update hook's names from `global_styles_*` to `theme_json_*`

Props oandregal, czapla, gziolo, bernhard-reiter.
See #56467.

Built from https://develop.svn.wordpress.org/trunk@54251


git-svn-id: http://core.svn.wordpress.org/trunk@53810 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
audrasjb 2022-09-20 13:13:21 +00:00
parent 9b3535cc14
commit fa4cc8f69d
4 changed files with 121 additions and 1 deletions

View File

@ -0,0 +1,72 @@
<?php
/**
* WP_Theme_JSON_Data class
*
* @package WordPress
* @subpackage Theme
* @since 6.1.0
*/
/**
* Class to provide access to update a theme.json structure.
*/
class WP_Theme_JSON_Data {
/**
* Container of the data to update.
*
* @since 6.1.0
* @var WP_Theme_JSON
*/
private $theme_json = null;
/**
* The origin of the data: default, theme, user, etc.
*
* @since 6.1.0
* @var string
*/
private $origin = '';
/**
* Constructor.
*
* @since 6.1.0
*
* @link https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/
*
* @param array $data Array following the theme.json specification.
* @param string $origin The origin of the data: default, theme, user.
*/
public function __construct( $data = array(), $origin = 'theme' ) {
$this->origin = $origin;
$this->theme_json = new WP_Theme_JSON( $data, $this->origin );
}
/**
* Updates the theme.json with the the given data.
*
* @since 6.1.0
*
* @param array $new_data Array following the theme.json specification.
*
* @return WP_Theme_JSON_Data The own instance with access to the modified data.
*/
public function update_with( $new_data ) {
$this->theme_json->merge( new WP_Theme_JSON( $new_data, $this->origin ) );
return $this;
}
/**
* Returns an array containing the underlying data
* following the theme.json specification.
*
* @since 6.1.0
*
* @return array
*/
public function get_data() {
return $this->theme_json->get_raw_data();
}
}

View File

@ -137,6 +137,15 @@ class WP_Theme_JSON_Resolver {
$config = static::read_json_file( __DIR__ . '/theme.json' );
$config = static::translate( $config );
/**
* 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.
*/
$theme_json = apply_filters( 'theme_json_default', new WP_Theme_JSON_Data( $config, 'default' ) );
$config = $theme_json->get_data();
static::$core = new WP_Theme_JSON( $config, 'default' );
return static::$core;
@ -172,6 +181,15 @@ class WP_Theme_JSON_Resolver {
if ( null === static::$theme ) {
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
/**
* Filters the data provided by the theme for global styles & settings.
*
* @since 6.1.0
*
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
$theme_json_data = $theme_json->get_data();
static::$theme = new WP_Theme_JSON( $theme_json_data );
if ( wp_get_theme()->parent() ) {
@ -258,6 +276,16 @@ class WP_Theme_JSON_Resolver {
}
}
/**
* 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.
*/
$theme_json = apply_filters( 'theme_json_blocks', new WP_Theme_JSON_Data( $config, 'core' ) );
$config = $theme_json->get_data();
// Core here means it's the lower level part of the styles chain.
// It can be a core or a third-party block.
return new WP_Theme_JSON( $config, 'core' );
@ -382,6 +410,15 @@ class WP_Theme_JSON_Resolver {
$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() );
/**
* 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.
*/
$theme_json = apply_filters( 'theme_json_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
$config = $theme_json->get_data();
return new WP_Theme_JSON( $config, 'custom' );
}
@ -396,6 +433,16 @@ class WP_Theme_JSON_Resolver {
$config = $decoded_data;
}
}
/**
* 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.
*/
$theme_json = apply_filters( 'theme_json_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
$config = $theme_json->get_data();
static::$user = new WP_Theme_JSON( $config, 'custom' );
return static::$user;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-54250';
$wp_version = '6.1-alpha-54251';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -172,6 +172,7 @@ require ABSPATH . WPINC . '/class-wp-date-query.php';
require ABSPATH . WPINC . '/theme.php';
require ABSPATH . WPINC . '/class-wp-theme.php';
require ABSPATH . WPINC . '/class-wp-theme-json-schema.php';
require ABSPATH . WPINC . '/class-wp-theme-json-data.php';
require ABSPATH . WPINC . '/class-wp-theme-json.php';
require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php';
require ABSPATH . WPINC . '/global-styles-and-settings.php';