Changeset 50956
- Timestamp:
- 05/24/2021 07:31:49 AM (3 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/edit-form-blocks.php
r50776 r50956 24 24 global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes; 25 25 26 $editor_name = 'post-editor'; 26 $editor_name = 'post-editor'; 27 $block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) ); 27 28 28 29 // Flag that we're loading the block editor. … … 59 60 ); 60 61 61 62 /** 63 * Preload common data by specifying an array of REST API paths that will be preloaded. 64 * 65 * Filters the array of paths that will be preloaded. 66 * 67 * @since 5.0.0 68 * 69 * @param string[] $preload_paths Array of paths to preload. 70 * @param WP_Post $post Post being edited. 71 */ 72 $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post ); 73 74 /* 75 * Ensure the global $post remains the same after API data is preloaded. 76 * Because API preloading can call the_content and other filters, plugins 77 * can unexpectedly modify $post. 78 */ 79 $backup_global_post = clone $post; 80 81 $preload_data = array_reduce( 82 $preload_paths, 83 'rest_preload_api_request', 84 array() 85 ); 86 87 // Restore the global $post as it was before API preloading. 88 $post = $backup_global_post; 89 90 wp_add_inline_script( 91 'wp-api-fetch', 92 sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), 93 'after' 94 ); 62 block_editor_rest_api_preload( $preload_paths, $block_editor_context ); 95 63 96 64 wp_add_inline_script( -
trunk/src/wp-includes/block-editor.php
r50920 r50956 270 270 return $editor_settings; 271 271 } 272 273 /** 274 * Preloads common data used with the block editor by specifying an array of 275 * REST API paths that will be preloaded for a given block editor context. 276 * 277 * @since 5.8.0 278 * 279 * @global WP_Post $post Global post object. 280 * 281 * @param array $preload_paths List of paths to preload. 282 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 283 * 284 * @return void 285 */ 286 function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) { 287 global $post; 288 289 /** 290 * Filters the array of REST API paths that will be used to preloaded common data 291 * to use with the block editor. 292 * 293 * @since 5.8.0 294 * 295 * @param string[] $preload_paths Array of paths to preload. 296 */ 297 $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context ); 298 if ( ! empty( $block_editor_context->post ) ) { 299 $selected_post = $block_editor_context->post; 300 301 /** 302 * Preload common data by specifying an array of REST API paths that will be preloaded. 303 * 304 * Filters the array of paths that will be preloaded. 305 * 306 * @since 5.0.0 307 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance. 308 * 309 * @param string[] $preload_paths Array of paths to preload. 310 * @param WP_Post $selected_post Post being edited. 311 */ 312 $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' ); 313 } 314 315 if ( empty( $preload_paths ) ) { 316 return; 317 } 318 319 /* 320 * Ensure the global $post remains the same after API data is preloaded. 321 * Because API preloading can call the_content and other filters, plugins 322 * can unexpectedly modify $post. 323 */ 324 $backup_global_post = ! empty( $post ) ? clone $post : $post; 325 326 $preload_data = array_reduce( 327 $preload_paths, 328 'rest_preload_api_request', 329 array() 330 ); 331 332 // Restore the global $post as it was before API preloading. 333 $post = $backup_global_post; 334 335 wp_add_inline_script( 336 'wp-api-fetch', 337 sprintf( 338 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', 339 wp_json_encode( $preload_data ) 340 ), 341 'after' 342 ); 343 } -
trunk/src/wp-settings.php
r50945 r50956 285 285 require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-taxonomies.php'; 286 286 require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-users.php'; 287 require ABSPATH . WPINC . '/class-wp-block-editor-context.php'; 287 288 require ABSPATH . WPINC . '/class-wp-block-type.php'; 288 289 require ABSPATH . WPINC . '/class-wp-block-pattern-categories-registry.php'; -
trunk/tests/phpunit/tests/blocks/block-editor.php
r50920 r50956 66 66 /** 67 67 * @ticket 52920 68 */ 69 function test_block_editor_context_no_settings() { 70 $context = new WP_Block_Editor_Context(); 71 72 $this->assertNull( $context->post ); 73 } 74 75 /** 76 * @ticket 52920 77 */ 78 function test_block_editor_context_post() { 79 $context = new WP_Block_Editor_Context( array( 'post' => $this->post ) ); 80 81 $this->assertSame( $this->post, $context->post ); 82 } 83 84 /** 85 * @ticket 52920 68 86 * @expectedDeprecated block_categories 69 87 */ … … 300 318 ); 301 319 } 320 321 /** 322 * @ticket 52920 323 */ 324 function test_block_editor_rest_api_preload_no_paths() { 325 $context = new WP_Block_Editor_Context(); 326 block_editor_rest_api_preload( array(), $context ); 327 328 $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] ); 329 $this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after ); 330 } 331 332 /** 333 * @ticket 52920 334 * @expectedDeprecated block_editor_preload_paths 335 */ 336 function test_block_editor_rest_api_preload_deprecated_filter_post_editor() { 337 function filter_remove_preload_paths( $preload_paths, $post ) { 338 if ( empty( $post ) ) { 339 return $preload_paths; 340 } 341 return array(); 342 } 343 add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 ); 344 345 $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 346 block_editor_rest_api_preload( 347 array( 348 array( '/wp/v2/blocks', 'OPTIONS' ), 349 ), 350 $context 351 ); 352 353 remove_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths' ); 354 355 $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] ); 356 $this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after ); 357 } 358 359 /** 360 * @ticket 52920 361 */ 362 function test_block_editor_rest_api_preload_filter_all() { 363 function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $context ) { 364 if ( empty( $context->post ) ) { 365 array_push( $preload_paths, array( '/wp/v2/types', 'OPTIONS' ) ); 366 } 367 368 return $preload_paths; 369 } 370 add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 ); 371 372 $context = new WP_Block_Editor_Context(); 373 block_editor_rest_api_preload( 374 array( 375 array( '/wp/v2/blocks', 'OPTIONS' ), 376 ), 377 $context 378 ); 379 380 remove_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths' ); 381 382 $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] ); 383 $this->assertContains( 'wp.apiFetch.createPreloadingMiddleware', $after ); 384 $this->assertContains( '"\/wp\/v2\/blocks"', $after ); 385 $this->assertContains( '"\/wp\/v2\/types"', $after ); 386 } 302 387 }
Note: See TracChangeset
for help on using the changeset viewer.