From 8f0e60c0a552cecd4c978085bec29f617bf2c8d0 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 2 Mar 2023 12:16:57 +0000 Subject: [PATCH 01/12] Scaffold out preloading --- .../navigation-block-preloading.php | 48 +++++++++++++++++++ lib/load.php | 1 + 2 files changed, 49 insertions(+) create mode 100644 lib/compat/wordpress-6.3/navigation-block-preloading.php diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php new file mode 100644 index 00000000000000..a1837826ebfdf9 --- /dev/null +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -0,0 +1,48 @@ + 'edit', + 'per_page' => '-1', + 'status' => array( 'publish, draft' ), + ), + + ); + + // add /wp/v2/navigation?_locale=user&context=edit&per_page=100&status%5B0%5D=publish&status%5B1%5D=draft to preload paths array + $preload_paths[] = array( + rest_get_route_for_post_type_items( + 'wp_navigation', + ), + array( + '_locale' => 'user', + 'context' => 'edit', + 'per_page' => '100', + 'status' => array( 'publish, draft' ), + ), + + ); + + echo '
';
+	var_dump( $preload_paths );
+	echo '
'; + + return $preload_paths; +} +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_permissions', 10, 2 ); diff --git a/lib/load.php b/lib/load.php index 79b076251e54ad..37dc2305678940 100644 --- a/lib/load.php +++ b/lib/load.php @@ -49,6 +49,7 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php'; require_once __DIR__ . '/compat/wordpress-6.3/rest-api.php'; require_once __DIR__ . '/compat/wordpress-6.3/theme-previews.php'; + require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php'; // Experimental. if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) { From 17a1ae7b94b7bf0b0a77ba52ac4599ed8bd5a938 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 8 Mar 2023 09:31:56 +0000 Subject: [PATCH 02/12] All GET and OPTIONS preloading --- .../navigation-block-preloading.php | 37 ++++--------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index a1837826ebfdf9..bef255b1d0247e 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -9,40 +9,19 @@ */ function gutenberg_preload_navigation_permissions( $preload_paths, $context ) { - $preload_paths[] = array( rest_get_route_for_post_type_items( 'wp_navigation' ), 'OPTIONS' ); - - $preload_paths[] = rest_get_route_for_post_type_items( 'wp_navigation' ); - // add /wp/v2/navigation?context=edit&per_page=-1&status%5B0%5D=publish&status%5B1%5D=draft to preload paths array - $preload_paths[] = array( - rest_get_route_for_post_type_items( - 'wp_navigation', - ), - array( - 'context' => 'edit', - 'per_page' => '-1', - 'status' => array( 'publish, draft' ), - ), - + $navigation_rest_route = rest_get_route_for_post_type_items( + 'wp_navigation' ); - // add /wp/v2/navigation?_locale=user&context=edit&per_page=100&status%5B0%5D=publish&status%5B1%5D=draft to preload paths array - $preload_paths[] = array( - rest_get_route_for_post_type_items( - 'wp_navigation', - ), - array( - '_locale' => 'user', - 'context' => 'edit', - 'per_page' => '100', - 'status' => array( 'publish, draft' ), - ), + // Preload the OPTIONS request for all Navigation posts request. + $preload_paths[] = array( $navigation_rest_route, 'OPTIONS' ); + // Preload the GET request for all Navigation posts request. + $preload_paths[] = array( + $navigation_rest_route . '?context=edit&per_page=100&status[0]=publish&status[1]=draft&_locale=user', + 'GET', ); - echo '
';
-	var_dump( $preload_paths );
-	echo '
'; - return $preload_paths; } add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_permissions', 10, 2 ); From 91d7f847845d80bb04883e995fe2212182b11dca Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 8 Mar 2023 09:43:09 +0000 Subject: [PATCH 03/12] Make preload paths more readable via add_query_arg --- .../navigation-block-preloading.php | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index bef255b1d0247e..8510fb75d4ad08 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -16,9 +16,32 @@ function gutenberg_preload_navigation_permissions( $preload_paths, $context ) { // Preload the OPTIONS request for all Navigation posts request. $preload_paths[] = array( $navigation_rest_route, 'OPTIONS' ); - // Preload the GET request for all Navigation posts request. + // Preload the GET request for all 'published' or 'draft' Navigation posts. $preload_paths[] = array( - $navigation_rest_route . '?context=edit&per_page=100&status[0]=publish&status[1]=draft&_locale=user', + add_query_arg( + array( + 'context' => 'edit', + 'per_page' => '100', + 'status[0]' => 'publish', + 'status[1]' => 'draft', + '_locale' => 'user', + ), + $navigation_rest_route + ), + 'GET', + ); + + // Preload the GET request for 'published' Navigation posts only. + $preload_paths[] = array( + add_query_arg( + array( + 'context' => 'edit', + 'per_page' => '100', + 'status' => 'publish', + '_locale' => 'user', + ), + $navigation_rest_route + ), 'GET', ); From aa9555ce7522bc726db276f9557b5d74a8413609 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 8 Mar 2023 09:44:42 +0000 Subject: [PATCH 04/12] Add comment to unusual usage --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 8510fb75d4ad08..aacd339ec333cd 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -22,9 +22,10 @@ function gutenberg_preload_navigation_permissions( $preload_paths, $context ) { array( 'context' => 'edit', 'per_page' => '100', + '_locale' => 'user', + // array indices are required to avoid query being encoded and not matching in cache. 'status[0]' => 'publish', 'status[1]' => 'draft', - '_locale' => 'user', ), $navigation_rest_route ), From 835382448558ac1eb5adc32c76e5d9da3fc923f4 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 8 Mar 2023 11:57:41 +0000 Subject: [PATCH 05/12] Resolve PHPCS --- .../wordpress-6.3/navigation-block-preloading.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index aacd339ec333cd..78a39a0091bc32 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -1,13 +1,18 @@ Date: Wed, 8 Mar 2023 11:58:00 +0000 Subject: [PATCH 06/12] Fix formatting --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 78a39a0091bc32..f02491bddbedae 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -9,7 +9,7 @@ /** * Preloads requests needed for Navigation posts * - * @param array $preload_paths Preload paths to be filtered. + * @param array $preload_paths Preload paths to be filtered. * @return array */ function gutenberg_preload_navigation_permissions( $preload_paths ) { From b188a195bb8fc17c1c6960f3b4541a73e795482d Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 8 Mar 2023 13:03:03 +0000 Subject: [PATCH 07/12] =?UTF-8?q?Rename=20away=20from=20=E2=80=9Cpermissio?= =?UTF-8?q?ns=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index f02491bddbedae..9f8102ea7fd9b0 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -12,7 +12,7 @@ * @param array $preload_paths Preload paths to be filtered. * @return array */ -function gutenberg_preload_navigation_permissions( $preload_paths ) { +function gutenberg_preload_navigation_posts( $preload_paths ) { $navigation_rest_route = rest_get_route_for_post_type_items( 'wp_navigation' @@ -53,4 +53,4 @@ function gutenberg_preload_navigation_permissions( $preload_paths ) { return $preload_paths; } -add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_permissions', 10, 2 ); +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_posts', 10, 2 ); From c28dcb25a29c44aad4e9808822df266699d3ccad Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 8 Mar 2023 13:07:25 +0000 Subject: [PATCH 08/12] Limit to Site Editor --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 9f8102ea7fd9b0..5df3249fcfb2d1 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -12,7 +12,12 @@ * @param array $preload_paths Preload paths to be filtered. * @return array */ -function gutenberg_preload_navigation_posts( $preload_paths ) { +function gutenberg_preload_navigation_posts( $preload_paths, $context ) { + + // Limit to the Site Editor. + if ( ! empty( $context->name ) && 'core/edit-site' !== $context->name ) { + return $preload_paths; + } $navigation_rest_route = rest_get_route_for_post_type_items( 'wp_navigation' From 33ea7e66747da0d132d1d940169e54aac70a52ef Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 11 May 2023 10:48:55 +0100 Subject: [PATCH 09/12] Add context to doc block --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 5df3249fcfb2d1..df61fffd9aed42 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -9,7 +9,8 @@ /** * Preloads requests needed for Navigation posts * - * @param array $preload_paths Preload paths to be filtered. + * @param array $preload_paths Preload paths to be filtered. + * @param WP_Block_Editor_Context $context The current block editor context. * @return array */ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { From 4e58c8bc362382de3e353bfe2c4517e879e51917 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 11 May 2023 11:02:52 +0100 Subject: [PATCH 10/12] Preload Browse Mode sidebar Navigation --- .../navigation-block-preloading.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index df61fffd9aed42..e9c4b0023f678b 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -50,13 +50,28 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { 'context' => 'edit', 'per_page' => '100', 'status' => 'publish', - '_locale' => 'user', ), $navigation_rest_route ), 'GET', ); + // Preload request for Browse Mode sidebar. + $preload_paths[] = array( + add_query_arg( + array( + 'context' => 'edit', + 'per_page' => '1', + 'status' => 'publish', + 'order' => 'desc', + 'orderby' => 'date', + ), + $navigation_rest_route + ), + 'GET', + ); + + return $preload_paths; } add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_posts', 10, 2 ); From 615ec2e42c2b0e466933a500bff4117d9b9d903f Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 11 May 2023 11:07:34 +0100 Subject: [PATCH 11/12] Remove redundant preload --- .../navigation-block-preloading.php | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index e9c4b0023f678b..99f7581aec41ed 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -27,7 +27,7 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { // Preload the OPTIONS request for all Navigation posts request. $preload_paths[] = array( $navigation_rest_route, 'OPTIONS' ); - // Preload the GET request for all 'published' or 'draft' Navigation posts. + // Preload the GET request for ALL 'published' or 'draft' Navigation posts. $preload_paths[] = array( add_query_arg( array( @@ -43,20 +43,7 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { 'GET', ); - // Preload the GET request for 'published' Navigation posts only. - $preload_paths[] = array( - add_query_arg( - array( - 'context' => 'edit', - 'per_page' => '100', - 'status' => 'publish', - ), - $navigation_rest_route - ), - 'GET', - ); - - // Preload request for Browse Mode sidebar. + // Preload request for Browse Mode sidebar "Navigation" section. $preload_paths[] = array( add_query_arg( array( From f2ab9dc0006de772e608cbf29b975b835ee68142 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 11 May 2023 11:21:00 +0100 Subject: [PATCH 12/12] Use int not string for numeric --- lib/compat/wordpress-6.3/navigation-block-preloading.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php index 99f7581aec41ed..82fe81b236a590 100644 --- a/lib/compat/wordpress-6.3/navigation-block-preloading.php +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -32,7 +32,7 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { add_query_arg( array( 'context' => 'edit', - 'per_page' => '100', + 'per_page' => 100, '_locale' => 'user', // array indices are required to avoid query being encoded and not matching in cache. 'status[0]' => 'publish', @@ -48,7 +48,7 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { add_query_arg( array( 'context' => 'edit', - 'per_page' => '1', + 'per_page' => 1, 'status' => 'publish', 'order' => 'desc', 'orderby' => 'date', @@ -58,7 +58,6 @@ function gutenberg_preload_navigation_posts( $preload_paths, $context ) { 'GET', ); - return $preload_paths; } add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_posts', 10, 2 );