Skip to content

Commit c4d8047

Browse files
committed
Script Loader: Fix adding default version to script/style URL when args are supplied via enqueued handle.
Also fixes phpdoc for some member variables of `WP_Scripts` and `WP_Styles`. Developed in #10608 Follow-up to [61358]. Props westonruter, peterwilsoncc. See #64224, #64238. Fixes #64372. git-svn-id: https://develop.svn.wordpress.org/trunk@61397 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6cb3085 commit c4d8047

File tree

4 files changed

+274
-47
lines changed

4 files changed

+274
-47
lines changed

src/wp-includes/class-wp-scripts.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,26 @@ class WP_Scripts extends WP_Dependencies {
2222
* Full URL with trailing slash.
2323
*
2424
* @since 2.6.0
25-
* @var string
25+
* @see wp_default_scripts()
26+
* @var string|null
2627
*/
2728
public $base_url;
2829

2930
/**
3031
* URL of the content directory.
3132
*
3233
* @since 2.8.0
33-
* @var string
34+
* @see wp_default_scripts()
35+
* @var string|null
3436
*/
3537
public $content_url;
3638

3739
/**
3840
* Default version string for scripts.
3941
*
4042
* @since 2.6.0
41-
* @var string
43+
* @see wp_default_scripts()
44+
* @var string|null
4245
*/
4346
public $default_version;
4447

@@ -118,6 +121,7 @@ class WP_Scripts extends WP_Dependencies {
118121
* List of default directories.
119122
*
120123
* @since 2.8.0
124+
* @see wp_default_scripts()
121125
* @var string[]|null
122126
*/
123127
public $default_dirs;
@@ -413,9 +417,19 @@ public function do_item( $handle, $group = false ) {
413417
$src = $this->base_url . $src;
414418
}
415419

416-
if ( ! empty( $ver ) ) {
417-
$src = add_query_arg( 'ver', $ver, $src );
420+
$query_args = array();
421+
if ( empty( $obj->ver ) && null !== $obj->ver && is_string( $this->default_version ) ) {
422+
$query_args['ver'] = $this->default_version;
423+
} elseif ( is_scalar( $obj->ver ) ) {
424+
$query_args['ver'] = (string) $obj->ver;
425+
}
426+
if ( isset( $this->args[ $handle ] ) ) {
427+
parse_str( $this->args[ $handle ], $parsed_args );
428+
if ( $parsed_args ) {
429+
$query_args = array_merge( $query_args, $parsed_args );
430+
}
418431
}
432+
$src = add_query_arg( rawurlencode_deep( $query_args ), $src );
419433

420434
/** This filter is documented in wp-includes/class-wp-scripts.php */
421435
$src = esc_url_raw( apply_filters( 'script_loader_src', $src, $handle ) );

src/wp-includes/class-wp-styles.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@ class WP_Styles extends WP_Dependencies {
2222
* Full URL with trailing slash.
2323
*
2424
* @since 2.6.0
25-
* @var string
25+
* @see wp_default_styles()
26+
* @var string|null
2627
*/
2728
public $base_url;
2829

2930
/**
3031
* URL of the content directory.
3132
*
3233
* @since 2.8.0
33-
* @var string
34+
* @see wp_default_styles()
35+
* @var string|null
3436
*/
3537
public $content_url;
3638

3739
/**
3840
* Default version string for stylesheets.
3941
*
4042
* @since 2.6.0
41-
* @var string
43+
* @see wp_default_styles()
44+
* @var string|null
4245
*/
4346
public $default_version;
4447

4548
/**
4649
* The current text direction.
4750
*
4851
* @since 2.6.0
52+
* @see wp_default_styles()
4953
* @var string
5054
*/
5155
public $text_direction = 'ltr';
@@ -96,6 +100,7 @@ class WP_Styles extends WP_Dependencies {
96100
* List of default directories.
97101
*
98102
* @since 2.8.0
103+
* @see wp_default_styles()
99104
* @var string[]|null
100105
*/
101106
public $default_dirs;
@@ -218,7 +223,7 @@ public function do_item( $handle, $group = false ) {
218223
return true;
219224
}
220225

221-
$href = $this->_css_href( $src, $ver, $handle );
226+
$href = $this->_css_href( $src, $obj->ver, $handle );
222227
if ( ! $href ) {
223228
return true;
224229
}
@@ -425,19 +430,29 @@ public function all_deps( $handles, $recursion = false, $group = false ) {
425430
*
426431
* @since 2.6.0
427432
*
428-
* @param string $src The source of the enqueued style.
429-
* @param string $ver The version of the enqueued style.
430-
* @param string $handle The style's registered handle.
433+
* @param string $src The source of the enqueued style.
434+
* @param string|false|null $ver The version of the enqueued style.
435+
* @param string $handle The style's registered handle.
431436
* @return string Style's fully-qualified URL.
432437
*/
433438
public function _css_href( $src, $ver, $handle ) {
434439
if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
435440
$src = $this->base_url . $src;
436441
}
437442

438-
if ( ! empty( $ver ) ) {
439-
$src = add_query_arg( 'ver', $ver, $src );
443+
$query_args = array();
444+
if ( empty( $ver ) && null !== $ver && is_string( $this->default_version ) ) {
445+
$query_args['ver'] = $this->default_version;
446+
} elseif ( is_scalar( $ver ) ) {
447+
$query_args['ver'] = (string) $ver;
448+
}
449+
if ( isset( $this->args[ $handle ] ) ) {
450+
parse_str( $this->args[ $handle ], $parsed_args );
451+
if ( $parsed_args ) {
452+
$query_args = array_merge( $query_args, $parsed_args );
453+
}
440454
}
455+
$src = add_query_arg( rawurlencode_deep( $query_args ), $src );
441456

442457
/**
443458
* Filters an enqueued style's fully-qualified URL.

0 commit comments

Comments
 (0)