Changeset 57327
- Timestamp:
- 01/23/2024 03:32:03 AM (9 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-script-modules.php
r57271 r57327 1 1 <?php 2 2 /** 3 * Modules API: WP_Script_Modules class.3 * Script Modules API: WP_Script_Modules class. 4 4 * 5 5 * Native support for ES Modules and Import Maps. … … 10 10 11 11 /** 12 * Core class used to register modules.12 * Core class used to register script modules. 13 13 * 14 14 * @since 6.5.0 … … 16 16 class WP_Script_Modules { 17 17 /** 18 * Holds the registered modules, keyed bymodule identifier.18 * Holds the registered script modules, keyed by script module identifier. 19 19 * 20 20 * @since 6.5.0 … … 24 24 25 25 /** 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> 30 30 */ 31 31 private $enqueued_before_registered = array(); 32 32 33 33 /** 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 ] ) ) { 67 66 $dependencies = array(); 68 67 foreach ( $deps as $dependency ) { … … 86 85 } 87 86 88 $this->registered[ $ module_id ] = array(87 $this->registered[ $id ] = array( 89 88 'src' => $src, 90 89 'version' => $version, 91 'enqueue' => isset( $this->enqueued_before_registered[ $ module_id ] ),90 'enqueue' => isset( $this->enqueued_before_registered[ $id ] ), 92 91 'dependencies' => $dependencies, 93 92 'enqueued' => false, … … 98 97 99 98 /** 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; 140 135 } 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; 143 138 } 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 themodule.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 module164 * preloads.165 * 166 * It adds the actions to print the enqueued modules and module preloads to167 * both `wp_head` and `wp_footer` because in classic themes, the modules168 * used by the theme and plugins will likely be able to be printed in the169 * `head`, but the ones used by the blocks will need to be enqueued in the170 * `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 print174 * the ones that are available when the `wp_head` is rendered, so the browser175 * s tarts 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. 176 171 * 177 172 * 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. 179 175 * 180 176 * @since 6.5.0 181 177 */ 182 178 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' ) ); 187 183 add_action( 'wp_footer', array( $this, 'print_import_map' ) ); 188 184 } 189 185 190 186 /** 191 * Prints the enqueued modules using script tags with type="module"187 * Prints the enqueued script modules using script tags with type="module" 192 188 * attributes. 193 189 * 194 * If a enqueued module has already been printed, it will not be printed again195 * 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'] ) { 202 198 // Mark it as enqueued so it doesn't get enqueued again. 203 $this->registered[ $ module_id ]['enqueued'] = true;199 $this->registered[ $id ]['enqueued'] = true; 204 200 205 201 wp_print_script_tag( 206 202 array( 207 203 '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', 210 206 ) 211 207 ); … … 215 211 216 212 /** 217 * Prints the the static dependencies of the enqueued modules using link tags218 * with rel="modulepreload" attributes.219 * 220 * If a module is marked for enqueue, it will not be preloaded. If a preloaded221 * module has already been printed, it will not be printed again on subsequent222 * 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 ) { 228 224 // 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'] ) { 230 226 // Mark it as preloaded so it doesn't get preloaded again. 231 $this->registered[ $ module_id ]['preloaded'] = true;227 $this->registered[ $id ]['preloaded'] = true; 232 228 233 229 echo sprintf( 234 230 '<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' ) 237 233 ); 238 234 } … … 263 259 * @since 6.5.0 264 260 * 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 { 269 265 $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 ); 272 268 } 273 269 return array( 'imports' => $imports ); … … 275 271 276 272 /** 277 * Retrieves the list of modules marked for enqueue.278 * 279 * @since 6.5.0 280 * 281 * @return array Modules marked for enqueue, keyed bymodule 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 { 284 280 $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; 288 284 } 289 285 } … … 292 288 293 289 /** 294 * Retrieves all the dependencies for the given module identifiers, filtered295 * by import types.290 * Retrieves all the dependencies for the given script module identifiers, 291 * filtered by import types. 296 292 * 297 293 * It will consolidate an array containing a set of unique dependencies based … … 301 297 * @since 6.5.0 302 298 * 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' ) ) { 309 306 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 ) { 312 309 $dependencies = array(); 313 foreach ( $this->registered[ $ module_id ]['dependencies'] as $dependency ) {310 foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) { 314 311 if ( 315 312 in_array( $dependency['import'], $import_types, true ) && 316 313 isset( $this->registered[ $dependency['id'] ] ) && 317 ! isset( $dependency_ modules[ $dependency['id'] ] )314 ! isset( $dependency_script_modules[ $dependency['id'] ] ) 318 315 ) { 319 316 $dependencies[ $dependency['id'] ] = $this->registered[ $dependency['id'] ]; 320 317 } 321 318 } 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 ) ); 323 320 }, 324 321 array() … … 327 324 328 325 /** 329 * Gets the versioned URL for a module src.326 * Gets the versioned URL for a script module src. 330 327 * 331 328 * If $version is set to false, the version number is the currently installed … … 335 332 * @since 6.5.0 336 333 * 337 * @param array $ module Themodule.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 { 341 338 $args = array(); 342 if ( false === $ module['version'] ) {339 if ( false === $script_module['version'] ) { 343 340 $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']; 346 343 } 347 344 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']; 351 348 } 352 349 } -
trunk/src/wp-includes/script-modules.php
r57269 r57327 1 1 <?php 2 2 /** 3 * Script Modules API: Module functions3 * Script Modules API: Script Module functions 4 4 * 5 5 * @since 6.5.0 … … 19 19 * @return WP_Script_Modules The main WP_Script_Modules instance. 20 20 */ 21 function wp_ modules(){21 function wp_script_modules(): WP_Script_Modules { 22 22 static $instance = null; 23 23 if ( is_null( $instance ) ) { … … 29 29 30 30 /** 31 * Registers the module if no module with that module identifier has already32 * been registered.31 * Registers the script module if no script module with that script module 32 * identifier has already been registered. 33 33 * 34 34 * @since 6.5.0 35 35 * 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. 61 60 */ 62 function wp_register_ module( $module_id, $src,$deps = array(), $version = false ) {63 wp_ modules()->register( $module_id, $src, $deps, $version );61 function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) { 62 wp_script_modules()->register( $id, $src, $deps, $version ); 64 63 } 65 64 66 65 /** 67 * Marks the module to be enqueued in the page.66 * Marks the script module to be enqueued in the page. 68 67 * 69 * If a src is provided and the module has not been registered yet, it will be70 * registered.68 * If a src is provided and the script module has not been registered yet, it 69 * will be registered. 71 70 * 72 71 * @since 6.5.0 73 72 * 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. 102 97 */ 103 function wp_enqueue_ module( $module_id, $src = '',$deps = array(), $version = false ) {104 wp_ modules()->enqueue( $module_id, $src, $deps, $version );98 function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) { 99 wp_script_modules()->enqueue( $id, $src, $deps, $version ); 105 100 } 106 101 107 102 /** 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. 109 104 * 110 105 * @since 6.5.0 111 106 * 112 * @param string $ module_id The identifier of themodule.107 * @param string $id The identifier of the script module. 113 108 */ 114 function wp_dequeue_ module( $module_id ) {115 wp_ modules()->dequeue( $module_id );109 function wp_dequeue_script_module( string $id ) { 110 wp_script_modules()->dequeue( $id ); 116 111 } -
trunk/tests/phpunit/tests/script-modules/wpScriptModules.php
r57269 r57327 18 18 * @var WP_Script_Modules 19 19 */ 20 protected $ modules;20 protected $script_modules; 21 21 22 22 /** … … 25 25 public function set_up() { 26 26 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 } 48 45 } 49 46 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. 57 54 */ 58 55 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' ) ); 60 57 preg_match( '/<script type="importmap" id="wp-importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string ); 61 58 return json_decode( $import_map_string[1], true )['imports']; … … 63 60 64 61 /** 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 } 82 76 } 83 77 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. 111 105 * 112 106 * @ticket 56313 … … 115 109 * @covers ::enqueue() 116 110 * @covers ::dequeue() 117 * @covers ::print_enqueued_ modules()111 * @covers ::print_enqueued_script_modules() 118 112 */ 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 be135 * 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. 136 130 * 137 131 * @ticket 56313 … … 139 133 * @covers ::register() 140 134 * @covers ::enqueue() 141 * @covers ::print_enqueued_ modules()135 * @covers ::print_enqueued_script_modules() 142 136 */ 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 ensures157 * 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. 158 152 * 159 153 * @ticket 56313 … … 162 156 * @covers ::enqueue() 163 157 * @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'] ) ); 178 172 } 179 173 180 174 /** 181 175 * 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. 183 177 * 184 178 * @ticket 56313 … … 189 183 */ 190 184 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' ); 195 189 196 190 $import_map = $this->get_import_map(); … … 203 197 /** 204 198 * 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. 206 200 * 207 201 * @ticket 56313 … … 212 206 */ 213 207 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' ); 219 213 220 214 $import_map = $this->get_import_map(); … … 235 229 */ 236 230 public function test_wp_import_map_recursive_dependencies() { 237 $this-> modules->register(231 $this->script_modules->register( 238 232 'foo', 239 233 '/foo.js', … … 246 240 ) 247 241 ); 248 $this-> modules->register(242 $this->script_modules->register( 249 243 'static-dep', 250 244 '/static-dep.js', … … 260 254 ) 261 255 ); 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' ); 267 261 268 262 $import_map = $this->get_import_map(); … … 286 280 */ 287 281 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' ) ); 292 286 293 287 $this->assertEmpty( $import_map_markup ); … … 302 296 * @covers ::register() 303 297 * @covers ::enqueue() 304 * @covers ::print_ module_preloads()298 * @covers ::print_script_module_preloads() 305 299 */ 306 300 public function test_wp_enqueue_preloaded_static_dependencies() { 307 $this-> modules->register(301 $this->script_modules->register( 308 302 'foo', 309 303 '/foo.js', … … 316 310 ) 317 311 ); 318 $this-> modules->register(312 $this->script_modules->register( 319 313 'static-dep', 320 314 '/static-dep.js', … … 330 324 ) 331 325 ); 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'] ) ); 346 340 } 347 341 … … 353 347 * @covers ::register() 354 348 * @covers ::enqueue() 355 * @covers ::print_ module_preloads()349 * @covers ::print_script_module_preloads() 356 350 */ 357 351 public function test_wp_dont_preload_static_dependencies_of_dynamic_dependencies() { 358 $this-> modules->register(352 $this->script_modules->register( 359 353 'foo', 360 354 '/foo.js', … … 367 361 ) 368 362 ); 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( 395 389 'foo', 396 390 '/foo.js', … … 400 394 ) 401 395 ); 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 module416 * 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. 417 411 * 418 412 * @ticket 56313 … … 422 416 * @covers ::print_import_map() 423 417 */ 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( 426 420 'foo', 427 421 '/foo.js', … … 431 425 ) 432 426 ); 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. 437 431 438 432 $import_map = $this->get_import_map(); … … 452 446 */ 453 447 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' ); 455 449 $get_versioned_src->setAccessible( true ); 456 450 … … 460 454 ); 461 455 462 $result = $get_versioned_src->invoke( $this-> modules, $module_with_version );456 $result = $get_versioned_src->invoke( $this->script_modules, $module_with_version ); 463 457 $this->assertEquals( 'http://example.com/module.js?ver=1.0', $result ); 464 458 … … 468 462 ); 469 463 470 $result = $get_versioned_src->invoke( $this-> modules, $module_without_version );464 $result = $get_versioned_src->invoke( $this->script_modules, $module_without_version ); 471 465 $this->assertEquals( 'http://example.com/module.js', $result ); 472 466 … … 476 470 ); 477 471 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 ); 479 473 $this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result ); 480 474 … … 484 478 ); 485 479 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 ); 487 481 $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result ); 488 482 } … … 490 484 /** 491 485 * Tests that the correct version is propagated to the import map, enqueued 492 * modules and preloadedmodules.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() 499 493 * @covers ::print_import_map() 500 * @covers ::print_ module_preloads()494 * @covers ::print_script_module_preloads() 501 495 * @covers ::get_version_query_string() 502 496 */ 503 497 public function test_version_is_propagated_correctly() { 504 $this-> modules->register(498 $this->script_modules->register( 505 499 'foo', 506 500 '/foo.js', … … 510 504 '1.0' 511 505 ); 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'] ); 517 511 518 512 $import_map = $this->get_import_map(); 519 513 $this->assertEquals( '/dep.js?ver=2.0', $import_map['dep'] ); 520 514 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 589 601 * src. 590 602 * … … 592 604 * 593 605 * @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() 655 651 * @covers ::print_import_map() 656 652 */ 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'] ); 666 662 $this->assertCount( 1, $import_map ); 667 663 $this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
Note: See TracChangeset
for help on using the changeset viewer.