mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-05 23:27:04 +00:00
Editor: Extract block_editor_rest_api_preload method for use with different editor screens
It is going to be used on the new widgets editor screen. This patch also introduced a new class WP_Block_Editor_Context that is going to be used with revised block editor filters to let extenders to keep their existing behavior. It should also allow to provide more settings through the context class as new screens get introduced like the navigation editor. Props azaozz, chrisvanpatten, timothyblynjacobs, youknowriad. Fixes #52920. git-svn-id: https://develop.svn.wordpress.org/trunk@50956 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -63,6 +63,24 @@ class WP_Test_Block_Editor extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52920
|
||||
*/
|
||||
function test_block_editor_context_no_settings() {
|
||||
$context = new WP_Block_Editor_Context();
|
||||
|
||||
$this->assertNull( $context->post );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52920
|
||||
*/
|
||||
function test_block_editor_context_post() {
|
||||
$context = new WP_Block_Editor_Context( array( 'post' => $this->post ) );
|
||||
|
||||
$this->assertSame( $this->post, $context->post );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52920
|
||||
* @expectedDeprecated block_categories
|
||||
@@ -299,4 +317,71 @@ class WP_Test_Block_Editor extends WP_UnitTestCase {
|
||||
$settings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52920
|
||||
*/
|
||||
function test_block_editor_rest_api_preload_no_paths() {
|
||||
$context = new WP_Block_Editor_Context();
|
||||
block_editor_rest_api_preload( array(), $context );
|
||||
|
||||
$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
|
||||
$this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52920
|
||||
* @expectedDeprecated block_editor_preload_paths
|
||||
*/
|
||||
function test_block_editor_rest_api_preload_deprecated_filter_post_editor() {
|
||||
function filter_remove_preload_paths( $preload_paths, $post ) {
|
||||
if ( empty( $post ) ) {
|
||||
return $preload_paths;
|
||||
}
|
||||
return array();
|
||||
}
|
||||
add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 );
|
||||
|
||||
$context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
|
||||
block_editor_rest_api_preload(
|
||||
array(
|
||||
array( '/wp/v2/blocks', 'OPTIONS' ),
|
||||
),
|
||||
$context
|
||||
);
|
||||
|
||||
remove_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths' );
|
||||
|
||||
$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
|
||||
$this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52920
|
||||
*/
|
||||
function test_block_editor_rest_api_preload_filter_all() {
|
||||
function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $context ) {
|
||||
if ( empty( $context->post ) ) {
|
||||
array_push( $preload_paths, array( '/wp/v2/types', 'OPTIONS' ) );
|
||||
}
|
||||
|
||||
return $preload_paths;
|
||||
}
|
||||
add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 );
|
||||
|
||||
$context = new WP_Block_Editor_Context();
|
||||
block_editor_rest_api_preload(
|
||||
array(
|
||||
array( '/wp/v2/blocks', 'OPTIONS' ),
|
||||
),
|
||||
$context
|
||||
);
|
||||
|
||||
remove_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths' );
|
||||
|
||||
$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
|
||||
$this->assertContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
|
||||
$this->assertContains( '"\/wp\/v2\/blocks"', $after );
|
||||
$this->assertContains( '"\/wp\/v2\/types"', $after );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user