From 72501257f5b405f6eb8c9193d094ed371dc256b2 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 1 Oct 2021 20:16:34 +0200 Subject: [PATCH 01/42] Add preloading for /__experimental/menus endpoint. --- lib/navigation-page.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 568fca8cb3bbfa..a1e8d40895dfeb 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -36,6 +36,7 @@ function gutenberg_navigation_init( $hook ) { '/__experimental/menu-locations', array( '/wp/v2/pages', 'OPTIONS' ), array( '/wp/v2/posts', 'OPTIONS' ), + '/__experimental/menus?per_page=100&context=edit&_locale=user', ); $settings = array_merge( From 8c2df908e96cded81c2f27c3beb9a09542545841 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 1 Oct 2021 22:15:56 +0200 Subject: [PATCH 02/42] Add a new filter to allow to modify the data that has already been preloaded. --- lib/editor-settings.php | 12 ++++++++++-- lib/navigation-page.php | 1 - 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 7e09cd52e6e228..08df53889a5210 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -70,16 +70,24 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett */ $preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] ); - $preload_data = array_reduce( + $preloaded_data = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); + + /** + * Allows to change the preloaded data. + * + * @param string[] $preload_paths Array with the preloaded data. + */ + $preloaded_data = apply_filters( "{$editor_name}_preloaded_data", $preloaded_data ); + wp_add_inline_script( 'wp-api-fetch', sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', - wp_json_encode( $preload_data ) + wp_json_encode( $preloaded_data ) ), 'after' ); diff --git a/lib/navigation-page.php b/lib/navigation-page.php index a1e8d40895dfeb..568fca8cb3bbfa 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -36,7 +36,6 @@ function gutenberg_navigation_init( $hook ) { '/__experimental/menu-locations', array( '/wp/v2/pages', 'OPTIONS' ), array( '/wp/v2/posts', 'OPTIONS' ), - '/__experimental/menus?per_page=100&context=edit&_locale=user', ); $settings = array_merge( From eb69f2d69623ea29093a8d61e2b287f931497b38 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 1 Oct 2021 23:13:05 +0200 Subject: [PATCH 03/42] Commit WIP. --- lib/editor-settings.php | 2 +- lib/navigation-page.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 08df53889a5210..21301711bc7381 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -77,7 +77,7 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett ); /** - * Allows to change the preloaded data. + * Allows to modify the preloaded data. * * @param string[] $preload_paths Array with the preloaded data. */ diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 568fca8cb3bbfa..44c16309b37764 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -20,6 +20,16 @@ class="edit-navigation" 100, + 'context' => 'edit', + '_locale' => 'user', + + )); +} + /** * Initialize the Gutenberg Navigation page. * @@ -36,6 +46,7 @@ function gutenberg_navigation_init( $hook ) { '/__experimental/menu-locations', array( '/wp/v2/pages', 'OPTIONS' ), array( '/wp/v2/posts', 'OPTIONS' ), + gutenberg_navigation_get_menus_endpoint_url(), ); $settings = array_merge( @@ -82,3 +93,9 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b } add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' ); + +function gutenberg_navigation_editor_preload_menus_data($preloaded_data) +{ + +} +add_filter('navigation_editor_preloaded_data', 'gutenberg_navigation_editor_preload_menus_data'); From 482eb34eca41682fe903298814908279f14c7b9c Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 4 Oct 2021 13:03:38 +0200 Subject: [PATCH 04/42] Preload more endpoints. --- lib/navigation-page.php | 60 +++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 44c16309b37764..346dee7f82625d 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -10,45 +10,69 @@ * * @since 7.8.0 */ -function gutenberg_navigation_page() { - ?> +function gutenberg_navigation_page() { ?> 100, - 'context' => 'edit', - '_locale' => 'user', +function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { + return '/__experimental/menus?' . http_build_query( + array( + 'per_page' => $results_per_page, + 'context' => 'edit', + '_locale' => 'user', + ) + ); +} - )); +function gutenberg_navigation_get_menu_endpoint( $menu_id ) { + return "/__experimental/menus/{$menu_id}?" . http_build_query( + array( + 'context' => 'edit', + ) + ); +} + +function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_page = 100 ) { + return "/__experimental/menu-items?{$menu_id}&" . http_build_query( + array( + 'per_page' => $results_per_page, + 'context' => 'edit', + '_locale' => 'user', + ) + ); } /** * Initialize the Gutenberg Navigation page. * - * @since 7.8.0 - * * @param string $hook Page. + * @since 7.8.0 */ function gutenberg_navigation_init( $hook ) { if ( 'gutenberg_page_gutenberg-navigation' !== $hook ) { - return; + return; } + $menus = wp_get_nav_menus(); + $first_menu_id = count( $menus ) ? $menus[0]->term_id : null; + $preload_paths = array( '/__experimental/menu-locations', array( '/wp/v2/pages', 'OPTIONS' ), array( '/wp/v2/posts', 'OPTIONS' ), - gutenberg_navigation_get_menus_endpoint_url(), + gutenberg_navigation_get_menus_endpoint(), ); + if ( $first_menu_id ) { + $preload_paths[] = gutenberg_navigation_get_menu_endpoint( $first_menu_id ); + $preload_paths[] = gutenberg_navigation_get_menu_items_endpoint( $first_menu_id ); + } + $settings = array_merge( gutenberg_get_default_block_editor_settings(), array( @@ -76,6 +100,7 @@ function gutenberg_navigation_init( $hook ) { wp_enqueue_style( 'wp-format-library' ); do_action( 'enqueue_block_editor_assets' ); } + add_action( 'admin_enqueue_scripts', 'gutenberg_navigation_init' ); /** @@ -94,8 +119,3 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' ); -function gutenberg_navigation_editor_preload_menus_data($preloaded_data) -{ - -} -add_filter('navigation_editor_preloaded_data', 'gutenberg_navigation_editor_preload_menus_data'); From d06b9e033779c28114c6a78aca33d7ffc2125e47 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 4 Oct 2021 13:08:25 +0200 Subject: [PATCH 05/42] Fix preload paths. --- lib/navigation-page.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 346dee7f82625d..01417afead304a 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -38,10 +38,11 @@ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { } function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_page = 100 ) { - return "/__experimental/menu-items?{$menu_id}&" . http_build_query( + return '/__experimental/menu-items?' . http_build_query( array( - 'per_page' => $results_per_page, 'context' => 'edit', + 'menus' => $menu_id, + 'per_page' => $results_per_page, '_locale' => 'user', ) ); From b42deac341b0eafc76ec4254296f3097219eb577 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 4 Oct 2021 18:10:28 +0200 Subject: [PATCH 06/42] Don't add slash if the key is empty. Otherwise we will get urls like /wp/v2/types/?context=edit (trailing slash). --- packages/core-data/src/resolvers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 92eff202ce743e..7ff03976a8b4b0 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -85,7 +85,7 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( { // for how the request is made to the REST API. // eslint-disable-next-line @wordpress/no-unused-vars-before-return - const path = addQueryArgs( entity.baseURL + '/' + key, { + const path = addQueryArgs( entity.baseURL + ( key ? '/' + key : '' ), { ...entity.baseURLParams, ...query, } ); From 55a460d84c227332202e6dba67bc14d1ba27be83 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 4 Oct 2021 18:13:17 +0200 Subject: [PATCH 07/42] Preload /wp/v2/types URLs. --- lib/navigation-page.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 01417afead304a..cac82124db700f 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -48,6 +48,14 @@ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_pa ); } +function gutenberg_navigation_get_types_endpoint() { + return '/wp/v2/types?' . http_build_query( + array( + 'context' => 'edit', + ) + ); +} + /** * Initialize the Gutenberg Navigation page. * @@ -67,6 +75,7 @@ function gutenberg_navigation_init( $hook ) { array( '/wp/v2/pages', 'OPTIONS' ), array( '/wp/v2/posts', 'OPTIONS' ), gutenberg_navigation_get_menus_endpoint(), + gutenberg_navigation_get_types_endpoint(), ); if ( $first_menu_id ) { From 282e7e0a3c8155fb4568060dfc48dc8e4407caf2 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 00:14:26 +0200 Subject: [PATCH 08/42] Commit WIP. --- phpunit/navigation-page-test.php | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 phpunit/navigation-page-test.php diff --git a/phpunit/navigation-page-test.php b/phpunit/navigation-page-test.php new file mode 100644 index 00000000000000..e1ee3b7a35d988 --- /dev/null +++ b/phpunit/navigation-page-test.php @@ -0,0 +1,55 @@ +callback = $this->createMock( WP_Navigation_Page_Test_Callback::class ); + add_filter( 'navigation_editor_preload_paths', array( $this->callback, 'preload_paths_callback' ) ); + add_filter( 'wp_get_nav_menus', array( $this->callback, 'wp_nav_menus_callback' ) ); + } + + public function tearDown() { + parent::tearDown(); + remove_filter( 'navigation_editor_preload_paths', array( $this->callback, 'preload_paths_callback' ) ); + remove_filter( 'wp_get_nav_menus', array( $this->callback, 'wp_nav_menus_callback' ) ); + } + + function test_gutenberg_navigation_get_menus_endpoint() { + $expected_preload_paths = array( + '/__experimental/menu-locations', + array( + '/wp/v2/pages', + 'OPTIONS', + ), + array( + '/wp/v2/posts', + 'OPTIONS', + ), + '/__experimental/menus?per_page=100&context=edit&_locale=user', + '/wp/v2/types?context=edit', + ); + + $this->callback->expects( $this->once() ) + ->method( 'preload_paths_callback' ) + ->with( $expected_preload_paths ) + ->willReturn( array() ); + + $menu_id = mt_rand( 1, 1000 ); + $menu = new StdClass; + $menu->term_id = $menu_id; + $this->callback->expects( $this->once() ) + ->method( 'wp_nav_menus_callback' ) + ->willReturn( new WP_Term() ); + + set_current_screen( 'gutenberg_page_gutenberg-navigation' ); + gutenberg_navigation_init( 'gutenberg_page_gutenberg-navigation' ); + } +} + +class WP_Navigation_Page_Test_Callback { + + public function preload_paths_callback() {} + public function wp_nav_menus_callback() {} +} From 8b5537206d2e48215b068fd1a02f9d41c6c60f63 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 14:50:29 +0200 Subject: [PATCH 09/42] 1. Fix the test. 2. Add PHPDOC blocks. 3. Remove {$editor_name}_preloaded_data filter. We are not going to use it. --- lib/editor-settings.php | 12 ++---------- phpunit/navigation-page-test.php | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 21301711bc7381..7e09cd52e6e228 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -70,24 +70,16 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett */ $preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] ); - $preloaded_data = array_reduce( + $preload_data = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); - - /** - * Allows to modify the preloaded data. - * - * @param string[] $preload_paths Array with the preloaded data. - */ - $preloaded_data = apply_filters( "{$editor_name}_preloaded_data", $preloaded_data ); - wp_add_inline_script( 'wp-api-fetch', sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', - wp_json_encode( $preloaded_data ) + wp_json_encode( $preload_data ) ), 'after' ); diff --git a/phpunit/navigation-page-test.php b/phpunit/navigation-page-test.php index e1ee3b7a35d988..1ed5ef8ea05d43 100644 --- a/phpunit/navigation-page-test.php +++ b/phpunit/navigation-page-test.php @@ -1,6 +1,14 @@ callback, 'wp_nav_menus_callback' ) ); } - function test_gutenberg_navigation_get_menus_endpoint() { + function test_gutenberg_navigation_init_function_generates_correct_preload_paths() { + $menu_id = mt_rand( 1, 1000 ); $expected_preload_paths = array( '/__experimental/menu-locations', array( @@ -29,6 +38,8 @@ function test_gutenberg_navigation_get_menus_endpoint() { ), '/__experimental/menus?per_page=100&context=edit&_locale=user', '/wp/v2/types?context=edit', + "/__experimental/menus/{$menu_id}?context=edit", + "/__experimental/menu-items?context=edit&menus={$menu_id}&per_page=100&_locale=user", ); $this->callback->expects( $this->once() ) @@ -36,18 +47,21 @@ function test_gutenberg_navigation_get_menus_endpoint() { ->with( $expected_preload_paths ) ->willReturn( array() ); - $menu_id = mt_rand( 1, 1000 ); - $menu = new StdClass; + $menu = new stdClass(); $menu->term_id = $menu_id; $this->callback->expects( $this->once() ) ->method( 'wp_nav_menus_callback' ) - ->willReturn( new WP_Term() ); + ->with( array() ) + ->willReturn( array( new WP_Term( $menu ) ) ); set_current_screen( 'gutenberg_page_gutenberg-navigation' ); gutenberg_navigation_init( 'gutenberg_page_gutenberg-navigation' ); } } +/** + * This is a utility test class for creating mocks of the callback functions + */ class WP_Navigation_Page_Test_Callback { public function preload_paths_callback() {} From 630dc7a74f076cfc7e1f0c41d14f6b795107b26f Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 15:13:40 +0200 Subject: [PATCH 10/42] Fix indents. --- lib/navigation-page.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index cac82124db700f..c8c93006dc15b7 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -10,10 +10,11 @@ * * @since 7.8.0 */ -function gutenberg_navigation_page() { ?> +function gutenberg_navigation_page() { + ?> Date: Wed, 6 Oct 2021 16:01:16 +0200 Subject: [PATCH 11/42] Add PHPDOC blocks to the public functions introduced in this PR. --- lib/navigation-page.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index c8c93006dc15b7..14da9a20fb6fe7 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -20,6 +20,12 @@ class="edit-navigation" endpoint + * + * @param int $menu_id + * @return string + */ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { return "/__experimental/menus/{$menu_id}?" . http_build_query( array( @@ -38,6 +50,13 @@ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { ); } +/** + * This function returns an url for the /__experimental/menu-items endpoint + * + * @param int $menu_id + * @param int $results_per_page + * @return string + */ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_page = 100 ) { return '/__experimental/menu-items?' . http_build_query( array( @@ -49,6 +68,11 @@ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_pa ); } +/** + * This function returns an url for the /wp/v2/types endpoint + * + * @return string + */ function gutenberg_navigation_get_types_endpoint() { return '/wp/v2/types?' . http_build_query( array( From 7a85496d3594443748d7fee3baa51e306720821a Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 16:04:21 +0200 Subject: [PATCH 12/42] Add @since tags. --- lib/navigation-page.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 14da9a20fb6fe7..96b943087346b1 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -23,6 +23,8 @@ class="edit-navigation" /** * This function returns an url for the /__experimental/menus endpoint * + * @since 11.6.0 + * * @param int $results_per_page * @return string */ @@ -39,6 +41,8 @@ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { /** * This function returns an url for the /__experimental/menus/ endpoint * + * @since 11.6.0 + * * @param int $menu_id * @return string */ @@ -53,6 +57,8 @@ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { /** * This function returns an url for the /__experimental/menu-items endpoint * + * @since 11.6.0 + * * @param int $menu_id * @param int $results_per_page * @return string @@ -71,6 +77,8 @@ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_pa /** * This function returns an url for the /wp/v2/types endpoint * + * @since 11.6.0 + * * @return string */ function gutenberg_navigation_get_types_endpoint() { From 254c788649a2379ee0d42daa37840acf7a124676 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 17:58:24 +0200 Subject: [PATCH 13/42] OPTIONS type requests should be cached the same way as GET type requets. We must allow using cached data only once (to avoid using outdated data). --- packages/api-fetch/src/middlewares/preloading.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index a4658baefc8427..27453ba2a8538e 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -72,10 +72,14 @@ function createPreloadingMiddleware( preloadedData ) { cache[ method ] && cache[ method ][ path ] ) { + // Unsetting the cache key ensures that the data is only preloaded a single time + const cacheData = cache[ method ][ path ]; + delete cache[ method ][ path ]; + return Promise.resolve( parse - ? cache[ method ][ path ].body - : cache[ method ][ path ] + ? cacheData.body + : cacheData ); } } From c8b4eed3128dad7be7d89305977bd03cf04cfdeb Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 18:09:23 +0200 Subject: [PATCH 14/42] Fix the code style. --- packages/api-fetch/src/middlewares/preloading.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index 27453ba2a8538e..955299c8b931fd 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -76,11 +76,7 @@ function createPreloadingMiddleware( preloadedData ) { const cacheData = cache[ method ][ path ]; delete cache[ method ][ path ]; - return Promise.resolve( - parse - ? cacheData.body - : cacheData - ); + return Promise.resolve( parse ? cacheData.body : cacheData ); } } From 14e4d0fd5b371a50af9acc3514fff26e461b0830 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 18:20:58 +0200 Subject: [PATCH 15/42] Use WP function build_query instead of http_build_query. --- lib/navigation-page.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 96b943087346b1..7f9cb8e0c0dacc 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -29,7 +29,7 @@ class="edit-navigation" * @return string */ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { - return '/__experimental/menus?' . http_build_query( + return '/__experimental/menus?' . build_query( array( 'per_page' => $results_per_page, 'context' => 'edit', @@ -47,7 +47,7 @@ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { * @return string */ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { - return "/__experimental/menus/{$menu_id}?" . http_build_query( + return "/__experimental/menus/{$menu_id}?" . build_query( array( 'context' => 'edit', ) @@ -64,7 +64,7 @@ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { * @return string */ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_page = 100 ) { - return '/__experimental/menu-items?' . http_build_query( + return '/__experimental/menu-items?' . build_query( array( 'context' => 'edit', 'menus' => $menu_id, @@ -82,7 +82,7 @@ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_pa * @return string */ function gutenberg_navigation_get_types_endpoint() { - return '/wp/v2/types?' . http_build_query( + return '/wp/v2/types?' . build_query( array( 'context' => 'edit', ) From 6f526a2c29328acdce0c637dabdb2eec0ca3d6f6 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 6 Oct 2021 18:43:37 +0200 Subject: [PATCH 16/42] Fix code style. --- lib/navigation-page.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 7f9cb8e0c0dacc..7044cb91eb4e39 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -25,7 +25,7 @@ class="edit-navigation" * * @since 11.6.0 * - * @param int $results_per_page + * @param int $results_per_page Results per page. * @return string */ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { @@ -43,7 +43,7 @@ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { * * @since 11.6.0 * - * @param int $menu_id + * @param int $menu_id Menu ID. * @return string */ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { @@ -59,8 +59,8 @@ function gutenberg_navigation_get_menu_endpoint( $menu_id ) { * * @since 11.6.0 * - * @param int $menu_id - * @param int $results_per_page + * @param int $menu_id Menu ID. + * @param int $results_per_page Results per page. * @return string */ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_page = 100 ) { From 94b412e0f981465846dde3bf68e95b218b1a4621 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 7 Oct 2021 21:00:50 +0200 Subject: [PATCH 17/42] Format the code. --- packages/api-fetch/src/middlewares/preloading.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index 955299c8b931fd..32a1d00e9abf48 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -72,8 +72,9 @@ function createPreloadingMiddleware( preloadedData ) { cache[ method ] && cache[ method ][ path ] ) { - // Unsetting the cache key ensures that the data is only preloaded a single time const cacheData = cache[ method ][ path ]; + + // Unsetting the cache key ensures that the data is only preloaded a single time delete cache[ method ][ path ]; return Promise.resolve( parse ? cacheData.body : cacheData ); From f071d8d4248ad1a491b0fab9132cbba1be98db9b Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 8 Oct 2021 19:28:39 +0200 Subject: [PATCH 18/42] Add a new filter to modify the preloaded data. This can be handy in many situations when we need to change the preloaded data. --- lib/editor-settings.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 7e09cd52e6e228..5e757bc8302526 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -75,6 +75,14 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett 'rest_preload_api_request', array() ); + + /** + * Filters the array of data that has been preloaded + * + * @param Array $preload_data Array of the preloaded data + */ + $preload_data = apply_filters( "{$editor_name}_preload_data", $preload_data ); + wp_add_inline_script( 'wp-api-fetch', sprintf( From b206204ec8299f20b923ed3d8483c1717540bdd1 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 8 Oct 2021 22:14:55 +0200 Subject: [PATCH 19/42] Wire up createMenuPreloadingMiddleware so that we can preload menus data separately. --- lib/navigation-page.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 7044cb91eb4e39..c8381ced76b2d1 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -112,7 +112,6 @@ function gutenberg_navigation_init( $hook ) { ); if ( $first_menu_id ) { - $preload_paths[] = gutenberg_navigation_get_menu_endpoint( $first_menu_id ); $preload_paths[] = gutenberg_navigation_get_menu_items_endpoint( $first_menu_id ); } @@ -160,3 +159,25 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b } add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' ); + +function gutenberg_navigation_editor_preload_menus( $preload_data ) { + $menus_data_path = gutenberg_navigation_get_menus_endpoint(); + $menus_data = empty( $preload_data[ $menus_data_path ] ) ? array() : $preload_data[ $menus_data_path ]; + if ( ! $menus_data ) { + return $preload_data; + } + + wp_add_inline_script( + 'wp-edit-navigation', + sprintf( + 'wp.apiFetch.use( wp.editNavigation.createMenuPreloadingMiddleware( %s ) );', + wp_json_encode( $menus_data ) + ), + 'after' + ); + + unset( $preload_data[ $menus_data_path ] ); + return $preload_data; +} + +add_filter( 'navigation_editor_preload_data', 'gutenberg_navigation_editor_preload_menus' ); From 61c150b917b0f6a591065601b63b1d3bf4d7be36 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 8 Oct 2021 22:17:43 +0200 Subject: [PATCH 20/42] Fix PHPDOC blocks. --- lib/editor-settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 5e757bc8302526..0b791d31a8b6d7 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -77,9 +77,9 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett ); /** - * Filters the array of data that has been preloaded + * Filters the array of data that has been preloaded. * - * @param Array $preload_data Array of the preloaded data + * @param Array $preload_data Array containing the preloaded data. */ $preload_data = apply_filters( "{$editor_name}_preload_data", $preload_data ); From 436e7c7319dd8878336b5c4293798c9cbf7a679a Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 8 Oct 2021 22:30:54 +0200 Subject: [PATCH 21/42] Add PHPDOC block. --- lib/navigation-page.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index c8381ced76b2d1..bd90592c381301 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -160,6 +160,13 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' ); +/** + * This function removes menu-related data from the "common" preloading middleware and calls + * createMenuPreloadingMiddleware middleware because we need to use custom preloading logic for menus. + * + * @param $preload_data Array containing the preloaded data. + * @return array Filtered preload data. + */ function gutenberg_navigation_editor_preload_menus( $preload_data ) { $menus_data_path = gutenberg_navigation_get_menus_endpoint(); $menus_data = empty( $preload_data[ $menus_data_path ] ) ? array() : $preload_data[ $menus_data_path ]; From 535f4e307f3d01eb3c3eb4e4b1fb9484af0c670d Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 16:51:25 +0200 Subject: [PATCH 22/42] Implement menu preloading middleware. --- lib/navigation-page.php | 8 +- packages/edit-navigation/src/index.js | 2 + packages/edit-navigation/src/utils/index.js | 103 ++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 packages/edit-navigation/src/utils/index.js diff --git a/lib/navigation-page.php b/lib/navigation-page.php index bd90592c381301..0bc6de35009189 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -169,7 +169,13 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b */ function gutenberg_navigation_editor_preload_menus( $preload_data ) { $menus_data_path = gutenberg_navigation_get_menus_endpoint(); - $menus_data = empty( $preload_data[ $menus_data_path ] ) ? array() : $preload_data[ $menus_data_path ]; + $menus_data = empty( $preload_data[ $menus_data_path ] ) ? + array() + : + array( + $menus_data_path => $preload_data[ $menus_data_path ] + ); + if ( ! $menus_data ) { return $preload_data; } diff --git a/packages/edit-navigation/src/index.js b/packages/edit-navigation/src/index.js index fe3629e2829494..907dc8515935de 100644 --- a/packages/edit-navigation/src/index.js +++ b/packages/edit-navigation/src/index.js @@ -90,3 +90,5 @@ export function initialize( id, settings ) { document.getElementById( id ) ); } + +export { createMenuPreloadingMiddleware } from './utils'; diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js new file mode 100644 index 00000000000000..58fa4ae646b62e --- /dev/null +++ b/packages/edit-navigation/src/utils/index.js @@ -0,0 +1,103 @@ + +/** + * @param {Object} preloadedData + * @return {Function} Preloading middleware. + */ +export function createMenuPreloadingMiddleware( preloadedData ) { + const cache = Object.keys( preloadedData ).reduce( ( result, path ) => { + result[ getStablePath( path ) ] = preloadedData[ path ]; + return result; + }, /** @type {Record} */ ( {} ) ); + + return ( options, next ) => { + const { parse = true } = options; + if ( 'string' !== typeof options.path ) { + return next( options ); + } + + const method = options.method || 'GET'; + if ( 'GET' !== method ) { + return next( options ); + } + + const path = getStablePath( options.path ); + if ( cache[ path ] ) { + return sendSuccessResponse( cache[ path ], parse ); + } + + const matches = path.match( + /^\/__experimental\/menus\/(\d+)\?context=edit$/ + ); + if ( ! matches ) { + return next( options ); + } + + const key = Object.keys( cache )?.[ 0 ]; + if ( ! key ) { + return next( options ); + } + + const menuData = cache[ key ]?.body; + if ( ! menuData ) { + return next( options ); + } + + const menuId = parseInt( matches[ 1 ] ); + const menu = menuData.filter( ( { id } ) => { + return id === menuId; + } ); + + if ( menu ) { + return sendSuccessResponse( menu ); + } + + return next( options ); + }; +} + +function sendSuccessResponse( responseData, parse ) { + return Promise.resolve( + parse + ? responseData.body + : new window.Response( JSON.stringify( responseData.body ), { + status: 200, + statusText: 'OK', + headers: responseData.headers, + } ) + ); +} + +/** + * Given a path, returns a normalized path where equal query parameter values + * will be treated as identical, regardless of order they appear in the original + * text. + * + * @param {string} path Original path. + * + * @return {string} Normalized path. + */ +export function getStablePath( path ) { + const splitted = path.split( '?' ); + const query = splitted[ 1 ]; + const base = splitted[ 0 ]; + if ( ! query ) { + return base; + } + + // 'b=1&c=2&a=5' + return ( + base + + '?' + + query + // [ 'b=1', 'c=2', 'a=5' ] + .split( '&' ) + // [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ] + .map( ( entry ) => entry.split( '=' ) ) + // [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ] + .sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) ) + // [ 'a=5', 'b=1', 'c=2' ] + .map( ( pair ) => pair.join( '=' ) ) + // 'a=5&b=1&c=2' + .join( '&' ) + ); +} From a34a2eef1a0fb1b54e2b62ad0e29988701838c2b Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 17:02:32 +0200 Subject: [PATCH 23/42] Remove the gutenberg_navigation_get_menu_endpoint function. It's not used. --- lib/navigation-page.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 0bc6de35009189..75addd590ae633 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -38,22 +38,6 @@ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { ); } -/** - * This function returns an url for the /__experimental/menus/ endpoint - * - * @since 11.6.0 - * - * @param int $menu_id Menu ID. - * @return string - */ -function gutenberg_navigation_get_menu_endpoint( $menu_id ) { - return "/__experimental/menus/{$menu_id}?" . build_query( - array( - 'context' => 'edit', - ) - ); -} - /** * This function returns an url for the /__experimental/menu-items endpoint * From c16d3eef11853ccb0fb3e43d3a7d09046de5f8aa Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 17:34:30 +0200 Subject: [PATCH 24/42] We should only load data once. --- packages/edit-navigation/src/utils/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index 58fa4ae646b62e..3ba9b5ac491f71 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -9,6 +9,9 @@ export function createMenuPreloadingMiddleware( preloadedData ) { return result; }, /** @type {Record} */ ( {} ) ); + let menusDataLoaded = false; + let menuDataLoaded = false; + return ( options, next ) => { const { parse = true } = options; if ( 'string' !== typeof options.path ) { @@ -21,10 +24,15 @@ export function createMenuPreloadingMiddleware( preloadedData ) { } const path = getStablePath( options.path ); - if ( cache[ path ] ) { + if ( ! menusDataLoaded && cache[ path ] ) { + menusDataLoaded = true; return sendSuccessResponse( cache[ path ], parse ); } + if ( menuDataLoaded ) { + return next( options ); + } + const matches = path.match( /^\/__experimental\/menus\/(\d+)\?context=edit$/ ); @@ -48,6 +56,7 @@ export function createMenuPreloadingMiddleware( preloadedData ) { } ); if ( menu ) { + menuDataLoaded = true; return sendSuccessResponse( menu ); } @@ -55,6 +64,13 @@ export function createMenuPreloadingMiddleware( preloadedData ) { }; } +/** + * This is a helper function that sends a success response. + * + * @param {Object} responseData An object with the menu data + * @param {boolean} parse A boolean that controls whether to send a response or just the response data + * @return {Object} Resolved promise + */ function sendSuccessResponse( responseData, parse ) { return Promise.resolve( parse From 6ccc620b70c61c84951548e7321010b89db66a6f Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 17:50:11 +0200 Subject: [PATCH 25/42] Add PHPDOC block. --- packages/edit-navigation/src/utils/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index 3ba9b5ac491f71..c6f221159348a0 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -1,5 +1,11 @@ /** + * The purpose of this function is to create a middleware that is responsible for preloading menu-related data. + * It uses data that is returned from the /__experimental/menus endpoint for requests + * to the /__experimental/menu/ endpoint, because the data is the same. + * This way, we can avoid making additional REST API requests. + * This middleware can be removed if/when we implement caching at the wordpress/data level. + * * @param {Object} preloadedData * @return {Function} Preloading middleware. */ From 24ba0b59747c3b31f4735b74b3629849f9fc5b0e Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 18:03:53 +0200 Subject: [PATCH 26/42] Fix the unit test. --- phpunit/navigation-page-test.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/phpunit/navigation-page-test.php b/phpunit/navigation-page-test.php index 1ed5ef8ea05d43..f4aabf02cbddcd 100644 --- a/phpunit/navigation-page-test.php +++ b/phpunit/navigation-page-test.php @@ -38,7 +38,6 @@ function test_gutenberg_navigation_init_function_generates_correct_preload_paths ), '/__experimental/menus?per_page=100&context=edit&_locale=user', '/wp/v2/types?context=edit', - "/__experimental/menus/{$menu_id}?context=edit", "/__experimental/menu-items?context=edit&menus={$menu_id}&per_page=100&_locale=user", ); @@ -57,6 +56,11 @@ function test_gutenberg_navigation_init_function_generates_correct_preload_paths set_current_screen( 'gutenberg_page_gutenberg-navigation' ); gutenberg_navigation_init( 'gutenberg_page_gutenberg-navigation' ); } + + function test_gutenberg_navigation_editor_preload_menus_function_returns_correct_data() { + gutenberg_navigation_editor_preload_menus(array()); + $this->addToAssertionCount(1); + } } /** From 87f5c983d7c5c95e12a6b2e38720f072102038fb Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 18:21:38 +0200 Subject: [PATCH 27/42] Improve test code coverage. Add a new unit test for test_gutenberg_navigation_editor_preload_menus_function_returns_correct_data --- lib/navigation-page.php | 2 +- phpunit/navigation-page-test.php | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 75addd590ae633..ea4a8c1b88d8fb 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -157,7 +157,7 @@ function gutenberg_navigation_editor_preload_menus( $preload_data ) { array() : array( - $menus_data_path => $preload_data[ $menus_data_path ] + $menus_data_path => $preload_data[ $menus_data_path ], ); if ( ! $menus_data ) { diff --git a/phpunit/navigation-page-test.php b/phpunit/navigation-page-test.php index f4aabf02cbddcd..52cc4c2ec8022a 100644 --- a/phpunit/navigation-page-test.php +++ b/phpunit/navigation-page-test.php @@ -58,11 +58,23 @@ function test_gutenberg_navigation_init_function_generates_correct_preload_paths } function test_gutenberg_navigation_editor_preload_menus_function_returns_correct_data() { - gutenberg_navigation_editor_preload_menus(array()); - $this->addToAssertionCount(1); + $menus_endpoint = gutenberg_navigation_get_menus_endpoint(); + $preload_data = array( + '/__experimental/menu-locations' => array( 'some menu locations' ), + 'OPTIONS' => array( + array( 'some options requests' ), + ), + $menus_endpoint => ( 'some menus' ), + ); + + $result = gutenberg_navigation_editor_preload_menus( $preload_data ); + $this->assertArrayHasKey( '/__experimental/menu-locations', $result ); + $this->assertArrayHasKey( 'OPTIONS', $result ); + $this->assertArrayNotHasKey( $menus_endpoint, $result ); } } + /** * This is a utility test class for creating mocks of the callback functions */ From c58002e7697691feeb8c5117eba13779595171a2 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:26:29 +0200 Subject: [PATCH 28/42] Update lib/editor-settings.php Co-authored-by: Pascal Birchler --- lib/editor-settings.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 0b791d31a8b6d7..356258a97d888d 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -79,6 +79,8 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett /** * Filters the array of data that has been preloaded. * + * The dynamic portion of the hook name, `$editor_name`, refers to the type of block editor. + * * @param Array $preload_data Array containing the preloaded data. */ $preload_data = apply_filters( "{$editor_name}_preload_data", $preload_data ); From f37e48175c5752e24672286b46747f1ba51fcefb Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 18:27:25 +0200 Subject: [PATCH 29/42] Fix code style. --- lib/editor-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 356258a97d888d..3cbd679ec7720f 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -80,7 +80,7 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett * Filters the array of data that has been preloaded. * * The dynamic portion of the hook name, `$editor_name`, refers to the type of block editor. - * + * * @param Array $preload_data Array containing the preloaded data. */ $preload_data = apply_filters( "{$editor_name}_preload_data", $preload_data ); From 368367b1484cc44fe85547dcc54e1063cb7e385e Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 18:47:04 +0200 Subject: [PATCH 30/42] Fix code style. --- lib/navigation-page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index ea4a8c1b88d8fb..fe9fe1016629db 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -148,7 +148,7 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b * This function removes menu-related data from the "common" preloading middleware and calls * createMenuPreloadingMiddleware middleware because we need to use custom preloading logic for menus. * - * @param $preload_data Array containing the preloaded data. + * @param Array $preload_data Array containing the preloaded data. * @return array Filtered preload data. */ function gutenberg_navigation_editor_preload_menus( $preload_data ) { From 79a7e3a80cefb98d32115035c77232310a155df1 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 18:58:44 +0200 Subject: [PATCH 31/42] Fix code style. --- packages/edit-navigation/src/utils/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index c6f221159348a0..b85dd3e14cc6c3 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -1,4 +1,3 @@ - /** * The purpose of this function is to create a middleware that is responsible for preloading menu-related data. * It uses data that is returned from the /__experimental/menus endpoint for requests From fcfdc8c0bbef408ebd2b373ed92c5cbddab58de1 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 21:19:13 +0200 Subject: [PATCH 32/42] Fix bug in createMenuPreloadingMiddleware. We should send correct response for /__experimental/menu/ endpoint. --- packages/edit-navigation/src/utils/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index b85dd3e14cc6c3..57048b14fdd411 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -60,9 +60,13 @@ export function createMenuPreloadingMiddleware( preloadedData ) { return id === menuId; } ); - if ( menu ) { + if ( 0 < menu.length ) { menuDataLoaded = true; - return sendSuccessResponse( menu ); + // We don't have headers because we "emulate" this request + return sendSuccessResponse( + { body: menu[ 0 ], headers: {} }, + parse + ); } return next( options ); From 4d5878bbd93565775cab0856f91f74066db4d936 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 21:59:26 +0200 Subject: [PATCH 33/42] Revert this change as it needs to be moved into a separate PR. --- packages/api-fetch/src/middlewares/preloading.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index 32a1d00e9abf48..a4658baefc8427 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -72,12 +72,11 @@ function createPreloadingMiddleware( preloadedData ) { cache[ method ] && cache[ method ][ path ] ) { - const cacheData = cache[ method ][ path ]; - - // Unsetting the cache key ensures that the data is only preloaded a single time - delete cache[ method ][ path ]; - - return Promise.resolve( parse ? cacheData.body : cacheData ); + return Promise.resolve( + parse + ? cache[ method ][ path ].body + : cache[ method ][ path ] + ); } } From 6fc54fb0c0a7bb5d97c8c99a883bfeac91397764 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 11 Oct 2021 22:14:49 +0200 Subject: [PATCH 34/42] Add public access modifier to the functions. --- phpunit/navigation-page-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/navigation-page-test.php b/phpunit/navigation-page-test.php index 52cc4c2ec8022a..297bf9ea9ed6b0 100644 --- a/phpunit/navigation-page-test.php +++ b/phpunit/navigation-page-test.php @@ -24,7 +24,7 @@ public function tearDown() { remove_filter( 'wp_get_nav_menus', array( $this->callback, 'wp_nav_menus_callback' ) ); } - function test_gutenberg_navigation_init_function_generates_correct_preload_paths() { + public function test_gutenberg_navigation_init_function_generates_correct_preload_paths() { $menu_id = mt_rand( 1, 1000 ); $expected_preload_paths = array( '/__experimental/menu-locations', @@ -57,7 +57,7 @@ function test_gutenberg_navigation_init_function_generates_correct_preload_paths gutenberg_navigation_init( 'gutenberg_page_gutenberg-navigation' ); } - function test_gutenberg_navigation_editor_preload_menus_function_returns_correct_data() { + public function test_gutenberg_navigation_editor_preload_menus_function_returns_correct_data() { $menus_endpoint = gutenberg_navigation_get_menus_endpoint(); $preload_data = array( '/__experimental/menu-locations' => array( 'some menu locations' ), From 83a1edc937479edd1df738b322f7e79df0d9cdb4 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 12 Oct 2021 18:15:04 +0200 Subject: [PATCH 35/42] Fix version name. --- lib/navigation-page.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index fe9fe1016629db..d15da263bea5a9 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -23,7 +23,7 @@ class="edit-navigation" /** * This function returns an url for the /__experimental/menus endpoint * - * @since 11.6.0 + * @since 11.8.0 * * @param int $results_per_page Results per page. * @return string @@ -41,7 +41,7 @@ function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) { /** * This function returns an url for the /__experimental/menu-items endpoint * - * @since 11.6.0 + * @since 11.8.0 * * @param int $menu_id Menu ID. * @param int $results_per_page Results per page. @@ -61,7 +61,7 @@ function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_pa /** * This function returns an url for the /wp/v2/types endpoint * - * @since 11.6.0 + * @since 11.8.0 * * @return string */ From a62c9075f92aed66dd7f584564eb0efc4f41ca3f Mon Sep 17 00:00:00 2001 From: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> Date: Tue, 12 Oct 2021 18:17:18 +0200 Subject: [PATCH 36/42] Update packages/edit-navigation/src/utils/index.js Co-authored-by: Adam Zielinski --- packages/edit-navigation/src/utils/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index 57048b14fdd411..fb3db883d35e06 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -56,9 +56,7 @@ export function createMenuPreloadingMiddleware( preloadedData ) { } const menuId = parseInt( matches[ 1 ] ); - const menu = menuData.filter( ( { id } ) => { - return id === menuId; - } ); + const menu = menuData.filter( ( { id } ) => id === menuId ); if ( 0 < menu.length ) { menuDataLoaded = true; From 612d17b6d1a07a83db7bf35173efddc14e1b9897 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 12 Oct 2021 19:03:47 +0200 Subject: [PATCH 37/42] Remove the yoda condition as it can be harder to read it for some people. --- packages/edit-navigation/src/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index fb3db883d35e06..b77660ae7587a9 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -58,7 +58,7 @@ export function createMenuPreloadingMiddleware( preloadedData ) { const menuId = parseInt( matches[ 1 ] ); const menu = menuData.filter( ( { id } ) => id === menuId ); - if ( 0 < menu.length ) { + if ( menu.length > 0 ) { menuDataLoaded = true; // We don't have headers because we "emulate" this request return sendSuccessResponse( From 75519d9b4e4c08585f912d234c79041e063b77a7 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 12 Oct 2021 19:51:38 +0200 Subject: [PATCH 38/42] 1. Format the code. 2. Don't use variable filter name. Pass the ditor_name as the second parameter. --- lib/editor-settings.php | 2 +- lib/navigation-page.php | 21 ++++++++++++--------- phpunit/navigation-page-test.php | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 3cbd679ec7720f..c50e3f526be2ad 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -83,7 +83,7 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett * * @param Array $preload_data Array containing the preloaded data. */ - $preload_data = apply_filters( "{$editor_name}_preload_data", $preload_data ); + $preload_data = apply_filters( 'block_editor_preload_data', $preload_data, $editor_name ); wp_add_inline_script( 'wp-api-fetch', diff --git a/lib/navigation-page.php b/lib/navigation-page.php index d15da263bea5a9..39cb0cf5c6d5e7 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -148,17 +148,20 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b * This function removes menu-related data from the "common" preloading middleware and calls * createMenuPreloadingMiddleware middleware because we need to use custom preloading logic for menus. * - * @param Array $preload_data Array containing the preloaded data. + * @param Array $preload_data Array containing the preloaded data. + * @param string $context Current editor name. * @return array Filtered preload data. */ -function gutenberg_navigation_editor_preload_menus( $preload_data ) { +function gutenberg_navigation_editor_preload_menus( $preload_data, $context ) { + if ( 'navigation_editor' !== $context ) { + return $preload_data; + } + $menus_data_path = gutenberg_navigation_get_menus_endpoint(); - $menus_data = empty( $preload_data[ $menus_data_path ] ) ? - array() - : - array( - $menus_data_path => $preload_data[ $menus_data_path ], - ); + $menus_data = array(); + if ( ! empty( $preload_data[ $menus_data_path ] ) ) { + $menus_data = array( $menus_data_path => $preload_data[ $menus_data_path ] ); + } if ( ! $menus_data ) { return $preload_data; @@ -177,4 +180,4 @@ function gutenberg_navigation_editor_preload_menus( $preload_data ) { return $preload_data; } -add_filter( 'navigation_editor_preload_data', 'gutenberg_navigation_editor_preload_menus' ); +add_filter( 'block_editor_preload_data', 'gutenberg_navigation_editor_preload_menus', 10, 2 ); diff --git a/phpunit/navigation-page-test.php b/phpunit/navigation-page-test.php index 297bf9ea9ed6b0..eb1558cbd4518c 100644 --- a/phpunit/navigation-page-test.php +++ b/phpunit/navigation-page-test.php @@ -67,7 +67,7 @@ public function test_gutenberg_navigation_editor_preload_menus_function_returns_ $menus_endpoint => ( 'some menus' ), ); - $result = gutenberg_navigation_editor_preload_menus( $preload_data ); + $result = gutenberg_navigation_editor_preload_menus( $preload_data, 'navigation_editor' ); $this->assertArrayHasKey( '/__experimental/menu-locations', $result ); $this->assertArrayHasKey( 'OPTIONS', $result ); $this->assertArrayNotHasKey( $menus_endpoint, $result ); From d9f51e286b9293d9171746a59daaac0906e197d5 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 12 Oct 2021 20:18:03 +0200 Subject: [PATCH 39/42] 1. Update PHPDOC block. 2. Remove redundant code. --- lib/editor-settings.php | 4 +++- packages/edit-navigation/src/utils/index.js | 4 ---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/editor-settings.php b/lib/editor-settings.php index c50e3f526be2ad..db5ae81802815e 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -81,7 +81,9 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett * * The dynamic portion of the hook name, `$editor_name`, refers to the type of block editor. * - * @param Array $preload_data Array containing the preloaded data. + * @param array $preload_data Array containing the preloaded data. + * @param string $editor_name Current editor name. + * @param array Array containing the filtered preloaded data. */ $preload_data = apply_filters( 'block_editor_preload_data', $preload_data, $editor_name ); diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index b77660ae7587a9..aeb3a7cf305a66 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -46,10 +46,6 @@ export function createMenuPreloadingMiddleware( preloadedData ) { } const key = Object.keys( cache )?.[ 0 ]; - if ( ! key ) { - return next( options ); - } - const menuData = cache[ key ]?.body; if ( ! menuData ) { return next( options ); From eedbff59aaae8a27ed3367984085d1e79b1a8c56 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> Date: Wed, 13 Oct 2021 14:26:42 +0200 Subject: [PATCH 40/42] Update lib/navigation-page.php Co-authored-by: Jonny Harris --- lib/navigation-page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 39cb0cf5c6d5e7..1ac65e40efc619 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -85,7 +85,7 @@ function gutenberg_navigation_init( $hook ) { } $menus = wp_get_nav_menus(); - $first_menu_id = count( $menus ) ? $menus[0]->term_id : null; + $first_menu_id = ! empty( $menus ) ? $menus[0]->term_id : null; $preload_paths = array( '/__experimental/menu-locations', From 78b501e433a23155167fc74bbae1e104d0b94076 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 13 Oct 2021 18:07:38 +0200 Subject: [PATCH 41/42] Fix the comment. --- packages/edit-navigation/src/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-navigation/src/utils/index.js b/packages/edit-navigation/src/utils/index.js index aeb3a7cf305a66..2229f6a1c8b329 100644 --- a/packages/edit-navigation/src/utils/index.js +++ b/packages/edit-navigation/src/utils/index.js @@ -3,7 +3,7 @@ * It uses data that is returned from the /__experimental/menus endpoint for requests * to the /__experimental/menu/ endpoint, because the data is the same. * This way, we can avoid making additional REST API requests. - * This middleware can be removed if/when we implement caching at the wordpress/data level. + * This middleware can be removed if/when we implement caching at the wordpress/core-data level. * * @param {Object} preloadedData * @return {Function} Preloading middleware. From 41ea18345ef522b25ec4ebfc9d2ba6a7dbd50a2e Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Fri, 15 Oct 2021 17:30:19 +0200 Subject: [PATCH 42/42] Make the middleware unstable. We don't want it to become a stable API for now. See https://github.com/WordPress/gutenberg/pull/35402#discussion_r728636852 --- lib/navigation-page.php | 2 +- packages/edit-navigation/src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 1ac65e40efc619..959c5d73864760 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -170,7 +170,7 @@ function gutenberg_navigation_editor_preload_menus( $preload_data, $context ) { wp_add_inline_script( 'wp-edit-navigation', sprintf( - 'wp.apiFetch.use( wp.editNavigation.createMenuPreloadingMiddleware( %s ) );', + 'wp.apiFetch.use( wp.editNavigation.__unstableCreateMenuPreloadingMiddleware( %s ) );', wp_json_encode( $menus_data ) ), 'after' diff --git a/packages/edit-navigation/src/index.js b/packages/edit-navigation/src/index.js index 907dc8515935de..c16264fc619a1f 100644 --- a/packages/edit-navigation/src/index.js +++ b/packages/edit-navigation/src/index.js @@ -91,4 +91,4 @@ export function initialize( id, settings ) { ); } -export { createMenuPreloadingMiddleware } from './utils'; +export { createMenuPreloadingMiddleware as __unstableCreateMenuPreloadingMiddleware } from './utils';