Combine.php
7.58 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
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
<?php
namespace WP_Rocket\Engine\Optimization\Minify\CSS;
use WP_Rocket\Dependencies\Minify\CSS as MinifyCSS;
use WP_Rocket\Engine\Optimization\CSSTrait;
use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface;
use WP_Rocket\Logger\Logger;
/**
* Minify & Combine CSS files
*
* @since 3.1
*/
class Combine extends AbstractCSSOptimization implements ProcessorInterface {
use CSSTrait;
/**
* Array of styles
*
* @var array
*/
private $styles = [];
/**
* Combined CSS filename
*
* @var string
*/
private $filename;
/**
* Minifies and combines all CSS files into one
*
* @since 3.1
*
* @param string $html HTML content.
* @return string
*/
public function optimize( $html ) {
Logger::info( 'CSS COMBINE PROCESS STARTED.', [ 'css combine process' ] );
$html_nocomments = $this->hide_comments( $html );
$styles = $this->find( '<link\s+([^>]+[\s"\'])?href\s*=\s*[\'"]\s*?(?<url>[^\'"]+\.css(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html_nocomments );
if ( ! $styles ) {
Logger::debug( 'No `<link>` tags found.', [ 'css combine process' ] );
return $html;
}
Logger::debug(
'Found ' . count( $styles ) . ' `<link>` tag(s).',
[
'css combine process',
'tags' => $styles,
]
);
$styles = $this->parse( $styles );
if ( empty( $styles ) ) {
Logger::debug( 'No `<link>` tags to optimize.', [ 'css combine process' ] );
return $html;
}
Logger::debug(
count( $styles ) . ' `<link>` tag(s) remaining.',
[
'css combine process',
'tags' => $styles,
]
);
if ( ! $this->combine() ) {
Logger::error( 'CSS combine process failed.', [ 'css combine process' ] );
return $html;
}
return $this->insert_combined_css( $html );
}
/**
* Parses all found styles tag to keep only the ones to combine
*
* @since 3.7
*
* @param array $styles Array of matched styles.
* @return array
*/
private function parse( array $styles ) {
foreach ( $styles as $key => $style ) {
if ( $this->is_combine_excluded_media( $style[0] ) ) {
Logger::debug(
'Style is excluded due to media attribute.',
[
'css combine process',
'tag' => $style[0],
]
);
continue;
}
if ( $this->is_external_file( $style['url'] ) ) {
if ( $this->is_excluded_external( $style['url'] ) ) {
unset( $styles[ $key ] );
continue;
}
$this->styles[ $style['url'] ] = [
'type' => 'external',
'tag' => $style[0],
'url' => rocket_add_url_protocol( strtok( $style['url'], '?' ) ),
];
continue;
}
if ( $this->is_minify_excluded_file( $style ) ) {
Logger::debug(
'Style is excluded.',
[
'css combine process',
'tag' => $style[0],
]
);
unset( $styles[ $key ] );
continue;
}
$this->styles[ $style['url'] ] = [
'type' => 'internal',
'tag' => $style[0],
'url' => strtok( $style['url'], '?' ),
];
}
return $styles;
}
/**
* Checks if the provided external URL is excluded from combine
*
* @since 3.7
*
* @param array $url External URL to check.
* @return boolean
*/
private function is_excluded_external( $url ) {
foreach ( $this->get_excluded_externals() as $excluded ) {
if ( false !== strpos( $url, $excluded ) ) {
Logger::debug(
'Style is external.',
[
'css combine process',
'url' => $url,
]
);
return true;
}
}
return false;
}
/**
* Gets external URLs excluded from combine
*
* @since 3.7
*
* @return array
*/
private function get_excluded_externals() {
/**
* Filters CSS external URLs to exclude from the combine process
*
* @since 3.7
*
* @param array $pattern Patterns to match.
*/
$excluded_externals = (array) apply_filters( 'rocket_combine_css_excluded_external', [] );
return array_merge( $excluded_externals, $this->options->get( 'exclude_css', [] ) );
}
/**
* Combine the CSS content into one file and save it
*
* @since 3.1
*
* @return bool True if successful, false otherwise
*/
protected function combine() {
if ( empty( $this->styles ) ) {
return false;
}
$file_hash = implode( ',', array_column( $this->styles, 'url' ) );
$this->filename = md5( $file_hash . $this->minify_key ) . '.css';
$combined_file = $this->minify_base_path . $this->filename;
if ( rocket_direct_filesystem()->exists( $combined_file ) ) {
Logger::debug(
'Combined CSS file already exists.',
[
'css combine process',
'path' => $combined_file,
]
);
return true;
}
$combined_content = $this->get_content( $combined_file );
$combined_content = $this->apply_font_display_swap( $combined_content );
if ( empty( $combined_content ) ) {
Logger::error(
'No combined content.',
[
'css combine process',
'path' => $combined_file,
]
);
return false;
}
if ( ! $this->write_file( $combined_content, $combined_file ) ) {
Logger::error(
'Combined CSS file could not be created.',
[
'css combine process',
'path' => $combined_file,
]
);
return false;
}
Logger::debug(
'Combined CSS file successfully created.',
[
'css combine process',
'path' => $combined_file,
]
);
return true;
}
/**
* Insert the combined CSS file and remove the original CSS tags
*
* The combined CSS file is added after the closing </title> tag, and the replacement occurs only once. The original CSS tags are then removed from the HTML.
*
* @since 3.3.3
*
* @param string $html HTML content.
* @return string
*/
protected function insert_combined_css( $html ) {
foreach ( $this->styles as $style ) {
$html = str_replace( $style['tag'], '', $html );
}
$minify_url = $this->get_minify_url( $this->filename );
Logger::info(
'Combined CSS file successfully added.',
[
'css combine process',
'url' => $minify_url,
]
);
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
return preg_replace( '/<\/title>/i', '$0<link rel="stylesheet" href="' . esc_url( $minify_url ) . '" media="all" data-minify="1" />', $html, 1 );
}
/**
* Gathers the content from all styles to combine & minify it if needed
*
* @since 3.7
*
* @param string $combined_file Absolute path to the combined file.
* @return string
*/
private function get_content( $combined_file ) {
$minifier = new MinifyCSS();
foreach ( $this->styles as $key => $style ) {
if ( 'internal' === $style['type'] ) {
$filepath = $this->get_file_path( $style['url'] );
if ( ! $filepath ) {
unset( $this->styles[ $key ] );
continue;
}
$file_content = $this->get_file_content( $filepath );
$file_content = $this->rewrite_paths( $filepath, $combined_file, $file_content );
} elseif ( 'external' === $style['type'] ) {
$file_content = $this->local_cache->get_content( $style['url'] );
$file_content = $this->rewrite_paths( $style['url'], $combined_file, $file_content );
}
if ( empty( $file_content ) ) {
unset( $this->styles[ $key ] );
continue;
}
$minifier->add( $file_content );
}
$content = $minifier->minify();
if ( empty( $content ) ) {
Logger::debug( 'No CSS content.', [ 'css combine process' ] );
}
return $content;
}
/**
* Check if media query is valid to be excluded from combine or not.
*
* @since 3.8
*
* @param string $tag Stylesheet HTML tag.
* @return bool Ture if it's excluded else false.
*/
private function is_combine_excluded_media( $tag ) {
return (
false !== strpos( $tag, 'media=' )
&&
! preg_match( '/media=["\'](?:\s*|[^"\']*?\b(?:\s*?,\s*?)?(all|screen)(?:\s*?,\s*?[^"\']*)?)["\']/i', $tag )
);
}
}