Completed
Push — add/crowdsignal-shortcode ( 65c42e...1b4a63 )
by Kuba
14:46 queued 06:22
created

Jetpack_Photon_Static_Assets_CDN::cdnize_assets()   B

Complexity

Conditions 11
Paths 4

Size

Total Lines 45

Duplication

Lines 20
Ratio 44.44 %

Importance

Changes 0
Metric Value
cc 11
nc 4
nop 0
dl 20
loc 45
rs 7.3166
c 0
b 0
f 0

How to fix   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: 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
	}
34
35
	/**
36
	 * Sets up CDN URLs for assets that are enqueued by the WordPress Core.
37
	 */
38
	public static function cdnize_assets() {
39
		global $wp_scripts, $wp_styles, $wp_version;
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 ( self::is_public_version( $version ) ) {
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 ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) {
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 ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ) ) ) {
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 a given plugin.
144
	 *
145
	 * @param string $plugin plugin slug string.
146
	 * @param string $version plugin version number string.
147
	 * @return array
148
	 */
149
	public static function get_plugin_assets( $plugin, $version ) {
150
		if ( 'jetpack' === $plugin && JETPACK__VERSION === $version ) {
151
			$assets = array(); // The variable will be redefined in the included file.
152
153
			include JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php';
154
			return $assets;
155
		}
156
157
		/**
158
		 * Used for other plugins to provide their bundled assets via filter to
159
		 * prevent the need of storing them in an option or an external api request
160
		 * to w.org.
161
		 *
162
		 * @since 6.6
163
		 *
164
		 * @param array $assets The assets array for the plugin.
165
		 * @param string $version The version of the plugin being requested.
166
		 */
167
		$assets = apply_filters( "jetpack_cdn_plugin_assets-{$plugin}", null, $version );
168
		if ( is_array( $assets ) ) {
169
			return $assets;
170
		}
171
172
		if ( ! self::is_public_version( $version ) ) {
173
			return false;
174
		}
175
176
		$cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() );
177
		if ( isset( $cache[ $plugin ][ $version ] ) ) {
178
			if ( is_array( $cache[ $plugin ][ $version ] ) ) {
179
				return $cache[ $plugin ][ $version ];
180
			}
181
			if ( is_numeric( $cache[ $plugin ][ $version ] ) ) {
182
				// Cache an empty result for up to 24h.
183
				if ( intval( $cache[ $plugin ][ $version ] ) + DAY_IN_SECONDS > time() ) {
184
					return array();
185
				}
186
			}
187
		}
188
189
		$url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version );
190
191
		if ( wp_http_supports( array( 'ssl' ) ) ) {
192
			$url = set_url_scheme( $url, 'https' );
193
		}
194
195
		$response = wp_remote_get( $url );
196
197
		$body = trim( wp_remote_retrieve_body( $response ) );
198
		$body = json_decode( $body, true );
199
200
		$return = time();
201
		if ( is_array( $body ) ) {
202
			$return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) );
203
		}
204
205
		$cache[ $plugin ]             = array();
206
		$cache[ $plugin ][ $version ] = $return;
207
		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...
208
209
		return $return;
210
	}
211
212
	/**
213
	 * Checks a path whether it is a JS or CSS file.
214
	 *
215
	 * @param String $path file path.
216
	 * @return Boolean whether the file is a JS or CSS.
217
	 */
218
	public static function is_js_or_css_file( $path ) {
219
		return ( false === strpos( $path, '?' ) ) && in_array( substr( $path, -3 ), array( 'css', '.js' ), true );
220
	}
221
222
	/**
223
	 * Checks whether the version string indicates a production version.
224
	 *
225
	 * @param String  $version the version string.
226
	 * @param Boolean $include_beta_and_rc whether to count beta and RC versions as production.
227
	 * @return Boolean
228
	 */
229
	public static function is_public_version( $version, $include_beta_and_rc = false ) {
230
		if ( preg_match( '/^\d+(\.\d+)+$/', $version ) ) {
231
			// matches `1` `1.2` `1.2.3`.
232
			return true;
233
		} elseif ( $include_beta_and_rc && preg_match( '/^\d+(\.\d+)+(-(beta|rc)\d?)$/i', $version ) ) {
234
			// matches `1.2.3` `1.2.3-beta` `1.2.3-beta1` `1.2.3-rc` `1.2.3-rc2`.
235
			return true;
236
		}
237
		// unrecognized version.
238
		return false;
239
	}
240
}
241
Jetpack_Photon_Static_Assets_CDN::go();
242