Skip to content

Commit

Permalink
Cherry Pick to 6.7: Preload: fix multiple regressions around global s…
Browse files Browse the repository at this point in the history
…tyles (#66468) (#66542)

* Sync with #66543
* Move backport log to 6.7

---------

Co-authored-by: ellatrix <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
5 people authored Oct 29, 2024
1 parent 88c51c7 commit cf008a0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.7/7661.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7661

* https://github.com/WordPress/gutenberg/pull/66468
19 changes: 19 additions & 0 deletions lib/compat/wordpress-6.7/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
} );
Expand All @@ -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`,
} );
Expand Down
26 changes: 20 additions & 6 deletions packages/editor/src/components/global-styles-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit cf008a0

Please sign in to comment.