Skip to content

Commit aea8f68

Browse files
authored
Image CDN: Prevent warning when WP global is null (#45015)
1 parent f40c3b2 commit aea8f68

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: other
3+
4+
Image CDN: Prevent PHP warnings when handling malformed data.

projects/plugins/jetpack/modules/photon-cdn.php

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,34 @@ public static function cdnize_assets() {
7777

7878
if ( self::is_public_version( $version ) ) {
7979
$site_url = trailingslashit( site_url() );
80-
foreach ( $wp_scripts->registered as $handle => $thing ) {
81-
if ( wp_startswith( $thing->src, self::CDN ) ) {
82-
continue;
83-
}
84-
if ( ! is_string( $thing->src ) ) {
85-
continue;
86-
}
87-
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
88-
if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) {
89-
$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
90-
$wp_scripts->registered[ $handle ]->ver = null;
80+
if ( $wp_scripts instanceof WP_Scripts && is_array( $wp_scripts->registered ) ) {
81+
foreach ( $wp_scripts->registered as $handle => $thing ) {
82+
if ( wp_startswith( $thing->src, self::CDN ) ) {
83+
continue;
84+
}
85+
if ( ! is_string( $thing->src ) ) {
86+
continue;
87+
}
88+
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
89+
if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) {
90+
$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
91+
$wp_scripts->registered[ $handle ]->ver = null;
92+
}
9193
}
9294
}
93-
foreach ( $wp_styles->registered as $handle => $thing ) {
94-
if ( wp_startswith( $thing->src, self::CDN ) ) {
95-
continue;
96-
}
97-
if ( ! is_string( $thing->src ) ) {
98-
continue;
99-
}
100-
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
101-
if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) {
102-
$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
103-
$wp_styles->registered[ $handle ]->ver = null;
95+
if ( $wp_styles instanceof WP_Styles && is_array( $wp_styles->registered ) ) {
96+
foreach ( $wp_styles->registered as $handle => $thing ) {
97+
if ( wp_startswith( $thing->src, self::CDN ) ) {
98+
continue;
99+
}
100+
if ( ! is_string( $thing->src ) ) {
101+
continue;
102+
}
103+
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
104+
if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) {
105+
$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
106+
$wp_styles->registered[ $handle ]->ver = null;
107+
}
104108
}
105109
}
106110
}
@@ -195,27 +199,31 @@ public static function cdnize_plugin_assets( $plugin_slug, $current_version ) {
195199
return false;
196200
}
197201

198-
foreach ( $wp_scripts->registered as $handle => $thing ) {
199-
if ( wp_startswith( $thing->src, self::CDN ) ) {
200-
continue;
201-
}
202-
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
203-
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
204-
if ( in_array( $local_path, $assets, true ) ) {
205-
$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
206-
$wp_scripts->registered[ $handle ]->ver = null;
202+
if ( $wp_scripts instanceof WP_Scripts && is_array( $wp_scripts->registered ) ) {
203+
foreach ( $wp_scripts->registered as $handle => $thing ) {
204+
if ( wp_startswith( $thing->src, self::CDN ) ) {
205+
continue;
206+
}
207+
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
208+
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
209+
if ( in_array( $local_path, $assets, true ) ) {
210+
$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
211+
$wp_scripts->registered[ $handle ]->ver = null;
212+
}
207213
}
208214
}
209215
}
210-
foreach ( $wp_styles->registered as $handle => $thing ) {
211-
if ( wp_startswith( $thing->src, self::CDN ) ) {
212-
continue;
213-
}
214-
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
215-
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
216-
if ( in_array( $local_path, $assets, true ) ) {
217-
$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
218-
$wp_styles->registered[ $handle ]->ver = null;
216+
if ( $wp_styles instanceof WP_Styles && is_array( $wp_styles->registered ) ) {
217+
foreach ( $wp_styles->registered as $handle => $thing ) {
218+
if ( wp_startswith( $thing->src, self::CDN ) ) {
219+
continue;
220+
}
221+
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
222+
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
223+
if ( in_array( $local_path, $assets, true ) ) {
224+
$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
225+
$wp_styles->registered[ $handle ]->ver = null;
226+
}
219227
}
220228
}
221229
}

0 commit comments

Comments
 (0)