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

Refactor gutenberg_initialize_editor function and remove block_editor_preload_data filter #35838

Merged
merged 6 commits into from
Oct 25, 2021
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
11 changes: 0 additions & 11 deletions lib/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 14 additions & 22 deletions lib/navigation-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand Down Expand Up @@ -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' );
Expand All @@ -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(
Expand All @@ -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 );
55 changes: 38 additions & 17 deletions phpunit/navigation-page-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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",
);
Expand All @@ -57,29 +73,34 @@ 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
*/
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() {}
}