forked from BoldGrid/w3-total-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtension_Amp_Plugin.php
304 lines (210 loc) · 7.81 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
<?php
namespace W3TC;
class Extension_Amp_Plugin {
private $is_amp_endpoint = null;
static public 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'
) );
}
static public 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' ) );
}
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;
}
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;
}
public function w3tc_newrelic_should_disable_auto_rum( $reject_reason ) {
if ( $this->is_amp_endpoint() ) {
return 'AMP endpoint';
}
return $reject_reason;
}
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;
}
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 ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' ) {
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;
}
public function w3tc_footer_comment( $strings ) {
if ( $this->is_amp_endpoint() ) {
$strings[] = 'AMP page, minification is limited';
}
return $strings;
}
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;
}
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 );
}
}
public function w3tc_pagecache_set_header( $header, $header_original ) {
if ( $header_original['n'] == 'Link' && empty( $header_original['v'] ) ) {
// forces removal of Link header for file_generic when its set by
// parent .htaccess
return $header_original;
}
return $header;
}
public function w3tc_config_default_values( $default_values ) {
$default_values['amp'] = array(
'url_type' => 'tag',
'url_postfix' => 'amp'
);
return $default_values;
}
static public function pagecache_normalize_url_fragments( $url_fragments ) {
$c = Dispatcher::config();
if ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' ) {
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;
}
static public function pagecache_page_key( $o ) {
$c = Dispatcher::config();
if ( isset( $o['url_fragments']['amp_extension'] ) ) {
$o['key'][1] .= '_amp';
}
return $o;
}
public function w3tc_pagecache_rules_x_accept_qs( $query_strings ) {
$c = Dispatcher::config();
if ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' ) {
$query_strings[] = $c->get_string( array( 'amp', 'url_postfix' ) );
}
return $query_strings;
}
public function w3tc_pagecache_rules_apache_accept_qs_rules( $query_rules, $query ) {
$c = Dispatcher::config();
if ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' &&
$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;
}
public function w3tc_pagecache_rules_apache_uri_prefix( $uri_prefix ) {
$c = Dispatcher::config();
if ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' ) {
$uri_prefix .= '%{ENV:W3TC_AMP}';
}
return $uri_prefix;
}
public function w3tc_pagecache_rules_nginx_accept_qs_rules( $query_rules, $query ) {
$c = Dispatcher::config();
if ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' &&
$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;
}
public function w3tc_pagecache_rules_nginx_uri_prefix( $uri_prefix ) {
$c = Dispatcher::config();
if ( $c->get_string( array( 'amp', 'url_type' ) ) == 'querystring' ) {
$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' ) );