-
Notifications
You must be signed in to change notification settings - Fork 211
/
class-critical-css.php
360 lines (314 loc) · 7.77 KB
/
class-critical-css.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
<?php
namespace WP_Rocket\Optimization\CSS;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/**
* Handles the critical CSS generation process.
*
* @since 2.11
* @author Remy Perona
*/
class Critical_CSS {
/**
* Background Process instance
*
* @since 2.11
* @var object $process Background Process instance.
* @access public
*/
public $process;
/**
* Items for which we generate a critical CSS
*
* @since 2.11
* @var array $items An array of items.
* @access public
*/
public $items = [];
/**
* Path to the critical CSS directory
*
* @since 2.11
* @var string path to the critical css directory
* @access private
*/
private $critical_css_path;
/**
* Class constructor.
*
* @since 2.11
* @author Remy Perona
*
* @param Critical_CSS_Generation $process Background process instance.
*/
public function __construct( Critical_CSS_Generation $process ) {
$this->process = $process;
$this->items[] = [
'type' => 'front_page',
'url' => home_url( '/' ),
];
$this->critical_css_path = WP_ROCKET_CRITICAL_CSS_PATH . get_current_blog_id() . '/';
}
/**
* Returns the current site critical CSS path
*
* @since 3.3.5
* @author Remy Perona
*
* @return string
*/
public function get_critical_css_path() {
return $this->critical_css_path;
}
/**
* Performs the critical CSS generation
*
* @since 2.11
* @author Remy Perona
*/
public function process_handler() {
/**
* Filters the critical CSS generation process
*
* Use this filter to prevent the automatic critical CSS generation.
*
* @since 2.11.5
* @author Remy Perona
*
* @param bool $do_rocket_critical_css_generation True to activate the automatic generation, false to prevent it.
*/
if ( ! apply_filters( 'do_rocket_critical_css_generation', true ) ) { // WPCS: prefix ok.
return;
}
if ( get_transient( 'rocket_critical_css_generation_process_running' ) ) {
return;
}
$this->clean_critical_css();
$this->stop_generation();
$this->set_items();
array_map( [ $this->process, 'push_to_queue' ], $this->items );
$transient = [
'generated' => 0,
'total' => count( $this->items ),
'items' => [],
];
set_transient( 'rocket_critical_css_generation_process_running', $transient, HOUR_IN_SECONDS );
$this->process->save()->dispatch();
}
/**
* Stop the critical CSS generation process
*
* @since 3.3
* @author Remy Perona
*/
public function stop_generation() {
if ( method_exists( $this->process, 'cancel_process' ) ) {
$this->process->cancel_process();
}
}
/**
* Deletes critical CSS files
*
* @since 2.11
* @author Remy Perona
*/
public function clean_critical_css() {
try {
$directory = new \RecursiveDirectoryIterator( $this->critical_css_path, \FilesystemIterator::SKIP_DOTS );
} catch ( \UnexpectedValueException $e ) {
// no logging yet.
return;
}
try {
$files = new \RecursiveIteratorIterator( $directory, \RecursiveIteratorIterator::CHILD_FIRST );
} catch ( \Exception $e ) {
// no logging yet.
return;
}
if ( ! $files ) {
return;
}
foreach ( $files as $file ) {
rocket_direct_filesystem()->delete( $file );
}
}
/**
* Gets all public post types
*
* @since 2.11
* @author Remy Perona
*/
public function get_public_post_types() {
global $wpdb;
$post_types = get_post_types(
[
'public' => true,
'publicly_queryable' => true,
]
);
$post_types[] = 'page';
/**
* Filters the post types excluded from critical CSS generation
*
* @since 2.11
* @author Remy Perona
*
* @param array $excluded_post_types An array of post types names.
* @return array
*/
$excluded_post_types = apply_filters(
'rocket_cpcss_excluded_post_types',
[
'elementor_library',
'oceanwp_library',
'tbuilder_layout',
'tbuilder_layout_part',
'slider',
'karma-slider',
'tt-gallery',
'xlwcty_thankyou',
'fusion_template',
'blocks',
]
);
$post_types = array_diff( $post_types, $excluded_post_types );
$post_types = esc_sql( $post_types );
$post_types = "'" . implode( "','", $post_types ) . "'";
$rows = $wpdb->get_results( // WPCS: unprepared SQL ok.
"
SELECT MAX(ID) as ID, post_type
FROM (
SELECT ID, post_type
FROM $wpdb->posts
WHERE post_type IN ( $post_types )
AND post_status = 'publish'
ORDER BY post_date DESC
) AS posts
GROUP BY post_type"
);
return $rows;
}
/**
* Gets all public taxonomies
*
* @since 2.11
* @author Remy Perona
*/
public function get_public_taxonomies() {
global $wpdb;
$taxonomies = get_taxonomies(
[
'public' => true,
'publicly_queryable' => true,
]
);
/**
* Filters the taxonomies excluded from critical CSS generation
*
* @since 2.11
* @author Remy Perona
*
* @param array $excluded_taxonomies An array of taxonomies names.
* @return array
*/
$excluded_taxonomies = apply_filters(
'rocket_cpcss_excluded_taxonomies',
[
'post_format',
'product_shipping_class',
'karma-slider-category',
'truethemes-gallery-category',
'coupon_campaign',
'element_category',
]
);
$taxonomies = array_diff( $taxonomies, $excluded_taxonomies );
$taxonomies = esc_sql( $taxonomies );
$taxonomies = "'" . implode( "','", $taxonomies ) . "'";
$rows = $wpdb->get_results( // WPCS: unprepared SQL ok.
"
SELECT MAX( term_id ) AS ID, taxonomy
FROM (
SELECT term_id, taxonomy
FROM $wpdb->term_taxonomy
WHERE taxonomy IN ( $taxonomies )
AND count > 0
) AS taxonomies
GROUP BY taxonomy
"
);
return $rows;
}
/**
* Sets the items for which we generate critical CSS
*
* @since 2.11
* @author Remy Perona
*/
public function set_items() {
$page_for_posts = get_option( 'page_for_posts' );
if ( 'page' === get_option( 'show_on_front' ) && ! empty( $page_for_posts ) ) {
$this->items[] = [
'type' => 'home',
'url' => get_permalink( get_option( 'page_for_posts' ) ),
];
}
$post_types = $this->get_public_post_types();
foreach ( $post_types as $post_type ) {
$this->items[] = [
'type' => $post_type->post_type,
'url' => get_permalink( $post_type->ID ),
];
}
$taxonomies = $this->get_public_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
$this->items[] = [
'type' => $taxonomy->taxonomy,
'url' => get_term_link( (int) $taxonomy->ID, $taxonomy->taxonomy ),
];
}
/**
* Filters the array containing the items to send to the critical CSS generator
*
* @since 2.11.4
* @author Remy Perona
*
* @param Array $this->items Array containing the type/url pair for each item to send.
*/
$this->items = apply_filters( 'rocket_cpcss_items', $this->items );
}
/**
* Determines if critical CSS is available for the current page
*
* @since 2.11
* @author Remy Perona
*
* @return bool|string False if critical CSS file doesn't exist, file path otherwise
*/
public function get_current_page_critical_css() {
$name = 'front_page.css';
if ( is_home() && 'page' === get_option( 'show_on_front' ) ) {
$name = 'home.css';
} elseif ( is_front_page() ) {
$name = 'front_page.css';
} elseif ( is_category() ) {
$name = 'category.css';
} elseif ( is_tag() ) {
$name = 'post_tag.css';
} elseif ( is_tax() ) {
$taxonomy = get_queried_object()->taxonomy;
$name = $taxonomy . '.css';
} elseif ( is_singular() ) {
$post_type = get_post_type();
$name = $post_type . '.css';
}
$file = $this->critical_css_path . $name;
if ( ! rocket_direct_filesystem()->is_readable( $file ) ) {
$critical_css = get_rocket_option( 'critical_css', '' );
if ( ! empty( $critical_css ) ) {
return 'fallback';
}
return false;
}
return $file;
}
}