/
Extractor.php
278 lines (221 loc) · 6.79 KB
/
Extractor.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
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
use WP_Rocket\Engine\Optimization\RegexTrait;
class Extractor {
use RegexTrait;
/**
* Comment mapping.
*
* @var array
*/
protected $comments_mapping = [];
/**
* Extract background images from CSS.
*
* @param string $content CSS content.
* @param string $file_url File we are extracting css from.
* @return array
*/
public function extract( string $content, string $file_url = '' ): array {
$this->comments_mapping = [];
$comment_regex = '#/\*[^*]*\*+([^/][^*]*\*+)*/#';
$content = preg_replace_callback(
$comment_regex,
function ( $matches ) {
$id = '/*' . uniqid( 'bg_css_comment' ) . '*/';
$this->comments_mapping[ $id ] = $matches[0];
return $id;
},
$content
);
$background_regex = '(?<selector>(?:[ \-,:\w.()\n\r^>[*"\'=\]#]|(?:\[[^\]]+\]))+)\s?{[^{}]*background\s*:(?<property>[^;}]*)[^}]*}';
$background_image_regex = '(?<selector>(?:[ \-,:\w.()\n\r^>[*"\'=\]#]|(?:\[[^\]]+\]))+)\s?{[^{}]*background-image\s*:(?<property>[^;}]*)[^}]*}';
/**
* Lazyload background property regex.
*
* @param string $regex Lazyload background property regex.
*/
$background_regex = $this->apply_string_filter( 'lazyload_css_extract_bg_property_regex', $background_regex );
/**
* Lazyload background property regex.
*
* @param string $regex Lazyload background property regex.
*/
$background_image_regex = $this->apply_string_filter( 'lazyload_css_extract_bg_img_property_regex', $background_image_regex );
$background_matches = $this->find( $background_regex, $content, 'mi' );
$background_image_matches = $this->find( $background_image_regex, $content, 'mi' );
$matches = array_merge( $background_matches, $background_image_matches );
if ( empty( $matches ) ) {
return [];
}
$results = [];
foreach ( $matches as $match ) {
if ( ! key_exists( 'property', $match ) || ! key_exists( 'selector', $match ) ) {
continue;
}
$property = $match['property'];
$selector = $match['selector'];
$property = trim( $property );
$selector = trim( $selector );
$urls = $this->extract_urls( $property, $file_url );
$block = trim( $match[0] );
foreach ( $this->comments_mapping as $id => $comment ) {
$block = str_replace( $id, $comment, $block );
}
foreach ( $urls as $url ) {
$results[ $selector ][] = [
'selector' => $selector,
'url' => $url['url'],
'original' => $url['original'],
'block' => $block,
];
}
}
return $results;
}
/**
* Extract URLS from a CSS property.
*
* @param string $content Content from the CSS property.
* @param string $file_url URL of the css file.
* @return array
*/
protected function extract_urls( string $content, string $file_url = '' ): array {
/**
* Lazyload URL regex.
*
* @param string $regex Lazyload URL regex.
*/
$regex = $this->apply_string_filter( 'lazyload_css_extract_url_regex', '(?<tag>url\([\'"]?(?<url>[^\)]*)[\'"]?\))' );
$matches = $this->find( $regex, $content, 'mi' );
$output = [];
/**
* Lazyload ignored URLs.
*
* @param string[] $urls Ignored URLs.
*/
$ignored_urls = (array) apply_filters( 'rocket_lazyload_css_ignored_urls', [] );
foreach ( $matches as $match ) {
$url = $match['url'] ?: '';
$url = str_replace( '"', '', $url );
$url = str_replace( "'", '', $url );
$url = trim( $url );
$url = $this->make_url_complete( $url, $file_url );
if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $url || $this->is_url_ignored( $url, $ignored_urls ) ) {
continue;
}
if ( ! $this->is_url_external( $url ) && ! $this->is_relative( $url ) ) {
$url = $this->apply_string_filter( 'css_url', $url );
}
$original_url = trim( $match['tag'], ' ,' );
$output[] = [
'url' => $url,
'original' => $original_url,
];
}
return $output;
}
/**
* Apply a string filter.
*
* @param string $name Name from the filter.
* @param string $value Value to pass to the filter that will be returned.
* @param array ...$args Additional values.
* @return string
*/
protected function apply_string_filter( string $name, string $value, ...$args ) {
$output = apply_filters( 'rocket_' . $name, $value, ...$args );
if ( ! is_string( $output ) ) {
return $value;
}
return $output;
}
/**
* Check if the URLs is ignored.
*
* @param string $url URL to check.
* @param array $ignored_urls List of ignored URLs.
*
* @return bool
*/
protected function is_url_ignored( string $url, array $ignored_urls ): bool {
foreach ( $ignored_urls as $ignored_url ) {
if ( strpos( $url, $ignored_url ) !== false ) {
return true;
}
}
return false;
}
/**
* Complete the URL if necessary.
*
* @param string $url URL to complete.
* @param string $file_url URL of the CSS File.
*
* @return string
*/
protected function make_url_complete( string $url, string $file_url ): string {
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( $host || $this->is_relative( $url ) && ! empty( $file_url ) ) {
return $this->transform_relative_to_absolute( $url, $file_url );
}
return rocket_get_home_url() . '/' . trim( $url, '/ ' );
}
/**
* Transform a relative URL to an absolute URL based on the base URL.
*
* @param string $rel Relative URL to transform.
* @param string $base Base URL.
*
* @return string
*/
protected function transform_relative_to_absolute( string $rel, string $base = '' ): string {
if ( ! is_null( wp_parse_url( $rel, PHP_URL_SCHEME ) ) ) {
return $rel;
}
if ( '#' === $rel[0] || '?' === $rel[0] ) {
return $base . $rel;
}
$base_url_parts = wp_parse_url( $base );
$path = ! empty( $base_url_parts['path'] ) ? $base_url_parts['path'] : '/';
$path = preg_replace( '#/[^/]*$#', '', $path );
if ( '/' === $rel[0] ) {
$path = '';
}
$abs = "$path/$rel";
$re = [ '#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#' ];
$dots_count = substr_count( $rel, '..' );
foreach ( range( 1, $dots_count ) as $n ) {
$abs_before = $abs;
$abs = preg_replace( $re, '/', $abs, -1, $n );
if ( $abs_before === $abs ) {
break;
}
}
return trailingslashit( rocket_get_home_url() ) . ltrim( $abs, '/' );
}
/**
* Check if the URL is external.
*
* @param string $url URL to check.
*
* @return bool
*/
protected function is_url_external( string $url ): bool {
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! $host ) {
return false;
}
$home_host = wp_parse_url( rocket_get_home_url(), PHP_URL_HOST );
return $host !== $home_host;
}
/**
* Check if the URL is relative.
*
* @param string $url URL to check.
* @return bool
*/
protected function is_relative( string $url ): bool {
return preg_match( '/^\./', $url );
}
}