Skip to content

Commit

Permalink
Preload: fix multiple regressions around global styles (WordPress#66468)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
  • Loading branch information
4 people authored and karthick-murugan committed Nov 13, 2024
1 parent 3975cff commit d3606ad
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/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
29 changes: 29 additions & 0 deletions lib/compat/wordpress-6.8/preload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Preload theme and global styles paths to avoid flash of variation styles in
* post editor.
*
* @param array $paths REST API paths to preload.
* @param WP_Block_Editor_Context $context Current block editor context.
* @return array Filtered preload paths.
*/
function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {
$excluded_paths = array();
if ( 'core/edit-site' === $context->name || 'core/edit-post' === $context->name ) {
$stylesheet = get_stylesheet();
$global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id();
$paths[] = '/wp/v2/global-styles/themes/' . $stylesheet . '?context=view';
$paths[] = '/wp/v2/global-styles/themes/' . $stylesheet . '/variations?context=view';
$paths[] = array( '/wp/v2/global-styles/' . $global_styles_id, 'OPTIONS' );
$excluded_paths[] = '/wp/v2/global-styles/themes/' . $stylesheet;
$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_8', 10, 2 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.7/rest-api.php';

// WordPress 6.8 compat.
require __DIR__ . '/compat/wordpress-6.8/preload.php';
require __DIR__ . '/compat/wordpress-6.8/remove-default-css.php';
require __DIR__ . '/compat/wordpress-6.8/block-comments.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php';
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 d3606ad

Please sign in to comment.