Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks to filter theme.json data #3247

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/wp-includes/class-wp-theme-json-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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
*
* @param array $data Array following the theme.json specification.
oandregal marked this conversation as resolved.
Show resolved Hide resolved
* @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 the underlying data.
*
* @since 6.1.0
*
* @return array
oandregal marked this conversation as resolved.
Show resolved Hide resolved
*/
public function get_data() {
return $this->theme_json->get_raw_data();
}

}
37 changes: 37 additions & 0 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ 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.
*
oandregal marked this conversation as resolved.
Show resolved Hide resolved
* @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 +179,13 @@ 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.
*
* @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 +272,14 @@ public static function get_block_data() {
}
}

/**
* Filters the data provided by the blocks for global styles & settings.
*
* @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 @@ -377,6 +399,13 @@ 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.
*
* @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 @@ -391,6 +420,14 @@ public static function get_user_data() {
$config = $decoded_data;
}
}

/**
* Filters the data provided by the user for global styles & settings.
*
* @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
1 change: 1 addition & 0 deletions src/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