Completed
Push — update/build-to-prepare-for-wp... ( a960c5...31a28f )
by
unknown
156:22 queued 147:01
created

Assets::staticize_subdomain()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Assets package.
4
 *
5
 * @package  automattic/jetpack-assets
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Constants as Jetpack_Constants;
11
12
/**
13
 * Class Assets
14
 */
15
class Assets {
16
	/**
17
	 * Holds all the scripts handles that should be loaded in a deferred fashion.
18
	 *
19
	 * @var array
20
	 */
21
	private $defer_script_handles = array();
22
	/**
23
	 * The singleton instance of this class.
24
	 *
25
	 * @var Assets
26
	 */
27
	protected static $instance;
28
29
	/**
30
	 * Constructor.
31
	 *
32
	 * Static-only class, so nothing here.
33
	 */
34
	private function __construct() {}
35
36
	/**
37
	 * Get the singleton instance of the class.
38
	 *
39
	 * @return Assets
40
	 */
41
	public static function instance() {
42
		if ( ! isset( self::$instance ) ) {
43
			self::$instance = new Assets();
44
			self::$instance->init_hooks();
45
		}
46
47
		return self::$instance;
48
	}
49
50
	/**
51
	 * Initalize the hooks as needed.
52
	 */
53
	private function init_hooks() {
54
		/*
55
		 * Load some scripts asynchronously.
56
		 */
57
		add_filter( 'script_loader_tag', array( $this, 'script_add_async' ), 10, 2 );
58
	}
59
60
	/**
61
	 * A public method for adding the async script.
62
	 *
63
	 * @param string $script_handle Script handle.
64
	 */
65
	public function add_async_script( $script_handle ) {
66
		$this->defer_script_handles[] = $script_handle;
67
	}
68
69
	/**
70
	 * Add an async attribute to scripts that can be loaded deferred.
71
	 * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
72
	 *
73
	 * @param string $tag    The <script> tag for the enqueued script.
74
	 * @param string $handle The script's registered handle.
75
	 */
76
	public function script_add_async( $tag, $handle ) {
77
		if ( empty( $this->defer_script_handles ) ) {
78
			return $tag;
79
		}
80
81
		if ( in_array( $handle, $this->defer_script_handles, true ) ) {
82
			return preg_replace( '/^<script /i', '<script defer ', $tag );
83
		}
84
85
		return $tag;
86
	}
87
88
	/**
89
	 * Given a minified path, and a non-minified path, will return
90
	 * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy.
91
	 *
92
	 * If $package_path is provided, then the minified or non-minified file URL will be generated
93
	 * relative to the root package directory.
94
	 *
95
	 * Both `$min_base` and `$non_min_base` can be either full URLs, or are expected to be relative to the
96
	 * root Jetpack directory.
97
	 *
98
	 * @param string $min_path     minified path.
99
	 * @param string $non_min_path non-minified path.
100
	 * @param string $package_path Optional. A full path to a file inside a package directory
101
	 *                             The URL will be relative to its directory. Default empty.
102
	 *                             Typically this is done by passing __FILE__ as the argument.
103
	 *
104
	 * @return string The URL to the file
105
	 * @since 5.6.0
106
	 */
107
	public static function get_file_url_for_environment( $min_path, $non_min_path, $package_path = '' ) {
108
		$path = ( Jetpack_Constants::is_defined( 'SCRIPT_DEBUG' ) && Jetpack_Constants::get_constant( 'SCRIPT_DEBUG' ) )
109
			? $non_min_path
110
			: $min_path;
111
112
		/*
113
		 * If the path is actually a full URL, keep that.
114
		 * We look for a host value, since enqueues are sometimes without a scheme.
115
		 */
116
		$file_parts = wp_parse_url( $path );
117
		if ( ! empty( $file_parts['host'] ) ) {
118
			$url = $path;
119
		} else {
120
			$plugin_path = empty( $package_path ) ? Jetpack_Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) : $package_path;
121
122
			$url = plugins_url( $path, $plugin_path );
123
		}
124
125
		/**
126
		 * Filters the URL for a file passed through the get_file_url_for_environment function.
127
		 *
128
		 * @since 8.1.0
129
		 *
130
		 * @package assets
131
		 *
132
		 * @param string $url The URL to the file.
133
		 * @param string $min_path The minified path.
134
		 * @param string $non_min_path The non-minified path.
135
		 */
136
		return apply_filters( 'jetpack_get_file_for_environment', $url, $min_path, $non_min_path );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $min_path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
137
	}
138
139
	/**
140
	 * A helper function that lets you enqueue scripts in an async fashion.
141
	 *
142
	 * @param string $handle        Name of the script. Should be unique.
143
	 * @param string $min_path      Minimized script path.
144
	 * @param string $non_min_path  Full Script path.
145
	 * @param array  $deps           Array of script dependencies.
146
	 * @param bool   $ver             The script version.
147
	 * @param bool   $in_footer       Should the script be included in the footer.
148
	 */
149
	public static function enqueue_async_script( $handle, $min_path, $non_min_path, $deps = array(), $ver = false, $in_footer = true ) {
150
		$assets_instance = self::instance();
151
		$assets_instance->add_async_script( $handle );
152
		wp_enqueue_script( $handle, self::get_file_url_for_environment( $min_path, $non_min_path ), $deps, $ver, $in_footer );
153
	}
154
155
	/**
156
	 * Passes an array of URLs to wp_resource_hints.
157
	 *
158
	 * @since 8.8.0
159
	 *
160
	 * @param string|array $urls URLs to hint.
161
	 * @param string       $type One of the supported resource types: dns-prefetch (default), preconnect, prefetch, or prerender.
162
	 */
163
	public static function add_resource_hint( $urls, $type = 'dns-prefetch' ) {
164
		add_filter(
165
			'wp_resource_hints',
166
			function ( $hints, $resource_type ) use ( $urls, $type ) {
167
				if ( $resource_type === $type ) {
168
					// Type casting to array required since the function accepts a single string.
169
					foreach ( (array) $urls as $url ) {
170
						$hints[] = $url;
171
					}
172
				}
173
				return $hints;
174
			},
175
			10,
176
			2
177
		);
178
	}
179
180
	/**
181
	 * Serve a WordPress.com static resource via a randomized wp.com subdomain.
182
	 *
183
	 * @since 9.3.0
184
	 *
185
	 * @param string $url WordPress.com static resource URL.
186
	 *
187
	 * @return string $url
188
	 */
189
	public static function staticize_subdomain( $url ) {
190
		// Extract hostname from URL.
191
		$host = wp_parse_url( $url, PHP_URL_HOST );
192
193
		// Explode hostname on '.'.
194
		$exploded_host = explode( '.', $host );
195
196
		// Retrieve the name and TLD.
197
		if ( count( $exploded_host ) > 1 ) {
198
			$name = $exploded_host[ count( $exploded_host ) - 2 ];
199
			$tld  = $exploded_host[ count( $exploded_host ) - 1 ];
200
			// Rebuild domain excluding subdomains.
201
			$domain = $name . '.' . $tld;
202
		} else {
203
			$domain = $host;
204
		}
205
		// Array of Automattic domains.
206
		$domains_allowed = array( 'wordpress.com', 'wp.com' );
207
208
		// Return $url if not an Automattic domain.
209
		if ( ! in_array( $domain, $domains_allowed, true ) ) {
210
			return $url;
211
		}
212
213
		if ( \is_ssl() ) {
214
			return preg_replace( '|https?://[^/]++/|', 'https://s-ssl.wordpress.com/', $url );
215
		}
216
217
		/*
218
		 * Generate a random subdomain id by taking the modulus of the crc32 value of the URL.
219
		 * Valid values are 0, 1, and 2.
220
		 */
221
		$static_counter = abs( crc32( basename( $url ) ) % 3 );
222
223
		return preg_replace( '|://[^/]+?/|', "://s$static_counter.wp.com/", $url );
224
	}
225
}
226