-
Notifications
You must be signed in to change notification settings - Fork 211
/
Subscriber.php
277 lines (234 loc) · 5.58 KB
/
Subscriber.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
<?php
namespace WP_Rocket\Engine\CDN;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Subscriber for the CDN feature
*
* @since 3.4
*/
class Subscriber implements Subscriber_Interface {
/**
* WP Rocket Options instance
*
* @var Options_Data
*/
private $options;
/**
* CDN instance
*
* @var CDN
*/
private $cdn;
/**
* Constructor
*
* @param Options_Data $options WP Rocket Options instance.
* @param CDN $cdn CDN instance.
*/
public function __construct( Options_Data $options, CDN $cdn ) {
$this->options = $options;
$this->cdn = $cdn;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.4
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_buffer' => [
[ 'rewrite', 20 ],
[ 'rewrite_srcset', 21 ],
],
'rocket_css_content' => 'rewrite_css_properties',
'rocket_cdn_hosts' => [ 'get_cdn_hosts', 10, 2 ],
'rocket_dns_prefetch' => 'add_dns_prefetch_cdn',
'rocket_facebook_sdk_url' => 'add_cdn_url',
'rocket_css_url' => [ 'add_cdn_url', 10, 2 ],
'rocket_js_url' => [ 'add_cdn_url', 10, 2 ],
'rocket_asset_url' => [ 'maybe_replace_url', 10, 2 ],
];
}
/**
* Rewrites URLs to the CDN URLs if allowed
*
* @since 3.4
*
* @param string $html HTML content.
* @return string
*/
public function rewrite( $html ) {
if ( ! $this->is_allowed() ) {
return $html;
}
return $this->cdn->rewrite( $html );
}
/**
* Rewrites URLs in srcset attributes to the CDN URLs if allowed
*
* @since 3.4.0.4
*
* @param string $html HTML content.
* @return string
*/
public function rewrite_srcset( $html ) {
if ( ! $this->is_allowed() ) {
return $html;
}
return $this->cdn->rewrite_srcset( $html );
}
/**
* Rewrites URLs to the CDN URLs in CSS files
*
* @since 3.4
*
* @param string $content CSS content.
* @return string
*/
public function rewrite_css_properties( $content ) {
/**
* Filters the application of the CDN on CSS properties
*
* @since 2.6
*
* @param bool true to apply CDN to properties, false otherwise
*/
$do_rewrite = apply_filters( 'do_rocket_cdn_css_properties', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
if ( ! $do_rewrite ) {
return $content;
}
if ( ! $this->is_cdn_enabled() ) {
return $content;
}
return $this->cdn->rewrite_css_properties( $content );
}
/**
* Gets the host value for each CDN URLs
*
* @since 3.4
*
* @param array $hosts Base hosts.
* @param array $zones Zones to get the CND URLs associated with.
*
* @return array
*/
public function get_cdn_hosts( array $hosts = [], array $zones = [ 'all' ] ) {
$cdn_urls = $this->cdn->get_cdn_urls( $zones );
if ( empty( $cdn_urls ) ) {
return $hosts;
}
foreach ( $cdn_urls as $cdn_url ) {
$parsed = get_rocket_parse_url( rocket_add_url_protocol( $cdn_url ) );
if ( empty( $parsed['host'] ) ) {
continue;
}
$hosts[] = untrailingslashit( $parsed['host'] . $parsed['path'] );
}
return array_unique( $hosts );
}
/**
* Adds CDN URLs to the DNS prefetch links
*
* @since 3.4
*
* @param array $domains Domain names to DNS prefetch.
* @return array
*/
public function add_dns_prefetch_cdn( $domains ) {
if ( ! $this->is_allowed() ) {
return $domains;
}
$cdn_urls = $this->cdn->get_cdn_urls( [ 'all', 'images', 'css_and_js', 'css', 'js' ] );
if ( ! $cdn_urls ) {
return $domains;
}
return array_merge( $domains, $cdn_urls );
}
/**
* Adds the CDN URL on the provided URL
*
* @since 3.4
*
* @param string $url URL to rewrite.
* @param string $original_url Original URL for this URL. Optional.
* @return string
*/
public function add_cdn_url( $url, $original_url = '' ) {
if ( ! empty( $original_url ) ) {
if ( $this->cdn->is_excluded( $original_url ) ) {
return $url;
}
}
return $this->cdn->rewrite_url( $url );
}
/**
* Replace CDN URL with site URL on the provided asset URL.
*
* @since 3.5.3
*
* @param string $url URL of the asset.
* @param array $zones Array of corresponding zones for the asset.
* @return string
*/
public function maybe_replace_url( $url, array $zones = [ 'all' ] ) {
if ( ! $this->is_allowed() ) {
return $url;
}
$url_parts = get_rocket_parse_url( $url );
if ( empty( $url_parts['host'] ) ) {
return $url;
}
$site_url_parts = get_rocket_parse_url( site_url() );
if ( empty( $site_url_parts['host'] ) ) {
return $url;
}
if ( $url_parts['host'] === $site_url_parts['host'] ) {
return $url;
}
$cdn_urls = $this->cdn->get_cdn_urls( $zones );
if ( empty( $cdn_urls ) ) {
return $url;
}
$cdn_urls = array_map( 'rocket_add_url_protocol', $cdn_urls );
$site_url = $site_url_parts['scheme'] . '://' . $site_url_parts['host'];
foreach ( $cdn_urls as $cdn_url ) {
if ( false === strpos( $url, $cdn_url ) ) {
continue;
}
return str_replace( $cdn_url, $site_url, $url );
}
return $url;
}
/**
* Checks if CDN can be applied
*
* @since 3.4
*
* @return boolean
*/
private function is_allowed() {
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
return false;
}
if ( ! $this->is_cdn_enabled() ) {
return false;
}
if ( is_rocket_post_excluded_option( 'cdn' ) ) {
return false;
}
return true;
}
/**
* Checks if the CDN option is enabled
*
* @since 3.5.5
*
* @return bool
*/
private function is_cdn_enabled() {
return (bool) $this->options->get( 'cdn', 0 );
}
}