Skip to content

Commit 938310b

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
# Conflicts: # psalm-baseline.xml # src/Optimization/Enqueue.php
2 parents e6ab6f3 + 80498e5 commit 938310b

File tree

14 files changed

+81
-262
lines changed

14 files changed

+81
-262
lines changed

.phive/phars.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phive xmlns="https://phar.io/phive">
3+
<phar name="phpmd" version="^2.15.0" installed="2.15.0" location="../../home/codespace/.local/bin/phpmd" copy="false"/>
4+
<phar name="phpstan" version="^1.12.5" installed="1.12.5" location="../../home/codespace/.local/bin/phpstan" copy="false"/>
5+
<phar name="psalm" version="^5.26.1" installed="5.26.1" location="../../home/codespace/.local/bin/psalm" copy="false"/>
6+
<phar name="php-cs-fixer" version="^3.64.0" installed="3.64.0" location="../../home/codespace/.local/bin/php-cs-fixer" copy="false"/>
7+
</phive>

phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ parameters:
55
count: 1
66
path: src/Optimization/CleanUp.php
77

8+
-
9+
message: "#^Constant WPINC not found\\.$#"
10+
count: 1
11+
path: src/Optimization/Enqueue.php
12+
813
-
914
message: "#^Property WP_Taxonomy\\:\\:\\$update_count_callback \\(callable\\) in isset\\(\\) is not nullable\\.$#"
1015
count: 1
@@ -25,6 +30,11 @@ parameters:
2530
count: 2
2631
path: src/Optimization/WPQuery.php
2732

33+
-
34+
message: "#^Parameter \\#1 \\$maybeint of function absint expects array\\|bool\\|float\\|int\\|resource\\|string\\|null, mixed given\\.$#"
35+
count: 2
36+
path: src/Optimization/WPQuery.php
37+
2838
-
2939
message: "#^Call to function is_array\\(\\) with array\\{file\\: string, width\\: int, height\\: int, mime\\-type\\: string\\} will always evaluate to true\\.$#"
3040
count: 1

src/Optimization/CleanUp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function protectXmlrpc(): void {
8585
add_action( 'xmlrpc_call', static function ( string $action ) use ( $disableMethods ): void {
8686
if ( \in_array( $action, $disableMethods, true ) ) {
8787
wp_die(
88-
sprintf( '%s are not supported', $action ),
88+
\sprintf( '%s are not supported', $action ),
8989
'Not Allowed!',
9090
[ 'response' => 403 ]
9191
);

src/Optimization/Enqueue.php

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public function load(): void {
1414
add_action( 'wp_enqueue_scripts', self::jsToFooter( ... ) );
1515
add_action( 'wp_enqueue_scripts', self::jqueryFromCdn( ... ) );
1616
add_filter( 'style_loader_tag', self::addAsyncStyle( ... ), 10, 4 );
17-
add_filter( 'script_loader_tag', self::addAsyncScript( ... ), 10, 2 );
1817

1918
if ( ! is_admin() ) {
2019
add_filter( 'script_loader_src', self::setScriptVersion( ... ), 15, 2 );
@@ -36,34 +35,6 @@ public static function addAsyncStyle( string $tag, string $handle, string $href,
3635
return $tag;
3736
}
3837

39-
public static function addAsyncScript( string $tag, string $handle ): string {
40-
if ( is_admin() ) {
41-
return $tag;
42-
}
43-
44-
$methods = [
45-
'async' => ['polyfill.io'],
46-
'defer' => ['google-recaptcha'],
47-
];
48-
49-
foreach ( $methods as $method => $handlers ) {
50-
/** @var string[] $validHandlers */
51-
$validHandlers = apply_filters( "app_{$method}_scripts_handlers", $handlers );
52-
53-
if ( empty( $validHandlers ) ) {
54-
continue;
55-
}
56-
57-
if ( ! \in_array( $handle, $validHandlers, true ) ) {
58-
continue;
59-
}
60-
61-
$tag = str_replace( ' src', " {$method} src", $tag );
62-
}
63-
64-
return $tag;
65-
}
66-
6738
public static function setScriptVersion( string $scriptSrc, string $handle ): string {
6839
if ( ! filter_var( $scriptSrc, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ) ) {
6940
return $scriptSrc;
@@ -97,57 +68,47 @@ public static function setScriptVersion( string $scriptSrc, string $handle ): st
9768
public static function jqueryFromCdn(): void {
9869
$registered = wp_scripts()->registered;
9970

71+
$suffix = SCRIPT_DEBUG ? '' : '.min';
72+
10073
$jqCore = $registered['jquery-core'];
10174

102-
$jsdelivrUrl = 'https://cdn.jsdelivr.net/npm/jquery-ui@1.12.1';
75+
$jqUiCore = $registered['jquery-ui-core'];
76+
$jqMigrate = $registered['jquery-migrate'];
10377

104-
foreach ( array_keys( $registered ) as $handle ) {
105-
if ( str_starts_with( (string) $handle, 'jquery-effects-' ) ) {
106-
$isCore = 'jquery-effects-core' === $handle;
78+
$jqVer = trim( (string) $jqCore->ver, '-wp' );
79+
$jqUiVer = trim( (string) $jqUiCore->ver, '-wp' );
80+
$jqMigrateVer = trim( (string) $jqMigrate->ver, '-wp' );
10781

108-
$newUrl = \sprintf(
109-
'%s/ui/effect%s.min.js',
110-
$jsdelivrUrl,
111-
$isCore ? '' : str_replace( 'jquery-effects-', '-', (string) $handle )
112-
);
82+
foreach ( $registered as $handle => $dependency ) {
11383

114-
self::deregisterScript( (string) $handle, $newUrl );
84+
if ( empty( $dependency->src ) ) {
85+
continue;
11586
}
11687

117-
if ( str_starts_with( (string) $handle, 'jquery-ui-' ) ) {
118-
$newUrl = match ( $handle ) {
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(
122-
'%s/ui/widgets/%s.min.js',
123-
$jsdelivrUrl,
124-
str_replace( 'jquery-ui-', '', (string) $handle )
125-
),
126-
};
127-
128-
self::deregisterScript( (string) $handle, $newUrl );
88+
if ( ! str_contains( $dependency->src, '/'.WPINC.'/' ) ) {
89+
continue;
12990
}
130-
}
13191

132-
$jqVer = trim( (string) $jqCore->ver, '-wp' );
92+
$isJqueryUiCore = str_starts_with( (string) $handle, 'jquery-effects-' ) || str_starts_with( (string) $handle, 'jquery-ui-' );
13393

134-
self::deregisterScript( $jqCore->handle, \sprintf( 'https://code.jquery.com/jquery-%s.min.js', esc_attr( $jqVer ) ) );
135-
self::deregisterScript( 'jquery' );
94+
if ( ! $isJqueryUiCore ) {
95+
continue;
96+
}
13697

137-
wp_register_script( 'jquery', false, [$jqCore->handle], $jqVer, true );
138-
}
98+
$dependency->src = false;
99+
}
100+
101+
self::deregisterScript( $jqCore->handle, \sprintf( 'https://cdn.jsdelivr.net/npm/jquery@%s/dist/jquery%s.js', esc_attr( $jqVer ), $suffix ) );
102+
self::deregisterScript( $jqMigrate->handle, \sprintf( 'https://cdn.jsdelivr.net/npm/jquery-migrate@%s/dist/jquery-migrate%s.js', esc_attr( $jqMigrateVer ), $suffix ) );
103+
self::deregisterScript( $jqUiCore->handle, \sprintf( 'https://cdn.jsdelivr.net/npm/jquery-ui-dist@%s/jquery-ui%s.js', esc_attr( $jqUiVer ), $suffix ) );
139104

140-
public static function jsToFooter(): void {
141-
remove_action( 'wp_head', 'wp_print_scripts' );
142-
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
143-
remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
144105
}
145106

146107
public static function deregisterScript( string $handle, ?string $newUrl = null, bool $enqueue = false ): void {
147108
$registered = wp_scripts()->registered;
148109

149-
if ( ! empty( $registered[$handle] ) ) {
150-
$jsLib = $registered[$handle];
110+
if ( ! empty( $registered[ $handle ] ) ) {
111+
$jsLib = $registered[ $handle ];
151112

152113
wp_dequeue_script( $jsLib->handle );
153114
wp_deregister_script( $jsLib->handle );
@@ -164,8 +125,8 @@ public static function deregisterScript( string $handle, ?string $newUrl = null,
164125
public static function deregisterStyle( string $handle, ?string $newUrl = null, bool $enqueue = false ): void {
165126
$registered = wp_styles()->registered;
166127

167-
if ( ! empty( $registered[$handle] ) ) {
168-
$cssLib = $registered[$handle];
128+
if ( ! empty( $registered[ $handle ] ) ) {
129+
$cssLib = $registered[ $handle ];
169130

170131
wp_dequeue_style( $cssLib->handle );
171132
wp_deregister_style( $cssLib->handle );
@@ -178,6 +139,12 @@ public static function deregisterStyle( string $handle, ?string $newUrl = null,
178139
}
179140
}
180141

142+
private static function jsToFooter(): void {
143+
remove_action( 'wp_head', 'wp_print_scripts' );
144+
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
145+
remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
146+
}
147+
181148
private static function prepareScriptFilePath( string $scriptSrc ): bool|string {
182149
$rootDir = app_locate_root_dir();
183150

src/Optimization/LastPostModified.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static function isLocked( string $postType ): bool {
6363
}
6464

6565
private static function getLockName( string $postType ): string {
66-
return sprintf( '%s_%s_lock', self::OPTION_PREFIX, $postType );
66+
return \sprintf( '%s_%s_lock', self::OPTION_PREFIX, $postType );
6767
}
6868

6969
private static function bumpLastPostModified( WP_Post $wpPost ): void {
@@ -78,7 +78,7 @@ private static function bumpLastPostModified( WP_Post $wpPost ): void {
7878
}
7979

8080
private static function getOptionName( string $timezone, string $postType ): string {
81-
return sprintf( '%s_%s_%s', self::OPTION_PREFIX, strtolower( $timezone ), $postType );
81+
return \sprintf( '%s_%s_%s', self::OPTION_PREFIX, strtolower( $timezone ), $postType );
8282
}
8383

8484
private static function getLastPostModified( string $timezone, string $postType ): mixed {

src/Optimization/Media.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static function svgMetaData( array $metadata, int|string $attachmentId ):
9393
public static function setAttachmentTitle( array $data, array $postarr ): array {
9494
if ( ! empty( $postarr['file'] ) ) {
9595
$url = pathinfo( $postarr['file'] );
96-
$extension = empty( $url['extension'] ) ? '' : sprintf( '.%s', $url['extension'] );
96+
$extension = empty( $url['extension'] ) ? '' : \sprintf( '.%s', $url['extension'] );
9797

9898
$title = rtrim( $data['post_title'], $extension );
9999

@@ -133,7 +133,7 @@ public static function replaceGravatar( string $avatar, mixed $idOrEmail, int $s
133133
}
134134

135135
// Return the avatar.
136-
return sprintf(
136+
return \sprintf(
137137
'<img alt="%1$s" src="%2$s" class="avatar avatar-%3$s photo" height="%3$s" width="%3$s" style="background:#eee;" />',
138138
esc_attr( $alt ),
139139
'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
@@ -160,13 +160,11 @@ public static function defaultAvatar( string $avatarList ): string {
160160
}
161161

162162
/**
163-
* @param mixed|null $months
164-
*
165163
* @return stdClass[]
166164
*
167165
* @see https://github.com/Automattic/vip-go-mu-plugins-built/blob/master/performance/vip-tweaks.php#L65
168166
*/
169-
public static function mediaLibraryMonthsWithFiles( $months = null ): array {
167+
public static function mediaLibraryMonthsWithFiles( mixed $months = null ): array {
170168
global $wpdb;
171169

172170
/** @var false|stdClass[] $months */
@@ -279,7 +277,7 @@ public static function resizeImageOnTheFly( array|bool $image, int|string $attac
279277
return $image;
280278
}
281279

282-
/** @var array{file: string, sizes: mixed}|false $meta
280+
/** @var array{file: string, sizes: mixed}|false $meta
283281
*/
284282
$meta = wp_get_attachment_metadata( (int) $attachmentId );
285283

@@ -288,15 +286,15 @@ public static function resizeImageOnTheFly( array|bool $image, int|string $attac
288286
}
289287

290288
$upload = wp_upload_dir();
291-
$filePath = sprintf( '%s/%s', $upload['basedir'], $meta['file'] );
289+
$filePath = \sprintf( '%s/%s', $upload['basedir'], $meta['file'] );
292290

293291
if ( ! file_exists( $filePath ) ) {
294292
return $image;
295293
}
296294

297295
$imageDirname = pathinfo( $filePath, PATHINFO_DIRNAME );
298296

299-
$imageBaseUrl = sprintf( '%s/%s', $upload['baseurl'], $imageDirname );
297+
$imageBaseUrl = \sprintf( '%s/%s', $upload['baseurl'], $imageDirname );
300298

301299
/** @var array<array-key,array{width?: int|string, height?: int|string}>|false $sizes */
302300
$sizes = ! empty( $meta['sizes'] ) ? (array) $meta['sizes'] : false;
@@ -319,15 +317,15 @@ public static function resizeImageOnTheFly( array|bool $image, int|string $attac
319317
$resized = image_make_intermediate_size( $filePath, $width, $height, true );
320318

321319
if ( ! empty( $resized ) ) {
322-
$metaSizeKey = sprintf( 'resized-%dx%d', $resized['width'], $resized['height'] );
320+
$metaSizeKey = \sprintf( 'resized-%dx%d', $resized['width'], $resized['height'] );
323321

324322
/** @var array{sizes:array<string,mixed>} $meta */
325323
$meta['sizes'][ $metaSizeKey ] = $resized;
326324

327325
wp_update_attachment_metadata( (int) $attachmentId, $meta );
328326

329327
/** @var array{string,int,int,bool} $image */
330-
$image[0] = sprintf( '%s/%s', $imageBaseUrl, $resized['file'] );
328+
$image[0] = \sprintf( '%s/%s', $imageBaseUrl, $resized['file'] );
331329
$image[1] = $resized['width'];
332330
$image[2] = $resized['height'];
333331
}

src/Optimization/TermCount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function maybeRecountPostsForTerm( int $termId, int $termTaxId, string $t
141141
return;
142142
}
143143

144-
if ( sprintf( 'edit-%s', $taxonomy ) === $screen->id ) {
144+
if ( \sprintf( 'edit-%s', $taxonomy ) === $screen->id ) {
145145
wp_update_term_count_now( [ $termTaxId ], $taxonomy );
146146
}
147147
}
@@ -230,7 +230,7 @@ private function quickUpdateTermsCount( int $objectId, array $termTaxIds, string
230230
$isIncrement = 'increment' === $transitionType;
231231

232232
$wpdb->query(
233-
sprintf(
233+
\sprintf(
234234
'update %s as tt set tt.count = tt.count %s 1 where tt.term_taxonomy_id in %s %s',
235235
$wpdb->term_taxonomy,
236236
$isIncrement ? '+' : '-',

src/Optimization/Update.php

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

2020
// Remove bulk action for updating themes/plugins.
2121
foreach ( [ 'plugins', 'themes', 'plugins-network', 'themes-network' ] as $bulkAction ) {
22-
add_filter( sprintf( 'bulk_actions-%s', $bulkAction ), self::removeBulkActions( ... ) );
22+
add_filter( \sprintf( 'bulk_actions-%s', $bulkAction ), self::removeBulkActions( ... ) );
2323
}
2424

2525
// Admin UI items.
@@ -58,7 +58,7 @@ public function load(): void {
5858
$siteTransients = [ 'themes', 'plugins', 'core' ];
5959

6060
foreach ( $siteTransients as $siteTransient ) {
61-
add_filter( sprintf( 'pre_site_transient_update_%s', $siteTransient ), self::lastCheckedCore( ... ) );
61+
add_filter( \sprintf( 'pre_site_transient_update_%s', $siteTransient ), self::lastCheckedCore( ... ) );
6262
}
6363

6464
add_filter( 'site_transient_update_plugins', self::removePluginUpdates( ... ) );
@@ -369,7 +369,7 @@ public static function availableTranslations(): array {
369369
'strings' => [
370370
'continue' => __( 'Continue' ),
371371
],
372-
'package' => sprintf(
372+
'package' => \sprintf(
373373
'https://downloads.wordpress.org/translation/core/%s/%s.zip',
374374
esc_attr( $wp_version ),
375375
esc_attr( $language )

src/Optimization/WPQuery.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
final class WPQuery implements AutoloadInterface {
1515

1616
/**
17-
* @var string
18-
*/
17+
* @var string
18+
*/
1919
public const INVALIDATE_TIME_KEY = 'invalidate-time';
2020

2121
/**
@@ -59,7 +59,7 @@ public function postsClausesRequest( array $clauses, WP_Query $wpQuery ): array
5959

6060
/** @noinspection SqlConstantCondition */
6161
$pdoStatement = $pdo->prepare(
62-
sprintf(
62+
\sprintf(
6363
'select %s.ID from %s %s where 1=1 %s group by %s.ID order by %s.post_date',
6464
$wpdb->posts,
6565
$wpdb->posts,
@@ -160,7 +160,7 @@ private function invalidateFoundPostsCache(): void {
160160
/**
161161
* @return false|int
162162
*/
163-
private function getInvalidateTime() {
163+
private function getInvalidateTime(): bool|int {
164164
/** @var false|int $time */
165165
$time = wp_cache_get( self::INVALIDATE_TIME_KEY, Cache::QUERY_CACHE_GROUP );
166166

@@ -174,7 +174,7 @@ private function getInvalidateTime() {
174174
/**
175175
* @return false|int
176176
*/
177-
private function getTimeFoundPosts() {
177+
private function getTimeFoundPosts(): bool|int {
178178
/** @var false|int $time */
179179
$time = wp_cache_get( $this->generateFoundPostCacheKey( true ), Cache::QUERY_CACHE_GROUP );
180180

@@ -186,7 +186,7 @@ private function getTimeFoundPosts() {
186186
}
187187

188188
private function generateFoundPostCacheKey( bool $addTime = false ): string {
189-
return sprintf(
189+
return \sprintf(
190190
'%s%s-%s',
191191
$addTime ? 'time-' : '',
192192
self::FOUND_POSTS_KEY,

0 commit comments

Comments
 (0)