Completed
Push — feature/assets-cdn ( 4f639f...7aef3f )
by George
07:52
created

get_plugin_assets()   C

Complexity

Conditions 11
Paths 17

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 17
nop 2
dl 0
loc 62
rs 6.6824
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Module Name: Photon 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
Jetpack::dns_prefetch( array(
16
	'//c0.wp.com',
17
) );
18
19
class Jetpack_Photon_Static_Assets_CDN {
20
	const CDN = 'https://c0.wp.com/';
21
22
	/**
23
	 * Sets up action handlers needed for Jetpack CDN.
24
	 */
25
	public static function go() {
26
		add_action( 'wp_print_scripts', array( __CLASS__, 'cdnize_assets' ) );
27
		add_action( 'wp_print_styles', array( __CLASS__, 'cdnize_assets' ) );
28
		add_action( 'admin_print_scripts', array( __CLASS__, 'cdnize_assets' ) );
29
		add_action( 'admin_print_styles', array( __CLASS__, 'cdnize_assets' ) );
30
		add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) );
31
	}
32
33
	/**
34
	 * Sets up CDN URLs for assets that are enqueued by the WordPress Core.
35
	 */
36
	public static function cdnize_assets() {
37
		global $wp_scripts, $wp_styles, $wp_version;
38
39
		$known_core_files = self::get_core_assets();
40
41
		/**
42
		 * Filters Jetpack CDN's Core version number and locale. Can be used to override the values
43
		 * that Jetpack uses to retrieve assets. Expects the values to be returned in an array.
44
		 *
45
		 * @since 6.6
46
		 *
47
		 * @param array $values array( $version  = core assets version, i.e. 4.9.8, $locale = desired locale )
48
		 */
49
		list( $version, $locale ) = apply_filters(
0 ignored issues
show
Unused Code introduced by
The assignment to $locale is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
50
			'jetpack_cdn_core_version_and_locale',
51
			array( $wp_version, get_locale() )
52
		);
53
54
		if ( ! empty( $known_core_files ) && is_array( $known_core_files ) ) {
55
			$site_url = trailingslashit( site_url() );
56 View Code Duplication
			foreach ( $wp_scripts->registered as $handle => $thing ) {
57
				if ( wp_startswith( $thing->src, self::CDN ) ) {
58
					continue;
59
				}
60
				$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
61
				if ( in_array( $src, $known_core_files, true ) ) {
62
					$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
63
					$wp_scripts->registered[ $handle ]->ver = null;
64
				}
65
			}
66 View Code Duplication
			foreach ( $wp_styles->registered as $handle => $thing ) {
67
				if ( wp_startswith( $thing->src, self::CDN ) ) {
68
					continue;
69
				}
70
				$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
71
				if ( in_array( $src, $known_core_files, true ) ) {
72
					$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
73
					$wp_styles->registered[ $handle ]->ver = null;
74
				}
75
			}
76
		}
77
78
		self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION );
79
		if ( class_exists( 'WooCommerce' ) ) {
80
			self::cdnize_plugin_assets( 'woocommerce', WC_VERSION );
81
		}
82
	}
83
84
	/**
85
	 * Sets up CDN URLs for supported plugin assets.
86
	 *
87
	 * @param String $plugin_slug plugin slug string.
88
	 * @param String $current_version plugin version string.
89
	 * @return null|bool
90
	 */
91
	public static function cdnize_plugin_assets( $plugin_slug, $current_version ) {
92
		global $wp_scripts, $wp_styles;
93
94
		/**
95
		 * Filters Jetpack CDN's plugin slug and version number. Can be used to override the values
96
		 * that Jetpack uses to retrieve assets. For example, when testing a development version of Jetpack
97
		 * the assets are not yet published, so you may need to override the version value to either
98
		 * trunk, or the latest available version. Expects the values to be returned in an array.
99
		 *
100
		 * @since 6.6
101
		 *
102
		 * @param array $values array( $slug = the plugin repository slug, i.e. jetpack, $version = the plugin version, i.e. 6.6 )
103
		 */
104
		list( $plugin_slug, $current_version ) = apply_filters(
105
			'jetpack_cdn_plugin_slug_and_version',
106
			array( $plugin_slug, $current_version )
107
		);
108
109
		$assets               = self::get_plugin_assets( $plugin_slug, $current_version );
110
		$plugin_directory_url = plugins_url() . '/' . $plugin_slug . '/';
111
112
		if ( is_wp_error( $assets ) || ! is_array( $assets ) ) {
113
			return false;
114
		}
115
116 View Code Duplication
		foreach ( $wp_scripts->registered as $handle => $thing ) {
117
			if ( wp_startswith( $thing->src, self::CDN ) ) {
118
				continue;
119
			}
120
			if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
121
				$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
122
				if ( in_array( $local_path, $assets, true ) ) {
123
					$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
124
					$wp_scripts->registered[ $handle ]->ver = null;
125
				}
126
			}
127
		}
128 View Code Duplication
		foreach ( $wp_styles->registered as $handle => $thing ) {
129
			if ( wp_startswith( $thing->src, self::CDN ) ) {
130
				continue;
131
			}
132
			if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
133
				$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
134
				if ( in_array( $local_path, $assets, true ) ) {
135
					$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
136
					$wp_styles->registered[ $handle ]->ver = null;
137
				}
138
			}
139
		}
140
	}
141
142
	/**
143
	 * Returns cdn-able assets for core.
144
	 *
145
	 * @param String $version Core version number string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $version not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
146
	 * @param String $locale locale string, for example, en_US.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $locale not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
147
	 * @return array|bool
148
	 */
149
	public static function get_core_assets( $version = null, $locale = null ) {
150
		if ( empty( $version ) ) {
151
			$version = $GLOBALS['wp_version'];
152
		}
153
		if ( empty( $locale ) ) {
154
			$locale = get_locale();
155
		}
156
157
		/** This filter is already documented in modules/photon-cdn.php */
158
		list( $version, $locale ) = apply_filters(
159
			'jetpack_cdn_core_version_and_locale',
160
			array( $version, $locale )
161
		);
162
163
		$cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() );
164
		if ( isset( $cache['core'][ $version ][ $locale ] ) ) {
165
			return $cache['core'][ $version ][ $locale ];
166
		}
167
168
		require_once ABSPATH . 'wp-admin/includes/update.php';
169
		$checksums = get_core_checksums( $version, $locale );
170
171
		if ( empty( $checksums ) ) {
172
			return false;
173
		}
174
175
		$return = array_filter( array_keys( $checksums ), array( __CLASS__, 'is_js_or_css_file' ) );
176
177
		if ( ! isset( $cache['core'][ $version ] ) ) {
178
			$cache['core']             = array();
179
			$cache['core'][ $version ] = array();
180
		}
181
		$cache['core'][ $version ][ $locale ] = $return;
182
		Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
184
		return $return;
185
	}
186
187
	/**
188
	 * Returns cdn-able assets for a given plugin.
189
	 *
190
	 * @param string $plugin plugin slug string.
191
	 * @param string $version plugin version number string.
192
	 * @return array
193
	 */
194
	public static function get_plugin_assets( $plugin, $version ) {
195
		if ( 'jetpack' === $plugin && JETPACK__VERSION === $version ) {
196
			$assets = array(); // The variable will be redefined in the included file.
197
198
			include JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php';
199
			return $assets;
200
		}
201
202
		/**
203
		 * Used for other plugins to provide their bundled assets via filter to
204
		 * prevent the need of storing them in an option or an external api request
205
		 * to w.org.
206
		 *
207
		 * @since 6.6
208
		 *
209
		 * @param array $assets The assets array for the plugin.
210
		 * @param string $version The version of the plugin being requested.
211
		 */
212
		$assets = apply_filters( "jetpack_cdn_plugin_assets-{$plugin}", null, $version );
213
		if ( is_array( $assets ) ) {
214
			return $assets;
215
		}
216
217
		if ( ! self::is_public_version( $version ) ) {
218
			return false;
219
		}
220
221
		$cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() );
222
		if ( isset( $cache[ $plugin ][ $version ] ) ) {
223
			if ( is_array( $cache[ $plugin ][ $version ] ) ) {
224
				return $cache[ $plugin ][ $version ];
225
			}
226
			if ( is_numeric( $cache[ $plugin ][ $version ] ) ) {
227
				// Cache an empty result for up to 24h.
228
				if ( intval( $cache[ $plugin ][ $version ] ) + DAY_IN_SECONDS > time() ) {
229
					return array();
230
				}
231
			}
232
		}
233
234
		$url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version );
235
236
		if ( wp_http_supports( array( 'ssl' ) ) ) {
237
			$url = set_url_scheme( $url, 'https' );
238
		}
239
240
		$response = wp_remote_get( $url );
241
242
		$body = trim( wp_remote_retrieve_body( $response ) );
243
		$body = json_decode( $body, true );
244
245
		$return = time();
246
		if ( is_array( $body ) ) {
247
			$return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) );
248
		}
249
250
		$cache[ $plugin ]             = array();
251
		$cache[ $plugin ][ $version ] = $return;
252
		Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
253
254
		return $return;
255
	}
256
257
	/**
258
	 * Checks a path whether it is a JS or CSS file.
259
	 *
260
	 * @param String $path file path.
261
	 * @return Boolean whether the file is a JS or CSS.
262
	 */
263
	public static function is_js_or_css_file( $path ) {
264
		return in_array( substr( $path, -3 ), array( 'css', '.js' ), true );
265
	}
266
267
	/**
268
	 * Checks whether the version string indicates a production version.
269
	 *
270
	 * @param String  $version the version string.
271
	 * @param Boolean $include_beta_and_rc whether to count beta and RC versions as production.
272
	 * @return Boolean
273
	 */
274
	public static function is_public_version( $version, $include_beta_and_rc = false ) {
275
		if ( preg_match( '/^\d+(\.\d+)+$/', $version ) ) {
276
			// matches `1` `1.2` `1.2.3`.
277
			return true;
278
		} elseif ( $include_beta_and_rc && preg_match( '/^\d+(\.\d+)+(-(beta|rc)\d?)$/i', $version ) ) {
279
			// matches `1.2.3` `1.2.3-beta` `1.2.3-beta1` `1.2.3-rc` `1.2.3-rc2`.
280
			return true;
281
		}
282
		// unrecognized version.
283
		return false;
284
	}
285
}
286
Jetpack_Photon_Static_Assets_CDN::go();
287