Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload: fix multiple regressions around global styles #66468

Merged
merged 10 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`!
Comment on lines +60 to +63
Copy link
Member

@ramonjd ramonjd Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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}`!
/*
* 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}`.
*/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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
Loading