-
Notifications
You must be signed in to change notification settings - Fork 33
/
jsconcat.php
273 lines (223 loc) · 7.84 KB
/
jsconcat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/*
Plugin Name: JS Concat
Plugin URI: http://wp-plugins.org/#
Description: Concatenates JS
Author: Automattic
Version: 0.01
Author URI: http://automattic.com/
*/
require_once( dirname( __FILE__ ) . '/concat-utils.php' );
if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) )
define( 'ALLOW_GZIP_COMPRESSION', true );
class WPcom_JS_Concat extends WP_Scripts {
private $old_scripts;
public $allow_gzip_compression;
function __construct( $scripts ) {
if ( empty( $scripts ) || ! ( $scripts instanceof WP_Scripts ) ) {
$this->old_scripts = new WP_Scripts();
} else {
$this->old_scripts = $scripts;
}
// Unset all the object properties except our private copy of the scripts object.
// We have to unset everything so that the overload methods talk to $this->old_scripts->whatever
// instead of $this->whatever.
foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
if ( 'old_scripts' === $key ) {
continue;
}
unset( $this->$key );
}
}
protected function has_inline_content( $handle ) {
$before_output = $this->get_data( $handle, 'before' );
if ( ! empty( $before_output ) ) {
return true;
}
$after_output = $this->get_data( $handle, 'after' );
if ( ! empty( $after_output ) ) {
return true;
}
// JavaScript translations
$has_translations = ! empty( $this->registered[ $handle ]->textdomain );
if ( $has_translations ) {
return true;
}
return false;
}
function do_items( $handles = false, $group = false ) {
$handles = false === $handles ? $this->queue : (array) $handles;
$javascripts= array();
$siteurl = apply_filters( 'ngx_http_concat_site_url', $this->base_url );
$this->all_deps( $handles );
$level = 0;
foreach( $this->to_do as $key => $handle ) {
if ( in_array( $handle, $this->done ) || !isset( $this->registered[$handle] ) )
continue;
if ( 0 === $group && $this->groups[$handle] > 0 ) {
$this->in_footer[] = $handle;
unset( $this->to_do[$key] );
continue;
}
if ( ! $this->registered[$handle]->src ) { // Defines a group.
if ( $this->do_item( $handle, $group ) ) {
$this->done[] = $handle;
}
continue;
}
if ( false === $group && in_array( $handle, $this->in_footer, true ) )
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
$obj = $this->registered[$handle];
$js_url = $obj->src;
$js_url_parsed = parse_url( $js_url );
$extra = $obj->extra;
// Don't concat by default
$do_concat = false;
// Only try to concat static js files
if ( false !== strpos( $js_url_parsed['path'], '.js' ) )
$do_concat = true;
// Don't try to concat externally hosted scripts
$is_internal_url = WPCOM_Concat_Utils::is_internal_url( $js_url, $siteurl );
if ( ! $is_internal_url ) {
$do_concat = false;
}
// Concat and canonicalize the paths only for
// existing scripts that aren't outside ABSPATH
$js_realpath = WPCOM_Concat_Utils::realpath( $js_url, $siteurl );
if ( ! $js_realpath || 0 !== strpos( $js_realpath, ABSPATH ) )
$do_concat = false;
else
$js_url_parsed['path'] = substr( $js_realpath, strlen( ABSPATH ) - 1 );
if ( $this->has_inline_content( $handle ) ) {
$do_concat = false;
}
// Skip core scripts that use Strict Mode
if ( 'react' === $handle || 'react-dom' === $handle ) {
$do_concat = false;
}
// Allow plugins to disable concatenation of certain scripts.
$do_concat = apply_filters( 'js_do_concat', $do_concat, $handle );
if ( true === $do_concat ) {
if ( !isset( $javascripts[$level] ) )
$javascripts[$level]['type'] = 'concat';
$javascripts[$level]['paths'][] = $js_url_parsed['path'];
$javascripts[$level]['handles'][] = $handle;
} else {
$level++;
$javascripts[$level]['type'] = 'do_item';
$javascripts[$level]['handle'] = $handle;
$level++;
}
unset( $this->to_do[$key] );
}
if ( empty( $javascripts ) )
return $this->done;
foreach ( $javascripts as $js_array ) {
if ( 'do_item' == $js_array['type'] ) {
if ( $this->do_item( $js_array['handle'], $group ) )
$this->done[] = $js_array['handle'];
} else if ( 'concat' == $js_array['type'] ) {
array_map( array( $this, 'print_extra_script' ), $js_array['handles'] );
if ( isset( $js_array['paths'] ) && count( $js_array['paths'] ) > 1) {
$paths = array_map( function( $url ) { return ABSPATH . $url; }, $js_array['paths'] );
$mtime = max( array_map( 'filemtime', $paths ) );
$path_str = implode( ',', $js_array['paths'] ) . "?m={$mtime}j";
if ( $this->allow_gzip_compression ) {
$path_64 = base64_encode( gzcompress( $path_str ) );
if ( strlen( $path_str ) > ( strlen( $path_64 ) + 1 ) )
$path_str = '-' . $path_64;
}
$href = $siteurl . "/_static/??" . $path_str;
} elseif ( isset( $js_array['paths'] ) && is_array( $js_array['paths'] ) ) {
$href = $this->cache_bust_mtime( $siteurl . $js_array['paths'][0], $siteurl );
}
$this->done = array_merge( $this->done, $js_array['handles'] );
// Print before/after scripts from wp_inline_scripts() and concatenated script tag
if ( isset( $js_array['extras']['before'] ) ) {
foreach ( $js_array['extras']['before'] as $inline_before ) {
echo $inline_before;
}
}
// Allowed attributes taken from: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
$allowed_attributes = array(
'async',
'defer',
'nomodule',
'crossorigin',
'integrity',
'type',
'nonce',
'referrerpolicy'
);
$attr_string = '';
/**
* Allow adding extra arguments for the script tag.
* Either associative array or regular array.
* E.g.
* [ 'async', 'defer', 'nonce' => '$random_generated_number' ]
*
* @param string $href URL for the script.
* @param array $js_array array that contains the type, path, and handle for the scripts being processed.
* @param WPcom_JS_Concat this instance of WPcom_JS_Concat.
*/
foreach ( (array) apply_filters( 'js_concat_script_attributes', [], $href, $js_array, $this ) as $k => $v ) {
if ( is_int( $k ) && in_array( $v, $allowed_attributes ) ) {
$attr_string .= sprintf( ' %s', esc_attr( $v ) );
} else if ( array_search( $k, $allowed_attributes ) ){
$attr_string .= sprintf( ' %s="%s"', sanitize_key( is_int( $k ) ? $v : $k ), esc_attr( $v ) );
}
}
if ( isset( $href ) ) {
$script_html = sprintf( '<script type="text/javascript" src="%s" %s></script>', $href, $attr_string );
echo apply_filters( "ngx_http_concat_script_loader_tag", $script_html, $href, $attr_string );
}
if ( isset( $js_array['extras']['after'] ) ) {
foreach ( $js_array['extras']['after'] as $inline_after ) {
echo $inline_after;
}
}
}
}
do_action( 'js_concat_did_items', $javascripts );
return $this->done;
}
function cache_bust_mtime( $url, $siteurl ) {
if ( strpos( $url, '?m=' ) )
return $url;
$parts = parse_url( $url );
if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) )
return $url;
$file = WPCOM_Concat_Utils::realpath( $url, $siteurl );
$mtime = false;
if ( file_exists( $file ) )
$mtime = filemtime( $file );
if ( ! $mtime )
return $url;
if ( false === strpos( $url, '?' ) ) {
$q = '';
} else {
list( $url, $q ) = explode( '?', $url, 2 );
if ( strlen( $q ) )
$q = '&' . $q;
}
return "$url?m={$mtime}g{$q}";
}
function __isset( $key ) {
return isset( $this->old_scripts->$key );
}
function __unset( $key ) {
unset( $this->old_scripts->$key );
}
function &__get( $key ) {
return $this->old_scripts->$key;
}
function __set( $key, $value ) {
$this->old_scripts->$key = $value;
}
}
function js_concat_init() {
global $wp_scripts;
$wp_scripts = new WPcom_JS_Concat( $wp_scripts );
$wp_scripts->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
}
add_action( 'init', 'js_concat_init' );