From 51a16672981e21ac584e0aaeb106c527bfd5716a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 14 Sep 2022 17:02:30 +0200 Subject: [PATCH 1/6] Add hooks to filter theme.json data --- src/wp-includes/class-wp-theme-json-data.php | 69 +++++++++++++++++++ .../class-wp-theme-json-resolver.php | 37 ++++++++++ src/wp-settings.php | 1 + 3 files changed, 107 insertions(+) create mode 100644 src/wp-includes/class-wp-theme-json-data.php 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..7fc97fab6cec8 --- /dev/null +++ b/src/wp-includes/class-wp-theme-json-data.php @@ -0,0 +1,69 @@ +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 + */ + 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..e328396d85b9e 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -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. + * + * @param WP_Theme_JSON_Data Class to access and update the underlying data. + */ + $theme_json = apply_filters( 'global_styles_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 +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( 'global_styles_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 +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( 'global_styles_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 +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( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + $config = $theme_json->get_data(); return new WP_Theme_JSON( $config, 'custom' ); } @@ -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( 'global_styles_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'; From 0f2fae3d2b2ba292122436e56131864054f1ee05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 14 Sep 2022 17:33:25 +0200 Subject: [PATCH 2/6] Rename hooks from global_styles_ to theme_json_ --- src/wp-includes/class-wp-theme-json-resolver.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index e328396d85b9e..f85b8b70cd169 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -142,7 +142,7 @@ public static function get_core_data() { * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data( $config, 'default' ) ); + $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' ); @@ -184,7 +184,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + $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 ); @@ -277,7 +277,7 @@ public static function get_block_data() { * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data( $config, 'core' ) ); + $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. @@ -404,7 +404,7 @@ public static function get_user_data() { * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + $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' ); } @@ -426,7 +426,7 @@ public static function get_user_data() { * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + $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' ); From 5940e25cd5885df2ac78e79567c07032be892073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 15 Sep 2022 09:51:36 +0200 Subject: [PATCH 3/6] Use tabs for indentation --- src/wp-includes/class-wp-theme-json-data.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-data.php b/src/wp-includes/class-wp-theme-json-data.php index 7fc97fab6cec8..e55aed6a32bba 100644 --- a/src/wp-includes/class-wp-theme-json-data.php +++ b/src/wp-includes/class-wp-theme-json-data.php @@ -15,7 +15,7 @@ class WP_Theme_JSON_Data { /** * Container of the data to update. * - * @since 6.1.0 + * @since 6.1.0 * @var WP_Theme_JSON */ private $theme_json = null; @@ -23,7 +23,7 @@ class WP_Theme_JSON_Data { /** * The origin of the data: default, theme, user, etc. * - * @since 6.1.0 + * @since 6.1.0 * @var string */ private $origin = ''; @@ -31,8 +31,8 @@ class WP_Theme_JSON_Data { /** * Constructor. * - * @since 6.1.0 - * + * @since 6.1.0 + * * @param array $data Array following the theme.json specification. * @param string $origin The origin of the data: default, theme, user. */ @@ -44,8 +44,8 @@ public function __construct( $data = array(), $origin = 'theme' ) { /** * Updates the theme.json with the the given data. * - * @since 6.1.0 - * + * @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. @@ -57,8 +57,8 @@ public function update_with( $new_data ) { /** * Returns the underlying data. - * - * @since 6.1.0 + * + * @since 6.1.0 * * @return array */ From d329e35293418d98dff8611a5d5241415068b9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 16 Sep 2022 12:10:09 +0200 Subject: [PATCH 4/6] Add @since PHPDoc to filters comments --- src/wp-includes/class-wp-theme-json-resolver.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index f85b8b70cd169..c5530c69850b4 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -140,6 +140,8 @@ public static function get_core_data() { /** * 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' ) ); @@ -182,6 +184,8 @@ public static function get_theme_data( $deprecated = array(), $options = array() /** * 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' ) ); @@ -275,6 +279,8 @@ 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' ) ); @@ -402,6 +408,8 @@ public static function get_user_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' ) ); @@ -424,6 +432,8 @@ public static function get_user_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' ) ); From ebb78961581d428fe4322cbc351fa29c03ca4b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 16 Sep 2022 12:11:50 +0200 Subject: [PATCH 5/6] Better docs for WP_Theme_JSON_Data::get_data method --- src/wp-includes/class-wp-theme-json-data.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json-data.php b/src/wp-includes/class-wp-theme-json-data.php index e55aed6a32bba..44f85d4ecd22f 100644 --- a/src/wp-includes/class-wp-theme-json-data.php +++ b/src/wp-includes/class-wp-theme-json-data.php @@ -56,7 +56,8 @@ public function update_with( $new_data ) { } /** - * Returns the underlying data. + * Returns an array containing the underlying data + * following the theme.json specification. * * @since 6.1.0 * From 1896dde63cecc3fb6c75c538cdf3bb78bcd2ac99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 16 Sep 2022 12:13:54 +0200 Subject: [PATCH 6/6] Add link to theme.json docs --- src/wp-includes/class-wp-theme-json-data.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json-data.php b/src/wp-includes/class-wp-theme-json-data.php index 44f85d4ecd22f..089e65eddb630 100644 --- a/src/wp-includes/class-wp-theme-json-data.php +++ b/src/wp-includes/class-wp-theme-json-data.php @@ -33,6 +33,8 @@ class WP_Theme_JSON_Data { * * @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. */