Skip to content

Commit e6ab6f3

Browse files
committed
feature(core): update setScriptVersion method
1 parent 8f5f82e commit e6ab6f3

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/Optimization/Enqueue.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function load(): void {
2424

2525
public static function addAsyncStyle( string $tag, string $handle, string $href, string $media ): string {
2626
if ( 'print' === $media ) {
27-
return sprintf(
27+
return \sprintf(
2828
'<link rel="%s" id="%s-css" href="%s" media="%s" onload="this.media=\'all\'; this.onload=null;" />',
2929
'stylesheet',
3030
$handle,
@@ -71,23 +71,27 @@ public static function setScriptVersion( string $scriptSrc, string $handle ): st
7171

7272
$addVersion = (bool) apply_filters( 'enqueue_add_script_version', true, $handle );
7373

74-
if ( $addVersion && app_is_current_host( $scriptSrc ) ) {
75-
$file = self::prepareScriptFilePath( $scriptSrc );
74+
if ( ! $addVersion ) {
75+
return $scriptSrc;
76+
}
7677

77-
if ( ! empty( $file ) ) {
78-
$fileMap = $file.'.map';
78+
if ( ! app_is_current_host( $scriptSrc ) ) {
79+
return $scriptSrc;
80+
}
7981

80-
$timestamp = is_readable( $fileMap ) ? $fileMap : $file;
82+
$file = self::prepareScriptFilePath( $scriptSrc );
8183

82-
$scriptSrc = add_query_arg( [
83-
'ver' => filemtime( (string) $timestamp ),
84-
], $scriptSrc );
85-
}
86-
} elseif ( str_contains( $scriptSrc, '?ver=' ) ) {
87-
$scriptSrc = remove_query_arg( 'ver', $scriptSrc );
84+
if ( empty( $file ) ) {
85+
return $scriptSrc;
8886
}
8987

90-
return $scriptSrc;
88+
$fileMap = $file.'.map';
89+
90+
$filePath = is_readable( $fileMap ) ? $fileMap : $file;
91+
92+
return add_query_arg( [
93+
'ver' => hash_file( 'sha256', (string) $filePath ),
94+
], $scriptSrc );
9195
}
9296

9397
public static function jqueryFromCdn(): void {
@@ -101,7 +105,7 @@ public static function jqueryFromCdn(): void {
101105
if ( str_starts_with( (string) $handle, 'jquery-effects-' ) ) {
102106
$isCore = 'jquery-effects-core' === $handle;
103107

104-
$newUrl = sprintf(
108+
$newUrl = \sprintf(
105109
'%s/ui/effect%s.min.js',
106110
$jsdelivrUrl,
107111
$isCore ? '' : str_replace( 'jquery-effects-', '-', (string) $handle )
@@ -112,9 +116,9 @@ public static function jqueryFromCdn(): void {
112116

113117
if ( str_starts_with( (string) $handle, 'jquery-ui-' ) ) {
114118
$newUrl = match ( $handle ) {
115-
'jquery-ui-core' => sprintf( '%s/ui/core.min.js', $jsdelivrUrl ),
116-
'jquery-ui-widget' => sprintf( '%s/ui/widget.min.js', $jsdelivrUrl ),
117-
default => sprintf(
119+
'jquery-ui-core' => \sprintf( '%s/ui/core.min.js', $jsdelivrUrl ),
120+
'jquery-ui-widget' => \sprintf( '%s/ui/widget.min.js', $jsdelivrUrl ),
121+
default => \sprintf(
118122
'%s/ui/widgets/%s.min.js',
119123
$jsdelivrUrl,
120124
str_replace( 'jquery-ui-', '', (string) $handle )
@@ -127,7 +131,7 @@ public static function jqueryFromCdn(): void {
127131

128132
$jqVer = trim( (string) $jqCore->ver, '-wp' );
129133

130-
self::deregisterScript( $jqCore->handle, sprintf( 'https://code.jquery.com/jquery-%s.min.js', esc_attr( $jqVer ) ) );
134+
self::deregisterScript( $jqCore->handle, \sprintf( 'https://code.jquery.com/jquery-%s.min.js', esc_attr( $jqVer ) ) );
131135
self::deregisterScript( 'jquery' );
132136

133137
wp_register_script( 'jquery', false, [$jqCore->handle], $jqVer, true );
@@ -183,7 +187,7 @@ private static function prepareScriptFilePath( string $scriptSrc ): bool|string
183187

184188
$path = ltrim( (string) parse_url( $scriptSrc, PHP_URL_PATH ), '/' );
185189

186-
$file = sprintf( '%s/%s', $rootDir, self::fixMultiSitePath( $path ) );
190+
$file = \sprintf( '%s/%s', $rootDir, self::fixMultiSitePath( $path ) );
187191

188192
return is_readable( $file ) ? $file : false;
189193
}

0 commit comments

Comments
 (0)