diff --git a/backport-changelog/6.7/7661.md b/backport-changelog/6.7/7661.md new file mode 100644 index 00000000000000..2170f50c2a4e86 --- /dev/null +++ b/backport-changelog/6.7/7661.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/7661 + +* https://github.com/WordPress/gutenberg/pull/66468 diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index 313367594caae0..89efdc4c4219d2 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -50,6 +50,25 @@ function gutenberg_block_editor_preload_paths_6_7( $paths, $context ) { } } + // Preload theme and global styles paths. + $excluded_paths = array(); + if ( 'core/edit-site' === $context->name || 'core/edit-post' === $context->name ) { + $active_theme = get_stylesheet(); + $global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id(); + $paths[] = '/wp/v2/global-styles/themes/' . $active_theme . '?context=view'; + $paths[] = '/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view'; + $paths[] = array( '/wp/v2/global-styles/' . $global_styles_id, 'OPTIONS' ); + + // Remove duplicate or unnecessary global styles paths. + $excluded_paths[] = '/wp/v2/global-styles/themes/' . $active_theme; + $excluded_paths[] = '/wp/v2/global-styles/' . $global_styles_id; + foreach ( $paths as $key => $path ) { + if ( in_array( $path, $excluded_paths, true ) ) { + unset( $paths[ $key ] ); + } + } + } + return $paths; } add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_7', 10, 2 ); diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 91a73a0b3511c2..a5c33e18efbc60 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -644,6 +644,7 @@ export const __experimentalGetCurrentThemeBaseGlobalStyles = () => async ( { resolveSelect, dispatch } ) => { const currentTheme = await resolveSelect.getCurrentTheme(); + // Please adjust the preloaded requests if this changes! const themeGlobalStyles = await apiFetch( { path: `/wp/v2/global-styles/themes/${ currentTheme.stylesheet }?context=view`, } ); @@ -657,6 +658,7 @@ export const __experimentalGetCurrentThemeGlobalStylesVariations = () => async ( { resolveSelect, dispatch } ) => { const currentTheme = await resolveSelect.getCurrentTheme(); + // Please adjust the preloaded requests if this changes! const variations = await apiFetch( { path: `/wp/v2/global-styles/themes/${ currentTheme.stylesheet }/variations?context=view`, } ); diff --git a/packages/editor/src/components/global-styles-provider/index.js b/packages/editor/src/components/global-styles-provider/index.js index 6f2d8177056cb1..3f3a3389801eb6 100644 --- a/packages/editor/src/components/global-styles-provider/index.js +++ b/packages/editor/src/components/global-styles-provider/index.js @@ -56,13 +56,27 @@ function useGlobalStylesUserConfig() { select( coreStore ).__experimentalGetCurrentGlobalStylesId(); let record; - const userCanEditGlobalStyles = canUser( 'update', { - kind: 'root', - name: 'globalStyles', - id: _globalStylesId, - } ); - if ( _globalStylesId ) { + // We want the global styles ID request to finish before triggering + // the OPTIONS request for user capabilities, otherwise it will + // fetch `/wp/v2/global-styles` instead of + // `/wp/v2/global-styles/{id}`! + // Please adjust the preloaded requests if this changes! + const userCanEditGlobalStyles = _globalStylesId + ? canUser( 'update', { + kind: 'root', + name: 'globalStyles', + id: _globalStylesId, + } ) + : null; + + if ( + _globalStylesId && + // We want the OPTIONS request for user capabilities to finish + // before getting the records, otherwise we'll fetch both! + typeof userCanEditGlobalStyles === 'boolean' + ) { + // Please adjust the preloaded requests if this changes! if ( userCanEditGlobalStyles ) { record = getEditedEntityRecord( 'root',