| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | /** |
| 3 | 3 | * Module Name: Asset CDN |
| 4 | - * Module Description: Jetpack’s Site Accelerator loads your site faster by optimizing your images and serving your images and static files from our global network of servers. | |
| 4 | + * Module Description: Serve static files like CSS and JS from Jetpack’s global CDN for faster load times. | |
| 5 | 5 | * Sort Order: 26 |
| 6 | 6 | * Recommendation Order: 1 |
| 7 | 7 | * First Introduced: 6.6 |
| 8 | 8 | * Requires Connection: No |
| @@ -15,11 +15,15 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | 17 | use Automattic\Jetpack\Assets; |
| 18 | 18 | |
| 19 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 20 | + exit( 0 ); | |
| 21 | +} | |
| 22 | + | |
| 19 | 23 | $GLOBALS['concatenate_scripts'] = false; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 20 | 24 | |
| 21 | -Assets::add_resource_hint( '//c0.wp.com', 'dns-prefetch' ); | |
| 25 | +Assets::add_resource_hint( '//c0.wp.com', 'preconnect' ); | |
| 22 | 26 | |
| 23 | 27 | /** |
| 24 | 28 | * Asset CDN module main class file. |
| 25 | 29 | */ |
| @@ -72,33 +76,37 @@ | ||
| 72 | 76 | ); |
| 73 | 77 | |
| 74 | 78 | if ( self::is_public_version( $version ) ) { |
| 75 | 79 | $site_url = trailingslashit( site_url() ); |
| 76 | - foreach ( $wp_scripts->registered as $handle => $thing ) { | |
| 77 | - if ( wp_startswith( $thing->src, self::CDN ) ) { | |
| 78 | - continue; | |
| 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 | + } | |
| 79 | 93 | } |
| 80 | - if ( ! is_string( $thing->src ) ) { | |
| 81 | - continue; | |
| 82 | - } | |
| 83 | - $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' ); | |
| 84 | - if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) { | |
| 85 | - $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src ); | |
| 86 | - $wp_scripts->registered[ $handle ]->ver = null; | |
| 87 | - } | |
| 88 | 94 | } |
| 89 | - foreach ( $wp_styles->registered as $handle => $thing ) { | |
| 90 | - if ( wp_startswith( $thing->src, self::CDN ) ) { | |
| 91 | - continue; | |
| 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 | + } | |
| 92 | 108 | } |
| 93 | - if ( ! is_string( $thing->src ) ) { | |
| 94 | - continue; | |
| 95 | - } | |
| 96 | - $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' ); | |
| 97 | - if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) { | |
| 98 | - $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src ); | |
| 99 | - $wp_styles->registered[ $handle ]->ver = null; | |
| 100 | - } | |
| 101 | 109 | } |
| 102 | 110 | } |
| 103 | 111 | |
| 104 | 112 | self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION ); |
| @@ -190,30 +198,34 @@ | ||
| 190 | 198 | if ( is_wp_error( $assets ) || ! is_array( $assets ) ) { |
| 191 | 199 | return false; |
| 192 | 200 | } |
| 193 | 201 | |
| 194 | - foreach ( $wp_scripts->registered as $handle => $thing ) { | |
| 195 | - if ( wp_startswith( $thing->src, self::CDN ) ) { | |
| 196 | - continue; | |
| 197 | - } | |
| 198 | - if ( wp_startswith( $thing->src, $plugin_directory_url ) ) { | |
| 199 | - $local_path = substr( $thing->src, strlen( $plugin_directory_url ) ); | |
| 200 | - if ( in_array( $local_path, $assets, true ) ) { | |
| 201 | - $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path ); | |
| 202 | - $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; | |
| 203 | 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 | + } | |
| 213 | + } | |
| 204 | 214 | } |
| 205 | 215 | } |
| 206 | - foreach ( $wp_styles->registered as $handle => $thing ) { | |
| 207 | - if ( wp_startswith( $thing->src, self::CDN ) ) { | |
| 208 | - continue; | |
| 209 | - } | |
| 210 | - if ( wp_startswith( $thing->src, $plugin_directory_url ) ) { | |
| 211 | - $local_path = substr( $thing->src, strlen( $plugin_directory_url ) ); | |
| 212 | - if ( in_array( $local_path, $assets, true ) ) { | |
| 213 | - $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path ); | |
| 214 | - $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; | |
| 215 | 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 | + } | |
| 227 | + } | |
| 216 | 228 | } |
| 217 | 229 | } |
| 218 | 230 | } |
| 219 | 231 | |
| @@ -225,9 +237,9 @@ | ||
| 225 | 237 | * @return array|bool Will return false if not a public version. |
| 226 | 238 | */ |
| 227 | 239 | public static function get_plugin_assets( $plugin, $version ) { |
| 228 | 240 | if ( 'jetpack' === $plugin && JETPACK__VERSION === $version ) { |
| 229 | - if ( ! self::is_public_version( $version ) ) { | |
| 241 | + if ( ! self::is_public_version( $version ) || ! file_exists( JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php' ) ) { | |
| 230 | 242 | return false; |
| 231 | 243 | } |
| 232 | 244 | |
| 233 | 245 | $assets = array(); // The variable will be redefined in the included file. |
| @@ -281,10 +293,13 @@ | ||
| 281 | 293 | $body = trim( wp_remote_retrieve_body( $response ) ); |
| 282 | 294 | $body = json_decode( $body, true ); |
| 283 | 295 | |
| 284 | 296 | $return = time(); |
| 285 | - if ( is_array( $body ) ) { | |
| 286 | - $return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) ); | |
| 297 | + if ( is_array( $body ) && isset( $body['files'] ) && is_array( $body['files'] ) ) { | |
| 298 | + $return = array_filter( | |
| 299 | + array_keys( $body['files'] ), | |
| 300 | + array( __CLASS__, 'is_js_or_css_file' ) | |
| 301 | + ); | |
| 287 | 302 | } |
| 288 | 303 | |
| 289 | 304 | $cache[ $plugin ] = array(); |
| 290 | 305 | $cache[ $plugin ][ $version ] = $return; |