PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.1.1
Jetpack – WP Security, Backup, Speed, & Growth v8.1.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / photon-cdn.php

photon-cdn.php in Jetpack – WP Security, Backup, Speed, & Growth 8.1.1, at modules/photon-cdn.php

299 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
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.
5 * Sort Order: 26
6 * Recommendation Order: 1
7 * First Introduced: 6.6
8 * Requires Connection: No
9 * Auto Activate: No
10 * Module Tags: Photos and Videos, Appearance, Recommended
11 * Feature: Recommended, Appearance
12 * Additional Search Queries: site accelerator, accelerate, static, assets, javascript, css, files, performance, cdn, bandwidth, content delivery network, pagespeed, combine js, optimize css
13 */
14
15 $GLOBALS['concatenate_scripts'] = false;
16
17 Jetpack::dns_prefetch( array(
18 '//c0.wp.com',
19 ) );
20
21 class Jetpack_Photon_Static_Assets_CDN {
22 const CDN = 'https://c0.wp.com/';
23
24 /**
25 * Sets up action handlers needed for Jetpack CDN.
26 */
27 public static function go() {
28 add_action( 'wp_print_scripts', array( __CLASS__, 'cdnize_assets' ) );
29 add_action( 'wp_print_styles', array( __CLASS__, 'cdnize_assets' ) );
30 add_action( 'admin_print_scripts', array( __CLASS__, 'cdnize_assets' ) );
31 add_action( 'admin_print_styles', array( __CLASS__, 'cdnize_assets' ) );
32 add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) );
33 add_filter( 'load_script_textdomain_relative_path', array( __CLASS__, 'fix_script_relative_path' ), 10, 2 );
34 }
35
36 /**
37 * Sets up CDN URLs for assets that are enqueued by the WordPress Core.
38 */
39 public static function cdnize_assets() {
40 global $wp_scripts, $wp_styles, $wp_version;
41
42 /*
43 * Short-circuit if AMP since not relevant as custom JS is not allowed and CSS is inlined.
44 * Note that it is not suitable to use the jetpack_force_disable_site_accelerator filter for this
45 * because it will be applied before the wp action, which is the point at which the queried object
46 * is available and we know whether the response will be AMP or not. This is particularly important
47 * for AMP-first (native AMP) pages where there are no AMP-specific URLs.
48 */
49 if ( Jetpack_AMP_Support::is_amp_request() ) {
50 return;
51 }
52
53 /**
54 * Filters Jetpack CDN's Core version number and locale. Can be used to override the values
55 * that Jetpack uses to retrieve assets. Expects the values to be returned in an array.
56 *
57 * @module photon-cdn
58 *
59 * @since 6.6.0
60 *
61 * @param array $values array( $version = core assets version, i.e. 4.9.8, $locale = desired locale )
62 */
63 list( $version, $locale ) = apply_filters(
64 'jetpack_cdn_core_version_and_locale',
65 array( $wp_version, get_locale() )
66 );
67
68 if ( self::is_public_version( $version ) ) {
69 $site_url = trailingslashit( site_url() );
70 foreach ( $wp_scripts->registered as $handle => $thing ) {
71 if ( wp_startswith( $thing->src, self::CDN ) ) {
72 continue;
73 }
74 $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
75 if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) {
76 $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
77 $wp_scripts->registered[ $handle ]->ver = null;
78 }
79 }
80 foreach ( $wp_styles->registered as $handle => $thing ) {
81 if ( wp_startswith( $thing->src, self::CDN ) ) {
82 continue;
83 }
84 $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
85 if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) {
86 $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
87 $wp_styles->registered[ $handle ]->ver = null;
88 }
89 }
90 }
91
92 self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION );
93 if ( class_exists( 'WooCommerce' ) ) {
94 self::cdnize_plugin_assets( 'woocommerce', WC_VERSION );
95 }
96 }
97
98 /**
99 * Ensure use of the correct relative path when determining the JavaScript file names.
100 *
101 * @param string $relative The relative path of the script. False if it could not be determined.
102 * @param string $src The full source url of the script.
103 * @return string The expected relative path for the CDN-ed URL.
104 */
105 public static function fix_script_relative_path( $relative, $src ) {
106
107 // Note relevant in AMP responses. See note above.
108 if ( Jetpack_AMP_Support::is_amp_request() ) {
109 return $relative;
110 }
111
112 $strpos = strpos( $src, '/wp-includes/' );
113
114 // We only treat URLs that have wp-includes in them. Cases like language textdomains
115 // can also use this filter, they don't need to be touched because they are local paths.
116 if ( false === $strpos ) {
117 return $relative;
118 }
119 return substr( $src, 1 + $strpos );
120 }
121
122 /**
123 * Sets up CDN URLs for supported plugin assets.
124 *
125 * @param String $plugin_slug plugin slug string.
126 * @param String $current_version plugin version string.
127 * @return null|bool
128 */
129 public static function cdnize_plugin_assets( $plugin_slug, $current_version ) {
130 global $wp_scripts, $wp_styles;
131
132 /**
133 * Filters Jetpack CDN's plugin slug and version number. Can be used to override the values
134 * that Jetpack uses to retrieve assets. For example, when testing a development version of Jetpack
135 * the assets are not yet published, so you may need to override the version value to either
136 * trunk, or the latest available version. Expects the values to be returned in an array.
137 *
138 * @module photon-cdn
139 *
140 * @since 6.6.0
141 *
142 * @param array $values array( $slug = the plugin repository slug, i.e. jetpack, $version = the plugin version, i.e. 6.6 )
143 */
144 list( $plugin_slug, $current_version ) = apply_filters(
145 'jetpack_cdn_plugin_slug_and_version',
146 array( $plugin_slug, $current_version )
147 );
148
149 $assets = self::get_plugin_assets( $plugin_slug, $current_version );
150 $plugin_directory_url = plugins_url() . '/' . $plugin_slug . '/';
151
152 if ( is_wp_error( $assets ) || ! is_array( $assets ) ) {
153 return false;
154 }
155
156 foreach ( $wp_scripts->registered as $handle => $thing ) {
157 if ( wp_startswith( $thing->src, self::CDN ) ) {
158 continue;
159 }
160 if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
161 $local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
162 if ( in_array( $local_path, $assets, true ) ) {
163 $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
164 $wp_scripts->registered[ $handle ]->ver = null;
165 }
166 }
167 }
168 foreach ( $wp_styles->registered as $handle => $thing ) {
169 if ( wp_startswith( $thing->src, self::CDN ) ) {
170 continue;
171 }
172 if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
173 $local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
174 if ( in_array( $local_path, $assets, true ) ) {
175 $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
176 $wp_styles->registered[ $handle ]->ver = null;
177 }
178 }
179 }
180 }
181
182 /**
183 * Returns cdn-able assets for a given plugin.
184 *
185 * @param string $plugin plugin slug string.
186 * @param string $version plugin version number string.
187 * @return array|bool Will return false if not a public version.
188 */
189 public static function get_plugin_assets( $plugin, $version ) {
190 if ( 'jetpack' === $plugin && JETPACK__VERSION === $version ) {
191 if ( ! self::is_public_version( $version ) ) {
192 return false;
193 }
194
195 $assets = array(); // The variable will be redefined in the included file.
196
197 include JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php';
198 return $assets;
199 }
200
201 /**
202 * Used for other plugins to provide their bundled assets via filter to
203 * prevent the need of storing them in an option or an external api request
204 * to w.org.
205 *
206 * @module photon-cdn
207 *
208 * @since 6.6.0
209 *
210 * @param array $assets The assets array for the plugin.
211 * @param string $version The version of the plugin being requested.
212 */
213 $assets = apply_filters( "jetpack_cdn_plugin_assets-{$plugin}", null, $version );
214 if ( is_array( $assets ) ) {
215 return $assets;
216 }
217
218 if ( ! self::is_public_version( $version ) ) {
219 return false;
220 }
221
222 $cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() );
223 if ( isset( $cache[ $plugin ][ $version ] ) ) {
224 if ( is_array( $cache[ $plugin ][ $version ] ) ) {
225 return $cache[ $plugin ][ $version ];
226 }
227 if ( is_numeric( $cache[ $plugin ][ $version ] ) ) {
228 // Cache an empty result for up to 24h.
229 if ( intval( $cache[ $plugin ][ $version ] ) + DAY_IN_SECONDS > time() ) {
230 return array();
231 }
232 }
233 }
234
235 $url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version );
236
237 if ( wp_http_supports( array( 'ssl' ) ) ) {
238 $url = set_url_scheme( $url, 'https' );
239 }
240
241 $response = wp_remote_get( $url );
242
243 $body = trim( wp_remote_retrieve_body( $response ) );
244 $body = json_decode( $body, true );
245
246 $return = time();
247 if ( is_array( $body ) ) {
248 $return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) );
249 }
250
251 $cache[ $plugin ] = array();
252 $cache[ $plugin ][ $version ] = $return;
253 Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true );
254
255 return $return;
256 }
257
258 /**
259 * Checks a path whether it is a JS or CSS file.
260 *
261 * @param String $path file path.
262 * @return Boolean whether the file is a JS or CSS.
263 */
264 public static function is_js_or_css_file( $path ) {
265 return ( false === strpos( $path, '?' ) ) && in_array( substr( $path, -3 ), array( 'css', '.js' ), true );
266 }
267
268 /**
269 * Checks whether the version string indicates a production version.
270 *
271 * @param String $version the version string.
272 * @param Boolean $include_beta_and_rc whether to count beta and RC versions as production.
273 * @return Boolean
274 */
275 public static function is_public_version( $version, $include_beta_and_rc = false ) {
276 if ( preg_match( '/^\d+(\.\d+)+$/', $version ) ) {
277 // matches `1` `1.2` `1.2.3`.
278 return true;
279 } elseif ( $include_beta_and_rc && preg_match( '/^\d+(\.\d+)+(-(beta|rc|pressable)\d?)$/i', $version ) ) {
280 // matches `1.2.3` `1.2.3-beta` `1.2.3-pressable` `1.2.3-beta1` `1.2.3-rc` `1.2.3-rc2`.
281 return true;
282 }
283 // unrecognized version.
284 return false;
285 }
286 }
287 /**
288 * Allow plugins to short-circuit the Asset CDN, even when the module is on.
289 *
290 * @module photon-cdn
291 *
292 * @since 6.7.0
293 *
294 * @param false bool Should the Asset CDN be blocked? False by default.
295 */
296 if ( true !== apply_filters( 'jetpack_force_disable_site_accelerator', false ) ) {
297 Jetpack_Photon_Static_Assets_CDN::go();
298 }
299