Changeset 63654
- Timestamp:
- 09/17/2026 09:04:13 AM (2 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
src/wp-includes/block-editor.php (modified) (4 diffs)
-
tests/phpunit/tests/blocks/editor.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-editor.php
r63320 r63654 669 669 * @since 5.8.0 670 670 * 671 * @global WP_Post $post Global post object. 672 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 673 * @global WP_Styles $wp_styles The WP_Styles object for printing styles. 671 * @global WP_Post $post Global post object. 672 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 673 * @global WP_Styles $wp_styles The WP_Styles object for printing styles. 674 * @global WP_Script_Modules $wp_script_modules The WP_Script_Modules object for printing script modules. 674 675 * 675 676 * @param (string|string[])[] $preload_paths List of paths to preload. … … 677 678 */ 678 679 function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) { 679 global $post, $wp_scripts, $wp_styles ;680 global $post, $wp_scripts, $wp_styles, $wp_script_modules; 680 681 681 682 /** … … 711 712 712 713 /* 713 * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after714 * API data is preloaded.714 * Ensure the globals $post, $wp_scripts, $wp_styles, and $wp_script_modules 715 * remain the same after API data is preloaded. 715 716 * Because API preloading can call the_content and other filters, plugins 716 717 * can unexpectedly modify the global $post or enqueue assets which are not 717 718 * intended for the block editor. 718 */ 719 $backup_global_post = ! empty( $post ) ? clone $post : $post; 720 $backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts; 721 $backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; 719 * 720 * Copies are swapped in for the duration of the preload and the original 721 * instances are restored afterwards, so that hooks and other references 722 * bound to those instances remain valid. 723 */ 724 $original_post = $post; 725 $original_wp_scripts = $wp_scripts; 726 $original_wp_styles = $wp_styles; 727 $original_wp_script_modules = $wp_script_modules; 728 729 $post = ! empty( $post ) ? clone $post : $post; 730 $wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts; 731 $wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; 732 $wp_script_modules = ! empty( $wp_script_modules ) ? clone $wp_script_modules : $wp_script_modules; 722 733 723 734 foreach ( $preload_paths as &$path ) { … … 740 751 ); 741 752 742 // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading. 743 $post = $backup_global_post; 744 $wp_scripts = $backup_wp_scripts; 745 $wp_styles = $backup_wp_styles; 753 // Restore the original $post, $wp_scripts, $wp_styles, and $wp_script_modules instances. 754 $post = $original_post; 755 if ( ! empty( $post ) ) { 756 setup_postdata( $post ); 757 } 758 $wp_scripts = $original_wp_scripts; 759 $wp_styles = $original_wp_styles; 760 $wp_script_modules = $original_wp_script_modules; 746 761 747 762 wp_add_inline_script( -
trunk/tests/phpunit/tests/blocks/editor.php
r63595 r63654 12 12 /** 13 13 * Sets up each test method. 14 * 15 * @global WP_Scripts $wp_scripts 16 * @global WP_Styles $wp_styles 17 * @global WP_Script_Modules $wp_script_modules 14 18 */ 15 19 public function set_up() { … … 34 38 $post_ID = 1; 35 39 36 global $wp_scripts, $wp_styles; 37 $this->original_wp_scripts = $wp_scripts; 38 $this->original_wp_styles = $wp_styles; 39 $wp_scripts = null; 40 $wp_styles = null; 40 global $wp_scripts, $wp_styles, $wp_script_modules; 41 $this->original_wp_scripts = $wp_scripts; 42 $this->original_wp_styles = $wp_styles; 43 $this->original_wp_script_modules = $wp_script_modules; 44 $wp_scripts = null; 45 $wp_styles = null; 46 $wp_script_modules = null; 41 47 wp_scripts(); 42 48 wp_styles(); 49 /* 50 * The script module print callbacks are bound to the instance that 51 * registers them, so the fresh instance must register its own hooks. 52 * WP_UnitTestCase restores $wp_filter between tests. 53 */ 54 wp_script_modules()->add_hooks(); 43 55 } 44 56 45 57 public function tear_down() { 46 global $wp_scripts, $wp_styles; 47 $wp_scripts = $this->original_wp_scripts; 48 $wp_styles = $this->original_wp_styles; 58 global $wp_scripts, $wp_styles, $wp_script_modules; 59 $wp_scripts = $this->original_wp_scripts; 60 $wp_styles = $this->original_wp_styles; 61 $wp_script_modules = $this->original_wp_script_modules; 62 63 $registry = WP_Block_Type_Registry::get_instance(); 64 if ( $registry->is_registered( 'tests/preload-view-assets' ) ) { 65 $registry->unregister( 'tests/preload-view-assets' ); 66 } 49 67 50 68 /** @var WP_REST_Server $wp_rest_server */ … … 61 79 } 62 80 63 /** 64 * @var WP_Scripts|null 65 */ 66 protected $original_wp_scripts; 67 68 /** 69 * @var WP_Styles|null 70 */ 71 protected $original_wp_styles; 81 protected ?WP_Scripts $original_wp_scripts; 82 83 protected ?WP_Styles $original_wp_styles; 84 85 protected ?WP_Script_Modules $original_wp_script_modules; 72 86 73 87 /** … … 687 701 688 702 /** 703 * Tests that scripts, styles, and script modules enqueued while rendering blocks 704 * during REST API preloading are not enqueued in the block editor. 705 * 706 * @ticket 64484 707 * @ticket 55151 708 * 709 * @covers ::block_editor_rest_api_preload 710 */ 711 public function test_block_editor_rest_api_preload_discards_assets_enqueued_during_preload() { 712 wp_register_script( 'test-view-script', '/test-view-script.js', array(), null ); 713 wp_register_style( 'test-view-style', '/test-view-style.css', array(), null ); 714 wp_register_script_module( 'test-view-script-module', '/test-view-script-module.js' ); 715 wp_register_script_module( 'test-editor-script-module', '/test-editor-script-module.js' ); 716 717 $rendered = false; 718 register_block_type( 719 'tests/preload-view-assets', 720 array( 721 'render_callback' => static function () use ( &$rendered ) { 722 $rendered = true; 723 return '<p>Block content.</p>'; 724 }, 725 'view_script_handles' => array( 'test-view-script' ), 726 'view_style_handles' => array( 'test-view-style' ), 727 'view_script_module_ids' => array( 'test-view-script-module' ), 728 ) 729 ); 730 731 $post_id = self::factory()->post->create( 732 array( 733 'post_content' => '<!-- wp:tests/preload-view-assets /-->', 734 ) 735 ); 736 wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); 737 738 // Enqueued before preloading, so it must still be enqueued afterwards. 739 wp_enqueue_script_module( 'test-editor-script-module' ); 740 741 block_editor_rest_api_preload( 742 array( "/wp/v2/posts/{$post_id}?context=edit" ), 743 new WP_Block_Editor_Context( array( 'post' => get_post( $post_id ) ) ) 744 ); 745 746 $admin_footer_scripts = get_echo( 747 static function () { 748 /** This action is documented in wp-admin/admin-footer.php */ 749 do_action( 'admin_print_footer_scripts' ); 750 } 751 ); 752 753 $this->assertTrue( $rendered, 'The block should render during preloading.' ); 754 $this->assertNotContains( 'test-view-script', wp_scripts()->queue, 'The block view script should not be enqueued.' ); 755 $this->assertNotContains( 'test-view-style', wp_styles()->queue, 'The block view style should not be enqueued.' ); 756 $this->assertStringNotContainsString( 'test-view-script-module', $admin_footer_scripts, 'The block view script module should not be printed.' ); 757 $this->assertStringContainsString( 'test-editor-script-module', $admin_footer_scripts, 'The script module enqueued before preloading should be printed.' ); 758 } 759 760 /** 689 761 * @ticket 57547 690 762 *
Note:
See TracChangeset
for help on using the changeset viewer.