Completed
Push — add/asset-cdn ( 06cbb6...c26150 )
by
unknown
13:10 queued 05:01
created

Asset_CDN::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin Name: Asset CDN
4
 * Description: Speed up Javascript and CSS
5
 * Plugin URI: https://github.com/automattic/jetpack
6
 * Author: Automattic
7
 * Author URI: https://automattic.com
8
 * Version: 0.1.0
9
 * Text Domain: asset-cdn
10
 * Domain Path: /languages/
11
 * License: GPLv2 or later
12
 */
13
14
/**
15
 * TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
16
 * - versioning (combine ver hashes) and cachebusting
17
 * - concat/minify/serve JS too
18
 * - asset inlining for smaller styles?
19
 * - critical CSS support?
20
 * - non-enqueued assets?
21
 */
22
23
class Asset_CDN {
24
	private static $__instance = null;
25
26
	private $cdn_server;
27
	private $concat_style_groups = array();
28
	private $concat_script_groups = array();
29
	private $inject_critical_css = false;
30
31
	/**
32
	 * Singleton implementation
33
	 *
34
	 * @return object
35
	 */
36
	public static function instance() {
37
		if ( ! is_a( self::$__instance, 'Asset_CDN' ) ) {
38
			self::$__instance = new Asset_CDN();
39
		}
40
41
		return self::$__instance;
42
	}
43
44
	public static function reset() {
45
		self::$__instance = null;
46
	}
47
48
	private function __construct() {
49
		$this->cdn_server = apply_filters( 'jetpack_asset_cdn_url', 'https://cdn.wpvm.io' );
50
		// $this->cdn_server = 'http://localhost:8090';
51
52
		// allow smaller CSS by only minifying assets on the page
53
		add_filter( 'jetpack_implode_frontend_css', '__return_false' );
54
55
		// rewrite CSS tags
56
		add_filter( 'script_loader_tag', array( $this, 'register_concat_scripts' ), -100, 3 );
57
		add_filter( 'style_loader_tag', array( $this, 'register_concat_styles' ), -100, 4 );
58
59
		add_action( 'wp_head', array( $this, 'render_concatenated_styles_head' ), PHP_INT_MAX );
60
		add_action( 'wp_head', array( $this, 'render_concatenated_scripts_head' ), PHP_INT_MAX );
61
		add_action( 'wp_footer', array( $this, 'render_concatenated_styles_footer' ), PHP_INT_MAX );
62
		add_action( 'wp_footer', array( $this, 'render_concatenated_scripts_footer' ), PHP_INT_MAX );
63
	}
64
65
	/**
66
	 * Render functions
67
	 */
68
69
	function render_concatenated_styles_head() {
70
		if ( isset( $this->concat_style_groups[0] ) ) {
71
			$this->render_concatenated_styles( $this->concat_style_groups[0] );
72
		}
73
	}
74
75
	function render_concatenated_styles_footer() {
76
		if ( isset( $this->concat_style_groups[1] ) ) {
77
			$this->render_concatenated_styles( $this->concat_style_groups[1] );
78
		}
79
	}
80
81
	private function render_concatenated_styles( $style_groups ) {
82
		// special URL to concatenation service
83
		global $wp_styles;
84
		$site_url = site_url();
85
		foreach( $style_groups as $media => $styles ) {
86
			$urls = array();
87
			$vers = array();
88
89
			foreach( $styles as $style ) {
90
				$urls[] = str_replace( untrailingslashit( $site_url ), '', $style->src );
91
				$vers[] = $style->ver ? $style->ver : $wp_styles->default_version;
92
			}
93
94
			$cdn_url = $this->cdn_server . '/css?b=' .
95
				urlencode( $site_url ) . '&' .
96
				http_build_query( array( 'f' => $urls ) ) . '&' .
97
				http_build_query( array( 'v' => $vers ) );
98
			// if we are injecting critical CSS, load the full CSS async
99
100
			if ( $this->inject_critical_css ) {
101
				echo '<!-- jetpack concat --><link rel="preload" onload="this.rel=\'stylesheet\'" as="style" type="text/css" media="' . $media . '" href="' . esc_attr( $cdn_url ) . '"/>';
102
			} else {
103
				echo '<!-- jetpack concat --><link rel="stylesheet" type="text/css" media="' . $media . '" href="' . esc_attr( $cdn_url ) . '"/>';
104
			}
105
		}
106
	}
107
108
	function render_concatenated_scripts_head() {
109
		if ( isset( $this->concat_script_groups[0] ) ) {
110
			$this->render_concatenated_scripts( $this->concat_script_groups[0] );
111
		}
112
	}
113
114
	function render_concatenated_scripts_footer() {
115
		if ( isset( $this->concat_script_groups[1] ) ) {
116
			$this->render_concatenated_scripts( $this->concat_script_groups[1] );
117
		}
118
	}
119
120
	private function render_concatenated_scripts( $scripts ) {
121
		// special URL to concatenation service
122
		global $wp_scripts;
123
		$site_url = site_url();
124
		$urls = array();
125
		$vers = array();
126
127
		foreach( $scripts as $script ) {
128
			$urls[] = str_replace( untrailingslashit( $site_url ), '', $script->src );
129
			$vers[] = $script->ver ? $script->ver : $wp_scripts->default_version;
130
			if ( isset( $script->extra['before'] ) && $script->extra['before'] ) {
131
				echo sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $script->extra['before'] );
132
			}
133
		}
134
135
		$cdn_url = $this->cdn_server . '/js?b=' .
136
			urlencode( $site_url ) . '&' .
137
			http_build_query( array( 'f' => $urls ) ) . '&' .
138
			http_build_query( array( 'v' => $vers ) );
139
140
		// TODO: if there is NO inline or external script tags in the body, render async (maybe?)
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
141
		echo '<script type="text/javascript" src="' . esc_attr( $cdn_url ) . '"></script>';
142
143
		foreach( $scripts as $script ) {
144
			if ( isset( $script->extra['after'] ) && $script->extra['after'] ) {
145
				echo sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $script->extra['after'] );
146
			}
147
		}
148
	}
149
150
	/**
151
	 * Asset modification functions
152
	 */
153
154
	/**
155
	 * Scripts
156
	 */
157
158 View Code Duplication
	public function register_concat_scripts( $tag, $handle, $src ) {
159
		global $wp_scripts;
160
161
		// don't do admin for now
162
		if ( is_admin() || ! isset( $wp_scripts->registered[$handle] ) ) {
163
			return $tag;
164
		}
165
166
		$script = $wp_scripts->registered[$handle];
167
168
		if ( $this->should_concat_script( $script ) ) {
169
			$this->buffer_script( $script );
170
			return '';
171
		}
172
173
		// TODO: if this script is dependent on one that has been buffered, we need to flush the concatenated
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
174
		// scripts and then output this script
175
176
		return $tag;
177
	}
178
179 View Code Duplication
	private function should_concat_script( $script ) {
180
		// only concat local scripts
181
		$is_local       = $this->is_local_url( $script->src );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 7 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
182
		// don't concat conditional scripts
183
		$is_conditional = isset( $script->extra['conditional'] );
184
		return apply_filters( 'jetpack_perf_concat_script', $is_local && ! $is_conditional, $script->handle, $script->src );
185
	}
186
187
	private function buffer_script( $script ) {
188
		$group = isset( $script->extra['group'] ) ? $script->extra['group'] : 0;
189
		if ( ! isset( $this->concat_script_groups[$group] ) ) {
190
			$this->concat_script_groups[$group] = array();
191
		}
192
		$this->concat_script_groups[$group][] = $script;
193
	}
194
195
	/**
196
	 * Styles
197
	 */
198
199 View Code Duplication
	public function register_concat_styles( $tag, $handle, $href, $media ) {
200
		global $wp_styles;
201
202
		// don't do admin for now
203
		if ( is_admin() || ! isset( $wp_styles->registered[$handle] ) ) {
204
			return $tag;
205
		}
206
207
		$style = $wp_styles->registered[$handle];
208
209
		if ( $this->should_concat_style( $style ) ) {
210
			$this->buffer_style( $style );
211
			return '';
212
		}
213
214
		return $tag;
215
	}
216
217
	private function buffer_style( $style ) {
218
		$group = isset( $style->extra['group'] ) ? $style->extra['group'] : 0;
219
		$media = $style->args;
220
		if ( ! $media ) {
221
			$media = 'all';
222
		}
223
		if ( ! isset( $this->concat_style_groups[$group] ) ) {
224
			$this->concat_style_groups[$group] = array();
225
		}
226
		if ( ! isset( $this->concat_style_groups[$group][$media] ) ) {
227
			$this->concat_style_groups[$group][$media] = array();
228
		}
229
		$this->concat_style_groups[$group][$media][] = $style;
230
	}
231
232 View Code Duplication
	private function should_concat_style( $style ) {
233
		// only concat local styles
234
		$is_local       = $this->is_local_url( $style->src );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 7 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
235
		// don't concat conditional styles
236
		$is_conditional = isset( $style->extra['conditional'] );
237
		return apply_filters( 'jetpack_perf_concat_style', $is_local && ! $is_conditional, $style->handle, $style->src );
238
	}
239
240
	private function is_local_url( $url ) {
241
		$site_url = site_url();
242
		return ( strncmp( $url, '/', 1 ) === 0 && strncmp( $url, '//', 2 ) !== 0 )
243
			|| strpos( $url, $site_url ) === 0;
244
	}
245
}