From 27ee2a35c65e3ecac5ff82c356fb450cd1230ced Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 26 Apr 2023 10:24:14 -0300 Subject: [PATCH 01/24] adding site global styles post type in the backend --- ...class-wp-theme-json-resolver-gutenberg.php | 137 +++++++++++++++++- ...berg-rest-global-styles-controller-6-3.php | 30 ++++ lib/compat/wordpress-6.3/rest-api.php | 33 +---- 3 files changed, 169 insertions(+), 31 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 2c61de7c5e24c2..6e95544e714e7a 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -29,6 +29,7 @@ class WP_Theme_JSON_Resolver_Gutenberg { 'core' => array(), 'blocks' => array(), 'theme' => array(), + 'site' => array(), 'user' => array(), ); @@ -64,6 +65,14 @@ class WP_Theme_JSON_Resolver_Gutenberg { */ protected static $user = null; + /** + * Container for data coming from the site. + * + * @since 6.3.0 + * @var WP_Theme_JSON + */ + protected static $site = null; + /** * Stores the ID of the custom post type * that holds the user data. @@ -73,6 +82,15 @@ class WP_Theme_JSON_Resolver_Gutenberg { */ protected static $user_custom_post_type_id = null; + /** + * Stores the ID of the custom post type + * that holds the site data. + * + * @since 6.3.0 + * @var int + */ + protected static $site_custom_post_type_id = null; + /** * Container to keep loaded i18n schema for `theme.json`. * @@ -478,6 +496,60 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post return $user_cpt; } + public static function get_site_data_from_wp_global_styles($create_post = false, $post_status_filter = array( 'publish' ) ) { + $theme = wp_get_theme(); + + /* + * Bail early if the theme does not support a theme.json. + * + * Since wp_theme_has_theme_json only supports the active + * theme, the extra condition for whether $theme is the active theme is + * present here. + */ + if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) { + return array(); + } + + $site_cpt = array(); + $post_type_filter = 'wp_global_styles'; + $stylesheet = $theme->get_stylesheet(); + $args = array( + 'posts_per_page' => 1, + 'orderby' => 'date', + 'order' => 'desc', + 'post_type' => $post_type_filter, + 'post_status' => $post_status_filter, + 'ignore_sticky_posts' => true, + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'name' => 'wp-global-styles-site', + ); + + $global_style_query = new WP_Query(); + $recent_posts = $global_style_query->query( $args ); + if ( count( $recent_posts ) === 1 ) { + $site_cpt = get_object_vars( $recent_posts[0] ); + } elseif ( $create_post ) { + $cpt_post_id = wp_insert_post( + array( + 'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }', + 'post_status' => 'publish', + 'post_title' => 'Custom Styles Site', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518. + 'post_type' => $post_type_filter, + 'post_name' => 'wp-global-styles-site', + ), + true + ); + if ( ! is_wp_error( $cpt_post_id ) ) { + $site_cpt = get_object_vars( get_post( $cpt_post_id ) ); + } + } + + return $site_cpt; + + } + /** * Returns the user's origin config. * @@ -531,6 +603,52 @@ public static function get_user_data() { return static::$user; } + public static function get_site_data() { + if ( null !== static::$site && static::has_same_registered_blocks( 'site' ) ) { + return static::$site; + } + + $config = array(); + $site_cpt = static::get_site_data_from_wp_global_styles(); + + if ( array_key_exists( 'post_content', $site_cpt ) ) { + $decoded_data = json_decode( $site_cpt['post_content'], true ); + + $json_decoding_error = json_last_error(); + if ( JSON_ERROR_NONE !== $json_decoding_error ) { + trigger_error( 'Error when decoding a theme.json schema for site 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( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'site' ) ); + $config = $theme_json->get_data(); + return new WP_Theme_JSON_Gutenberg( $config, 'site' ); + } + + // Very important to verify that the flag isGlobalStylesUserThemeJSON is true. + // If it's not true then the content was not escaped and is not safe. + if ( + is_array( $decoded_data ) && + isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && + $decoded_data['isGlobalStylesUserThemeJSON'] + ) { + unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); + $config = $decoded_data; + } + } + + /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ + $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'site' ) ); + $config = $theme_json->get_data(); + static::$site = new WP_Theme_JSON_Gutenberg( $config, 'site' ); + + return static::$site; + } + /** * Returns the data merged from multiple origins. * @@ -588,7 +706,7 @@ public static function get_merged_data( $origin = 'custom' ) { $result->set_spacing_sizes(); return $result; } - + $result->merge( static::get_site_data() ); $result->merge( static::get_user_data() ); $result->set_spacing_sizes(); return $result; @@ -607,7 +725,7 @@ public static function get_user_global_styles_post_id() { return static::$user_custom_post_type_id; } - $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); if ( array_key_exists( 'ID', $user_cpt ) ) { static::$user_custom_post_type_id = $user_cpt['ID']; @@ -616,6 +734,20 @@ public static function get_user_global_styles_post_id() { return static::$user_custom_post_type_id; } + public static function get_site_global_styles_post_id() { + if ( null !== static::$site_custom_post_type_id ) { + return static::$site_custom_post_type_id; + } + + $site_cpt = static::get_site_data_from_wp_global_styles( true ); + + if ( array_key_exists( 'ID', $site_cpt ) ) { + static::$site_custom_post_type_id = $site_cpt['ID']; + } + + return static::$site_custom_post_type_id; + } + /** * Determines whether the active theme has a theme.json file. * @@ -670,6 +802,7 @@ public static function clean_cached_data() { ); static::$theme = null; static::$user = null; + static::$site = null; static::$user_custom_post_type_id = null; static::$i18n_schema = null; } diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index 5eeb0a1014aed6..ed28e9411fc853 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -10,6 +10,36 @@ * Base Global Styles REST API Controller. */ class Gutenberg_REST_Global_Styles_Controller_6_3 extends Gutenberg_REST_Global_Styles_Controller_6_2 { + + /** + * Registers the controllers routes. + * + * @return void + */ + public function register_routes() { + parent::register_routes(); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base, + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_site_item' ), + 'permission_callback' => array( $this, 'get_theme_items_permissions_check' ), + ), + ) + ); + + } + + public function get_site_item () { + $active_global_styles_site_id = WP_Theme_JSON_Resolver_Gutenberg::get_site_global_styles_post_id(); + $active_global_styles_site = get_post( $active_global_styles_site_id ); + $active_global_styles_site = $this->prepare_item_for_response( $active_global_styles_site, new WP_REST_Request() ); + return $active_global_styles_site; + } + /** * Revision controller. * diff --git a/lib/compat/wordpress-6.3/rest-api.php b/lib/compat/wordpress-6.3/rest-api.php index e111980887ef5a..a79e7d91f199b3 100644 --- a/lib/compat/wordpress-6.3/rest-api.php +++ b/lib/compat/wordpress-6.3/rest-api.php @@ -29,36 +29,11 @@ function gutenberg_update_templates_template_parts_rest_controller( $args, $post } add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 ); - -/** - * Registers the Global Styles Revisions REST API routes. - */ -function gutenberg_register_global_styles_revisions_endpoints() { - $global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller(); - $global_styles_revisions_controller->register_routes(); -} -add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); - /** * Registers the Global Styles REST API routes. */ -function gutenberg_register_global_styles_endpoints() { - $global_styles_controller = new Gutenberg_REST_Global_Styles_Controller_6_3(); - $global_styles_controller->register_routes(); -} -add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); - -/** - * Update `wp_global_styles` post type to use Gutenberg's REST controller. - * - * @param array $args Array of arguments for registering a post type. - * @param string $post_type Post type key. - */ -function gutenberg_update_global_styles_rest_controller( $args, $post_type ) { - if ( in_array( $post_type, array( 'wp_global_styles' ), true ) ) { - $args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_3'; - $args['rest_base'] = 'global-styles'; - } - return $args; +function gutenberg_register_global_styles_endpoints_6_3() { + $editor_settings = new Gutenberg_REST_Global_Styles_Controller_6_3(); + $editor_settings->register_routes(); } -add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 ); +add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints_6_3' ); From a32f1ee00ec4b7a9309b80eb4c28f89dcbd1c2eb Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 26 Apr 2023 10:33:58 -0300 Subject: [PATCH 02/24] Consuming site global styles from the client --- packages/core-data/src/actions.js | 9 ++ packages/core-data/src/entities.js | 8 ++ packages/core-data/src/reducer.js | 10 ++ packages/core-data/src/resolvers.js | 13 +++ packages/core-data/src/selectors.ts | 7 ++ .../global-styles/global-styles-provider.js | 102 +++++++++++++++++- 6 files changed, 145 insertions(+), 4 deletions(-) diff --git a/packages/core-data/src/actions.js b/packages/core-data/src/actions.js index 4c36e2505e7f3d..100bc61b37c2a5 100644 --- a/packages/core-data/src/actions.js +++ b/packages/core-data/src/actions.js @@ -149,6 +149,15 @@ export function __experimentalReceiveCurrentGlobalStylesId( }; } +export function __experimentalReceiveCurrentSiteGlobalStylesId( + currentSiteGlobalStylesId +) { + return { + type: 'RECEIVE_CURRENT_SITE_GLOBAL_STYLES_ID', + id: currentSiteGlobalStylesId, + }; +} + /** * Returns an action object used in signalling that the theme base global styles have been received * Ignored from documentation as it's internal to the data store. diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 3b8a443bcf1e39..227921474d1f39 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -150,6 +150,14 @@ export const rootEntitiesConfig = [ plural: 'globalStylesVariations', // Should be different than name. getTitle: ( record ) => record?.title?.rendered || record?.title, }, + { + label: __( 'Global Styles Site' ), + name: 'siteGlobalStyles', + kind: 'root', + baseURL: '/wp/v2/global-styles', + baseURLParams: { context: 'edit' }, + getTitle: ( record ) => record?.title?.rendered || record?.title, + }, { label: __( 'Themes' ), name: 'theme', diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 6d5fefd52ddf42..b9f470edb615ff 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -163,6 +163,15 @@ export function themeBaseGlobalStyles( state = {}, action ) { return state; } +export function currentSiteGlobalStylesId( state = undefined, action ) { + switch ( action.type ) { + case 'RECEIVE_CURRENT_SITE_GLOBAL_STYLES_ID': + return action.id; + } + + return state; +} + /** * Reducer managing the theme global styles variations. * @@ -647,6 +656,7 @@ export default combineReducers( { users, currentTheme, currentGlobalStylesId, + currentSiteGlobalStylesId, currentUser, themeGlobalStyleVariations, themeBaseGlobalStyles, diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index b33bb42e653379..57f4fcb3773427 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -453,6 +453,19 @@ __experimentalGetTemplateForLink.shouldInvalidate = ( action ) => { ); }; +export const __experimentalGetCurrentSiteGlobalStylesId = + () => + async ( { dispatch } ) => { + const siteGlobalStyles = await apiFetch( { + path: '/wp/v2/global-styles/', + } ); + if ( siteGlobalStyles ) { + dispatch.__experimentalReceiveCurrentSiteGlobalStylesId( + siteGlobalStyles.id + ); + } + }; + export const __experimentalGetCurrentGlobalStylesId = () => async ( { dispatch, resolveSelect } ) => { diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 07f3c9f48c5ebb..a2cd92ef08b4c1 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -32,6 +32,7 @@ export interface State { blockPatterns: Array< unknown >; blockPatternCategories: Array< unknown >; currentGlobalStylesId: string; + currentSiteGlobalStylesId: string; currentTheme: string; currentUser: ET.User< 'edit' >; embedPreviews: Record< string, { html: string } >; @@ -948,6 +949,12 @@ export function __experimentalGetCurrentGlobalStylesId( state: State ): string { return state.currentGlobalStylesId; } +export function __experimentalGetCurrentSiteGlobalStylesId( + state: State +): string { + return state.currentSiteGlobalStylesId; +} + /** * Return theme supports data in the index. * diff --git a/packages/edit-site/src/components/global-styles/global-styles-provider.js b/packages/edit-site/src/components/global-styles/global-styles-provider.js index 7ddd518020569a..48708062945d59 100644 --- a/packages/edit-site/src/components/global-styles/global-styles-provider.js +++ b/packages/edit-site/src/components/global-styles/global-styles-provider.js @@ -34,6 +34,90 @@ export function mergeBaseAndUserConfigs( base, user ) { return mergeWith( {}, base, user, mergeTreesCustomizer ); } +export function mergeConfigs( base, site, user ) { + const baseConfig = mergeWith( {}, base, mergeTreesCustomizer ); + return mergeWith( baseConfig, site, user, mergeTreesCustomizer ); +} + +function useGlobalStylesSiteConfig() { + const { siteGlobalStylesId, isReady, settings, styles } = useSelect( + ( select ) => { + const { getEditedEntityRecord, hasFinishedResolution } = + select( coreStore ); + const _siteGlobalStylesId = + select( + coreStore + ).__experimentalGetCurrentSiteGlobalStylesId(); + const record = _siteGlobalStylesId + ? getEditedEntityRecord( + 'root', + 'siteGlobalStyles', + _siteGlobalStylesId + ) + : undefined; + let hasResolved = false; + if ( + hasFinishedResolution( + '__experimentalGetCurrentSiteGlobalStylesId' + ) + ) { + hasResolved = _siteGlobalStylesId + ? hasFinishedResolution( 'getEditedEntityRecord', [ + 'root', + 'siteGlobalStyles', + _siteGlobalStylesId, + ] ) + : true; + } + + return { + siteGlobalStylesId: _siteGlobalStylesId, + isReady: hasResolved, + settings: record?.settings, + styles: record?.styles, + }; + }, + [] + ); + + const { getEditedEntityRecord } = useSelect( coreStore ); + const { editEntityRecord } = useDispatch( coreStore ); + const config = useMemo( () => { + return { + settings: settings ?? {}, + styles: styles ?? {}, + }; + }, [ settings, styles ] ); + + const setConfig = useCallback( + ( callback, options = {} ) => { + const record = getEditedEntityRecord( + 'root', + 'siteGlobalStyles', + siteGlobalStylesId + ); + const currentConfig = { + styles: record?.styles ?? {}, + settings: record?.settings ?? {}, + }; + const updatedConfig = callback( currentConfig ); + editEntityRecord( + 'root', + 'siteGlobalStyles', + siteGlobalStylesId, + { + styles: cleanEmptyObject( updatedConfig.styles ) || {}, + settings: cleanEmptyObject( updatedConfig.settings ) || {}, + }, + options + ); + }, + [ siteGlobalStylesId ] + ); + + return [ isReady, config, setConfig ]; +} + function useGlobalStylesUserConfig() { const { globalStylesId, isReady, settings, styles } = useSelect( ( select ) => { @@ -126,27 +210,37 @@ function useGlobalStylesContext() { const [ isUserConfigReady, userConfig, setUserConfig ] = useGlobalStylesUserConfig(); const [ isBaseConfigReady, baseConfig ] = useGlobalStylesBaseConfig(); + const [ isSiteConfigReady, siteConfig, setSiteConfig ] = + useGlobalStylesSiteConfig(); + const mergedConfig = useMemo( () => { - if ( ! baseConfig || ! userConfig ) { + if ( ! baseConfig || ! userConfig || ! siteConfig ) { return {}; } - return mergeBaseAndUserConfigs( baseConfig, userConfig ); - }, [ userConfig, baseConfig ] ); + return mergeConfigs( baseConfig, siteConfig, userConfig ); + }, [ userConfig, baseConfig, siteConfig ] ); + const context = useMemo( () => { return { - isReady: isUserConfigReady && isBaseConfigReady, + isReady: + isUserConfigReady && isBaseConfigReady && isSiteConfigReady, + site: siteConfig, user: userConfig, base: baseConfig, merged: mergedConfig, setUserConfig, + setSiteConfig, }; }, [ mergedConfig, userConfig, + siteConfig, baseConfig, setUserConfig, + setSiteConfig, isUserConfigReady, isBaseConfigReady, + isSiteConfigReady, ] ); return context; From 2102a529d5696a0c4c1576f81785dad46dbd6a4e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 26 Apr 2023 10:47:45 -0300 Subject: [PATCH 03/24] undo unwanted changes made merging from trunk --- lib/compat/wordpress-6.3/rest-api.php | 33 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.3/rest-api.php b/lib/compat/wordpress-6.3/rest-api.php index a79e7d91f199b3..352690a7c7175d 100644 --- a/lib/compat/wordpress-6.3/rest-api.php +++ b/lib/compat/wordpress-6.3/rest-api.php @@ -29,11 +29,36 @@ function gutenberg_update_templates_template_parts_rest_controller( $args, $post } add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 ); + +/** + * Registers the Global Styles Revisions REST API routes. + */ +function gutenberg_register_global_styles_revisions_endpoints() { + $global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller(); + $global_styles_revisions_controller->register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); + /** * Registers the Global Styles REST API routes. */ -function gutenberg_register_global_styles_endpoints_6_3() { - $editor_settings = new Gutenberg_REST_Global_Styles_Controller_6_3(); - $editor_settings->register_routes(); +function gutenberg_register_global_styles_endpoints() { + $global_styles_controller = new Gutenberg_REST_Global_Styles_Controller_6_3(); + $global_styles_controller->register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); + +/** + * Update `wp_global_styles` post type to use Gutenberg's REST controller. + * + * @param array $args Array of arguments for registering a post type. + * @param string $post_type Post type key. + */ +function gutenberg_update_global_styles_rest_controller( $args, $post_type ) { + if ( in_array( $post_type, array( 'wp_global_styles' ), true ) ) { + $args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_3'; + $args['rest_base'] = 'global-styles'; + } + return $args; } -add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints_6_3' ); +add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 ); \ No newline at end of file From 0ad0aa4a32ab08a8a4e9320bc370b458db4fad48 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 26 Apr 2023 11:23:53 -0300 Subject: [PATCH 04/24] removing not needed variable --- lib/class-wp-theme-json-resolver-gutenberg.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 6e95544e714e7a..6f7241c9c1486e 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -512,7 +512,6 @@ public static function get_site_data_from_wp_global_styles($create_post = false, $site_cpt = array(); $post_type_filter = 'wp_global_styles'; - $stylesheet = $theme->get_stylesheet(); $args = array( 'posts_per_page' => 1, 'orderby' => 'date', From 58fa7d9ea01f100e7f7d5c8cbaf11573663e8c3f Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 26 Apr 2023 11:35:10 -0300 Subject: [PATCH 05/24] post name as variable --- lib/class-wp-theme-json-resolver-gutenberg.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 6f7241c9c1486e..73aa4d9d6bc186 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -512,6 +512,7 @@ public static function get_site_data_from_wp_global_styles($create_post = false, $site_cpt = array(); $post_type_filter = 'wp_global_styles'; + $post_name = 'wp-global-styles-site'; $args = array( 'posts_per_page' => 1, 'orderby' => 'date', @@ -522,7 +523,7 @@ public static function get_site_data_from_wp_global_styles($create_post = false, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'name' => 'wp-global-styles-site', + 'name' => $post_name, ); $global_style_query = new WP_Query(); @@ -536,7 +537,7 @@ public static function get_site_data_from_wp_global_styles($create_post = false, 'post_status' => 'publish', 'post_title' => 'Custom Styles Site', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518. 'post_type' => $post_type_filter, - 'post_name' => 'wp-global-styles-site', + 'post_name' => $post_name, ), true ); From f72324d27bd90a6012fc07d007aef49ca68807f2 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 28 Apr 2023 12:18:05 -0300 Subject: [PATCH 06/24] adding site to useGlobalStyle hook --- .../src/components/global-styles/hooks.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/global-styles/hooks.js b/packages/block-editor/src/components/global-styles/hooks.js index 17e4cec9369f45..13016f1e23689a 100644 --- a/packages/block-editor/src/components/global-styles/hooks.js +++ b/packages/block-editor/src/components/global-styles/hooks.js @@ -151,7 +151,9 @@ export function useGlobalStyle( merged: mergedConfig, base: baseConfig, user: userConfig, + site: siteConfig, setUserConfig, + setSiteConfig, } = useContext( GlobalStylesContext ); const appendedPath = path ? '.' + path : ''; const finalPath = ! blockName @@ -159,7 +161,8 @@ export function useGlobalStyle( : `styles.blocks.${ blockName }${ appendedPath }`; const setStyle = ( newValue ) => { - setUserConfig( ( currentConfig ) => { + const setConfig = source === 'site' ? setSiteConfig : setUserConfig; + setConfig( ( currentConfig ) => { // Deep clone `currentConfig` to avoid mutating it later. const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) ); set( @@ -196,6 +199,12 @@ export function useGlobalStyle( ? getValueFromVariable( mergedConfig, blockName, rawResult ) : rawResult; break; + case 'site': + rawResult = get( siteConfig, finalPath ); + result = shouldDecodeEncode + ? getValueFromVariable( mergedConfig, blockName, rawResult ) + : rawResult; + break; case 'base': rawResult = get( baseConfig, finalPath ); result = shouldDecodeEncode From 1849eac8565974dc4973f6a51af87408677d12cc Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 28 Apr 2023 15:11:59 -0300 Subject: [PATCH 07/24] adding site to useGlobalSetting hook --- .../src/components/global-styles/hooks.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/components/global-styles/hooks.js b/packages/block-editor/src/components/global-styles/hooks.js index 13016f1e23689a..8a9d41d7986309 100644 --- a/packages/block-editor/src/components/global-styles/hooks.js +++ b/packages/block-editor/src/components/global-styles/hooks.js @@ -86,7 +86,8 @@ export const useGlobalStylesReset = () => { }; export function useGlobalSetting( propertyPath, blockName, source = 'all' ) { - const { setUserConfig, ...configs } = useContext( GlobalStylesContext ); + const { setUserConfig, setSiteConfig, ...configs } = + useContext( GlobalStylesContext ); const appendedBlockPath = blockName ? '.blocks.' + blockName : ''; const appendedPropertyPath = propertyPath ? '.' + propertyPath : ''; @@ -128,13 +129,14 @@ export function useGlobalSetting( propertyPath, blockName, source = 'all' ) { appendedBlockPath, ] ); + const setConfig = source === 'site' ? setSiteConfig : setUserConfig; const setSetting = ( newValue ) => { - setUserConfig( ( currentConfig ) => { + setConfig( ( currentConfig ) => { // Deep clone `currentConfig` to avoid mutating it later. - const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) ); - set( newUserConfig, contextualPath, newValue ); + const newConfig = JSON.parse( JSON.stringify( currentConfig ) ); + set( newConfig, contextualPath, newValue ); - return newUserConfig; + return newConfig; } ); }; From f1be9f4b644e281ae069ab02bcf51d8a531a5c50 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 28 Apr 2023 15:20:52 -0300 Subject: [PATCH 08/24] adding site to global styles context --- packages/block-editor/src/components/global-styles/context.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-editor/src/components/global-styles/context.js b/packages/block-editor/src/components/global-styles/context.js index 630b5a2b9059d5..d3f7b65cf6a9ed 100644 --- a/packages/block-editor/src/components/global-styles/context.js +++ b/packages/block-editor/src/components/global-styles/context.js @@ -5,9 +5,11 @@ import { createContext } from '@wordpress/element'; export const DEFAULT_GLOBAL_STYLES_CONTEXT = { user: {}, + site: {}, base: {}, merged: {}, setUserConfig: () => {}, + setSiteConfig: () => {}, }; export const GlobalStylesContext = createContext( From 82c3ef2f6ab9773c7c2e6be3dfc9f1d5cc31e901 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 28 Apr 2023 15:21:32 -0300 Subject: [PATCH 09/24] reset site styles when reset global styles to default is run --- .../src/components/global-styles/hooks.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/components/global-styles/hooks.js b/packages/block-editor/src/components/global-styles/hooks.js index 8a9d41d7986309..cf5a8381823dea 100644 --- a/packages/block-editor/src/components/global-styles/hooks.js +++ b/packages/block-editor/src/components/global-styles/hooks.js @@ -74,14 +74,18 @@ const VALID_SETTINGS = [ ]; export const useGlobalStylesReset = () => { - const { user: config, setUserConfig } = useContext( GlobalStylesContext ); + const { + user: config, + setUserConfig, + setSiteConfig, + } = useContext( GlobalStylesContext ); const canReset = !! config && ! fastDeepEqual( config, EMPTY_CONFIG ); return [ canReset, - useCallback( - () => setUserConfig( () => EMPTY_CONFIG ), - [ setUserConfig ] - ), + useCallback( () => { + setUserConfig( () => EMPTY_CONFIG ); + setSiteConfig( () => EMPTY_CONFIG ); + }, [ setUserConfig ] ), ]; }; From 8158e3f9a09b341379b8f30b85a025f6ea0d982f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 May 2023 12:04:38 +0200 Subject: [PATCH 10/24] Make PHP linter happy --- lib/class-wp-theme-json-resolver-gutenberg.php | 4 ++-- .../class-gutenberg-rest-global-styles-controller-6-3.php | 6 +++--- lib/compat/wordpress-6.3/rest-api.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 73aa4d9d6bc186..915b08baca4266 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -496,7 +496,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post return $user_cpt; } - public static function get_site_data_from_wp_global_styles($create_post = false, $post_status_filter = array( 'publish' ) ) { + public static function get_site_data_from_wp_global_styles( $create_post = false, $post_status_filter = array( 'publish' ) ) { $theme = wp_get_theme(); /* @@ -512,7 +512,7 @@ public static function get_site_data_from_wp_global_styles($create_post = false, $site_cpt = array(); $post_type_filter = 'wp_global_styles'; - $post_name = 'wp-global-styles-site'; + $post_name = 'wp-global-styles-site'; $args = array( 'posts_per_page' => 1, 'orderby' => 'date', diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index ed28e9411fc853..88a80a754bf0c1 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -33,10 +33,10 @@ public function register_routes() { } - public function get_site_item () { + public function get_site_item() { $active_global_styles_site_id = WP_Theme_JSON_Resolver_Gutenberg::get_site_global_styles_post_id(); - $active_global_styles_site = get_post( $active_global_styles_site_id ); - $active_global_styles_site = $this->prepare_item_for_response( $active_global_styles_site, new WP_REST_Request() ); + $active_global_styles_site = get_post( $active_global_styles_site_id ); + $active_global_styles_site = $this->prepare_item_for_response( $active_global_styles_site, new WP_REST_Request() ); return $active_global_styles_site; } diff --git a/lib/compat/wordpress-6.3/rest-api.php b/lib/compat/wordpress-6.3/rest-api.php index 352690a7c7175d..e111980887ef5a 100644 --- a/lib/compat/wordpress-6.3/rest-api.php +++ b/lib/compat/wordpress-6.3/rest-api.php @@ -61,4 +61,4 @@ function gutenberg_update_global_styles_rest_controller( $args, $post_type ) { } return $args; } -add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 ); \ No newline at end of file +add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 ); From ed6b8fd1fe61e4ddf875ea310be3737bebd906bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 May 2023 13:37:37 +0200 Subject: [PATCH 11/24] Enable site as a valid origin for data --- lib/class-wp-theme-json-gutenberg.php | 1 + phpunit/class-wp-theme-json-resolver-test.php | 49 +++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index fbbf9a05d1c817..de42a6d7e2ed13 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -56,6 +56,7 @@ class WP_Theme_JSON_Gutenberg { 'default', 'blocks', 'theme', + 'site', 'custom', ); diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 4cc34bf97b32c7..d5d8650b87823b 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -388,10 +388,12 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q * @param string $block_styles_text Message. * @param bool $theme_palette Whether the theme palette is present. * @param string $theme_palette_text Message. + * @param bool $site_palette Whether the site palette is present. + * @param string $site_palette_text Message. * @param bool $user_palette Whether the user palette is present. * @param string $user_palette_text Message. */ - public function test_get_merged_data_returns_origin( $origin, $core_palette, $core_palette_text, $block_styles, $block_styles_text, $theme_palette, $theme_palette_text, $user_palette, $user_palette_text ) { + public function test_get_merged_data_returns_origin( $origin, $core_palette, $core_palette_text, $block_styles, $block_styles_text, $theme_palette, $theme_palette_text, $site_palette, $site_palette_text, $user_palette, $user_palette_text ) { // Make sure there is data from the blocks origin. register_block_type( 'my/block-with-styles', @@ -418,18 +420,32 @@ public function test_get_merged_data_returns_origin( $origin, $core_palette, $co // Make sure there is data from the theme origin. switch_theme( 'block-theme' ); + // Make sure there is data from the site origin. + wp_set_current_user( self::$administrator_id ); + $site_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_site_data_from_wp_global_styles( true ); + $site_config = json_decode( $site_cpt['post_content'], true ); + $site_config['settings']['color']['palette']['site'] = array( + array( + 'color' => 'olive', + 'name' => 'Olive green', + 'slug' => 'olive-green', + ), + ); + $site_cpt['post_content'] = wp_json_encode( $site_config ); + wp_update_post( $site_cpt, true, false ); + // Make sure there is data from the user origin. wp_set_current_user( self::$administrator_id ); - $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); - $config = json_decode( $user_cpt['post_content'], true ); - $config['settings']['color']['palette']['custom'] = array( + $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + $user_config = json_decode( $user_cpt['post_content'], true ); + $user_config['settings']['color']['palette']['custom'] = array( array( 'color' => 'hotpink', 'name' => 'My color', 'slug' => 'my-color', ), ); - $user_cpt['post_content'] = wp_json_encode( $config ); + $user_cpt['post_content'] = wp_json_encode( $user_config ); wp_update_post( $user_cpt, true, false ); $theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin ); @@ -444,6 +460,7 @@ static function( $element ) { ); $this->assertSame( $block_styles, count( $styles ) === 1, $block_styles_text ); $this->assertSame( $theme_palette, isset( $settings['color']['palette']['theme'] ), $theme_palette_text ); + $this->assertSame( $site_palette, isset( $settings['color']['palette']['site'] ), $site_palette_text ); $this->assertSame( $user_palette, isset( $settings['color']['palette']['custom'] ), $user_palette_text ); unregister_block_type( 'my/block-with-styles' ); @@ -464,6 +481,8 @@ public function data_get_merged_data_returns_origin() { 'block_styles_text' => 'Block styles should not be present', 'theme_palette' => false, 'theme_palette_text' => 'Theme palette should not be present', + 'site_palette' => false, + 'site_palette_text' => 'Site palette should not be present', 'user_palette' => false, 'user_palette_text' => 'User palette should not be present', ), @@ -475,6 +494,8 @@ public function data_get_merged_data_returns_origin() { 'block_styles_text' => 'Block styles must be present', 'theme_palette' => false, 'theme_palette_text' => 'Theme palette should not be present', + 'site_palette' => false, + 'site_palette_text' => 'Site palette should not be present', 'user_palette' => false, 'user_palette_text' => 'User palette should not be present', ), @@ -486,9 +507,25 @@ public function data_get_merged_data_returns_origin() { 'block_styles_text' => 'Block styles must be present', 'theme_palette' => true, 'theme_palette_text' => 'Theme palette must be present', + 'site_palette' => false, + 'site_palette_text' => 'Site palette should not be present', 'user_palette' => false, 'user_palette_text' => 'User palette should not be present', ), + 'origin_site' => array( + 'origin' => 'site', + 'core_palette' => true, + 'core_palette_text' => 'Core palette must be present', + 'block_styles' => true, + 'block_styles_text' => 'Block styles must be present', + 'theme_palette' => true, + 'theme_palette_text' => 'Theme palette must be present', + 'site_palette' => true, + 'site_palette_text' => 'Site palette should be present', + 'user_palette' => true, + 'user_palette_text' => 'User palette should not be present', + + ), 'origin_custom' => array( 'origin' => 'custom', 'core_palette' => true, @@ -497,6 +534,8 @@ public function data_get_merged_data_returns_origin() { 'block_styles_text' => 'Block styles must be present', 'theme_palette' => true, 'theme_palette_text' => 'Theme palette must be present', + 'site_palette' => true, + 'site_palette_text' => 'Site palette must be present', 'user_palette' => true, 'user_palette_text' => 'User palette must be present', ), From c461e25bc6e8341b0a7648729890a0cac9f50f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 May 2023 14:00:59 +0200 Subject: [PATCH 12/24] Add test for get_site_data_from_wp_global_styles --- phpunit/class-wp-theme-json-resolver-test.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index d5d8650b87823b..64f0a859c2a3df 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -608,4 +608,16 @@ public function test_get_style_variations_returns_all_variations() { ); } + /** + * @covers WP_Theme_JSON_Resolver_Gutenberg::get_site_data_from_wp_global_styles + */ + public function test_get_site_data_from_wp_global_styles_create_post() { + $empty_array = WP_Theme_JSON_Resolver_Gutenberg::get_site_data_from_wp_global_styles(); + $this->assertIsArray( $empty_array ); + $this->assertSameSets( array(), $empty_array ); + $post_data = WP_Theme_JSON_Resolver_Gutenberg::get_site_data_from_wp_global_styles( true ); + $this->assertIsArray( $post_data ); + $this->assertArrayHasKey( 'ID', $post_data ); + } + } From fc139bfdd96e83df673365f4745c2fafdddd77a1 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 2 May 2023 12:04:46 -0300 Subject: [PATCH 13/24] reverting unwanted change --- lib/class-wp-theme-json-resolver-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 915b08baca4266..9dc06e87e7b494 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -725,7 +725,7 @@ public static function get_user_global_styles_post_id() { return static::$user_custom_post_type_id; } - $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); + $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme(), true ); if ( array_key_exists( 'ID', $user_cpt ) ) { static::$user_custom_post_type_id = $user_cpt['ID']; From ce3bb54eb2a4358659e7e341d86ecaea24d7b28f Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 2 May 2023 12:10:59 -0300 Subject: [PATCH 14/24] Adding PHPDoc for get_site_global_styles_post_id and get_site_data functions --- lib/class-wp-theme-json-resolver-gutenberg.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 9dc06e87e7b494..2ed77c0d2330a3 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -603,6 +603,13 @@ public static function get_user_data() { return static::$user; } + /** + * Returns the site's origin config. + * + * @since 6.3.0 + * + * @return WP_Theme_JSON Entity that holds styles for site level data. + */ public static function get_site_data() { if ( null !== static::$site && static::has_same_registered_blocks( 'site' ) ) { return static::$site; @@ -734,6 +741,14 @@ public static function get_user_global_styles_post_id() { return static::$user_custom_post_type_id; } + /** + * Returns the ID of the custom post type + * that stores site data. + * + * @since 6.3.0 + * + * @return integer|null + */ public static function get_site_global_styles_post_id() { if ( null !== static::$site_custom_post_type_id ) { return static::$site_custom_post_type_id; From 92206bfa49b629d2f0a34c04e3723e4b8aed8a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 May 2023 17:16:27 +0200 Subject: [PATCH 15/24] get_merge_data: add support for site origin --- lib/class-wp-theme-json-resolver-gutenberg.php | 8 +++++++- phpunit/class-wp-theme-json-resolver-test.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 2ed77c0d2330a3..a6e5999b5ad6e7 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -686,7 +686,7 @@ public static function get_site_data() { * added the `$origin` parameter. * @since 6.1.0 Added block data and generation of spacingSizes array. * - * @param string $origin Optional. To what level should we merge data:'default', 'blocks', 'theme' or 'custom'. + * @param string $origin Optional. To what level should we merge data:'default', 'blocks', 'theme', 'site', or 'custom'. * 'custom' is used as default value as well as fallback value if the origin is unknown. * * @return WP_Theme_JSON @@ -713,7 +713,13 @@ public static function get_merged_data( $origin = 'custom' ) { $result->set_spacing_sizes(); return $result; } + $result->merge( static::get_site_data() ); + if ( 'site' === $origin ) { + $result->set_spacing_sizes(); + return $result; + } + $result->merge( static::get_user_data() ); $result->set_spacing_sizes(); return $result; diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 64f0a859c2a3df..4e7b62882c06fb 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -522,7 +522,7 @@ public function data_get_merged_data_returns_origin() { 'theme_palette_text' => 'Theme palette must be present', 'site_palette' => true, 'site_palette_text' => 'Site palette should be present', - 'user_palette' => true, + 'user_palette' => false, 'user_palette_text' => 'User palette should not be present', ), From 844035be101d63b81c3e937d60cdb8a44c7c018a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 May 2023 17:19:35 +0200 Subject: [PATCH 16/24] get_site_data_from_wp_global_styles: add PHPDoc --- lib/class-wp-theme-json-resolver-gutenberg.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index a6e5999b5ad6e7..37f4a37e5cc5c2 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -496,6 +496,22 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post return $user_cpt; } + /** + * Returns the custom post type that contains the site's origin config + * or an empty array if none is found. + * + * This can also create and return a new draft custom post type. + * + * @since 6.3.0 + * + * @param bool $create_post Optional. Whether a new custom post + * type should be created if none are + * found. Default false. + * @param array $post_status_filter Optional. Filter custom post type by + * post status. Default `array( 'publish' )`, + * so it only fetches published posts. + * @return array Custom Post Type for the user's origin config. + */ public static function get_site_data_from_wp_global_styles( $create_post = false, $post_status_filter = array( 'publish' ) ) { $theme = wp_get_theme(); From f37bfb7bd93e8eb87dc11af1326d2cab35417d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 May 2023 17:20:38 +0200 Subject: [PATCH 17/24] Update name of new filter --- lib/class-wp-theme-json-resolver-gutenberg.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 37f4a37e5cc5c2..6b85df9b7b1aff 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -647,7 +647,7 @@ public static function get_site_data() { * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'site' ) ); + $theme_json = apply_filters( 'wp_theme_json_data_site', new WP_Theme_JSON_Data_Gutenberg( $config, 'site' ) ); $config = $theme_json->get_data(); return new WP_Theme_JSON_Gutenberg( $config, 'site' ); } @@ -665,7 +665,7 @@ public static function get_site_data() { } /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ - $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'site' ) ); + $theme_json = apply_filters( 'wp_theme_json_data_site', new WP_Theme_JSON_Data_Gutenberg( $config, 'site' ) ); $config = $theme_json->get_data(); static::$site = new WP_Theme_JSON_Gutenberg( $config, 'site' ); From 9e1057088d28d05b2b94341fbc3fccf6aaed8f10 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 3 May 2023 15:32:50 -0300 Subject: [PATCH 18/24] add site global styles under 'site' key instead of 'custom' in global styles theme.json --- lib/class-wp-theme-json-gutenberg.php | 2 +- ...berg-rest-global-styles-controller-6-3.php | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index de42a6d7e2ed13..a893aff8bafc6d 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -596,7 +596,7 @@ public static function get_element_class_name( $element ) { * * @param array $theme_json A structure that follows the theme.json schema. * @param string $origin Optional. What source of data this object represents. - * One of 'default', 'theme', or 'custom'. Default 'theme'. + * One of 'default', 'theme', 'site' or 'custom'. Default 'theme'. */ public function __construct( $theme_json = array(), $origin = 'theme' ) { if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) { diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index 88a80a754bf0c1..d848fcd95eb8b7 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -78,4 +78,76 @@ protected function prepare_links( $id ) { return $links; } + + /** + * Prepare a global styles config output for response. + * + * @since 5.9.0 + * @since 6.2 Handling of style.css was added to WP_Theme_JSON. + * @since 6.3 Added support for site origin in global styles. + * + * @param WP_Post $post Global Styles post object. + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response Response object. + */ + public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $raw_config = json_decode( $post->post_content, true ); + $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON']; + $config = array(); + if ( $is_global_styles_user_theme_json ) { + $origin = ( isset ( $post->post_name ) && 'wp-global-styles-site' === $post->post_name ) ? 'site' : 'custom'; + $config = ( new WP_Theme_JSON_Gutenberg( $raw_config, $origin ) )->get_raw_data(); + } + + // Base fields for every post. + $data = array(); + $fields = $this->get_fields_for_response( $request ); + + if ( rest_is_field_included( 'id', $fields ) ) { + $data['id'] = $post->ID; + } + + if ( rest_is_field_included( 'title', $fields ) ) { + $data['title'] = array(); + } + if ( rest_is_field_included( 'title.raw', $fields ) ) { + $data['title']['raw'] = $post->post_title; + } + if ( rest_is_field_included( 'title.rendered', $fields ) ) { + add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); + + $data['title']['rendered'] = get_the_title( $post->ID ); + + remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); + } + + if ( rest_is_field_included( 'settings', $fields ) ) { + $data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(); + } + + if ( rest_is_field_included( 'styles', $fields ) ) { + $data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(); + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + $response = rest_ensure_response( $data ); + + if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { + $links = $this->prepare_links( $post->ID ); + $response->add_links( $links ); + if ( ! empty( $links['self']['href'] ) ) { + $actions = $this->get_available_actions(); + $self = $links['self']['href']; + foreach ( $actions as $rel ) { + $response->add_link( $rel, $self ); + } + } + } + + return $response; + } } From 8f644349f32bdc18709d3e1851afe2f170ad0c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 4 May 2023 12:22:51 +0200 Subject: [PATCH 19/24] REST controller: fix lint issue --- .../class-gutenberg-rest-global-styles-controller-6-3.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index d848fcd95eb8b7..e2ba67bf7f0250 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -95,7 +95,7 @@ public function prepare_item_for_response( $post, $request ) { // phpcs:ignore V $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON']; $config = array(); if ( $is_global_styles_user_theme_json ) { - $origin = ( isset ( $post->post_name ) && 'wp-global-styles-site' === $post->post_name ) ? 'site' : 'custom'; + $origin = ( isset( $post->post_name ) && 'wp-global-styles-site' === $post->post_name ) ? 'site' : 'custom'; $config = ( new WP_Theme_JSON_Gutenberg( $raw_config, $origin ) )->get_raw_data(); } From f08658b1d4485849d9720f36af2cdd17de1dc3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 4 May 2023 12:29:10 +0200 Subject: [PATCH 20/24] Add PHPDoc for get_site_item --- .../class-gutenberg-rest-global-styles-controller-6-3.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index e2ba67bf7f0250..93f1c10c856f15 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -33,6 +33,13 @@ public function register_routes() { } + /** + * Return the global styles config for the site origin. + * + * @since 6.3.0 + * + * @return WP_REST_Response|WP_Error + */ public function get_site_item() { $active_global_styles_site_id = WP_Theme_JSON_Resolver_Gutenberg::get_site_global_styles_post_id(); $active_global_styles_site = get_post( $active_global_styles_site_id ); From 79bb97f53ae90fd623e010943787ffad2bb834b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 4 May 2023 12:30:22 +0200 Subject: [PATCH 21/24] Move every method after the properties --- ...tenberg-rest-global-styles-controller-6-3.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index 93f1c10c856f15..882f90c2978d37 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -11,6 +11,14 @@ */ class Gutenberg_REST_Global_Styles_Controller_6_3 extends Gutenberg_REST_Global_Styles_Controller_6_2 { + /** + * Revision controller. + * + * @since 6.3.0 + * @var WP_REST_Revisions_Controller + */ + private $revisions_controller; + /** * Registers the controllers routes. * @@ -47,14 +55,6 @@ public function get_site_item() { return $active_global_styles_site; } - /** - * Revision controller. - * - * @since 6.3.0 - * @var WP_REST_Revisions_Controller - */ - private $revisions_controller; - /** * Prepares links for the request. * From 9e7c951d4851cd3eacfa3e43a6ece7fcfaea3f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 4 May 2023 12:38:56 +0200 Subject: [PATCH 22/24] get_site_item: follow existing patterns --- ...utenberg-rest-global-styles-controller-6-3.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index 882f90c2978d37..3d8d5c7bb86aeb 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -46,13 +46,18 @@ public function register_routes() { * * @since 6.3.0 * + * @param WP_REST_Request $request The request instance. + * * @return WP_REST_Response|WP_Error */ - public function get_site_item() { - $active_global_styles_site_id = WP_Theme_JSON_Resolver_Gutenberg::get_site_global_styles_post_id(); - $active_global_styles_site = get_post( $active_global_styles_site_id ); - $active_global_styles_site = $this->prepare_item_for_response( $active_global_styles_site, new WP_REST_Request() ); - return $active_global_styles_site; + public function get_site_item( $request ) { + $post_id = WP_Theme_JSON_Resolver_Gutenberg::get_site_global_styles_post_id(); + $post = $this->get_post( $post_id ); + if ( is_wp_error( $post ) ) { + return $post; + } + + return $this->prepare_item_for_response( $post, $request ); } /** From d4ba09086983b67c513364c7c11b421c9e7e9538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 4 May 2023 12:46:18 +0200 Subject: [PATCH 23/24] Remove unnecessary change --- .../class-gutenberg-rest-global-styles-controller-6-3.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php index 3d8d5c7bb86aeb..7a95242426cd96 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php @@ -10,7 +10,6 @@ * Base Global Styles REST API Controller. */ class Gutenberg_REST_Global_Styles_Controller_6_3 extends Gutenberg_REST_Global_Styles_Controller_6_2 { - /** * Revision controller. * From a45f8c4375ad796e34dd9e5b10241ef303d02c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 4 May 2023 12:52:22 +0200 Subject: [PATCH 24/24] Make linter happy --- lib/class-wp-theme-json-resolver-gutenberg.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 6b85df9b7b1aff..c82ed72011be47 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -504,12 +504,12 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post * * @since 6.3.0 * - * @param bool $create_post Optional. Whether a new custom post - * type should be created if none are - * found. Default false. - * @param array $post_status_filter Optional. Filter custom post type by - * post status. Default `array( 'publish' )`, - * so it only fetches published posts. + * @param bool $create_post Optional. Whether a new custom post + * type should be created if none are + * found. Default false. + * @param array $post_status_filter Optional. Filter custom post type by + * post status. Default `array( 'publish' )`, + * so it only fetches published posts. * @return array Custom Post Type for the user's origin config. */ public static function get_site_data_from_wp_global_styles( $create_post = false, $post_status_filter = array( 'publish' ) ) {