From 5f443d5a455cc94704d2779cfbe85e587f0db08c Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:41:06 +1000 Subject: [PATCH] Section Styles: Prevent flash of variation styles in post editor (#63071) Co-authored-by: aaronrobertshaw Co-authored-by: talldan Co-authored-by: ellatrix Co-authored-by: bgardner Co-authored-by: justintadlock --- backport-changelog/6.6/6959.md | 4 ++++ lib/compat/wordpress-6.6/rest-api.php | 17 +++++++++++++++++ packages/core-data/src/resolvers.js | 18 +++++++++++------- 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 backport-changelog/6.6/6959.md diff --git a/backport-changelog/6.6/6959.md b/backport-changelog/6.6/6959.md new file mode 100644 index 00000000000000..3232414bc22b11 --- /dev/null +++ b/backport-changelog/6.6/6959.md @@ -0,0 +1,4 @@ +https://github.com/WordPress/wordpress-develop/pull/6959 + +* https://github.com/WordPress/gutenberg/pull/63071 + diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 54796685f45ab8..19e31f20b7d9fc 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -87,3 +87,20 @@ function gutenberg_register_global_styles_revisions_endpoints() { } add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); + +/** + * 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_6( $paths, $context ) { + if ( 'core/edit-post' === $context->name ) { + $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::get_user_global_styles_post_id() . '?context=edit'; + } + return $paths; +} +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10, 2 ); diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 167b8ac052fe45..4ab446e6ec3233 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -543,13 +543,17 @@ export const __experimentalGetCurrentGlobalStylesId = const globalStylesURL = activeThemes?.[ 0 ]?._links?.[ 'wp:user-global-styles' ]?.[ 0 ] ?.href; - if ( globalStylesURL ) { - const globalStylesObject = await apiFetch( { - url: globalStylesURL, - } ); - dispatch.__experimentalReceiveCurrentGlobalStylesId( - globalStylesObject.id - ); + if ( ! globalStylesURL ) { + return; + } + + // Regex matches the ID at the end of a URL or immediately before + // the query string. + const matches = globalStylesURL.match( /\/(\d+)(?:\?|$)/ ); + const id = matches ? Number( matches[ 1 ] ) : null; + + if ( id ) { + dispatch.__experimentalReceiveCurrentGlobalStylesId( id ); } };