Changeset 57844
- Timestamp:
- 03/15/2024 02:59:10 PM (10 months ago)
- Location:
- branches/6.5
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.5
-
branches/6.5/src/wp-includes/class-wp-script-modules.php
r57644 r57844 192 192 array( 193 193 'type' => 'module', 194 'src' => $this->get_ versioned_src( $script_module),194 'src' => $this->get_src( $id ), 195 195 'id' => $id . '-js-module', 196 196 ) … … 213 213 echo sprintf( 214 214 '<link rel="modulepreload" href="%s" id="%s">', 215 esc_url( $this->get_ versioned_src( $script_module) ),215 esc_url( $this->get_src( $id ) ), 216 216 esc_attr( $id . '-js-modulepreload' ) 217 217 ); … … 265 265 $imports = array(); 266 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);267 $imports[ $id ] = $this->get_src( $id ); 268 268 } 269 269 return array( 'imports' => $imports ); … … 332 332 * @since 6.5.0 333 333 * 334 * @param array $script_module The script module.334 * @param string $id The script module identifier. 335 335 * @return string The script module src with a version if relevant. 336 336 */ 337 private function get_versioned_src( array $script_module ): string { 338 $args = array(); 337 private function get_src( string $id ): string { 338 if ( ! isset( $this->registered[ $id ] ) ) { 339 return ''; 340 } 341 342 $script_module = $this->registered[ $id ]; 343 $src = $script_module['src']; 344 339 345 if ( false === $script_module['version'] ) { 340 $ args['ver'] = get_bloginfo( 'version');346 $src = add_query_arg( 'ver', get_bloginfo( 'version' ), $src ); 341 347 } elseif ( null !== $script_module['version'] ) { 342 $args['ver'] = $script_module['version']; 343 } 344 if ( $args ) { 345 return add_query_arg( $args, $script_module['src'] ); 346 } 347 return $script_module['src']; 348 $src = add_query_arg( 'ver', $script_module['version'], $src ); 349 } 350 351 /** 352 * Filters the script module source. 353 * 354 * @since 6.5.0 355 * 356 * @param string $src Module source url. 357 * @param string $id Module identifier. 358 */ 359 $src = apply_filters( 'script_module_loader_src', $src, $id ); 360 361 return $src; 348 362 } 349 363 } -
branches/6.5/tests/phpunit/tests/script-modules/wpScriptModules.php
r57593 r57844 513 513 514 514 /** 515 * Tests the functionality of the `get_ versioned_src` method to ensure515 * Tests the functionality of the `get_src` method to ensure 516 516 * proper URLs with version strings are returned. 517 517 * 518 518 * @ticket 56313 519 519 * 520 * @covers ::get_versioned_src() 521 */ 522 public function test_get_versioned_src() { 523 $get_versioned_src = new ReflectionMethod( $this->script_modules, 'get_versioned_src' ); 524 $get_versioned_src->setAccessible( true ); 525 526 $module_with_version = array( 527 'src' => 'http://example.com/module.js', 528 'version' => '1.0', 529 ); 530 531 $result = $get_versioned_src->invoke( $this->script_modules, $module_with_version ); 520 * @covers ::get_src() 521 */ 522 public function test_get_src() { 523 $get_src = new ReflectionMethod( $this->script_modules, 'get_src' ); 524 $get_src->setAccessible( true ); 525 526 $this->script_modules->register( 527 'module_with_version', 528 'http://example.com/module.js', 529 array(), 530 '1.0' 531 ); 532 533 $result = $get_src->invoke( $this->script_modules, 'module_with_version' ); 532 534 $this->assertEquals( 'http://example.com/module.js?ver=1.0', $result ); 533 535 534 $module_without_version = array( 535 'src' => 'http://example.com/module.js', 536 'version' => null, 537 ); 538 539 $result = $get_versioned_src->invoke( $this->script_modules, $module_without_version ); 536 $this->script_modules->register( 537 'module_without_version', 538 'http://example.com/module.js', 539 array(), 540 null 541 ); 542 543 $result = $get_src->invoke( $this->script_modules, 'module_without_version' ); 540 544 $this->assertEquals( 'http://example.com/module.js', $result ); 541 545 542 $module_with_wp_version = array( 543 'src' => 'http://example.com/module.js', 544 'version' => false, 545 ); 546 547 $result = $get_versioned_src->invoke( $this->script_modules, $module_with_wp_version ); 546 $this->script_modules->register( 547 'module_with_wp_version', 548 'http://example.com/module.js', 549 array(), 550 false 551 ); 552 553 $result = $get_src->invoke( $this->script_modules, 'module_with_wp_version' ); 548 554 $this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result ); 549 555 550 $module_with_existing_query_string = array( 551 'src' => 'http://example.com/module.js?foo=bar', 552 'version' => '1.0', 553 ); 554 555 $result = $get_versioned_src->invoke( $this->script_modules, $module_with_existing_query_string ); 556 $this->script_modules->register( 557 'module_with_existing_query_string', 558 'http://example.com/module.js?foo=bar', 559 array(), 560 '1.0' 561 ); 562 563 $result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' ); 556 564 $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result ); 565 566 // Filter the version to include the ID in the final URL, to test the filter, this should affect the tests below. 567 add_filter( 568 'script_module_loader_src', 569 function ( $src, $id ) { 570 return add_query_arg( 'script_module_id', urlencode( $id ), $src ); 571 }, 572 10, 573 2 574 ); 575 576 $result = $get_src->invoke( $this->script_modules, 'module_without_version' ); 577 $this->assertEquals( 'http://example.com/module.js?script_module_id=module_without_version', $result ); 578 579 $result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' ); 580 $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0&script_module_id=module_with_existing_query_string', $result ); 557 581 } 558 582
Note: See TracChangeset
for help on using the changeset viewer.