Completed
Push — add/asset-cdn ( c81376...61c426 )
by
unknown
22:20 queued 14:15
created

Asset_CDN::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 16
rs 9.4285
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
	private function __construct() {
45
		// $this->cdn_server = 'https://cdn.wpvm.io';
46
		$this->cdn_server = 'http://localhost:8090';
47
48
		// allow smaller CSS by only minifying assets on the page
49
		add_filter( 'jetpack_implode_frontend_css', '__return_false' );
50
51
		// rewrite CSS tags
52
		add_filter( 'script_loader_tag', array( $this, 'register_concat_scripts' ), -100, 3 );
53
		add_filter( 'style_loader_tag', array( $this, 'register_concat_styles' ), -100, 4 );
54
55
		add_action( 'wp_head', array( $this, 'render_concatenated_styles_head' ), PHP_INT_MAX );
56
		add_action( 'wp_head', array( $this, 'render_concatenated_scripts_head' ), PHP_INT_MAX );
57
		add_action( 'wp_footer', array( $this, 'render_concatenated_styles_footer' ), PHP_INT_MAX );
58
		add_action( 'wp_footer', array( $this, 'render_concatenated_scripts_footer' ), PHP_INT_MAX );
59
	}
60
61
	/**
62
	 * Render functions
63
	 */
64
65
	function render_concatenated_styles_head() {
66
		if ( isset( $this->concat_style_groups[0] ) ) {
67
			$this->render_concatenated_styles( $this->concat_style_groups[0] );
68
		}
69
	}
70
71
	function render_concatenated_styles_footer() {
72
		if ( isset( $this->concat_style_groups[1] ) ) {
73
			$this->render_concatenated_styles( $this->concat_style_groups[1] );
74
		}
75
	}
76
77
	private function render_concatenated_styles( $style_groups ) {
78
		// special URL to concatenation service
79
		global $wp_styles;
80
		$site_url = site_url();
81
		foreach( $style_groups as $media => $styles ) {
82
			$urls = array();
83
			$vers = array();
84
85
			foreach( $styles as $style ) {
86
				$urls[] = str_replace( untrailingslashit( $site_url ), '', $style->src );
87
				$vers[] = $style->ver ? $style->ver : $wp_styles->default_version;
88
			}
89
90
			$cdn_url = $this->cdn_server . '/css?b=' .
91
				urlencode( $site_url ) . '&' .
92
				http_build_query( array( 'f' => $urls ) ) . '&' .
93
				http_build_query( array( 'v' => $vers ) );
94
			// if we are injecting critical CSS, load the full CSS async
95
			if ( $this->inject_critical_css ) {
96
				echo '<!-- jetpack concat --><link rel="preload" onload="this.rel=\'stylesheet\'" as="style" type="text/css" media="' . $media . '" href="' . esc_attr( $cdn_url ) . '"/>';
97
			} else {
98
				echo '<!-- jetpack concat --><link rel="stylesheet" type="text/css" media="' . $media . '" href="' . esc_attr( $cdn_url ) . '"/>';
99
			}
100
		}
101
	}
102
103
	function render_concatenated_scripts_head() {
104
		if ( isset( $this->concat_script_groups[0] ) ) {
105
			$this->render_concatenated_scripts( $this->concat_script_groups[0] );
106
		}
107
	}
108
109
	function render_concatenated_scripts_footer() {
110
		if ( isset( $this->concat_script_groups[1] ) ) {
111
			$this->render_concatenated_scripts( $this->concat_script_groups[1] );
112
		}
113
	}
114
115
	private function render_concatenated_scripts( $scripts ) {
116
		// special URL to concatenation service
117
		global $wp_scripts;
118
		$site_url = site_url();
119
		$urls = array();
120
		$vers = array();
121
122
		foreach( $scripts as $script ) {
123
			$urls[] = str_replace( untrailingslashit( $site_url ), '', $script->src );
124
			$vers[] = $script->ver ? $script->ver : $wp_scripts->default_version;
125
		}
126
127
		$cdn_url = $this->cdn_server . '/js?b=' .
128
			urlencode( $site_url ) . '&' .
129
			http_build_query( array( 'f' => $urls ) ) . '&' .
130
			http_build_query( array( 'v' => $vers ) );
131
		// 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...
132
		echo '<script type="text/javascript" src="' . esc_attr( $cdn_url ) . '"></script>';
133
	}
134
135
	/**
136
	 * Asset modification functions
137
	 */
138
139
	/**
140
	 * Scripts
141
	 */
142
143 View Code Duplication
	public function register_concat_scripts( $tag, $handle, $src ) {
144
		global $wp_scripts;
145
146
		// don't do admin for now
147
		if ( is_admin() || ! isset( $wp_scripts->registered[$handle] ) ) {
148
			return $tag;
149
		}
150
151
		$script = $wp_scripts->registered[$handle];
152
153
		if ( $this->should_concat_script( $script ) ) {
154
			$this->buffer_script( $script );
155
			return '';
156
		}
157
158
		return $tag;
159
	}
160
161 View Code Duplication
	private function should_concat_script( $script ) {
162
		// only concat local scripts
163
		$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...
164
		// don't concat conditional scripts
165
		$is_conditional = isset( $script->extra['conditional'] );
166
		return apply_filters( 'jetpack_perf_concat_script', $is_local && ! $is_conditional, $script->handle, $script->src );
167
	}
168
169
	private function buffer_script( $script ) {
170
		$group = isset( $script->extra['group'] ) ? $script->extra['group'] : 0;
171
		if ( ! isset( $this->concat_script_groups[$group] ) ) {
172
			$this->concat_script_groups[$group] = array();
173
		}
174
		$this->concat_script_groups[$group][] = $script;
175
	}
176
177
	/**
178
	 * Styles
179
	 */
180
181 View Code Duplication
	public function register_concat_styles( $tag, $handle, $href, $media ) {
182
		global $wp_styles;
183
184
		// don't do admin for now
185
		if ( is_admin() || ! isset( $wp_styles->registered[$handle] ) ) {
186
			return $tag;
187
		}
188
189
		$style = $wp_styles->registered[$handle];
190
191
		if ( $this->should_concat_style( $style ) ) {
192
			$this->buffer_style( $style );
193
			return '';
194
		}
195
196
		return $tag;
197
	}
198
199
	private function buffer_style( $style ) {
200
		$group = isset( $style->extra['group'] ) ? $style->extra['group'] : 0;
201
		$media = $style->args;
202
		if ( ! $media ) {
203
			$media = 'all';
204
		}
205
		if ( ! isset( $this->concat_style_groups[$group] ) ) {
206
			$this->concat_style_groups[$group] = array();
207
		}
208
		if ( ! isset( $this->concat_style_groups[$group][$media] ) ) {
209
			$this->concat_style_groups[$group][$media] = array();
210
		}
211
		$this->concat_style_groups[$group][$media][] = $style;
212
	}
213
214 View Code Duplication
	private function should_concat_style( $style ) {
215
		// only concat local styles
216
		$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...
217
		// don't concat conditional styles
218
		$is_conditional = isset( $style->extra['conditional'] );
219
		return apply_filters( 'jetpack_perf_concat_style', $is_local && ! $is_conditional, $style->handle, $style->src );
220
	}
221
222
	private function is_local_url( $url ) {
223
		$site_url = site_url();
224
		return ( strncmp( $url, '/', 1 ) === 0 && strncmp( $url, '//', 2 ) !== 0 )
225
			|| strpos( $url, $site_url ) === 0;
226
	}
227
}