PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.9.2
Jetpack – WP Security, Backup, Speed, & Growth v6.9.2
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
275 lines 9.2 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: Serve static assets from our 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: photon, image, cdn, performance, speed, assets
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 * Filters Jetpack CDN's Core version number and locale. Can be used to override the values
44 * that Jetpack uses to retrieve assets. Expects the values to be returned in an array.
45 *
46 * @module photon-cdn
47 *
48 * @since 6.6.0
49 *
50 * @param array $values array( $version = core assets version, i.e. 4.9.8, $locale = desired locale )
51 */
52 list( $version, $locale ) = apply_filters(
53 'jetpack_cdn_core_version_and_locale',
54 array( $wp_version, get_locale() )
55 );
56
57 if ( self::is_public_version( $version ) ) {
58 $site_url = trailingslashit( site_url() );
59 foreach ( $wp_scripts->registered as $handle => $thing ) {
60 if ( wp_startswith( $thing->src, self::CDN ) ) {
61 continue;
62 }
63 $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
64 if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) {
65 $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
66 $wp_scripts->registered[ $handle ]->ver = null;
67 }
68 }
69 foreach ( $wp_styles->registered as $handle => $thing ) {
70 if ( wp_startswith( $thing->src, self::CDN ) ) {
71 continue;
72 }
73 $src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
74 if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) {
75 $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
76 $wp_styles->registered[ $handle ]->ver = null;
77 }
78 }
79 }
80
81 self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION );
82 if ( class_exists( 'WooCommerce' ) ) {
83 self::cdnize_plugin_assets( 'woocommerce', WC_VERSION );
84 }
85 }
86
87 /**
88 * Ensure use of the correct relative path when determining the JavaScript file names.
89 *
90 * @param string $relative The relative path of the script. False if it could not be determined.
91 * @param string $src The full source url of the script.
92 * @return string The expected relative path for the CDN-ed URL.
93 */
94 public static function fix_script_relative_path( $relative, $src ) {
95 return substr( $src, 1 + strpos( $src, '/wp-includes/' ) );
96 }
97
98 /**
99 * Sets up CDN URLs for supported plugin assets.
100 *
101 * @param String $plugin_slug plugin slug string.
102 * @param String $current_version plugin version string.
103 * @return null|bool
104 */
105 public static function cdnize_plugin_assets( $plugin_slug, $current_version ) {
106 global $wp_scripts, $wp_styles;
107
108 /**
109 * Filters Jetpack CDN's plugin slug and version number. Can be used to override the values
110 * that Jetpack uses to retrieve assets. For example, when testing a development version of Jetpack
111 * the assets are not yet published, so you may need to override the version value to either
112 * trunk, or the latest available version. Expects the values to be returned in an array.
113 *
114 * @module photon-cdn
115 *
116 * @since 6.6.0
117 *
118 * @param array $values array( $slug = the plugin repository slug, i.e. jetpack, $version = the plugin version, i.e. 6.6 )
119 */
120 list( $plugin_slug, $current_version ) = apply_filters(
121 'jetpack_cdn_plugin_slug_and_version',
122 array( $plugin_slug, $current_version )
123 );
124
125 $assets = self::get_plugin_assets( $plugin_slug, $current_version );
126 $plugin_directory_url = plugins_url() . '/' . $plugin_slug . '/';
127
128 if ( is_wp_error( $assets ) || ! is_array( $assets ) ) {
129 return false;
130 }
131
132 foreach ( $wp_scripts->registered as $handle => $thing ) {
133 if ( wp_startswith( $thing->src, self::CDN ) ) {
134 continue;
135 }
136 if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
137 $local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
138 if ( in_array( $local_path, $assets, true ) ) {
139 $wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
140 $wp_scripts->registered[ $handle ]->ver = null;
141 }
142 }
143 }
144 foreach ( $wp_styles->registered as $handle => $thing ) {
145 if ( wp_startswith( $thing->src, self::CDN ) ) {
146 continue;
147 }
148 if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
149 $local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
150 if ( in_array( $local_path, $assets, true ) ) {
151 $wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
152 $wp_styles->registered[ $handle ]->ver = null;
153 }
154 }
155 }
156 }
157
158 /**
159 * Returns cdn-able assets for a given plugin.
160 *
161 * @param string $plugin plugin slug string.
162 * @param string $version plugin version number string.
163 * @return array|bool Will return false if not a public version.
164 */
165 public static function get_plugin_assets( $plugin, $version ) {
166 if ( 'jetpack' === $plugin && JETPACK__VERSION === $version ) {
167 if ( ! self::is_public_version( $version ) ) {
168 return false;
169 }
170
171 $assets = array(); // The variable will be redefined in the included file.
172
173 include JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php';
174 return $assets;
175 }
176
177 /**
178 * Used for other plugins to provide their bundled assets via filter to
179 * prevent the need of storing them in an option or an external api request
180 * to w.org.
181 *
182 * @module photon-cdn
183 *
184 * @since 6.6.0
185 *
186 * @param array $assets The assets array for the plugin.
187 * @param string $version The version of the plugin being requested.
188 */
189 $assets = apply_filters( "jetpack_cdn_plugin_assets-{$plugin}", null, $version );
190 if ( is_array( $assets ) ) {
191 return $assets;
192 }
193
194 if ( ! self::is_public_version( $version ) ) {
195 return false;
196 }
197
198 $cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() );
199 if ( isset( $cache[ $plugin ][ $version ] ) ) {
200 if ( is_array( $cache[ $plugin ][ $version ] ) ) {
201 return $cache[ $plugin ][ $version ];
202 }
203 if ( is_numeric( $cache[ $plugin ][ $version ] ) ) {
204 // Cache an empty result for up to 24h.
205 if ( intval( $cache[ $plugin ][ $version ] ) + DAY_IN_SECONDS > time() ) {
206 return array();
207 }
208 }
209 }
210
211 $url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version );
212
213 if ( wp_http_supports( array( 'ssl' ) ) ) {
214 $url = set_url_scheme( $url, 'https' );
215 }
216
217 $response = wp_remote_get( $url );
218
219 $body = trim( wp_remote_retrieve_body( $response ) );
220 $body = json_decode( $body, true );
221
222 $return = time();
223 if ( is_array( $body ) ) {
224 $return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) );
225 }
226
227 $cache[ $plugin ] = array();
228 $cache[ $plugin ][ $version ] = $return;
229 Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true );
230
231 return $return;
232 }
233
234 /**
235 * Checks a path whether it is a JS or CSS file.
236 *
237 * @param String $path file path.
238 * @return Boolean whether the file is a JS or CSS.
239 */
240 public static function is_js_or_css_file( $path ) {
241 return ( false === strpos( $path, '?' ) ) && in_array( substr( $path, -3 ), array( 'css', '.js' ), true );
242 }
243
244 /**
245 * Checks whether the version string indicates a production version.
246 *
247 * @param String $version the version string.
248 * @param Boolean $include_beta_and_rc whether to count beta and RC versions as production.
249 * @return Boolean
250 */
251 public static function is_public_version( $version, $include_beta_and_rc = false ) {
252 if ( preg_match( '/^\d+(\.\d+)+$/', $version ) ) {
253 // matches `1` `1.2` `1.2.3`.
254 return true;
255 } elseif ( $include_beta_and_rc && preg_match( '/^\d+(\.\d+)+(-(beta|rc|pressable)\d?)$/i', $version ) ) {
256 // matches `1.2.3` `1.2.3-beta` `1.2.3-pressable` `1.2.3-beta1` `1.2.3-rc` `1.2.3-rc2`.
257 return true;
258 }
259 // unrecognized version.
260 return false;
261 }
262 }
263 /**
264 * Allow plugins to short-circuit the Asset CDN, even when the module is on.
265 *
266 * @module photon-cdn
267 *
268 * @since 6.7.0
269 *
270 * @param false bool Should the Asset CDN be blocked? False by default.
271 */
272 if ( true !== apply_filters( 'jetpack_force_disable_site_accelerator', false ) ) {
273 Jetpack_Photon_Static_Assets_CDN::go();
274 }
275