diff --git a/lib/editor-settings.php b/lib/editor-settings.php index db5ae81802815e..527a5f9b878491 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -76,17 +76,6 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett array() ); - /** - * 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. - * @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 ); - wp_add_inline_script( 'wp-api-fetch', sprintf( diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 959c5d73864760..e9172d934fd15b 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -91,7 +91,6 @@ function gutenberg_navigation_init( $hook ) { '/__experimental/menu-locations', array( '/wp/v2/pages', 'OPTIONS' ), array( '/wp/v2/posts', 'OPTIONS' ), - gutenberg_navigation_get_menus_endpoint(), gutenberg_navigation_get_types_endpoint(), ); @@ -120,6 +119,8 @@ function gutenberg_navigation_init( $hook ) { ) ); + gutenberg_navigation_editor_preload_menus(); + wp_enqueue_script( 'wp-edit-navigation' ); wp_enqueue_style( 'wp-edit-navigation' ); wp_enqueue_script( 'wp-format-library' ); @@ -145,26 +146,22 @@ 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. + * This function calls createMenuPreloadingMiddleware middleware because + * we need to use custom preloading logic for menus. * - * @param Array $preload_data Array containing the preloaded data. - * @param string $context Current editor name. - * @return array Filtered preload data. + * @return void */ -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 = array(); - if ( ! empty( $preload_data[ $menus_data_path ] ) ) { - $menus_data = array( $menus_data_path => $preload_data[ $menus_data_path ] ); - } +function gutenberg_navigation_editor_preload_menus() { + $menus_data = array_reduce( + array( + gutenberg_navigation_get_menus_endpoint(), + ), + 'rest_preload_api_request', + array() + ); if ( ! $menus_data ) { - return $preload_data; + return; } wp_add_inline_script( @@ -175,9 +172,4 @@ function gutenberg_navigation_editor_preload_menus( $preload_data, $context ) { ), 'after' ); - - unset( $preload_data[ $menus_data_path ] ); - return $preload_data; } - -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 eb1558cbd4518c..d0356741bd2e04 100644 --- a/phpunit/navigation-page-test.php +++ b/phpunit/navigation-page-test.php @@ -11,20 +11,37 @@ class WP_Navigation_Page_Test extends WP_UnitTestCase { */ private $callback; + /** @var WP_Scripts */ + private static $wp_scripts; + + public static function setUpBeforeClass() { + parent::setUpBeforeClass(); + global $wp_scripts; + static::$wp_scripts = clone $wp_scripts; + } + + public static function tearDownAfterClass() { + parent::tearDownAfterClass(); + global $wp_scripts; + $wp_scripts = static::$wp_scripts; + } + public function setUp() { parent::setUp(); $this->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' ) ); + remove_filter( 'rest_pre_dispatch', array( $this->callback, 'preload_menus_rest_pre_dispatch_callback' ) ); } public function test_gutenberg_navigation_init_function_generates_correct_preload_paths() { + 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' ) ); + $menu_id = mt_rand( 1, 1000 ); $expected_preload_paths = array( '/__experimental/menu-locations', @@ -36,7 +53,6 @@ public function test_gutenberg_navigation_init_function_generates_correct_preloa '/wp/v2/posts', 'OPTIONS', ), - '/__experimental/menus?per_page=100&context=edit&_locale=user', '/wp/v2/types?context=edit', "/__experimental/menu-items?context=edit&menus={$menu_id}&per_page=100&_locale=user", ); @@ -57,24 +73,28 @@ public function test_gutenberg_navigation_init_function_generates_correct_preloa gutenberg_navigation_init( 'gutenberg_page_gutenberg-navigation' ); } - 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' ), - 'OPTIONS' => array( - array( 'some options requests' ), - ), - $menus_endpoint => ( 'some menus' ), - ); + public function test_gutenberg_navigation_editor_preload_menus_initializes_createMenuPreloadingMiddleware() { + add_filter( 'rest_pre_dispatch', array( $this->callback, 'preload_menus_rest_pre_dispatch_callback' ) ); + $scripts = wp_scripts(); + $handle = 'wp-edit-navigation'; + $scripts->remove( $handle ); + $scripts->add( $handle, 'https://test.test/test.js' ); + $response = new WP_REST_Response( array( 'someData' ) ); + $this->callback + ->expects( $this->once() ) + ->method( 'preload_menus_rest_pre_dispatch_callback' ) + ->willReturn( new $response ); + + gutenberg_navigation_editor_preload_menus(); - $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 ); + /** @var _WP_Dependency $result */ + $result = $scripts->get_data( $handle, 'after' ); + $this->assertIsArray( $result ); + $this->assertArrayHasKey( 1, $result ); + $this->assertStringContainsString( 'wp.editNavigation.__unstableCreateMenuPreloadingMiddleware', $result[1] ); } } - /** * This is a utility test class for creating mocks of the callback functions */ @@ -82,4 +102,5 @@ class WP_Navigation_Page_Test_Callback { public function preload_paths_callback() {} public function wp_nav_menus_callback() {} + public function preload_menus_rest_pre_dispatch_callback() {} }