From c27831121ac4bce4e3af40c27a793d3697b9b884 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 10 Feb 2025 03:54:35 +0000 Subject: [PATCH] Editor: Preload Global Styles REST requests based on user permissions. For the Post Editor, preload '/wp/v2/global-styles/' . $global_styles_id with a context corresponding to user caps, that is, 'edit' for users that can edit global styles, and 'view' for everyone else. Preloading the global styles endpoint according to role context means that admins and non admins, e.g., editors, avoid unnecessary client side requests. Props ramonopoly. Fixes #62322. git-svn-id: https://develop.svn.wordpress.org/trunk@59795 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/edit-form-blocks.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index 5dbb9f33e4..bd66ac60ba 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -51,8 +51,8 @@ wp_enqueue_script( 'wp-edit-post' ); $rest_path = rest_get_route_for_post( $post ); -$active_theme = get_stylesheet(); - +$active_theme = get_stylesheet(); +$global_styles_endpoint_context = current_user_can( 'edit_theme_options' ) ? 'edit' : 'view'; // Preload common data. $preload_paths = array( '/wp/v2/types?context=view', @@ -71,7 +71,14 @@ $preload_paths = array( '/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view', '/wp/v2/themes?context=edit&status=active', array( '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id(), 'OPTIONS' ), - '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id() . '?context=edit', + /* + * Preload the global styles path with the correct context based on user caps. + * NOTE: There is an equivalent conditional check in the client-side code to fetch + * the global styles entity using the appropriate context value. + * See the call to `canUser()`, under `useGlobalStylesUserConfig()` in `packages/edit-site/src/components/use-global-styles-user-config/index.js`. + * Please ensure that the equivalent check is kept in sync with this preload path. + */ + '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id() . '?context=' . $global_styles_endpoint_context, ); block_editor_rest_api_preload( $preload_paths, $block_editor_context );