-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathExtension_Amp_Plugin.php
390 lines (333 loc) · 11.1 KB
/
Extension_Amp_Plugin.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* File: Extension_Amp_Plugin.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Extension_Amp_Plugin
*
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
*/
class Extension_Amp_Plugin {
/**
* Is AMP endpoint
*
* @var bool|null
*/
private $is_amp_endpoint = null;
/**
* Adds actions for loading the AMP extension.
*
* @return void
*/
public static function wp_loaded() {
add_action( 'w3tc_extension_load', array( '\W3TC\Extension_Amp_Plugin', 'w3tc_extension_load' ) );
add_action( 'w3tc_extension_load_admin', array( '\W3TC\Extension_Amp_Plugin_Admin', 'w3tc_extension_load_admin' ) );
}
/**
* Loads the AMP extension by adding various filters.
*
* @return void
*/
public static function w3tc_extension_load() {
$o = new Extension_Amp_Plugin();
add_filter( 'w3tc_minify_js_enable', array( $o, 'w3tc_minify_jscss_enable' ) );
add_filter( 'w3tc_minify_css_enable', array( $o, 'w3tc_minify_jscss_enable' ) );
add_filter( 'w3tc_lazyload_can_process', array( $o, 'w3tc_lazyload_can_process' ) );
add_filter( 'w3tc_footer_comment', array( $o, 'w3tc_footer_comment' ) );
add_filter( 'w3tc_newrelic_should_disable_auto_rum', array( $o, 'w3tc_newrelic_should_disable_auto_rum' ) );
add_filter( 'pgcache_flush_post_queued_urls', array( $o, 'x_flush_post_queued_urls' ) );
add_filter( 'varnish_flush_post_queued_urls', array( $o, 'x_flush_post_queued_urls' ) );
add_filter( 'w3tc_pagecache_set', array( $o, 'w3tc_pagecache_set' ) );
add_filter( 'w3tc_config_default_values', array( $o, 'w3tc_config_default_values' ) );
// rules generation.
add_filter( 'w3tc_pagecache_rules_apache_accept_qs', array( $o, 'w3tc_pagecache_rules_x_accept_qs' ) );
add_filter( 'w3tc_pagecache_rules_apache_accept_qs_rules', array( $o, 'w3tc_pagecache_rules_apache_accept_qs_rules' ), 10, 2 );
add_filter( 'w3tc_pagecache_rules_apache_uri_prefix', array( $o, 'w3tc_pagecache_rules_apache_uri_prefix' ) );
add_filter( 'w3tc_pagecache_rules_nginx_accept_qs', array( $o, 'w3tc_pagecache_rules_x_accept_qs' ) );
add_filter( 'w3tc_pagecache_rules_nginx_accept_qs_rules', array( $o, 'w3tc_pagecache_rules_nginx_accept_qs_rules' ), 10, 2 );
add_filter( 'w3tc_pagecache_rules_nginx_uri_prefix', array( $o, 'w3tc_pagecache_rules_nginx_uri_prefix' ) );
}
/**
* Checks if the current request is an AMP endpoint.
*
* @return bool True if it is an AMP endpoint, false otherwise.
*/
private function is_amp_endpoint() {
// support for different plugins defining those own functions.
if ( is_null( $this->is_amp_endpoint ) ) {
if ( function_exists( 'is_amp_endpoint' ) ) {
$this->is_amp_endpoint = is_amp_endpoint();
} elseif ( function_exists( 'ampforwp_is_amp_endpoint' ) ) {
$this->is_amp_endpoint = ampforwp_is_amp_endpoint();
} elseif ( function_exists( 'is_better_amp' ) ) {
$this->is_amp_endpoint = is_better_amp();
}
}
return is_null( $this->is_amp_endpoint ) ? false : $this->is_amp_endpoint;
}
/**
* Enables or disables minification of JS and CSS for AMP endpoints.
*
* @param bool $enabled Whether minification is enabled.
*
* @return bool False if AMP endpoint, otherwise the original enabled value.
*/
public function w3tc_minify_jscss_enable( $enabled ) {
if ( $this->is_amp_endpoint() ) {
// amp has own rules for CSS and JS files, don't touch them by default.
return false;
}
return $enabled;
}
/**
* Determines if New Relic's auto RUM should be disabled for AMP endpoints.
*
* @param string $reject_reason The current rejection reason.
*
* @return string Modified rejection reason.
*/
public function w3tc_newrelic_should_disable_auto_rum( $reject_reason ) {
if ( $this->is_amp_endpoint() ) {
return 'AMP endpoint';
}
return $reject_reason;
}
/**
* Disables lazy loading on AMP endpoints.
*
* @param array $can_process Array indicating whether lazyload can process.
*
* @return array Modified can_process array.
*/
public function w3tc_lazyload_can_process( $can_process ) {
if ( $this->is_amp_endpoint() ) {
$can_process['enabled'] = false;
$can_process['reason'] = 'AMP endpoint';
}
return $can_process;
}
/**
* Modifies queued URLs to include AMP URLs.
*
* @param array $queued_urls List of queued URLs to be flushed.
*
* @return array Modified list of queued URLs.
*/
public function x_flush_post_queued_urls( $queued_urls ) {
$amp_urls = array();
$c = Dispatcher::config();
$url_postfix = $c->get_string( array( 'amp', 'url_postfix' ) );
if ( 'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) ) {
foreach ( $queued_urls as $url ) {
$amp_urls[] = $url . '?' . $url_postfix;
}
} else {
foreach ( $queued_urls as $url ) {
$amp_urls[] = trailingslashit( $url ) . $url_postfix;
}
}
$queued_urls = array_merge( $queued_urls, $amp_urls );
return $queued_urls;
}
/**
* Adds a comment to footer for AMP pages indicating limited minification.
*
* @param array $strings Array of strings for footer comments.
*
* @return array Modified array of footer comment strings.
*/
public function w3tc_footer_comment( $strings ) {
if ( $this->is_amp_endpoint() ) {
$strings[] = 'AMP page, minification is limited';
}
return $strings;
}
/**
* Modifies page cache set data for AMP pages.
*
* @param array $data The data to be cached.
*
* @return array Modified cache data.
*/
public function w3tc_pagecache_set( $data ) {
if ( $this->is_amp_endpoint() ) {
// workaround to prevent Link headers from parent page to appear in amp page coming from it's .htaccess.
$c = Dispatcher::config();
if ( $c->getf_boolean( 'minify.css.http2push' ) ||
$c->getf_boolean( 'minify.js.http2push' ) ) {
$data['headers'][] = array(
'n' => 'Link',
'v' => '',
'files_match' => '\\.html[_a-z]*$',
);
$this->w3tc_pagecache_set_header_register_once();
}
}
return $data;
}
/**
* Registers a filter for the page cache set header only once.
*
* @return void
*/
private function w3tc_pagecache_set_header_register_once() {
static $registered = false;
if ( ! $registered ) {
add_filter( 'w3tc_pagecache_set_header', array( $this, 'w3tc_pagecache_set_header' ), 20, 2 );
}
}
/**
* Modifies the page cache set header for AMP pages.
*
* @param array $header The current header.
* @param array $header_original The original header.
*
* @return array Modified header.
*/
public function w3tc_pagecache_set_header( $header, $header_original ) {
if ( 'Link' === $header_original['n'] && empty( $header_original['v'] ) ) {
// forces removal of Link header for file_generic when its set by parent .htaccess.
return $header_original;
}
return $header;
}
/**
* Sets the default values for AMP configuration.
*
* @param array $default_values The default configuration values.
*
* @return array Modified default values.
*/
public function w3tc_config_default_values( $default_values ) {
$default_values['amp'] = array(
'url_type' => 'tag',
'url_postfix' => 'amp',
);
return $default_values;
}
/**
* Normalizes URL fragments for AMP pages.
*
* @param array $url_fragments The URL fragments to be normalized.
*
* @return array Modified URL fragments.
*/
public static function pagecache_normalize_url_fragments( $url_fragments ) {
$c = Dispatcher::config();
if ( 'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) ) {
if ( ! empty( $url_fragments['querystring'] ) ) {
$qs = $c->get_string( array( 'amp', 'url_postfix' ) );
$url_qs = substr( $url_fragments['querystring'], 1 ); // cut off "?".
$regexp = Util_Environment::preg_quote( str_replace( '+', ' ', $qs ) );
if ( @preg_match( "~^(.*?&|)$regexp(=[^&]*)?(&.*|)$~i", $url_qs, $m ) ) {
$url_qs = $m[1] . $m[3];
$url_qs = preg_replace( '~[&]+~', '&', $url_qs );
$url_qs = trim( $url_qs, '&' );
$url_qs = empty( $url_qs ) ? '' : '?' . $url_qs;
$url_fragments['querystring'] = $url_qs;
$url_fragments['amp_extension'] = 1;
}
}
}
return $url_fragments;
}
/**
* Modifies the page cache key for AMP pages.
*
* @param array $o The original page cache data.
*
* @return array Modified page cache data.
*/
public static function pagecache_page_key( $o ) {
$c = Dispatcher::config();
if ( isset( $o['url_fragments']['amp_extension'] ) ) {
$o['key'][1] .= '_amp';
}
return $o;
}
/**
* Modifies query strings for AMP page cache rules.
*
* @param array $query_strings The query strings to be modified.
*
* @return array Modified query strings.
*/
public function w3tc_pagecache_rules_x_accept_qs( $query_strings ) {
$c = Dispatcher::config();
if ( 'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) ) {
$query_strings[] = $c->get_string( array( 'amp', 'url_postfix' ) );
}
return $query_strings;
}
/**
* Modifies Apache query rules for AMP URLs.
*
* @param array $query_rules The original query rules.
* @param string $query The query string to check.
*
* @return array Modified query rules.
*/
public function w3tc_pagecache_rules_apache_accept_qs_rules( $query_rules, $query ) {
$c = Dispatcher::config();
if (
'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) &&
$query === $c->get_string( array( 'amp', 'url_postfix' ) )
) {
$query_rules[1] = str_replace( '[E=', '[E=W3TC_AMP:_amp,E=', $query_rules[1] );
}
return $query_rules;
}
/**
* Modifies Apache URI prefix for AMP pages.
*
* @param string $uri_prefix The original URI prefix.
*
* @return string Modified URI prefix.
*/
public function w3tc_pagecache_rules_apache_uri_prefix( $uri_prefix ) {
$c = Dispatcher::config();
if ( 'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) ) {
$uri_prefix .= '%{ENV:W3TC_AMP}';
}
return $uri_prefix;
}
/**
* Modifies Nginx query rules for AMP URLs.
*
* @param array $query_rules The original query rules.
* @param string $query The query string to check.
*
* @return array Modified query rules.
*/
public function w3tc_pagecache_rules_nginx_accept_qs_rules( $query_rules, $query ) {
$c = Dispatcher::config();
if (
'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) &&
$query === $c->get_string( array( 'amp', 'url_postfix' ) )
) {
array_splice( $query_rules, 1, 0, ' set $w3tc_amp "_amp";' );
array_unshift( $query_rules, 'set $w3tc_amp "";' );
}
return $query_rules;
}
/**
* Modifies Nginx URI prefix for AMP pages.
*
* @param string $uri_prefix The original URI prefix.
*
* @return string Modified URI prefix.
*/
public function w3tc_pagecache_rules_nginx_uri_prefix( $uri_prefix ) {
$c = Dispatcher::config();
if ( 'querystring' === $c->get_string( array( 'amp', 'url_type' ) ) ) {
$uri_prefix .= '$w3tc_amp';
}
return $uri_prefix;
}
}
w3tc_add_action( 'pagecache_normalize_url_fragments', array( '\W3TC\Extension_Amp_Plugin', 'pagecache_normalize_url_fragments' ) );
w3tc_add_action( 'pagecache_page_key', array( '\W3TC\Extension_Amp_Plugin', 'pagecache_page_key' ) );
w3tc_add_action( 'wp_loaded', array( '\W3TC\Extension_Amp_Plugin', 'wp_loaded' ) );