Skip to content

Commit

Permalink
Section Styles: Prevent flash of variation styles in post editor (Wor…
Browse files Browse the repository at this point in the history
…dPress#63071)

Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: talldan <[email protected]>
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: bgardner <[email protected]>
Co-authored-by: justintadlock <[email protected]>
  • Loading branch information
6 people authored and carstingaxion committed Jul 18, 2024
1 parent 11fc621 commit b4ceb84
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions backport-changelog/6.6/6959.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/6959

* https://github.com/WordPress/gutenberg/pull/63071

17 changes: 17 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,20 @@ function gutenberg_register_wp_rest_themes_template_directory_uri_field() {
}
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_template_directory_uri_field' );

/**
* 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 );
18 changes: 11 additions & 7 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
};

Expand Down

0 comments on commit b4ceb84

Please sign in to comment.