Remove.php
7.99 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
namespace WP_Rocket\Engine\Optimization\QueryString;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\deprecated\DeprecatedClassTrait;
use WP_Rocket\Engine\Optimization\AbstractOptimization;
use WP_Rocket\Engine\Optimization\CSSTrait;
/**
* Remove query string from static resources.
*
* @since 3.1
* @since 3.6 Deprecated.
* @author Remy Perona
* @deprecated
*/
class Remove extends AbstractOptimization {
use DeprecatedClassTrait;
use CSSTrait;
/**
* Plugin options instance.
*
* @since 3.1
* @author Remy Perona
*
* @var Options_Data
*/
protected $options;
/**
* Cache busting base path
*
* @since 3.1
* @author Remy Perona
*
* @var string
*/
protected $busting_path;
/**
* Cache busting base URL
*
* @since 3.1
* @author Remy Perona
*
* @var string
*/
protected $busting_url;
/**
* Excluded files from optimization
*
* @since 3.1
* @author Remy Perona
*
* @var string
*/
protected $excluded_files;
/**
* Constructor
*
* @since 3.1
* @author Remy Perona
*
* @param Options_Data $options Plugin options instance.
* @param string $busting_path Base cache busting files path.
* @param string $busting_url Base cache busting files URL.
*/
public function __construct( Options_Data $options, $busting_path, $busting_url ) {
self::deprecated_class( '3.6' );
$this->options = $options;
$this->busting_path = $busting_path . get_current_blog_id() . '/';
$this->busting_url = $busting_url . get_current_blog_id() . '/';
}
/**
* Returns a regex-ready string with the excluded filepaths for the Remove Query Strings option
*
* @since 3.3.3
* @author Remy Perona
*
* @return string
*/
protected function get_excluded_files() {
static $excluded_files;
if ( isset( $excluded_files ) ) {
return $excluded_files;
}
/**
* Filters files to exclude from cache busting
*
* @since 2.9.3
* @author Remy Perona
*
* @param array $excluded_files An array of filepath to exclude.
*/
$excluded_files = apply_filters( 'rocket_exclude_cache_busting', [] );
if ( empty( $excluded_files ) ) {
$excluded_files = '';
return $excluded_files;
}
foreach ( $excluded_files as $i => $excluded_file ) {
// Escape character for future use in regex pattern.
$excluded_files[ $i ] = str_replace( '#', '\#', $excluded_file );
}
$excluded_files = implode( '|', $excluded_files );
return $excluded_files;
}
/**
* Remove query strings for CSS files that have one
*
* @since 3.1
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
*/
public function remove_query_strings_css( $html ) {
$html_nocomments = preg_replace( '/<!--(.*)-->/Uis', '', $html );
$styles = $this->find( '<link\s+([^>]+[\s\'"])?href\s*=\s*[\'"]\s*?([^\'"]+\.css(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html_nocomments );
if ( ! $styles ) {
return $html;
}
foreach ( $styles as $style ) {
$url = $style[2];
$url = $this->can_replace( $url );
if ( ! $url ) {
continue;
}
$optimized_url = $this->replace_url( $url, 'css' );
if ( ! $optimized_url ) {
continue;
}
$replace_style = str_replace( $style[2], $optimized_url, $style[0] );
$html = str_replace( $style[0], $replace_style, $html );
}
return $html;
}
/**
* Remove query strings for JS files that have one
*
* @since 3.1
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
*/
public function remove_query_strings_js( $html ) {
$html_nocomments = $this->hide_comments( $html );
$scripts = $this->find( '<script\s+([^>]+[\s\'"])?src\s*=\s*[\'"]\s*?([^\'"]+\.js(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html_nocomments );
if ( ! $scripts ) {
return $html;
}
foreach ( $scripts as $script ) {
$url = $script[2];
$url = $this->can_replace( $url );
if ( ! $url ) {
continue;
}
$optimized_url = $this->replace_url( $url, 'js' );
if ( ! $optimized_url ) {
continue;
}
$replace_script = str_replace( $script[2], $optimized_url, $script[0] );
$html = str_replace( $script[0], $replace_script, $html );
}
return $html;
}
/**
* Gets the CDN zones.
*
* @since 3.1
* @author Remy Perona
*
* @return array
*/
public function get_zones() {
return [ 'all', 'css_and_js', 'css', 'js' ];
}
/**
* Determines if we can optimize
*
* @since 3.1
* @author Remy Perona
*
* @return boolean
*/
public function is_allowed() {
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
return false;
}
if ( ! $this->options->get( 'remove_query_strings' ) ) {
return false;
}
return true;
}
/**
* Determines if we can perform the remove query string on that URL
*
* @since 3.1
* @author Remy Perona
*
* @param string $url source URL.
* @return bool\string
*/
protected function can_replace( $url ) {
$parsed_url = get_rocket_parse_url( $url );
if ( empty( $parsed_url['query'] ) ) {
return false;
}
$version = get_bloginfo( 'version' );
if ( false !== strpos( $url, 'ver=' . $version ) ) {
$url = rtrim( str_replace( [ 'ver=' . $version, '?&', '&&' ], [ '', '?', '&' ], $url ), '?&' );
}
if ( $this->is_external_file( $url ) ) {
return false;
}
if ( $this->is_excluded( $url ) ) {
return false;
}
return $url;
}
/**
* Determines if the URL is excluded
*
* @since 3.1
* @author Remy Perona
*
* @param string $url source URL.
* @return bool
*/
protected function is_excluded( $url ) {
$excluded_files = $this->get_excluded_files();
if ( ! empty( $excluded_files ) && preg_match( '#^' . $excluded_files . '$#', rocket_clean_exclude_file( $url ) ) ) {
return true;
}
return false;
}
/**
* Replaces the original URL with the cache busting URL
*
* @since 3.1
* @author Remy Perona
*
* @param string $url source URL.
* @param string $extension file extension.
* @return bool|string
*/
protected function replace_url( $url, $extension ) {
$query = wp_parse_url( $url, PHP_URL_QUERY );
if ( empty( $query ) ) {
return $url;
}
// This filter is documented in /inc/classes/optimization/class-abstract-optimization.php.
$internal_url = apply_filters( 'rocket_asset_url', $url, $this->get_zones() );
$parsed_url = get_rocket_parse_url( $internal_url );
$relative_src = ltrim( $parsed_url['path'] . '?' . $parsed_url['query'], '/' );
$filename = preg_replace( '/\.(' . $extension . ')\?(?:timestamp|ver)=([^&]+)(?:.*)/', '-$2.$1', $relative_src );
if ( $relative_src === $filename ) {
return $url;
}
$busting_file = $this->busting_path . $filename;
$busting_url = $this->get_busting_url( $filename, $extension, $url );
if ( rocket_direct_filesystem()->is_readable( $busting_file ) ) {
return $busting_url;
}
$file = $this->get_file_path( $url );
if ( ! $file ) {
return false;
}
$busting_content = $this->get_file_content( $file );
if ( ! $busting_content ) {
return false;
}
if ( 'css' === $extension ) {
$busting_content = $this->rewrite_paths( $file, $busting_file, $busting_content );
}
if ( ! $this->write_file( $busting_content, $busting_file ) ) {
return false;
}
return $busting_url;
}
/**
* Gets the cache busting URL
*
* @since 3.1
* @author Remy Perona
*
* @param string $filename Cache busting filename.
* @param string $extension File extension.
* @param string $original_url Original URL for the file.
* @return string
*/
protected function get_busting_url( $filename, $extension, $original_url ) {
$url = $this->busting_url . $filename;
switch ( $extension ) {
case 'css':
// This filter is documented in inc/classes/optimization/css/class-abstract-css-optimization.php.
$url = apply_filters( 'rocket_css_url', $url, $original_url );
break;
case 'js':
// This filter is documented in inc/classes/optimization/css/class-abstract-js-optimization.php.
$url = apply_filters( 'rocket_js_url', $url, $original_url );
break;
}
return $url;
}
}