From 58aa39fa516cc8a1a65d8f5bef56973e3f98f3ee Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 29 Oct 2024 11:40:47 +1100 Subject: [PATCH] Change context depending on user caps to prevent clientside request in post editor. Sync with https://github.com/WordPress/gutenberg/pull/66543 Update comment --- lib/compat/wordpress-6.6/rest-api.php | 17 +++++++++ lib/compat/wordpress-6.7/rest-api.php | 2 +- lib/compat/wordpress-6.8/preload.php | 0 lib/compat/wordpress-6.8/rest-api.php | 52 +++++++++++++++++++++++++++ lib/load.php | 1 + 5 files changed, 71 insertions(+), 1 deletion(-) delete mode 100644 lib/compat/wordpress-6.8/preload.php create mode 100644 lib/compat/wordpress-6.8/rest-api.php diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 706c4dd67306fd..eadd3b1d376a72 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -197,3 +197,20 @@ function gutenberg_register_wp_rest_post_types_controller_fields() { ); } add_action( 'rest_api_init', 'gutenberg_register_wp_rest_post_types_controller_fields' ); + +/** + * 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_Gutenberg::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/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index 89efdc4c4219d2..05b237ec8a7e1b 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -51,9 +51,9 @@ 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(); + $excluded_paths = array(); $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'; diff --git a/lib/compat/wordpress-6.8/preload.php b/lib/compat/wordpress-6.8/preload.php deleted file mode 100644 index e69de29bb2d1d6..00000000000000 diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php new file mode 100644 index 00000000000000..88701e3327df75 --- /dev/null +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -0,0 +1,52 @@ +name || 'core/edit-post' === $context->name ) { + $excluded_paths = array(); + $global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id(); + + /* + * Removes any edit or view context paths originating from Core, + * or elsewhere, e.g., gutenberg_block_editor_preload_paths_6_6(). + * Aside from not preloading unnecessary contexts, it also ensures there no duplicates, + * leading to a small optimization: block_editor_rest_api_preload() does not dedupe, + * and will fire off a WP_REST_Request for every path. In the case of + * `/wp/v2/global-styles/*` this will create a new WP_Theme_JSON() instance. + */ + $excluded_paths[] = '/wp/v2/global-styles/' . $global_styles_id . '?context=view'; + // Removes any edit context path originating from gutenberg_block_editor_preload_paths_6_6(). + $excluded_paths[] = '/wp/v2/global-styles/' . $global_styles_id . '?context=edit'; + foreach ( $paths as $key => $path ) { + if ( in_array( $path, $excluded_paths, true ) ) { + unset( $paths[ $key ] ); + } + } + + /* + * Now add the global styles path with the correct context based on user caps. + */ + $context = current_user_can( 'edit_theme_options' ) ? 'edit' : 'view'; + $paths[] = "/wp/v2/global-styles/$global_styles_id?context=$context"; + } + + return $paths; +} +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_8', 10, 2 ); diff --git a/lib/load.php b/lib/load.php index 2c8a0fd0347c92..dddb4ddc15466a 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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/rest-api.php'; require __DIR__ . '/compat/wordpress-6.8/block-comments.php'; require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php';