diff --git a/src/wp-includes/class-wp-theme-json-data.php b/src/wp-includes/class-wp-theme-json-data.php new file mode 100644 index 0000000000000..089e65eddb630 --- /dev/null +++ b/src/wp-includes/class-wp-theme-json-data.php @@ -0,0 +1,72 @@ +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(); + } + +} diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 5d0dd099c20e5..c5530c69850b4 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -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; @@ -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() ) { @@ -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' ); @@ -377,6 +405,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' ); } @@ -391,6 +428,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; diff --git a/src/wp-settings.php b/src/wp-settings.php index 6f0e33bd7e978..d80d79bb82d6b 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -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';