CacheDynamicResource.php
6.51 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
<?php
namespace WP_Rocket\Engine\Optimization;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Create a static file for CSS/JS generated by a PHP file
*
* @since 3.1
*/
class CacheDynamicResource extends AbstractOptimization implements Subscriber_Interface {
use CSSTrait;
/**
* Plugin options instance.
*
* @since 3.1
*
* @var Options_Data
*/
protected $options;
/**
* Cache busting base path
*
* @since 3.1
*
* @var string
*/
protected $busting_path;
/**
* Cache busting base URL
*
* @since 3.1
*
* @var string
*/
protected $busting_url;
/**
* Excluded files from optimization
*
* @since 3.1
*
* @var string
*/
protected $excluded_files;
/**
* Extension to use for the CDN zones selection
*
* @var string
*/
protected $extension = '';
/**
* Creates an instance of CacheDynamicResource.
*
* @since 3.1
*
* @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 ) {
$site_id = get_current_blog_id();
$this->options = $options;
$this->busting_path = "{$busting_path}{$site_id}/";
$this->busting_url = "{$busting_url}{$site_id}/";
/**
* Filters files to exclude from static dynamic resources
*
* @since 2.9.3
* @author Remy Perona
*
* @param array $excluded_files An array of filepath to exclude.
*/
$this->excluded_files = (array) apply_filters( 'rocket_exclude_static_dynamic_resources', [] );
$this->excluded_files[] = '/wp-admin/admin-ajax.php';
foreach ( $this->excluded_files as $i => $excluded_file ) {
// Escape character for future use in regex pattern.
$this->excluded_files[ $i ] = str_replace( '#', '\#', $excluded_file );
}
$this->excluded_files = implode( '|', $this->excluded_files );
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.1
*
* @return array
*/
public static function get_subscribed_events() {
return [
'style_loader_src' => [ 'cache_dynamic_resource', 16 ],
'script_loader_src' => [ 'cache_dynamic_resource', 16 ],
];
}
/**
* Filters the source dynamic php file to replace it with a static file
*
* @since 3.1
*
* @param string $src source URL.
*
* @return string
*/
public function cache_dynamic_resource( $src ) {
if ( ! $this->is_allowed() ) {
return $src;
}
switch ( current_filter() ) {
case 'script_loader_src':
$this->set_extension( 'js' );
break;
case 'style_loader_src':
$this->set_extension( 'css' );
break;
}
if ( $this->is_excluded_file( $src ) ) {
return $src;
}
return $this->replace_url( $src );
}
/**
* Replaces the dynamic URL by the static file URL
*
* @since 3.1
*
* @param string $src Source URL.
*
* @return string
*/
public function replace_url( $src ) {
$path = ltrim( rocket_extract_url_component( $src, PHP_URL_PATH ), '/' );
/**
* Filters the dynamic resource cache filename
*
* @since 2.9
*
* @param string $filename filename for the cache file
*/
$filename = apply_filters( 'rocket_dynamic_resource_cache_filename', preg_replace( '/\.php$/', '.' . $this->extension, $path ) );
$filename = ltrim( rocket_realpath( rtrim( str_replace( [ ' ', '%20' ], '-', $filename ) ) ), '/' );
$filepath = $this->busting_path . $filename;
if ( ! rocket_direct_filesystem()->is_readable( $filepath ) ) {
$content = $this->get_url_content( $src );
if ( ! $content ) {
return $src;
}
if ( 'css' === $this->extension ) {
$content = $this->rewrite_paths( $this->get_file_path( $src ), $filepath, $content );
$content = $this->apply_font_display_swap( $content );
}
if ( ! $this->write_file( $content, $filepath ) ) {
return $src;
}
}
return $this->get_cache_url( $filename );
}
/**
* Determines if we can optimize
*
* @since 3.1
*
* @return bool
*/
public function is_allowed() {
global $pagenow;
if ( rocket_bypass() ) {
return false;
}
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
return false;
}
if ( is_user_logged_in() && ! $this->options->get( 'cache_logged_user' ) ) {
return false;
}
if ( 'wp-login.php' === $pagenow ) {
return false;
}
return true;
}
/**
* Determines if the file is excluded from optimization
*
* @since 3.1
*
* @param string $src source URL.
*
* @return bool
*/
public function is_excluded_file( $src ) {
$file = get_rocket_parse_url( $src );
if ( isset( $file['path'] ) && ! preg_match( '#\.php$#', $file['path'] ) ) {
return true;
}
if ( $this->is_external_file( $src ) ) {
return true;
}
if ( preg_match( '#^' . $this->excluded_files . '$#', $file['path'] ) ) {
return true;
}
if ( ! isset( $file['query'] ) ) {
return false;
}
$file['query'] = remove_query_arg( 'ver', $file['query'] );
return (bool) $file['query'];
}
/**
* Sets the current file extension and minify key
*
* @since 3.1
*
* @param string $extension Current file extension.
*/
public function set_extension( $extension ) {
$this->extension = $extension;
$this->minify_key = $this->options->get( 'minify_' . $this->extension . '_key' );
}
/**
* Gets the CDN zones.
*
* @since 3.1
*
* @return array
*/
public function get_zones() {
return [ 'all', 'css_and_js', $this->extension ];
}
/**
* Gets the cache URL for the static file
*
* @since 3.1
*
* @param string $filename Filename for the static file.
*
* @return string
*/
protected function get_cache_url( $filename ) {
$cache_url = $this->busting_url . $filename;
switch ( $this->extension ) {
case 'css':
// This filter is documented in inc/classes/optimization/css/class-abstract-css-optimization.php.
$cache_url = apply_filters( 'rocket_css_url', $cache_url );
break;
case 'js':
// This filter is documented in inc/classes/optimization/css/class-abstract-js-optimization.php.
$cache_url = apply_filters( 'rocket_js_url', $cache_url );
break;
}
return $cache_url;
}
/**
* Gets content from an URL
*
* @since 3.1
*
* @param string $url URL to get the content from.
*
* @return string|bool
*/
protected function get_url_content( $url ) {
$content = wp_remote_retrieve_body( wp_remote_get( $url ) );
if ( ! $content ) {
return false;
}
return $content;
}
}