From 3a911759dc691a142fcefd3e9f4593e25aba54ca Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:42:16 +0700 Subject: [PATCH 1/6] Allow base global styles data in post editor --- lib/class-wp-rest-global-styles-controller-gutenberg.php | 7 +++---- .../editor/src/components/global-styles-provider/index.js | 7 ++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-rest-global-styles-controller-gutenberg.php b/lib/class-wp-rest-global-styles-controller-gutenberg.php index 421408e6f20b4a..cfcec0c8f956fb 100644 --- a/lib/class-wp-rest-global-styles-controller-gutenberg.php +++ b/lib/class-wp-rest-global-styles-controller-gutenberg.php @@ -586,12 +586,11 @@ public function get_item_schema() { * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */ public function get_theme_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - /* - * Verify if the current user has edit_theme_options capability. - * This capability is required to edit/view/delete templates. + * Verify if the current user has edit_posts capability. + * This capability is required to view global styles. */ - if ( ! current_user_can( 'edit_theme_options' ) ) { + if ( ! current_user_can( 'edit_posts' ) ) { return new WP_Error( 'rest_cannot_manage_global_styles', __( 'Sorry, you are not allowed to access the global styles on this site.', 'gutenberg' ), diff --git a/packages/editor/src/components/global-styles-provider/index.js b/packages/editor/src/components/global-styles-provider/index.js index 8426593d8f5f51..93dd4dbe1eda49 100644 --- a/packages/editor/src/components/global-styles-provider/index.js +++ b/packages/editor/src/components/global-styles-provider/index.js @@ -146,13 +146,10 @@ function useGlobalStylesUserConfig() { function useGlobalStylesBaseConfig() { const baseConfig = useSelect( ( select ) => { - const { __experimentalGetCurrentThemeBaseGlobalStyles, canUser } = + const { __experimentalGetCurrentThemeBaseGlobalStyles } = select( coreStore ); - return ( - canUser( 'read', { kind: 'root', name: 'theme' } ) && - __experimentalGetCurrentThemeBaseGlobalStyles() - ); + return __experimentalGetCurrentThemeBaseGlobalStyles(); }, [] ); return [ !! baseConfig, baseConfig ]; From 06f1fbde8de7fdc26b0a5760b285790b0aa62f2e Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:22:55 +0700 Subject: [PATCH 2/6] Try getting user global styles working --- lib/compat/wordpress-6.6/rest-api.php | 1 + packages/core-data/src/entities.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index fee9c71b86c070..aaef8c77c4f578 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -170,6 +170,7 @@ function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { $paths[] = '/wp/v2/global-styles/themes/' . get_stylesheet(); $paths[] = '/wp/v2/themes?context=edit&status=active'; $paths[] = '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id() . '?context=edit'; + $paths[] = '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id(); } return $paths; } diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 8d09402087cf90..0f7f7fad31de42 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -182,7 +182,7 @@ export const rootEntitiesConfig = [ name: 'globalStyles', kind: 'root', baseURL: '/wp/v2/global-styles', - baseURLParams: { context: 'edit' }, + baseURLParams: {}, plural: 'globalStylesVariations', // Should be different from name. getTitle: ( record ) => record?.title?.rendered || record?.title, getRevisionsUrl: ( parentId, revisionId ) => @@ -190,6 +190,17 @@ export const rootEntitiesConfig = [ revisionId ? '/' + revisionId : '' }`, supportsPagination: true, + getPath: ( path, query, baseURL, id ) => { + const context = + query.context || + ( query.operation === 'read' ? 'view' : 'edit' ); + const contextQuery = context ? `?context=${ context }` : ''; + return `${ baseURL }${ id ? '/' + id : '' }${ contextQuery }`; + }, + capabilities: { + read: 'edit_posts', + update: 'edit_theme_options', + }, }, { label: __( 'Themes' ), From 790febdf67899a8d659dd894b5cadeec982f4135 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 27 Aug 2024 16:53:54 +0800 Subject: [PATCH 3/6] Add working permissions checks for global styles base config --- .../global-styles-provider/index.js | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/global-styles-provider/index.js b/packages/editor/src/components/global-styles-provider/index.js index 93dd4dbe1eda49..deb070072a620c 100644 --- a/packages/editor/src/components/global-styles-provider/index.js +++ b/packages/editor/src/components/global-styles-provider/index.js @@ -146,10 +146,26 @@ function useGlobalStylesUserConfig() { function useGlobalStylesBaseConfig() { const baseConfig = useSelect( ( select ) => { - const { __experimentalGetCurrentThemeBaseGlobalStyles } = - select( coreStore ); + const { + __experimentalGetCurrentThemeBaseGlobalStyles, + getCurrentTheme, + canUser, + } = select( coreStore ); + + const canReadActiveTheme = canUser( 'read', 'themes?status=active' ); + if ( ! canReadActiveTheme ) { + return; + } + + const currentTheme = getCurrentTheme(); + const canUserReadBaseGlobalStyles = canUser( + 'read', + `global-styles/themes/${ currentTheme.stylesheet }` + ); - return __experimentalGetCurrentThemeBaseGlobalStyles(); + return canUserReadBaseGlobalStyles + ? __experimentalGetCurrentThemeBaseGlobalStyles() + : undefined; }, [] ); return [ !! baseConfig, baseConfig ]; From 24d51e8c98bbd4bf361ac8576c01a54127baa1e1 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 27 Aug 2024 17:06:00 +0800 Subject: [PATCH 4/6] Check there is a current theme stylesheet before trying to access base global styles --- .../editor/src/components/global-styles-provider/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/editor/src/components/global-styles-provider/index.js b/packages/editor/src/components/global-styles-provider/index.js index deb070072a620c..5f3688cde8dccb 100644 --- a/packages/editor/src/components/global-styles-provider/index.js +++ b/packages/editor/src/components/global-styles-provider/index.js @@ -158,6 +158,10 @@ function useGlobalStylesBaseConfig() { } const currentTheme = getCurrentTheme(); + if ( ! currentTheme?.stylesheet ) { + return; + } + const canUserReadBaseGlobalStyles = canUser( 'read', `global-styles/themes/${ currentTheme.stylesheet }` From 4baa3909ba243bf17f7ae236f2558f29ccca2db3 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 27 Aug 2024 17:35:39 +0800 Subject: [PATCH 5/6] Revert "Add working permissions checks for global styles base config" --- .../global-styles-provider/index.js | 26 +++---------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/packages/editor/src/components/global-styles-provider/index.js b/packages/editor/src/components/global-styles-provider/index.js index 5f3688cde8dccb..93dd4dbe1eda49 100644 --- a/packages/editor/src/components/global-styles-provider/index.js +++ b/packages/editor/src/components/global-styles-provider/index.js @@ -146,30 +146,10 @@ function useGlobalStylesUserConfig() { function useGlobalStylesBaseConfig() { const baseConfig = useSelect( ( select ) => { - const { - __experimentalGetCurrentThemeBaseGlobalStyles, - getCurrentTheme, - canUser, - } = select( coreStore ); - - const canReadActiveTheme = canUser( 'read', 'themes?status=active' ); - if ( ! canReadActiveTheme ) { - return; - } - - const currentTheme = getCurrentTheme(); - if ( ! currentTheme?.stylesheet ) { - return; - } - - const canUserReadBaseGlobalStyles = canUser( - 'read', - `global-styles/themes/${ currentTheme.stylesheet }` - ); + const { __experimentalGetCurrentThemeBaseGlobalStyles } = + select( coreStore ); - return canUserReadBaseGlobalStyles - ? __experimentalGetCurrentThemeBaseGlobalStyles() - : undefined; + return __experimentalGetCurrentThemeBaseGlobalStyles(); }, [] ); return [ !! baseConfig, baseConfig ]; From a1b00fc5b92a43f3857f71f4468bb06d97e905b3 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:48:39 +0700 Subject: [PATCH 6/6] Remove placeholder code --- packages/core-data/src/entities.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 0f7f7fad31de42..c8af0dc94348d4 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -190,17 +190,6 @@ export const rootEntitiesConfig = [ revisionId ? '/' + revisionId : '' }`, supportsPagination: true, - getPath: ( path, query, baseURL, id ) => { - const context = - query.context || - ( query.operation === 'read' ? 'view' : 'edit' ); - const contextQuery = context ? `?context=${ context }` : ''; - return `${ baseURL }${ id ? '/' + id : '' }${ contextQuery }`; - }, - capabilities: { - read: 'edit_posts', - update: 'edit_theme_options', - }, }, { label: __( 'Themes' ),