Make WordPress Core

Ticket #63486: 63486.diff

File 63486.diff, 4.3 KB (added by b1ink0, 6 months ago)

Patch to add script modules footer placement support

  • src/wp-includes/class-wp-script-modules.php

    diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php
    index 08d08a5d1a..fa705d1505 100644
    a b class WP_Script_Modules { 
    7575         * @param array             $args     {
    7676         *     Optional. An array of additional args. Default empty array.
    7777         *
     78         *     @type bool                $in_footer     Whether to print the script module in the footer. Default 'false'. Optional.
    7879         *     @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
    7980         * }
    8081         */
    class WP_Script_Modules { 
    101102                                }
    102103                        }
    103104
     105                        $in_footer = false;
     106                        if ( isset( $args['in_footer'] ) ) {
     107                                $in_footer = (bool) $args['in_footer'];
     108                        }
     109
    104110                        $fetchpriority = 'auto';
    105111                        if ( isset( $args['fetchpriority'] ) ) {
    106112                                if ( $this->is_valid_fetchpriority( $args['fetchpriority'] ) ) {
    class WP_Script_Modules { 
    124130                                'version'       => $version,
    125131                                'enqueue'       => isset( $this->enqueued_before_registered[ $id ] ),
    126132                                'dependencies'  => $dependencies,
     133                                'in_footer'     => $in_footer,
    127134                                'fetchpriority' => $fetchpriority,
    128135                        );
    129136                }
    class WP_Script_Modules { 
    209216         * @param array             $args     {
    210217         *     Optional. An array of additional args. Default empty array.
    211218         *
     219         *     @type bool                $in_footer     Whether to print the script module in the footer. Default 'false'. Optional.
    212220         *     @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
    213221         * }
    214222         */
    class WP_Script_Modules { 
    276284        }
    277285
    278286        /**
    279          * Prints the enqueued script modules using script tags with type="module"
    280          * attributes.
     287         * Prints the enqueued script modules in head or footer.
     288         *
     289         * For classic themes, all script modules are printed in the footer.
     290         * For block themes, allows script modules to be printed in the head or footer.
    281291         *
    282292         * @since 6.5.0
    283293         */
    284294        public function print_enqueued_script_modules() {
    285                 foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) {
     295                $script_modules = $this->get_marked_for_enqueue();
     296
     297                // If we're in wp_footer, just print everything.
     298                if ( 'wp_footer' === current_action() ) {
     299                        $this->print_script_modules( $script_modules );
     300                } else {
     301                        $head_modules   = array();
     302                        $footer_modules = array();
     303
     304                        foreach ( $script_modules as $id => $script_module ) {
     305                                if ( isset( $script_module['in_footer'] ) && $script_module['in_footer'] ) {
     306                                        $footer_modules[ $id ] = $script_module;
     307                                } else {
     308                                        $head_modules[ $id ] = $script_module;
     309                                }
     310                        }
     311                        $this->print_script_modules( $head_modules );
     312
     313                        // If there are footer modules, print them in the footer.
     314                        if ( count( $footer_modules ) > 0 ) {
     315                                add_action(
     316                                        'wp_footer',
     317                                        function () use ( $footer_modules ) {
     318                                                $this->print_script_modules( $footer_modules );
     319                                        }
     320                                );
     321                        }
     322                }
     323        }
     324
     325        /**
     326         * Prints the enqueued script modules using script tags with type="module"
     327         * attributes.
     328         *
     329         * @since 6.9.0
     330         *
     331         * @param array $modules The script modules to print.
     332         */
     333        private function print_script_modules( $modules ) {
     334                foreach ( $modules as $id => $script_module ) {
    286335                        $args = array(
    287336                                'type' => 'module',
    288337                                'src'  => $this->get_src( $id ),
  • src/wp-includes/script-modules.php

    diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php
    index 0d284833be..97e80f3ec8 100644
    a b function wp_script_modules(): WP_Script_Modules { 
    6464 * @param array             $args    {
    6565 *     Optional. An array of additional args. Default empty array.
    6666 *
     67 *     @type bool                $in_footer     Whether to print the script module in the footer. Default 'false'. Optional.
    6768 *     @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
    6869 * }
    6970 */
    function wp_register_script_module( string $id, string $src, array $deps = array 
    107108 * @param array             $args    {
    108109 *     Optional. An array of additional args. Default empty array.
    109110 *
     111 *     @type bool                $in_footer     Whether to print the script module in the footer. Default 'false'. Optional.
    110112 *     @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
    111113 * }
    112114 */