Make WordPress Core


Ignore:
Timestamp:
10/16/2025 11:30:56 PM (12 months ago)
Author:
westonruter
Message:

Editor: Opt to dequeue assets enqueued in hidden blocks, rather than to enqueue assets for non-hidden blocks.

This eliminates constant emptying out of the queues for styles, scripts, and script modules before rendering each block. This ensures that wp_script_is()/wp_style_is() will return true for assets that are actually enqueued. The WP_Script_Modules::$queue member which was made public in [60930] is now made private in favor of a WP_Script_Modules::get_queue() method, since there is no need to clear out the queue before rendering each block and restore after the rendering is complete.

Finally, as a very special case for unusual blocks which contain wp_head(), a check is done to see if the wp_enqueue_scripts action occurred during the rendering of a block; if so, then no assets will be dequeued even if no markup is rendered in the block, since it may be that a script was enqueued for the footer and not the head.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/10252

Follow-up to [60930].

Props westonruter, dd32, peterwilsoncc, nikunj8866, krupajnanda.
Fixes #63676.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r60930 r60951  
    544544                                'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
    545545                        ),
     546                        'admin_bar_assets_enqueued_in_block'      => array(
     547                                'set_up'                  => static function () {
     548                                        wp_enqueue_script( 'admin-bar' );
     549                                        wp_enqueue_style( 'admin-bar' );
     550
     551                                        add_filter(
     552                                                'render_block_core/static',
     553                                                static function ( $content ) {
     554                                                        $processor = new WP_HTML_Tag_Processor( $content );
     555                                                        $processor->next_tag();
     556                                                        $processor->add_class( wp_script_is( 'admin-bar', 'enqueued' ) ? 'yes-admin-bar-script-enqueued' : 'not-admin-bar-script-enqueued' );
     557                                                        $processor->add_class( wp_style_is( 'admin-bar', 'enqueued' ) ? 'yes-admin-bar-style-enqueued' : 'not-admin-bar-style-enqueued' );
     558                                                        return $processor->get_updated_html();
     559                                                },
     560                                                10,
     561                                                3
     562                                        );
     563                                },
     564                                'block_markup'            => '<!-- wp:static --><div class="static"></div><!-- /wp:static -->',
     565                                'expected_rendered_block' => '
     566                                        <div class="static yes-admin-bar-script-enqueued yes-admin-bar-style-enqueued"></div>
     567                                ',
     568                                'expected_styles'         => array( 'static-view-style', 'admin-bar' ),
     569                                'expected_scripts'        => array( 'static-view-script', 'admin-bar' ),
     570                                'expected_script_modules' => array( 'static-view-script-module' ),
     571                        ),
     572                        'enqueues_in_wp_head_block'               => array(
     573                                'set_up'                  => static function () {
     574                                        remove_all_actions( 'wp_head' );
     575                                        remove_all_actions( 'wp_enqueue_scripts' );
     576
     577                                        add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
     578                                        add_action( 'wp_head', 'wp_print_styles', 8 );
     579                                        add_action( 'wp_head', 'wp_print_head_scripts', 9 );
     580                                        remove_action( 'wp_print_styles', 'print_emoji_styles' );
     581
     582                                        add_action(
     583                                                'wp_enqueue_scripts',
     584                                                static function () {
     585                                                        wp_enqueue_script( 'for-footer', '/footer.js', array(), null, array( 'in_footer' => true ) );
     586                                                }
     587                                        );
     588                                        add_action(
     589                                                'wp_head',
     590                                                static function () {
     591                                                        wp_enqueue_style( 'for-footer', '/footer.css', array(), null );
     592                                                },
     593                                                10000
     594                                        );
     595                                },
     596                                'block_markup'            => '<!-- wp:wp-head /-->',
     597                                'expected_rendered_block' => '',
     598                                'expected_styles'         => array( 'for-footer' ),
     599                                'expected_scripts'        => array( 'for-footer' ),
     600                                'expected_script_modules' => array(),
     601                        ),
    546602                );
    547603        }
    … …  
    563619                        $set_up();
    564620                }
     621
     622                $this->registry->register(
     623                        'core/wp-head',
     624                        array(
     625                                'render_callback' => static function () {
     626                                        return get_echo( 'wp_head' );
     627                                },
     628                        )
     629                );
     630
    565631                wp_register_style( 'static-view-style', home_url( '/static-view-style.css' ) );
    566632                wp_register_script( 'static-view-script', home_url( '/static-view-script.js' ) );
    … …  
    609675                $rendered_block = $block->render();
    610676
     677                $this->assertSameSets( $expected_styles, wp_styles()->queue, 'Enqueued styles do not meet expectations' );
     678                $this->assertSameSets( $expected_scripts, wp_scripts()->queue, 'Enqueued scripts do not meet expectations' );
     679                $this->assertSameSets( $expected_script_modules, wp_script_modules()->get_queue(), 'Enqueued script modules do not meet expectations' );
     680
    611681                $this->assertEqualHTML(
    612682                        $expected_rendered_block,
    … …  
    615685                        "Rendered block does not contain expected HTML:\n$rendered_block"
    616686                );
    617 
    618                 remove_action( 'wp_print_styles', 'print_emoji_styles' );
    619 
    620                 $actual_styles  = array();
    621                 $printed_styles = get_echo( 'wp_print_styles' );
    622                 $processor      = new WP_HTML_Tag_Processor( $printed_styles );
    623                 while ( $processor->next_tag( array( 'tag_name' => 'LINK' ) ) ) {
    624                         if ( 1 === preg_match( '/^(.+)-css$/', $processor->get_attribute( 'id' ), $matches ) ) {
    625                                 $actual_styles[] = $matches[1];
    626                         }
    627                 }
    628                 $this->assertSameSets( $expected_styles, $actual_styles, 'Enqueued styles do not meet expectations' );
    629 
    630                 $actual_scripts  = array();
    631                 $printed_scripts = get_echo( 'wp_print_scripts' );
    632                 $processor       = new WP_HTML_Tag_Processor( $printed_scripts );
    633                 while ( $processor->next_tag( array( 'tag_name' => 'SCRIPT' ) ) ) {
    634                         if ( 1 === preg_match( '/^(.+)-js$/', $processor->get_attribute( 'id' ), $matches ) ) {
    635                                 $actual_scripts[] = $matches[1];
    636                         }
    637                 }
    638                 $this->assertSameSets( $expected_scripts, $actual_scripts, 'Enqueued scripts do not meet expectations' );
    639 
    640                 $actual_script_modules  = array();
    641                 $printed_script_modules = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
    642                 $processor              = new WP_HTML_Tag_Processor( $printed_script_modules );
    643                 while ( $processor->next_tag( array( 'tag_name' => 'SCRIPT' ) ) ) {
    644                         if ( 1 === preg_match( '/^(.+)-js-module$/', $processor->get_attribute( 'id' ), $matches ) ) {
    645                                 $actual_script_modules[] = $matches[1];
    646                         }
    647                 }
    648                 $this->assertSameSets( $expected_script_modules, $actual_script_modules, 'Enqueued script modules do not meet expectations' );
    649687        }
    650688
Note: See TracChangeset for help on using the changeset viewer.