Skip to content

Commit

Permalink
Editor: Backport hooks to filter theme.json data from Gutenberg to …
Browse files Browse the repository at this point in the history
…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:

- [WordPress/gutenberg#44015 gutenberg#44015]: Make global styles data filterable
- [WordPress/gutenberg#44109 gutenberg#44109]: Prepare `WP_Theme_JSON_Data` class for backporting
- [WordPress/gutenberg#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: https://core.svn.wordpress.org/trunk@53810 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
audrasjb committed Sep 20, 2022
1 parent 2886625 commit 60c2c85
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
72 changes: 72 additions & 0 deletions wp-includes/class-wp-theme-json-data.php
Original file line number Diff line number Diff line change
@@ -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();
}

}
47 changes: 47 additions & 0 deletions wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ public static function get_core_data() {

$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;
Expand Down Expand Up @@ -172,6 +181,15 @@ public static function get_theme_data( $deprecated = array(), $options = array()
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() ) {
Expand Down Expand Up @@ -258,6 +276,16 @@ public static function get_block_data() {
}
}

/**
* 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' );
Expand Down Expand Up @@ -382,6 +410,15 @@ public static function get_user_data() {
$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' );
}

Expand All @@ -396,6 +433,16 @@ public static function get_user_data() {
$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;
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
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';
Expand Down

0 comments on commit 60c2c85

Please sign in to comment.