Make WordPress Core

Changeset 57327


Ignore:
Timestamp:
01/23/2024 03:32:03 AM (3 years ago)
Author:
dmsnell
Message:

Script Modules API: Rename wp_module to wp_script_module

Renames all mentions to "module" with "script module", including function names, comments, and tests.

Follow up to [57269]

The list of functions renamed are:

  • wp_module() -> wp_script_module().
  • wp_register_module() -> wp_register_script_module().
  • wp_enqueue_module() -> wp_enqueue_script_module().
  • wp_dequeue_module() -> wp_dequeue_script_module().
  • WP_Script_Modules::print_enqueued_modules() -> WP_Script_Modules::print_enqueued_script_modules().
  • WP_Script_Modules::print_module_preloads() -> WP_Script_Modules::print_script_module_preloads().

It also adds PHP 7 typing to all the functions and improves the types of the $deps argument of wp_register_script_module() and wp_enqueue_script_module() using @type.

Props luisherranz, idad5, costdev, nefff, joemcgill, jorbin, swisspidy, jonsurrel, flixos90, gziolo, westonruter, bernhard-reiter, kamranzafar4343
See #56313

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-script-modules.php

    r57271 r57327  
    11<?php
    22/**
    3  * Modules API: WP_Script_Modules class.
     3 * Script Modules API: WP_Script_Modules class.
    44 *
    55 * Native support for ES Modules and Import Maps.
    … …  
    1010
    1111/**
    12  * Core class used to register modules.
     12 * Core class used to register script modules.
    1313 *
    1414 * @since 6.5.0
    … …  
    1616class WP_Script_Modules {
    1717        /**
    18          * Holds the registered modules, keyed by module identifier.
     18         * Holds the registered script modules, keyed by script module identifier.
    1919         *
    2020         * @since 6.5.0
    … …  
    2424
    2525        /**
    26          * Holds the module identifiers that were enqueued before registered.
    27          *
    28          * @since 6.5.0
    29          * @var array
     26         * Holds the script module identifiers that were enqueued before registered.
     27         *
     28         * @since 6.5.0
     29         * @var array<string, true>
    3030         */
    3131        private $enqueued_before_registered = array();
    3232
    3333        /**
    34          * Registers the module if no module with that module identifier has already
    35          * been registered.
    36          *
    37          * @since 6.5.0
    38          *
    39          * @param string                                                        $module_id The identifier of the module.
    40          *                                                                                 Should be unique. It will be used
    41          *                                                                                 in the final import map.
    42          * @param string                                                        $src       Full URL of the module, or path of
    43          *                                                                                 the module relative to the
    44          *                                                                                 WordPress root directory.
    45          * @param array<string|array{id: string, import?: 'static'|'dynamic' }> $deps      Optional. An array of module
    46          *                                                                                 identifiers of the dependencies of
    47          *                                                                                 this module. The dependencies can
    48          *                                                                                 be strings or arrays. If they are
    49          *                                                                                 arrays, they need an `id` key with
    50          *                                                                                 the module identifier, and can
    51          *                                                                                 contain an `import` key with either
    52          *                                                                                 `static` or `dynamic`. By default,
    53          *                                                                                 dependencies that don't contain an
    54          *                                                                                 `import` key are considered static.
    55          * @param string|false|null                                             $version   Optional. String specifying the
    56          *                                                                                 module version number. Defaults to
    57          *                                                                                 false. It is added to the URL as a
    58          *                                                                                 query string for cache busting
    59          *                                                                                 purposes. If $version is set to
    60          *                                                                                 false, the version number is the
    61          *                                                                                 currently installed WordPress
    62          *                                                                                 version. If $version is set to
    63          *                                                                                 null, no version is added.
    64          */
    65         public function register( $module_id, $src, $deps = array(), $version = false ) {
    66                 if ( ! isset( $this->registered[ $module_id ] ) ) {
     34         * Registers the script module if no script module with that script module
     35         * identifier has already been registered.
     36         *
     37         * @since 6.5.0
     38         *
     39         * @param string            $id       The identifier of the script module. Should be unique. It will be used in the
     40         *                                    final import map.
     41         * @param string            $src      Optional. Full URL of the script module, or path of the script module relative
     42         *                                    to the WordPress root directory. If it is provided and the script module has
     43         *                                    not been registered yet, it will be registered.
     44         * @param array             $deps     {
     45         *                                        Optional. List of dependencies.
     46         *
     47         *                                        @type string|array $0... {
     48         *                                            An array of script module identifiers of the dependencies of this script
     49         *                                            module. The dependencies can be strings or arrays. If they are arrays,
     50         *                                            they need an `id` key with the script module identifier, and can contain
     51         *                                            an `import` key with either `static` or `dynamic`. By default,
     52         *                                            dependencies that don't contain an `import` key are considered static.
     53         *
     54         *                                            @type string $id     The script module identifier.
     55         *                                            @type string $import Optional. Import type. May be either `static` or
     56         *                                                                 `dynamic`. Defaults to `static`.
     57         *                                        }
     58         *                                    }
     59         * @param string|false|null $version  Optional. String specifying the script module version number. Defaults to false.
     60         *                                    It is added to the URL as a query string for cache busting purposes. If $version
     61         *                                    is set to false, the version number is the currently installed WordPress version.
     62         *                                    If $version is set to null, no version is added.
     63         */
     64        public function register( string $id, string $src, array $deps = array(), $version = false ) {
     65                if ( ! isset( $this->registered[ $id ] ) ) {
    6766                        $dependencies = array();
    6867                        foreach ( $deps as $dependency ) {
    … …  
    8685                        }
    8786
    88                         $this->registered[ $module_id ] = array(
     87                        $this->registered[ $id ] = array(
    8988                                'src'          => $src,
    9089                                'version'      => $version,
    91                                 'enqueue'      => isset( $this->enqueued_before_registered[ $module_id ] ),
     90                                'enqueue'      => isset( $this->enqueued_before_registered[ $id ] ),
    9291                                'dependencies' => $dependencies,
    9392                                'enqueued'     => false,
    … …  
    9897
    9998        /**
    100          * Marks the module to be enqueued in the page the next time
    101          * `prints_enqueued_modules` is called.
    102          *
    103          * If a src is provided and the module has not been registered yet, it will be
    104          * registered.
    105          *
    106          * @since 6.5.0
    107          *
    108          * @param string                                                        $module_id The identifier of the module.
    109          *                                                                                 Should be unique. It will be used
    110          *                                                                                 in the final import map.
    111          * @param string                                                        $src       Optional. Full URL of the module,
    112          *                                                                                 or path of the module relative to
    113          *                                                                                 the WordPress root directory. If
    114          *                                                                                 it is provided and the module has
    115          *                                                                                 not been registered yet, it will be
    116          *                                                                                 registered.
    117          * @param array<string|array{id: string, import?: 'static'|'dynamic' }> $deps      Optional. An array of module
    118          *                                                                                 identifiers of the dependencies of
    119          *                                                                                 this module. The dependencies can
    120          *                                                                                 be strings or arrays. If they are
    121          *                                                                                 arrays, they need an `id` key with
    122          *                                                                                 the module identifier, and can
    123          *                                                                                 contain an `import` key with either
    124          *                                                                                 `static` or `dynamic`. By default,
    125          *                                                                                 dependencies that don't contain an
    126          *                                                                                 `import` key are considered static.
    127          * @param string|false|null                                             $version   Optional. String specifying the
    128          *                                                                                 module version number. Defaults to
    129          *                                                                                 false. It is added to the URL as a
    130          *                                                                                 query string for cache busting
    131          *                                                                                 purposes. If $version is set to
    132          *                                                                                 false, the version number is the
    133          *                                                                                 currently installed WordPress
    134          *                                                                                 version. If $version is set to
    135          *                                                                                 null, no version is added.
    136          */
    137         public function enqueue( $module_id, $src = '', $deps = array(), $version = false ) {
    138                 if ( isset( $this->registered[ $module_id ] ) ) {
    139                         $this->registered[ $module_id ]['enqueue'] = true;
     99         * Marks the script module to be enqueued in the page the next time
     100         * `print_enqueued_script_modules` is called.
     101         *
     102         * If a src is provided and the script module has not been registered yet, it
     103         * will be registered.
     104         *
     105         * @since 6.5.0
     106         *
     107         * @param string            $id       The identifier of the script module. Should be unique. It will be used in the
     108         *                                    final import map.
     109         * @param string            $src      Optional. Full URL of the script module, or path of the script module relative
     110         *                                    to the WordPress root directory. If it is provided and the script module has
     111         *                                    not been registered yet, it will be registered.
     112         * @param array             $deps     {
     113         *                                        Optional. List of dependencies.
     114         *
     115         *                                        @type string|array $0... {
     116         *                                            An array of script module identifiers of the dependencies of this script
     117         *                                            module. The dependencies can be strings or arrays. If they are arrays,
     118         *                                            they need an `id` key with the script module identifier, and can contain
     119         *                                            an `import` key with either `static` or `dynamic`. By default,
     120         *                                            dependencies that don't contain an `import` key are considered static.
     121         *
     122         *                                            @type string $id     The script module identifier.
     123         *                                            @type string $import Optional. Import type. May be either `static` or
     124         *                                                                 `dynamic`. Defaults to `static`.
     125         *                                        }
     126         *                                    }
     127         * @param string|false|null $version  Optional. String specifying the script module version number. Defaults to false.
     128         *                                    It is added to the URL as a query string for cache busting purposes. If $version
     129         *                                    is set to false, the version number is the currently installed WordPress version.
     130         *                                    If $version is set to null, no version is added.
     131         */
     132        public function enqueue( string $id, string $src = '', array $deps = array(), $version = false ) {
     133                if ( isset( $this->registered[ $id ] ) ) {
     134                        $this->registered[ $id ]['enqueue'] = true;
    140135                } elseif ( $src ) {
    141                         $this->register( $module_id, $src, $deps, $version );
    142                         $this->registered[ $module_id ]['enqueue'] = true;
     136                        $this->register( $id, $src, $deps, $version );
     137                        $this->registered[ $id ]['enqueue'] = true;
    143138                } else {
    144                         $this->enqueued_before_registered[ $module_id ] = true;
    145                 }
    146         }
    147 
    148         /**
    149          * Unmarks the module so it will no longer be enqueued in the page.
    150          *
    151          * @since 6.5.0
    152          *
    153          * @param string $module_id The identifier of the module.
    154          */
    155         public function dequeue( $module_id ) {
    156                 if ( isset( $this->registered[ $module_id ] ) ) {
    157                         $this->registered[ $module_id ]['enqueue'] = false;
    158                 }
    159                 unset( $this->enqueued_before_registered[ $module_id ] );
    160         }
    161 
    162         /**
    163          * Adds the hooks to print the import map, enqueued modules and module
    164          * preloads.
    165          *
    166          * It adds the actions to print the enqueued modules and module preloads to
    167          * both `wp_head` and `wp_footer` because in classic themes, the modules
    168          * used by the theme and plugins will likely be able to be printed in the
    169          * `head`, but the ones used by the blocks will need to be enqueued in the
    170          * `footer`.
    171          *
    172          * As all modules are deferred and dependencies are handled by the browser,
    173          * the order of the modules is not important, but it's still better to print
    174          * the ones that are available when the `wp_head` is rendered, so the browser
    175          * starts downloading those as soon as possible.
     139                        $this->enqueued_before_registered[ $id ] = true;
     140                }
     141        }
     142
     143        /**
     144         * Unmarks the script module so it will no longer be enqueued in the page.
     145         *
     146         * @since 6.5.0
     147         *
     148         * @param string $id The identifier of the script module.
     149         */
     150        public function dequeue( string $id ) {
     151                if ( isset( $this->registered[ $id ] ) ) {
     152                        $this->registered[ $id ]['enqueue'] = false;
     153                }
     154                unset( $this->enqueued_before_registered[ $id ] );
     155        }
     156
     157        /**
     158         * Adds the hooks to print the import map, enqueued script modules and script
     159         * module preloads.
     160         *
     161         * It adds the actions to print the enqueued script modules and script module
     162         * preloads to both `wp_head` and `wp_footer` because in classic themes, the
     163         * script modules used by the theme and plugins will likely be able to be
     164         * printed in the `head`, but the ones used by the blocks will need to be
     165         * enqueued in the `footer`.
     166         *
     167         * As all script modules are deferred and dependencies are handled by the
     168         * browser, the order of the script modules is not important, but it's still
     169         * better to print the ones that are available when the `wp_head` is rendered,
     170         * so the browser starts downloading those as soon as possible.
    176171         *
    177172         * The import map is also printed in the footer to be able to include the
    178          * dependencies of all the modules, including the ones printed in the footer.
     173         * dependencies of all the script modules, including the ones printed in the
     174         * footer.
    179175         *
    180176         * @since 6.5.0
    181177         */
    182178        public function add_hooks() {
    183                 add_action( 'wp_head', array( $this, 'print_enqueued_modules' ) );
    184                 add_action( 'wp_head', array( $this, 'print_module_preloads' ) );
    185                 add_action( 'wp_footer', array( $this, 'print_enqueued_modules' ) );
    186                 add_action( 'wp_footer', array( $this, 'print_module_preloads' ) );
     179                add_action( 'wp_head', array( $this, 'print_enqueued_script_modules' ) );
     180                add_action( 'wp_head', array( $this, 'print_script_module_preloads' ) );
     181                add_action( 'wp_footer', array( $this, 'print_enqueued_script_modules' ) );
     182                add_action( 'wp_footer', array( $this, 'print_script_module_preloads' ) );
    187183                add_action( 'wp_footer', array( $this, 'print_import_map' ) );
    188184        }
    189185
    190186        /**
    191          * Prints the enqueued modules using script tags with type="module"
     187         * Prints the enqueued script modules using script tags with type="module"
    192188         * attributes.
    193189         *
    194          * If a enqueued module has already been printed, it will not be printed again
    195          * on subsequent calls to this function.
    196          *
    197          * @since 6.5.0
    198          */
    199         public function print_enqueued_modules() {
    200                 foreach ( $this->get_marked_for_enqueue() as $module_id => $module ) {
    201                         if ( false === $module['enqueued'] ) {
     190         * If a enqueued script module has already been printed, it will not be
     191         * printed again on subsequent calls to this function.
     192         *
     193         * @since 6.5.0
     194         */
     195        public function print_enqueued_script_modules() {
     196                foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) {
     197                        if ( false === $script_module['enqueued'] ) {
    202198                                // Mark it as enqueued so it doesn't get enqueued again.
    203                                 $this->registered[ $module_id ]['enqueued'] = true;
     199                                $this->registered[ $id ]['enqueued'] = true;
    204200
    205201                                wp_print_script_tag(
    206202                                        array(
    207203                                                'type' => 'module',
    208                                                 'src'  => $this->get_versioned_src( $module ),
    209                                                 'id'   => $module_id . '-js-module',
     204                                                'src'  => $this->get_versioned_src( $script_module ),
     205                                                'id'   => $id . '-js-module',
    210206                                        )
    211207                                );
    … …  
    215211
    216212        /**
    217          * Prints the the static dependencies of the enqueued modules using link tags
    218          * with rel="modulepreload" attributes.
    219          *
    220          * If a module is marked for enqueue, it will not be preloaded. If a preloaded
    221          * module has already been printed, it will not be printed again on subsequent
    222          * calls to this function.
    223          *
    224          * @since 6.5.0
    225          */
    226         public function print_module_preloads() {
    227                 foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $module_id => $module ) {
     213         * Prints the the static dependencies of the enqueued script modules using
     214         * link tags with rel="modulepreload" attributes.
     215         *
     216         * If a script module is marked for enqueue, it will not be preloaded. If a
     217         * preloaded script module has already been printed, it will not be printed
     218         * again on subsequent calls to this function.
     219         *
     220         * @since 6.5.0
     221         */
     222        public function print_script_module_preloads() {
     223                foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $id => $script_module ) {
    228224                        // Don't preload if it's marked for enqueue or has already been preloaded.
    229                         if ( true !== $module['enqueue'] && false === $module['preloaded'] ) {
     225                        if ( true !== $script_module['enqueue'] && false === $script_module['preloaded'] ) {
    230226                                // Mark it as preloaded so it doesn't get preloaded again.
    231                                 $this->registered[ $module_id ]['preloaded'] = true;
     227                                $this->registered[ $id ]['preloaded'] = true;
    232228
    233229                                echo sprintf(
    234230                                        '<link rel="modulepreload" href="%s" id="%s">',
    235                                         esc_url( $this->get_versioned_src( $module ) ),
    236                                         esc_attr( $module_id . '-js-modulepreload' )
     231                                        esc_url( $this->get_versioned_src( $script_module ) ),
     232                                        esc_attr( $id . '-js-modulepreload' )
    237233                                );
    238234                        }
    … …  
    263259         * @since 6.5.0
    264260         *
    265          * @return array Array with an `imports` key mapping to an array of module identifiers and their respective URLs,
    266          *               including the version query.
    267          */
    268         private function get_import_map() {
     261         * @return array Array with an `imports` key mapping to an array of script module identifiers and their respective
     262         *               URLs, including the version query.
     263         */
     264        private function get_import_map(): array {
    269265                $imports = array();
    270                 foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $module_id => $module ) {
    271                         $imports[ $module_id ] = $this->get_versioned_src( $module );
     266                foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
     267                        $imports[ $id ] = $this->get_versioned_src( $script_module );
    272268                }
    273269                return array( 'imports' => $imports );
    … …  
    275271
    276272        /**
    277          * Retrieves the list of modules marked for enqueue.
    278          *
    279          * @since 6.5.0
    280          *
    281          * @return array Modules marked for enqueue, keyed by module identifier.
    282          */
    283         private function get_marked_for_enqueue() {
     273         * Retrieves the list of script modules marked for enqueue.
     274         *
     275         * @since 6.5.0
     276         *
     277         * @return array Script modules marked for enqueue, keyed by script module identifier.
     278         */
     279        private function get_marked_for_enqueue(): array {
    284280                $enqueued = array();
    285                 foreach ( $this->registered as $module_id => $module ) {
    286                         if ( true === $module['enqueue'] ) {
    287                                 $enqueued[ $module_id ] = $module;
     281                foreach ( $this->registered as $id => $script_module ) {
     282                        if ( true === $script_module['enqueue'] ) {
     283                                $enqueued[ $id ] = $script_module;
    288284                        }
    289285                }
    … …  
    292288
    293289        /**
    294          * Retrieves all the dependencies for the given module identifiers, filtered
    295          * by import types.
     290         * Retrieves all the dependencies for the given script module identifiers,
     291         * filtered by import types.
    296292         *
    297293         * It will consolidate an array containing a set of unique dependencies based
    … …  
    301297         * @since 6.5.0
    302298         *
    303          * @param array $module_ids   The identifiers of the modules for which to gather dependencies.
    304          * @param array $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
    305          *                            Default is both.
    306          * @return array List of dependencies, keyed by module identifier.
    307          */
    308         private function get_dependencies( $module_ids, $import_types = array( 'static', 'dynamic' ) ) {
     299
     300         * @param string[] $ids          The identifiers of the script modules for which to gather dependencies.
     301         * @param array    $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
     302         *                               Default is both.
     303         * @return array List of dependencies, keyed by script module identifier.
     304         */
     305        private function get_dependencies( array $ids, array $import_types = array( 'static', 'dynamic' ) ) {
    309306                return array_reduce(
    310                         $module_ids,
    311                         function ( $dependency_modules, $module_id ) use ( $import_types ) {
     307                        $ids,
     308                        function ( $dependency_script_modules, $id ) use ( $import_types ) {
    312309                                $dependencies = array();
    313                                 foreach ( $this->registered[ $module_id ]['dependencies'] as $dependency ) {
     310                                foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) {
    314311                                        if (
    315312                                        in_array( $dependency['import'], $import_types, true ) &&
    316313                                        isset( $this->registered[ $dependency['id'] ] ) &&
    317                                         ! isset( $dependency_modules[ $dependency['id'] ] )
     314                                        ! isset( $dependency_script_modules[ $dependency['id'] ] )
    318315                                        ) {
    319316                                                $dependencies[ $dependency['id'] ] = $this->registered[ $dependency['id'] ];
    320317                                        }
    321318                                }
    322                                 return array_merge( $dependency_modules, $dependencies, $this->get_dependencies( array_keys( $dependencies ), $import_types ) );
     319                                return array_merge( $dependency_script_modules, $dependencies, $this->get_dependencies( array_keys( $dependencies ), $import_types ) );
    323320                        },
    324321                        array()
    … …  
    327324
    328325        /**
    329          * Gets the versioned URL for a module src.
     326         * Gets the versioned URL for a script module src.
    330327         *
    331328         * If $version is set to false, the version number is the currently installed
    … …  
    335332         * @since 6.5.0
    336333         *
    337          * @param array $module The module.
    338          * @return string The module src with a version if relevant.
    339          */
    340         private function get_versioned_src( array $module ) {
     334         * @param array $script_module The script module.
     335         * @return string The script module src with a version if relevant.
     336         */
     337        private function get_versioned_src( array $script_module ): string {
    341338                $args = array();
    342                 if ( false === $module['version'] ) {
     339                if ( false === $script_module['version'] ) {
    343340                        $args['ver'] = get_bloginfo( 'version' );
    344                 } elseif ( null !== $module['version'] ) {
    345                         $args['ver'] = $module['version'];
     341                } elseif ( null !== $script_module['version'] ) {
     342                        $args['ver'] = $script_module['version'];
    346343                }
    347344                if ( $args ) {
    348                         return add_query_arg( $args, $module['src'] );
    349                 }
    350                 return $module['src'];
     345                        return add_query_arg( $args, $script_module['src'] );
     346                }
     347                return $script_module['src'];
    351348        }
    352349}
  • trunk/src/wp-includes/script-modules.php

    r57269 r57327  
    11<?php
    22/**
    3  * Script Modules API: Module functions
     3 * Script Modules API: Script Module functions
    44 *
    55 * @since 6.5.0
    … …  
    1919 * @return WP_Script_Modules The main WP_Script_Modules instance.
    2020 */
    21 function wp_modules() {
     21function wp_script_modules(): WP_Script_Modules {
    2222        static $instance = null;
    2323        if ( is_null( $instance ) ) {
    … …  
    2929
    3030/**
    31  * Registers the module if no module with that module identifier has already
    32  * been registered.
     31 * Registers the script module if no script module with that script module
     32 * identifier has already been registered.
    3333 *
    3434 * @since 6.5.0
    3535 *
    36  * @param string                                                        $module_id The identifier of the module.
    37  *                                                                                 Should be unique. It will be used
    38  *                                                                                 in the final import map.
    39  * @param string                                                        $src       Full URL of the module, or path of
    40  *                                                                                 the module relative to the
    41  *                                                                                 WordPress root directory.
    42  * @param array<string|array{id: string, import?: 'static'|'dynamic' }> $deps      Optional. An array of module
    43  *                                                                                 identifiers of the dependencies of
    44  *                                                                                 this module. The dependencies can
    45  *                                                                                 be strings or arrays. If they are
    46  *                                                                                 arrays, they need an `id` key with
    47  *                                                                                 the module identifier, and can
    48  *                                                                                 contain an `import` key with either
    49  *                                                                                 `static` or `dynamic`. By default,
    50  *                                                                                 dependencies that don't contain an
    51  *                                                                                 `import` key are considered static.
    52  * @param string|false|null                                             $version   Optional. String specifying the
    53  *                                                                                 module version number. Defaults to
    54  *                                                                                 false. It is added to the URL as a
    55  *                                                                                 query string for cache busting
    56  *                                                                                 purposes. If $version is set to
    57  *                                                                                 false, the version number is the
    58  *                                                                                 currently installed WordPress
    59  *                                                                                 version. If $version is set to
    60  *                                                                                 null, no version is added.
     36 * @param string            $id       The identifier of the script module. Should be unique. It will be used in the
     37 *                                    final import map.
     38 * @param string            $src      Optional. Full URL of the script module, or path of the script module relative
     39 *                                    to the WordPress root directory. If it is provided and the script module has
     40 *                                    not been registered yet, it will be registered.
     41 * @param array             $deps     {
     42 *                                        Optional. List of dependencies.
     43 *
     44 *                                        @type string|array $0... {
     45 *                                            An array of script module identifiers of the dependencies of this script
     46 *                                            module. The dependencies can be strings or arrays. If they are arrays,
     47 *                                            they need an `id` key with the script module identifier, and can contain
     48 *                                            an `import` key with either `static` or `dynamic`. By default,
     49 *                                            dependencies that don't contain an `import` key are considered static.
     50 *
     51 *                                            @type string $id     The script module identifier.
     52 *                                            @type string $import Optional. Import type. May be either `static` or
     53 *                                                                 `dynamic`. Defaults to `static`.
     54 *                                        }
     55 *                                    }
     56 * @param string|false|null $version  Optional. String specifying the script module version number. Defaults to false.
     57 *                                    It is added to the URL as a query string for cache busting purposes. If $version
     58 *                                    is set to false, the version number is the currently installed WordPress version.
     59 *                                    If $version is set to null, no version is added.
    6160 */
    62 function wp_register_module( $module_id, $src, $deps = array(), $version = false ) {
    63         wp_modules()->register( $module_id, $src, $deps, $version );
     61function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) {
     62        wp_script_modules()->register( $id, $src, $deps, $version );
    6463}
    6564
    6665/**
    67  * Marks the module to be enqueued in the page.
     66 * Marks the script module to be enqueued in the page.
    6867 *
    69  * If a src is provided and the module has not been registered yet, it will be
    70  * registered.
     68 * If a src is provided and the script module has not been registered yet, it
     69 * will be registered.
    7170 *
    7271 * @since 6.5.0
    7372 *
    74  * @param string                                                        $module_id The identifier of the module.
    75  *                                                                                 Should be unique. It will be used
    76  *                                                                                 in the final import map.
    77  * @param string                                                        $src       Optional. Full URL of the module,
    78  *                                                                                 or path of the module relative to
    79  *                                                                                 the WordPress root directory. If
    80  *                                                                                 it is provided and the module has
    81  *                                                                                 not been registered yet, it will be
    82  *                                                                                 registered.
    83  * @param array<string|array{id: string, import?: 'static'|'dynamic' }> $deps      Optional. An array of module
    84  *                                                                                 identifiers of the dependencies of
    85  *                                                                                 this module. The dependencies can
    86  *                                                                                 be strings or arrays. If they are
    87  *                                                                                 arrays, they need an `id` key with
    88  *                                                                                 the module identifier, and can
    89  *                                                                                 contain an `import` key with either
    90  *                                                                                 `static` or `dynamic`. By default,
    91  *                                                                                 dependencies that don't contain an
    92  *                                                                                 `import` key are considered static.
    93  * @param string|false|null                                             $version   Optional. String specifying the
    94  *                                                                                 module version number. Defaults to
    95  *                                                                                 false. It is added to the URL as a
    96  *                                                                                 query string for cache busting
    97  *                                                                                 purposes. If $version is set to
    98  *                                                                                 false, the version number is the
    99  *                                                                                 currently installed WordPress
    100  *                                                                                 version. If $version is set to
    101  *                                                                                 null, no version is added.
     73 * @param string            $id       The identifier of the script module. Should be unique. It will be used in the
     74 *                                    final import map.
     75 * @param string            $src      Optional. Full URL of the script module, or path of the script module relative
     76 *                                    to the WordPress root directory. If it is provided and the script module has
     77 *                                    not been registered yet, it will be registered.
     78 * @param array             $deps     {
     79 *                                        Optional. List of dependencies.
     80 *
     81 *                                        @type string|array $0... {
     82 *                                            An array of script module identifiers of the dependencies of this script
     83 *                                            module. The dependencies can be strings or arrays. If they are arrays,
     84 *                                            they need an `id` key with the script module identifier, and can contain
     85 *                                            an `import` key with either `static` or `dynamic`. By default,
     86 *                                            dependencies that don't contain an `import` key are considered static.
     87 *
     88 *                                            @type string $id     The script module identifier.
     89 *                                            @type string $import Optional. Import type. May be either `static` or
     90 *                                                                 `dynamic`. Defaults to `static`.
     91 *                                        }
     92 *                                    }
     93 * @param string|false|null $version  Optional. String specifying the script module version number. Defaults to false.
     94 *                                    It is added to the URL as a query string for cache busting purposes. If $version
     95 *                                    is set to false, the version number is the currently installed WordPress version.
     96 *                                    If $version is set to null, no version is added.
    10297 */
    103 function wp_enqueue_module( $module_id, $src = '', $deps = array(), $version = false ) {
    104         wp_modules()->enqueue( $module_id, $src, $deps, $version );
     98function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) {
     99        wp_script_modules()->enqueue( $id, $src, $deps, $version );
    105100}
    106101
    107102/**
    108  * Unmarks the module so it is no longer enqueued in the page.
     103 * Unmarks the script module so it is no longer enqueued in the page.
    109104 *
    110105 * @since 6.5.0
    111106 *
    112  * @param string $module_id The identifier of the module.
     107 * @param string $id The identifier of the script module.
    113108 */
    114 function wp_dequeue_module( $module_id ) {
    115         wp_modules()->dequeue( $module_id );
     109function wp_dequeue_script_module( string $id ) {
     110        wp_script_modules()->dequeue( $id );
    116111}
  • trunk/tests/phpunit/tests/script-modules/wpScriptModules.php

    r57269 r57327  
    1818         * @var WP_Script_Modules
    1919         */
    20         protected $modules;
     20        protected $script_modules;
    2121
    2222        /**
    … …  
    2525        public function set_up() {
    2626                parent::set_up();
    27                 $this->modules = new WP_Script_Modules();
    28         }
    29 
    30         /**
    31          * Gets a list of the enqueued modules.
    32          *
    33          * @return array Enqueued module URLs, keyed by module identifier.
    34          */
    35         public function get_enqueued_modules() {
    36                 $modules_markup   = get_echo( array( $this->modules, 'print_enqueued_modules' ) );
    37                 $p                = new WP_HTML_Tag_Processor( $modules_markup );
    38                 $enqueued_modules = array();
    39 
    40                 while ( $p->next_tag(
    41                         array(
    42                                 'tag'    => 'SCRIPT',
    43                                 'import' => 'module',
    44                         )
    45                 ) ) {
    46                         $id                      = preg_replace( '/-js-module$/', '', $p->get_attribute( 'id' ) );
    47                         $enqueued_modules[ $id ] = $p->get_attribute( 'src' );
     27                $this->script_modules = new WP_Script_Modules();
     28        }
     29
     30        /**
     31         * Gets a list of the enqueued script modules.
     32         *
     33         * @return array Enqueued script module URLs, keyed by script module identifier.
     34         */
     35        public function get_enqueued_script_modules() {
     36                $script_modules_markup   = get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
     37                $p                       = new WP_HTML_Tag_Processor( $script_modules_markup );
     38                $enqueued_script_modules = array();
     39
     40                while ( $p->next_tag( array( 'tag' => 'SCRIPT' ) ) ) {
     41                        if ( 'module' === $p->get_attribute( 'type' ) ) {
     42                                $id                             = preg_replace( '/-js-module$/', '', $p->get_attribute( 'id' ) );
     43                                $enqueued_script_modules[ $id ] = $p->get_attribute( 'src' );
     44                        }
    4845                }
    4946
    50                 return $enqueued_modules;
    51         }
    52 
    53         /**
    54          * Gets the modules listed in the import map.
    55          *
    56          * @return array Import map entry URLs, keyed by module identifier.
     47                return $enqueued_script_modules;
     48        }
     49
     50        /**
     51         * Gets the script modules listed in the import map.
     52         *
     53         * @return array Import map entry URLs, keyed by script module identifier.
    5754         */
    5855        public function get_import_map() {
    59                 $import_map_markup = get_echo( array( $this->modules, 'print_import_map' ) );
     56                $import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
    6057                preg_match( '/<script type="importmap" id="wp-importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string );
    6158                return json_decode( $import_map_string[1], true )['imports'];
    … …  
    6360
    6461        /**
    65          * Gets a list of preloaded modules.
    66          *
    67          * @return array Preloaded module URLs, keyed by module identifier.
    68          */
    69         public function get_preloaded_modules() {
    70                 $preloaded_markup  = get_echo( array( $this->modules, 'print_module_preloads' ) );
    71                 $p                 = new WP_HTML_Tag_Processor( $preloaded_markup );
    72                 $preloaded_modules = array();
    73 
    74                 while ( $p->next_tag(
    75                         array(
    76                                 'tag' => 'LINK',
    77                                 'rel' => 'modulepreload',
    78                         )
    79                 ) ) {
    80                         $id                       = preg_replace( '/-js-modulepreload$/', '', $p->get_attribute( 'id' ) );
    81                         $preloaded_modules[ $id ] = $p->get_attribute( 'href' );
     62         * Gets a list of preloaded script modules.
     63         *
     64         * @return array Preloaded script module URLs, keyed by script module identifier.
     65         */
     66        public function get_preloaded_script_modules() {
     67                $preloaded_markup         = get_echo( array( $this->script_modules, 'print_script_module_preloads' ) );
     68                $p                        = new WP_HTML_Tag_Processor( $preloaded_markup );
     69                $preloaded_script_modules = array();
     70
     71                while ( $p->next_tag( array( 'tag' => 'LINK' ) ) ) {
     72                        if ( 'modulepreload' === $p->get_attribute( 'rel' ) ) {
     73                                $id                              = preg_replace( '/-js-modulepreload$/', '', $p->get_attribute( 'id' ) );
     74                                $preloaded_script_modules[ $id ] = $p->get_attribute( 'href' );
     75                        }
    8276                }
    8377
    84                 return $preloaded_modules;
    85         }
    86 
    87         /**
    88          * Tests that a module gets enqueued correctly after being registered.
    89          *
    90          * @ticket 56313
    91          *
    92          * @covers ::register()
    93          * @covers ::enqueue()
    94          * @covers ::print_enqueued_modules()
    95          */
    96         public function test_wp_enqueue_module() {
    97                 $this->modules->register( 'foo', '/foo.js' );
    98                 $this->modules->register( 'bar', '/bar.js' );
    99                 $this->modules->enqueue( 'foo' );
    100                 $this->modules->enqueue( 'bar' );
    101 
    102                 $enqueued_modules = $this->get_enqueued_modules();
    103 
    104                 $this->assertCount( 2, $enqueued_modules );
    105                 $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    106                 $this->assertStringStartsWith( '/bar.js', $enqueued_modules['bar'] );
    107         }
    108 
    109         /**
    110         * Tests that a module can be dequeued after being enqueued.
     78                return $preloaded_script_modules;
     79        }
     80
     81        /**
     82         * Tests that a script module gets enqueued correctly after being registered.
     83         *
     84         * @ticket 56313
     85         *
     86         * @covers ::register()
     87         * @covers ::enqueue()
     88         * @covers ::print_enqueued_script_modules()
     89         */
     90        public function test_wp_enqueue_script_module() {
     91                $this->script_modules->register( 'foo', '/foo.js' );
     92                $this->script_modules->register( 'bar', '/bar.js' );
     93                $this->script_modules->enqueue( 'foo' );
     94                $this->script_modules->enqueue( 'bar' );
     95
     96                $enqueued_script_modules = $this->get_enqueued_script_modules();
     97
     98                $this->assertCount( 2, $enqueued_script_modules );
     99                $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     100                $this->assertStringStartsWith( '/bar.js', $enqueued_script_modules['bar'] );
     101        }
     102
     103        /**
     104        * Tests that a script module can be dequeued after being enqueued.
    111105        *
    112106        * @ticket 56313
    … …  
    115109        * @covers ::enqueue()
    116110        * @covers ::dequeue()
    117         * @covers ::print_enqueued_modules()
     111        * @covers ::print_enqueued_script_modules()
    118112        */
    119         public function test_wp_dequeue_module() {
    120                 $this->modules->register( 'foo', '/foo.js' );
    121                 $this->modules->register( 'bar', '/bar.js' );
    122                 $this->modules->enqueue( 'foo' );
    123                 $this->modules->enqueue( 'bar' );
    124                 $this->modules->dequeue( 'foo' ); // Dequeued.
    125 
    126                 $enqueued_modules = $this->get_enqueued_modules();
    127 
    128                 $this->assertCount( 1, $enqueued_modules );
    129                 $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    130                 $this->assertTrue( isset( $enqueued_modules['bar'] ) );
    131         }
    132 
    133         /**
    134         * Tests that a module can be enqueued before it is registered, and will be
    135         * handled correctly once registered.
     113        public function test_wp_dequeue_script_module() {
     114                $this->script_modules->register( 'foo', '/foo.js' );
     115                $this->script_modules->register( 'bar', '/bar.js' );
     116                $this->script_modules->enqueue( 'foo' );
     117                $this->script_modules->enqueue( 'bar' );
     118                $this->script_modules->dequeue( 'foo' ); // Dequeued.
     119
     120                $enqueued_script_modules = $this->get_enqueued_script_modules();
     121
     122                $this->assertCount( 1, $enqueued_script_modules );
     123                $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     124                $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
     125        }
     126
     127        /**
     128        * Tests that a script module can be enqueued before it is registered, and will
     129        * be handled correctly once registered.
    136130        *
    137131        * @ticket 56313
    … …  
    139133        * @covers ::register()
    140134        * @covers ::enqueue()
    141         * @covers ::print_enqueued_modules()
     135        * @covers ::print_enqueued_script_modules()
    142136        */
    143         public function test_wp_enqueue_module_works_before_register() {
    144                 $this->modules->enqueue( 'foo' );
    145                 $this->modules->register( 'foo', '/foo.js' );
    146                 $this->modules->enqueue( 'bar' ); // Not registered.
    147 
    148                 $enqueued_modules = $this->get_enqueued_modules();
    149 
    150                 $this->assertCount( 1, $enqueued_modules );
    151                 $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    152                 $this->assertFalse( isset( $enqueued_modules['bar'] ) );
    153         }
    154 
    155         /**
    156          * Tests that a module can be dequeued before it is registered and ensures
    157          * that it is not enqueued after registration.
     137        public function test_wp_enqueue_script_module_works_before_register() {
     138                $this->script_modules->enqueue( 'foo' );
     139                $this->script_modules->register( 'foo', '/foo.js' );
     140                $this->script_modules->enqueue( 'bar' ); // Not registered.
     141
     142                $enqueued_script_modules = $this->get_enqueued_script_modules();
     143
     144                $this->assertCount( 1, $enqueued_script_modules );
     145                $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     146                $this->assertFalse( isset( $enqueued_script_modules['bar'] ) );
     147        }
     148
     149        /**
     150         * Tests that a script module can be dequeued before it is registered and
     151         * ensures that it is not enqueued after registration.
    158152         *
    159153         * @ticket 56313
    … …  
    162156         * @covers ::enqueue()
    163157         * @covers ::dequeue()
    164          * @covers ::print_enqueued_modules()
    165          */
    166         public function test_wp_dequeue_module_works_before_register() {
    167                 $this->modules->enqueue( 'foo' );
    168                 $this->modules->enqueue( 'bar' );
    169                 $this->modules->dequeue( 'foo' );
    170                 $this->modules->register( 'foo', '/foo.js' );
    171                 $this->modules->register( 'bar', '/bar.js' );
    172 
    173                 $enqueued_modules = $this->get_enqueued_modules();
    174 
    175                 $this->assertCount( 1, $enqueued_modules );
    176                 $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    177                 $this->assertTrue( isset( $enqueued_modules['bar'] ) );
     158         * @covers ::print_enqueued_script_modules()
     159         */
     160        public function test_wp_dequeue_script_module_works_before_register() {
     161                $this->script_modules->enqueue( 'foo' );
     162                $this->script_modules->enqueue( 'bar' );
     163                $this->script_modules->dequeue( 'foo' );
     164                $this->script_modules->register( 'foo', '/foo.js' );
     165                $this->script_modules->register( 'bar', '/bar.js' );
     166
     167                $enqueued_script_modules = $this->get_enqueued_script_modules();
     168
     169                $this->assertCount( 1, $enqueued_script_modules );
     170                $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     171                $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
    178172        }
    179173
    180174        /**
    181175         * Tests that dependencies for a registered module are added to the import map
    182          * when the module is enqueued.
     176         * when the script module is enqueued.
    183177         *
    184178         * @ticket 56313
    … …  
    189183         */
    190184        public function test_wp_import_map_dependencies() {
    191                 $this->modules->register( 'foo', '/foo.js', array( 'dep' ) );
    192                 $this->modules->register( 'dep', '/dep.js' );
    193                 $this->modules->register( 'no-dep', '/no-dep.js' );
    194                 $this->modules->enqueue( 'foo' );
     185                $this->script_modules->register( 'foo', '/foo.js', array( 'dep' ) );
     186                $this->script_modules->register( 'dep', '/dep.js' );
     187                $this->script_modules->register( 'no-dep', '/no-dep.js' );
     188                $this->script_modules->enqueue( 'foo' );
    195189
    196190                $import_map = $this->get_import_map();
    … …  
    203197        /**
    204198         * Tests that dependencies are not duplicated in the import map when multiple
    205          * modules require the same dependency.
     199         * script modules require the same dependency.
    206200         *
    207201         * @ticket 56313
    … …  
    212206         */
    213207        public function test_wp_import_map_no_duplicate_dependencies() {
    214                 $this->modules->register( 'foo', '/foo.js', array( 'dep' ) );
    215                 $this->modules->register( 'bar', '/bar.js', array( 'dep' ) );
    216                 $this->modules->register( 'dep', '/dep.js' );
    217                 $this->modules->enqueue( 'foo' );
    218                 $this->modules->enqueue( 'bar' );
     208                $this->script_modules->register( 'foo', '/foo.js', array( 'dep' ) );
     209                $this->script_modules->register( 'bar', '/bar.js', array( 'dep' ) );
     210                $this->script_modules->register( 'dep', '/dep.js' );
     211                $this->script_modules->enqueue( 'foo' );
     212                $this->script_modules->enqueue( 'bar' );
    219213
    220214                $import_map = $this->get_import_map();
    … …  
    235229         */
    236230        public function test_wp_import_map_recursive_dependencies() {
    237                 $this->modules->register(
     231                $this->script_modules->register(
    238232                        'foo',
    239233                        '/foo.js',
    … …  
    246240                        )
    247241                );
    248                 $this->modules->register(
     242                $this->script_modules->register(
    249243                        'static-dep',
    250244                        '/static-dep.js',
    … …  
    260254                        )
    261255                );
    262                 $this->modules->register( 'dynamic-dep', '/dynamic-dep.js' );
    263                 $this->modules->register( 'nested-static-dep', '/nested-static-dep.js' );
    264                 $this->modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
    265                 $this->modules->register( 'no-dep', '/no-dep.js' );
    266                 $this->modules->enqueue( 'foo' );
     256                $this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js' );
     257                $this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
     258                $this->script_modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
     259                $this->script_modules->register( 'no-dep', '/no-dep.js' );
     260                $this->script_modules->enqueue( 'foo' );
    267261
    268262                $import_map = $this->get_import_map();
    … …  
    286280         */
    287281        public function test_wp_import_map_doesnt_print_if_no_dependencies() {
    288                 $this->modules->register( 'foo', '/foo.js' ); // No deps.
    289                 $this->modules->enqueue( 'foo' );
    290 
    291                 $import_map_markup = get_echo( array( $this->modules, 'print_import_map' ) );
     282                $this->script_modules->register( 'foo', '/foo.js' ); // No deps.
     283                $this->script_modules->enqueue( 'foo' );
     284
     285                $import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
    292286
    293287                $this->assertEmpty( $import_map_markup );
    … …  
    302296         * @covers ::register()
    303297         * @covers ::enqueue()
    304          * @covers ::print_module_preloads()
     298         * @covers ::print_script_module_preloads()
    305299         */
    306300        public function test_wp_enqueue_preloaded_static_dependencies() {
    307                 $this->modules->register(
     301                $this->script_modules->register(
    308302                        'foo',
    309303                        '/foo.js',
    … …  
    316310                        )
    317311                );
    318                 $this->modules->register(
     312                $this->script_modules->register(
    319313                        'static-dep',
    320314                        '/static-dep.js',
    … …  
    330324                        )
    331325                );
    332                 $this->modules->register( 'dynamic-dep', '/dynamic-dep.js' );
    333                 $this->modules->register( 'nested-static-dep', '/nested-static-dep.js' );
    334                 $this->modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
    335                 $this->modules->register( 'no-dep', '/no-dep.js' );
    336                 $this->modules->enqueue( 'foo' );
    337 
    338                 $preloaded_modules = $this->get_preloaded_modules();
    339 
    340                 $this->assertCount( 2, $preloaded_modules );
    341                 $this->assertStringStartsWith( '/static-dep.js', $preloaded_modules['static-dep'] );
    342                 $this->assertStringStartsWith( '/nested-static-dep.js', $preloaded_modules['nested-static-dep'] );
    343                 $this->assertFalse( isset( $preloaded_modules['no-dep'] ) );
    344                 $this->assertFalse( isset( $preloaded_modules['dynamic-dep'] ) );
    345                 $this->assertFalse( isset( $preloaded_modules['nested-dynamic-dep'] ) );
     326                $this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js' );
     327                $this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
     328                $this->script_modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
     329                $this->script_modules->register( 'no-dep', '/no-dep.js' );
     330                $this->script_modules->enqueue( 'foo' );
     331
     332                $preloaded_script_modules = $this->get_preloaded_script_modules();
     333
     334                $this->assertCount( 2, $preloaded_script_modules );
     335                $this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
     336                $this->assertStringStartsWith( '/nested-static-dep.js', $preloaded_script_modules['nested-static-dep'] );
     337                $this->assertFalse( isset( $preloaded_script_modules['no-dep'] ) );
     338                $this->assertFalse( isset( $preloaded_script_modules['dynamic-dep'] ) );
     339                $this->assertFalse( isset( $preloaded_script_modules['nested-dynamic-dep'] ) );
    346340        }
    347341
    … …  
    353347         * @covers ::register()
    354348         * @covers ::enqueue()
    355          * @covers ::print_module_preloads()
     349         * @covers ::print_script_module_preloads()
    356350         */
    357351        public function test_wp_dont_preload_static_dependencies_of_dynamic_dependencies() {
    358                 $this->modules->register(
     352                $this->script_modules->register(
    359353                        'foo',
    360354                        '/foo.js',
    … …  
    367361                        )
    368362                );
    369                 $this->modules->register( 'static-dep', '/static-dep.js' );
    370                 $this->modules->register( 'dynamic-dep', '/dynamic-dep.js', array( 'nested-static-dep' ) );
    371                 $this->modules->register( 'nested-static-dep', '/nested-static-dep.js' );
    372                 $this->modules->register( 'no-dep', '/no-dep.js' );
    373                 $this->modules->enqueue( 'foo' );
    374 
    375                 $preloaded_modules = $this->get_preloaded_modules();
    376 
    377                 $this->assertCount( 1, $preloaded_modules );
    378                 $this->assertStringStartsWith( '/static-dep.js', $preloaded_modules['static-dep'] );
    379                 $this->assertFalse( isset( $preloaded_modules['dynamic-dep'] ) );
    380                 $this->assertFalse( isset( $preloaded_modules['nested-static-dep'] ) );
    381                 $this->assertFalse( isset( $preloaded_modules['no-dep'] ) );
    382         }
    383 
    384         /**
    385          * Tests that preloaded dependencies don't include enqueued modules.
    386          *
    387          * @ticket 56313
    388          *
    389          * @covers ::register()
    390          * @covers ::enqueue()
    391          * @covers ::print_module_preloads()
    392          */
    393         public function test_wp_preloaded_dependencies_filter_enqueued_modules() {
    394                 $this->modules->register(
     363                $this->script_modules->register( 'static-dep', '/static-dep.js' );
     364                $this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js', array( 'nested-static-dep' ) );
     365                $this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
     366                $this->script_modules->register( 'no-dep', '/no-dep.js' );
     367                $this->script_modules->enqueue( 'foo' );
     368
     369                $preloaded_script_modules = $this->get_preloaded_script_modules();
     370
     371                $this->assertCount( 1, $preloaded_script_modules );
     372                $this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
     373                $this->assertFalse( isset( $preloaded_script_modules['dynamic-dep'] ) );
     374                $this->assertFalse( isset( $preloaded_script_modules['nested-static-dep'] ) );
     375                $this->assertFalse( isset( $preloaded_script_modules['no-dep'] ) );
     376        }
     377
     378        /**
     379         * Tests that preloaded dependencies don't include enqueued script modules.
     380         *
     381         * @ticket 56313
     382         *
     383         * @covers ::register()
     384         * @covers ::enqueue()
     385         * @covers ::print_script_module_preloads()
     386         */
     387        public function test_wp_preloaded_dependencies_filter_enqueued_script_modules() {
     388                $this->script_modules->register(
    395389                        'foo',
    396390                        '/foo.js',
    … …  
    400394                        )
    401395                );
    402                 $this->modules->register( 'dep', '/dep.js' );
    403                 $this->modules->register( 'enqueued-dep', '/enqueued-dep.js' );
    404                 $this->modules->enqueue( 'foo' );
    405                 $this->modules->enqueue( 'enqueued-dep' ); // Not preloaded.
    406 
    407                 $preloaded_modules = $this->get_preloaded_modules();
    408 
    409                 $this->assertCount( 1, $preloaded_modules );
    410                 $this->assertTrue( isset( $preloaded_modules['dep'] ) );
    411                 $this->assertFalse( isset( $preloaded_modules['enqueued-dep'] ) );
    412         }
    413 
    414         /**
    415          * Tests that enqueued modules with dependants correctly add both the module
    416          * and its dependencies to the import map.
     396                $this->script_modules->register( 'dep', '/dep.js' );
     397                $this->script_modules->register( 'enqueued-dep', '/enqueued-dep.js' );
     398                $this->script_modules->enqueue( 'foo' );
     399                $this->script_modules->enqueue( 'enqueued-dep' ); // Not preloaded.
     400
     401                $preloaded_script_modules = $this->get_preloaded_script_modules();
     402
     403                $this->assertCount( 1, $preloaded_script_modules );
     404                $this->assertTrue( isset( $preloaded_script_modules['dep'] ) );
     405                $this->assertFalse( isset( $preloaded_script_modules['enqueued-dep'] ) );
     406        }
     407
     408        /**
     409         * Tests that enqueued script modules with dependants correctly add both the
     410         * script module and its dependencies to the import map.
    417411         *
    418412         * @ticket 56313
    … …  
    422416         * @covers ::print_import_map()
    423417         */
    424         public function test_wp_enqueued_modules_with_dependants_add_import_map() {
    425                 $this->modules->register(
     418        public function test_wp_enqueued_script_modules_with_dependants_add_import_map() {
     419                $this->script_modules->register(
    426420                        'foo',
    427421                        '/foo.js',
    … …  
    431425                        )
    432426                );
    433                 $this->modules->register( 'dep', '/dep.js' );
    434                 $this->modules->register( 'enqueued-dep', '/enqueued-dep.js' );
    435                 $this->modules->enqueue( 'foo' );
    436                 $this->modules->enqueue( 'enqueued-dep' ); // Also in the import map.
     427                $this->script_modules->register( 'dep', '/dep.js' );
     428                $this->script_modules->register( 'enqueued-dep', '/enqueued-dep.js' );
     429                $this->script_modules->enqueue( 'foo' );
     430                $this->script_modules->enqueue( 'enqueued-dep' ); // Also in the import map.
    437431
    438432                $import_map = $this->get_import_map();
    … …  
    452446         */
    453447        public function test_get_versioned_src() {
    454                 $get_versioned_src = new ReflectionMethod( $this->modules, 'get_versioned_src' );
     448                $get_versioned_src = new ReflectionMethod( $this->script_modules, 'get_versioned_src' );
    455449                $get_versioned_src->setAccessible( true );
    456450
    … …  
    460454                );
    461455
    462                 $result = $get_versioned_src->invoke( $this->modules, $module_with_version );
     456                $result = $get_versioned_src->invoke( $this->script_modules, $module_with_version );
    463457                $this->assertEquals( 'http://example.com/module.js?ver=1.0', $result );
    464458
    … …  
    468462                );
    469463
    470                 $result = $get_versioned_src->invoke( $this->modules, $module_without_version );
     464                $result = $get_versioned_src->invoke( $this->script_modules, $module_without_version );
    471465                $this->assertEquals( 'http://example.com/module.js', $result );
    472466
    … …  
    476470                );
    477471
    478                 $result = $get_versioned_src->invoke( $this->modules, $module_with_wp_version );
     472                $result = $get_versioned_src->invoke( $this->script_modules, $module_with_wp_version );
    479473                $this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result );
    480474
    … …  
    484478                );
    485479
    486                 $result = $get_versioned_src->invoke( $this->modules, $module_with_existing_query_string );
     480                $result = $get_versioned_src->invoke( $this->script_modules, $module_with_existing_query_string );
    487481                $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result );
    488482        }
    … …  
    490484        /**
    491485         * Tests that the correct version is propagated to the import map, enqueued
    492          * modules and preloaded modules.
    493          *
    494          * @ticket 56313
    495          *
    496          * @covers ::register()
    497          * @covers ::enqueue()
    498          * @covers ::print_enqueued_modules()
     486         * script modules and preloaded script modules.
     487         *
     488         * @ticket 56313
     489         *
     490         * @covers ::register()
     491         * @covers ::enqueue()
     492         * @covers ::print_enqueued_script_modules()
    499493         * @covers ::print_import_map()
    500          * @covers ::print_module_preloads()
     494         * @covers ::print_script_module_preloads()
    501495         * @covers ::get_version_query_string()
    502496         */
    503497        public function test_version_is_propagated_correctly() {
    504                 $this->modules->register(
     498                $this->script_modules->register(
    505499                        'foo',
    506500                        '/foo.js',
    … …  
    510504                        '1.0'
    511505                );
    512                 $this->modules->register( 'dep', '/dep.js', array(), '2.0' );
    513                 $this->modules->enqueue( 'foo' );
    514 
    515                 $enqueued_modules = $this->get_enqueued_modules();
    516                 $this->assertEquals( '/foo.js?ver=1.0', $enqueued_modules['foo'] );
     506                $this->script_modules->register( 'dep', '/dep.js', array(), '2.0' );
     507                $this->script_modules->enqueue( 'foo' );
     508
     509                $enqueued_script_modules = $this->get_enqueued_script_modules();
     510                $this->assertEquals( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
    517511
    518512                $import_map = $this->get_import_map();
    519513                $this->assertEquals( '/dep.js?ver=2.0', $import_map['dep'] );
    520514
    521                 $preloaded_modules = $this->get_preloaded_modules();
    522                 $this->assertEquals( '/dep.js?ver=2.0', $preloaded_modules['dep'] );
    523         }
    524 
    525         /**
    526          * Tests that it can print the enqueued modules multiple times, and it will
    527          * only print the modules that have not been printed before.
    528          *
    529          * @ticket 56313
    530          *
    531          * @covers ::register()
    532          * @covers ::enqueue()
    533          * @covers ::print_enqueued_modules()
    534          */
    535         public function test_print_enqueued_modules_can_be_called_multiple_times() {
    536                 $this->modules->register( 'foo', '/foo.js' );
    537                 $this->modules->register( 'bar', '/bar.js' );
    538                 $this->modules->enqueue( 'foo' );
    539 
    540                 $enqueued_modules = $this->get_enqueued_modules();
    541                 $this->assertCount( 1, $enqueued_modules );
    542                 $this->assertTrue( isset( $enqueued_modules['foo'] ) );
    543 
    544                 $this->modules->enqueue( 'bar' );
    545 
    546                 $enqueued_modules = $this->get_enqueued_modules();
    547                 $this->assertCount( 1, $enqueued_modules );
    548                 $this->assertTrue( isset( $enqueued_modules['bar'] ) );
    549 
    550                 $enqueued_modules = $this->get_enqueued_modules();
    551                 $this->assertCount( 0, $enqueued_modules );
    552         }
    553 
    554         /**
    555          * Tests that it can print the preloaded modules multiple times, and it will
    556          * only print the modules that have not been printed before.
    557          *
    558          * @ticket 56313
    559          *
    560          * @covers ::register()
    561          * @covers ::enqueue()
    562          * @covers ::print_module_preloads()
    563          */
    564         public function test_print_preloaded_modules_can_be_called_multiple_times() {
    565                 $this->modules->register( 'foo', '/foo.js', array( 'static-dep-1', 'static-dep-2' ) );
    566                 $this->modules->register( 'bar', '/bar.js', array( 'static-dep-3' ) );
    567                 $this->modules->register( 'static-dep-1', '/static-dep-1.js' );
    568                 $this->modules->register( 'static-dep-3', '/static-dep-3.js' );
    569                 $this->modules->enqueue( 'foo' );
    570 
    571                 $preloaded_modules = $this->get_preloaded_modules();
    572                 $this->assertCount( 1, $preloaded_modules );
    573                 $this->assertTrue( isset( $preloaded_modules['static-dep-1'] ) );
    574 
    575                 $this->modules->register( 'static-dep-2', '/static-dep-2.js' );
    576                 $this->modules->enqueue( 'bar' );
    577 
    578                 $preloaded_modules = $this->get_preloaded_modules();
    579                 $this->assertCount( 2, $preloaded_modules );
    580                 $this->assertTrue( isset( $preloaded_modules['static-dep-2'] ) );
    581                 $this->assertTrue( isset( $preloaded_modules['static-dep-3'] ) );
    582 
    583                 $preloaded_modules = $this->get_preloaded_modules();
    584                 $this->assertCount( 0, $preloaded_modules );
    585         }
    586 
    587         /**
    588          * Tests that a module is not registered when calling enqueue without a valid
     515                $preloaded_script_modules = $this->get_preloaded_script_modules();
     516                $this->assertEquals( '/dep.js?ver=2.0', $preloaded_script_modules['dep'] );
     517        }
     518
     519        /**
     520         * Tests that it can print the enqueued script modules multiple times, and it
     521         * will only print the script modules that have not been printed before.
     522         *
     523         * @ticket 56313
     524         *
     525         * @covers ::register()
     526         * @covers ::enqueue()
     527         * @covers ::print_enqueued_script_modules()
     528         */
     529        public function test_print_enqueued_script_modules_can_be_called_multiple_times() {
     530                $this->script_modules->register( 'foo', '/foo.js' );
     531                $this->script_modules->register( 'bar', '/bar.js' );
     532                $this->script_modules->enqueue( 'foo' );
     533
     534                $enqueued_script_modules = $this->get_enqueued_script_modules();
     535                $this->assertCount( 1, $enqueued_script_modules );
     536                $this->assertTrue( isset( $enqueued_script_modules['foo'] ) );
     537
     538                $this->script_modules->enqueue( 'bar' );
     539
     540                $enqueued_script_modules = $this->get_enqueued_script_modules();
     541                $this->assertCount( 1, $enqueued_script_modules );
     542                $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
     543
     544                $enqueued_script_modules = $this->get_enqueued_script_modules();
     545                $this->assertCount( 0, $enqueued_script_modules );
     546        }
     547
     548        /**
     549         * Tests that it can print the preloaded script modules multiple times, and it
     550         * will only print the script modules that have not been printed before.
     551         *
     552         * @ticket 56313
     553         *
     554         * @covers ::register()
     555         * @covers ::enqueue()
     556         * @covers ::print_script_module_preloads()
     557         */
     558        public function test_print_preloaded_script_modules_can_be_called_multiple_times() {
     559                $this->script_modules->register( 'foo', '/foo.js', array( 'static-dep-1', 'static-dep-2' ) );
     560                $this->script_modules->register( 'bar', '/bar.js', array( 'static-dep-3' ) );
     561                $this->script_modules->register( 'static-dep-1', '/static-dep-1.js' );
     562                $this->script_modules->register( 'static-dep-3', '/static-dep-3.js' );
     563                $this->script_modules->enqueue( 'foo' );
     564
     565                $preloaded_script_modules = $this->get_preloaded_script_modules();
     566                $this->assertCount( 1, $preloaded_script_modules );
     567                $this->assertTrue( isset( $preloaded_script_modules['static-dep-1'] ) );
     568
     569                $this->script_modules->register( 'static-dep-2', '/static-dep-2.js' );
     570                $this->script_modules->enqueue( 'bar' );
     571
     572                $preloaded_script_modules = $this->get_preloaded_script_modules();
     573                $this->assertCount( 2, $preloaded_script_modules );
     574                $this->assertTrue( isset( $preloaded_script_modules['static-dep-2'] ) );
     575                $this->assertTrue( isset( $preloaded_script_modules['static-dep-3'] ) );
     576
     577                $preloaded_script_modules = $this->get_preloaded_script_modules();
     578                $this->assertCount( 0, $preloaded_script_modules );
     579        }
     580
     581        /**
     582         * Tests that a script module is not registered when calling enqueue without a
     583         * valid src.
     584         *
     585         * @ticket 56313
     586         *
     587         * @covers ::enqueue()
     588         * @covers ::print_enqueued_script_modules()
     589         */
     590        public function test_wp_enqueue_script_module_doesnt_register_without_a_valid_src() {
     591                $this->script_modules->enqueue( 'foo' );
     592
     593                $enqueued_script_modules = $this->get_enqueued_script_modules();
     594
     595                $this->assertCount( 0, $enqueued_script_modules );
     596                $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     597        }
     598
     599        /**
     600         * Tests that a script module is registered when calling enqueue with a valid
    589601         * src.
    590602         *
    … …  
    592604         *
    593605         * @covers ::enqueue()
    594          * @covers ::print_enqueued_modules()
    595          */
    596         public function test_wp_enqueue_module_doesnt_register_without_a_valid_src() {
    597                 $this->modules->enqueue( 'foo' );
    598 
    599                 $enqueued_modules = $this->get_enqueued_modules();
    600 
    601                 $this->assertCount( 0, $enqueued_modules );
    602                 $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    603         }
    604 
    605         /**
    606          * Tests that a module is registered when calling enqueue with a valid src.
    607          *
    608          * @ticket 56313
    609          *
    610          * @covers ::enqueue()
    611          * @covers ::print_enqueued_modules()
    612          */
    613         public function test_wp_enqueue_module_registers_with_valid_src() {
    614                 $this->modules->enqueue( 'foo', '/foo.js' );
    615 
    616                 $enqueued_modules = $this->get_enqueued_modules();
    617 
    618                 $this->assertCount( 1, $enqueued_modules );
    619                 $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    620         }
    621 
    622         /**
    623          * Tests that a module is registered when calling enqueue with a valid src the
    624          * second time.
    625          *
    626          * @ticket 56313
    627          *
    628          * @covers ::enqueue()
    629          * @covers ::print_enqueued_modules()
    630          */
    631         public function test_wp_enqueue_module_registers_with_valid_src_the_second_time() {
    632                 $this->modules->enqueue( 'foo' ); // Not valid src.
    633 
    634                 $enqueued_modules = $this->get_enqueued_modules();
    635 
    636                 $this->assertCount( 0, $enqueued_modules );
    637                 $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    638 
    639                 $this->modules->enqueue( 'foo', '/foo.js' ); // Valid src.
    640 
    641                 $enqueued_modules = $this->get_enqueued_modules();
    642 
    643                 $this->assertCount( 1, $enqueued_modules );
    644                 $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    645         }
    646 
    647         /**
    648          * Tests that a module is registered with all the params when calling enqueue.
    649          *
    650          * @ticket 56313
    651          *
    652          * @covers ::register()
    653          * @covers ::enqueue()
    654          * @covers ::print_enqueued_modules()
     606         * @covers ::print_enqueued_script_modules()
     607         */
     608        public function test_wp_enqueue_script_module_registers_with_valid_src() {
     609                $this->script_modules->enqueue( 'foo', '/foo.js' );
     610
     611                $enqueued_script_modules = $this->get_enqueued_script_modules();
     612
     613                $this->assertCount( 1, $enqueued_script_modules );
     614                $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     615        }
     616
     617        /**
     618         * Tests that a script module is registered when calling enqueue with a valid
     619         * src the second time.
     620         *
     621         * @ticket 56313
     622         *
     623         * @covers ::enqueue()
     624         * @covers ::print_enqueued_script_modules()
     625         */
     626        public function test_wp_enqueue_script_module_registers_with_valid_src_the_second_time() {
     627                $this->script_modules->enqueue( 'foo' ); // Not valid src.
     628
     629                $enqueued_script_modules = $this->get_enqueued_script_modules();
     630
     631                $this->assertCount( 0, $enqueued_script_modules );
     632                $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     633
     634                $this->script_modules->enqueue( 'foo', '/foo.js' ); // Valid src.
     635
     636                $enqueued_script_modules = $this->get_enqueued_script_modules();
     637
     638                $this->assertCount( 1, $enqueued_script_modules );
     639                $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     640        }
     641
     642        /**
     643         * Tests that a script module is registered with all the params when calling
     644         * enqueue.
     645         *
     646         * @ticket 56313
     647         *
     648         * @covers ::register()
     649         * @covers ::enqueue()
     650         * @covers ::print_enqueued_script_modules()
    655651         * @covers ::print_import_map()
    656652         */
    657         public function test_wp_enqueue_module_registers_all_params() {
    658                 $this->modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
    659                 $this->modules->register( 'dep', '/dep.js' );
    660 
    661                 $enqueued_modules = $this->get_enqueued_modules();
    662                 $import_map       = $this->get_import_map();
    663 
    664                 $this->assertCount( 1, $enqueued_modules );
    665                 $this->assertEquals( '/foo.js?ver=1.0', $enqueued_modules['foo'] );
     653        public function test_wp_enqueue_script_module_registers_all_params() {
     654                $this->script_modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
     655                $this->script_modules->register( 'dep', '/dep.js' );
     656
     657                $enqueued_script_modules = $this->get_enqueued_script_modules();
     658                $import_map              = $this->get_import_map();
     659
     660                $this->assertCount( 1, $enqueued_script_modules );
     661                $this->assertEquals( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
    666662                $this->assertCount( 1, $import_map );
    667663                $this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
Note: See TracChangeset for help on using the changeset viewer.