PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.6.3
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.6.3
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / MetaBox.php

MetaBox.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 3.6.3, at includes/classes/MetaBox.php

497 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Metabox related functionalities
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache;
9
10 use const PoweredCache\Constants\POST_META_DISABLE_CSS_OPTIMIZATION;
11 use const PoweredCache\Constants\POST_META_DISABLE_JS_DEFER;
12 use const PoweredCache\Constants\POST_META_DISABLE_JS_DELAY;
13 use const PoweredCache\Constants\POST_META_DISABLE_JS_OPTIMIZATION;
14 use const PoweredCache\Constants\POST_META_DISABLE_UCSS_KEY;
15 use const PoweredCache\Constants\POST_META_SPECIFIC_CRITICAL_CSS_KEY;
16 use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY;
17 use const PoweredCache\Constants\POST_META_DISABLE_CRITICAL_CSS_KEY;
18 use const PoweredCache\Constants\POST_META_DISABLE_LAZYLOAD_KEY;
19 use const PoweredCache\Constants\POST_META_SPECIFIC_UCSS_KEY;
20
21 /**
22 * Class MetaBox
23 */
24 class MetaBox {
25
26 /**
27 * Placeholder constructor
28 */
29 public function __construct() {
30 }
31
32 /**
33 * Return an instance of the current class
34 *
35 * @return MetaBox
36 * @since 2.0
37 */
38 public static function factory() {
39 static $instance = false;
40
41 if ( ! $instance ) {
42 $instance = new self();
43 $instance->setup();
44 }
45
46 return $instance;
47 }
48
49 /**
50 * Setup hooks
51 */
52 public function setup() {
53 add_action( 'init', [ $this, 'register_meta_field' ] );
54 add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
55 add_action( 'save_post', [ $this, 'save_post_meta' ], 10, 2 );
56
57 foreach ( self::get_available_post_types() as $post_type ) {
58 add_action( 'rest_after_insert_' . $post_type, [ $this, 'maybe_remove_default_meta' ], 10, 2 );
59 }
60 }
61
62 /**
63 * Get available post types for meta register
64 *
65 * @return array
66 * @since 2.1
67 */
68 private static function get_available_post_types() {
69 $public_posts_types = get_post_types(
70 [
71 'public' => true,
72 'publicly_queryable' => true,
73 ],
74 'names',
75 'or'
76 );
77
78 unset( $public_posts_types['attachment'] );
79
80 return (array) $public_posts_types;
81 }
82
83 /**
84 * Add metabox
85 */
86 public function add_meta_boxes() {
87
88 if ( ! current_user_can( 'edit_others_posts' ) ) {
89 return;
90 }
91
92 $is_block_editor_compatible = false;
93 $back_compat = true;
94
95 if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) {
96 $is_block_editor_compatible = true;
97 $back_compat = false;
98 }
99
100 $settings = \PoweredCache\Utils\get_settings();
101
102 $required_meta_settings = [
103 'enable_page_cache',
104 'enable_lazy_load',
105 'minify_css',
106 'combine_css',
107 'minify_js',
108 'combine_js',
109 'critical_css',
110 'remove_unused_css',
111 ];
112
113 $show_meta_box = false;
114 foreach ( $required_meta_settings as $setting_item ) {
115 if ( $settings[ $setting_item ] ) {
116 $show_meta_box = true;
117 break;
118 }
119 }
120
121 if ( ! $show_meta_box ) {
122 return;
123 }
124
125 add_meta_box(
126 'powered_cache_post_meta',
127 esc_html__( 'Powered Cache', 'powered-cache' ),
128 [ $this, 'meta_box' ],
129 '',
130 'side',
131 'high',
132 [
133 '__block_editor_compatible_meta_box' => $is_block_editor_compatible,
134 '__back_compat_meta_box' => $back_compat,
135 ]
136 );
137 }
138
139 /**
140 * Display metabox output for classic editor
141 *
142 * @param Object $post \WP_Post
143 */
144 public function meta_box( $post ) {
145 $settings = \PoweredCache\Utils\get_settings();
146 $is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true );
147
148 ?>
149 <div id="powered-cache-meta-box">
150 <?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?>
151 <?php if ( $settings['enable_page_cache'] ) : ?>
152 <fieldset>
153 <legend class="screen-reader-text"><?php esc_html_e( 'Powered Cache Status', 'powered-cache' ); ?></legend>
154 <input <?php checked( $is_cache_disabled, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>" value="1">
155 <label for="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>"><?php esc_html_e( 'Don\'t cache this post', 'powered-cache' ); ?></label>
156 </fieldset>
157 <?php endif; ?>
158 <?php if ( $settings['enable_lazy_load'] ) : ?>
159 <?php $is_lazyload_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_LAZYLOAD_KEY, true ); ?>
160 <fieldset>
161 <legend class="screen-reader-text"><?php esc_html_e( 'Disable Lazy Load', 'powered-cache' ); ?></legend>
162 <input <?php checked( $is_lazyload_disabled, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>" value="1">
163 <label for="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>"><?php esc_html_e( 'Disable lazy loading for this post', 'powered-cache' ); ?></label>
164 </fieldset>
165 <?php endif; ?>
166 <?php if ( $settings['minify_css'] || $settings['combine_css'] ) : ?>
167 <?php $is_css_optimization_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CSS_OPTIMIZATION, true ); ?>
168 <fieldset>
169 <legend class="screen-reader-text"><?php esc_html_e( 'Disable CSS optimization (minify/concat) for this post', 'powered-cache' ); ?></legend>
170 <input <?php checked( $is_css_optimization_disabled, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_CSS_OPTIMIZATION ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_CSS_OPTIMIZATION ); ?>" value="1">
171 <label for="<?php echo esc_attr( POST_META_DISABLE_CSS_OPTIMIZATION ); ?>"><?php esc_html_e( 'Disable CSS optimization (minify/concat) for this post', 'powered-cache' ); ?></label>
172 </fieldset>
173 <?php endif; ?>
174 <?php if ( $settings['minify_js'] || $settings['combine_js'] ) : ?>
175 <?php $is_js_optimization_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_JS_OPTIMIZATION, true ); ?>
176 <fieldset>
177 <legend class="screen-reader-text"><?php esc_html_e( 'Disable JS optimization (minify/concat) for this post', 'powered-cache' ); ?></legend>
178 <input <?php checked( $is_js_optimization_disabled, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_JS_OPTIMIZATION ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_JS_OPTIMIZATION ); ?>" value="1">
179 <label for="<?php echo esc_attr( POST_META_DISABLE_JS_OPTIMIZATION ); ?>"><?php esc_html_e( 'Disable JS optimization (minify/concat) for this post', 'powered-cache' ); ?></label>
180 </fieldset>
181 <?php endif; ?>
182 <?php if ( $settings['js_defer'] ) : ?>
183 <?php $is_js_defer_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_JS_DEFER, true ); ?>
184 <fieldset>
185 <legend class="screen-reader-text"><?php esc_html_e( 'Disable JS defer for this post', 'powered-cache' ); ?></legend>
186 <input <?php checked( $is_js_defer_disabled, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_JS_DEFER ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_JS_DEFER ); ?>" value="1">
187 <label for="<?php echo esc_attr( POST_META_DISABLE_JS_DEFER ); ?>"><?php esc_html_e( 'Disable JS Defer', 'powered-cache' ); ?></label>
188 </fieldset>
189 <?php endif; ?>
190 <?php if ( $settings['js_delay'] ) : ?>
191 <?php $is_js_delay_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_JS_DELAY, true ); ?>
192 <fieldset>
193 <legend class="screen-reader-text"><?php esc_html_e( 'Disable JS delay for this post', 'powered-cache' ); ?></legend>
194 <input <?php checked( $is_js_delay_disabled, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_JS_DELAY ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_JS_DELAY ); ?>" value="1">
195 <label for="<?php echo esc_attr( POST_META_DISABLE_JS_DELAY ); ?>"><?php esc_html_e( 'Disable JS Delay', 'powered-cache' ); ?></label>
196 </fieldset>
197 <?php endif; ?>
198
199
200 <?php if ( $settings['critical_css'] ) : ?>
201 <?php $disable_critical = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CRITICAL_CSS_KEY, true ); ?>
202 <?php $generate_post_specific_critical = (bool) get_post_meta( $post->ID, POST_META_SPECIFIC_CRITICAL_CSS_KEY, true ); ?>
203 <fieldset>
204 <legend class="screen-reader-text"><?php esc_html_e( 'Disable Critical CSS on this post', 'powered-cache' ); ?></legend>
205 <input <?php disabled( $generate_post_specific_critical, true ); ?> <?php checked( $disable_critical, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_CRITICAL_CSS_KEY ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_CRITICAL_CSS_KEY ); ?>" value="1">
206 <label for="<?php echo esc_attr( POST_META_DISABLE_CRITICAL_CSS_KEY ); ?>"><?php esc_html_e( 'Disable Critical CSS on this post', 'powered-cache' ); ?></label>
207 </fieldset>
208
209 <fieldset>
210 <legend class="screen-reader-text"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></legend>
211 <input <?php disabled( $disable_critical, true ); ?> <?php checked( $generate_post_specific_critical, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>" name="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>" value="1">
212 <label for="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></label>
213 </fieldset>
214 <?php endif; ?>
215
216 <?php if ( $settings['remove_unused_css'] ) : ?>
217 <?php $disable_ucss = (bool) get_post_meta( $post->ID, POST_META_DISABLE_UCSS_KEY, true ); ?>
218 <?php $generate_post_specific_ucss = (bool) get_post_meta( $post->ID, POST_META_SPECIFIC_UCSS_KEY, true ); ?>
219 <fieldset>
220 <legend class="screen-reader-text"><?php esc_html_e( 'Disable UCSS on this post', 'powered-cache' ); ?></legend>
221 <input <?php disabled( $generate_post_specific_ucss, true ); ?> <?php checked( $disable_ucss, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_DISABLE_UCSS_KEY ); ?>" name="<?php echo esc_attr( POST_META_DISABLE_UCSS_KEY ); ?>" value="1">
222 <label for="<?php echo esc_attr( POST_META_DISABLE_UCSS_KEY ); ?>"><?php esc_html_e( 'Disable UCSS on this post', 'powered-cache' ); ?></label>
223 </fieldset>
224
225 <fieldset>
226 <legend class="screen-reader-text"><?php esc_html_e( 'Generate specific UCSS', 'powered-cache' ); ?></legend>
227 <input <?php disabled( $disable_ucss, true ); ?> <?php checked( $generate_post_specific_ucss, true ); ?> type="checkbox" id="<?php echo esc_attr( POST_META_SPECIFIC_UCSS_KEY ); ?>" name="<?php echo esc_attr( POST_META_SPECIFIC_UCSS_KEY ); ?>" value="1">
228 <label for="<?php echo esc_attr( POST_META_SPECIFIC_UCSS_KEY ); ?>"><?php esc_html_e( 'Generate specific UCSS', 'powered-cache' ); ?></label>
229 </fieldset>
230 <?php endif; ?>
231
232 </div>
233 <?php
234 }
235
236
237 /**
238 * Register meta fields for block editor
239 */
240 public function register_meta_field() {
241 $settings = \PoweredCache\Utils\get_settings();
242
243 foreach ( self::get_available_post_types() as $post_type ) {
244 if ( $settings['enable_page_cache'] ) {
245 register_post_meta(
246 $post_type,
247 POST_META_DISABLE_CACHE_KEY,
248 [
249 'show_in_rest' => true,
250 'single' => true,
251 'type' => 'boolean',
252 'auth_callback' => function () {
253 return current_user_can( 'edit_others_posts' );
254 },
255 ]
256 );
257 }
258
259 if ( $settings['enable_lazy_load'] ) {
260 register_post_meta(
261 $post_type,
262 POST_META_DISABLE_LAZYLOAD_KEY,
263 [
264 'show_in_rest' => true,
265 'single' => true,
266 'type' => 'boolean',
267 'auth_callback' => function () {
268 return current_user_can( 'edit_others_posts' );
269 },
270 ]
271 );
272 }
273
274 if ( $settings['critical_css'] ) {
275 register_post_meta(
276 $post_type,
277 POST_META_DISABLE_CRITICAL_CSS_KEY,
278 [
279 'show_in_rest' => true,
280 'single' => true,
281 'default' => false,
282 'type' => 'boolean',
283 'auth_callback' => function () {
284 return current_user_can( 'edit_others_posts' );
285 },
286 ]
287 );
288
289 register_post_meta(
290 $post_type,
291 POST_META_SPECIFIC_CRITICAL_CSS_KEY,
292 [
293 'show_in_rest' => true,
294 'single' => true,
295 'default' => false,
296 'type' => 'boolean',
297 'auth_callback' => function () {
298 return current_user_can( 'edit_others_posts' );
299 },
300 ]
301 );
302 }
303
304 if ( $settings['remove_unused_css'] ) {
305 register_post_meta(
306 $post_type,
307 POST_META_DISABLE_UCSS_KEY,
308 [
309 'show_in_rest' => true,
310 'single' => true,
311 'default' => false,
312 'type' => 'boolean',
313 'auth_callback' => function () {
314 return current_user_can( 'edit_others_posts' );
315 },
316 ]
317 );
318
319 register_post_meta(
320 $post_type,
321 POST_META_SPECIFIC_UCSS_KEY,
322 [
323 'show_in_rest' => true,
324 'single' => true,
325 'default' => false,
326 'type' => 'boolean',
327 'auth_callback' => function () {
328 return current_user_can( 'edit_others_posts' );
329 },
330 ]
331 );
332 }
333
334 if ( $settings['minify_css'] || $settings['combine_css'] ) {
335 register_post_meta(
336 $post_type,
337 POST_META_DISABLE_CSS_OPTIMIZATION,
338 [
339 'show_in_rest' => true,
340 'single' => true,
341 'default' => false,
342 'type' => 'boolean',
343 'auth_callback' => function () {
344 return current_user_can( 'edit_others_posts' );
345 },
346 ]
347 );
348 }
349
350 if ( $settings['minify_js'] || $settings['combine_js'] ) {
351 register_post_meta(
352 $post_type,
353 POST_META_DISABLE_JS_OPTIMIZATION,
354 [
355 'show_in_rest' => true,
356 'single' => true,
357 'default' => false,
358 'type' => 'boolean',
359 'auth_callback' => function () {
360 return current_user_can( 'edit_others_posts' );
361 },
362 ]
363 );
364 }
365
366 if ( $settings['js_defer'] ) {
367 register_post_meta(
368 $post_type,
369 POST_META_DISABLE_JS_DEFER,
370 [
371 'show_in_rest' => true,
372 'single' => true,
373 'default' => false,
374 'type' => 'boolean',
375 'auth_callback' => function () {
376 return current_user_can( 'edit_others_posts' );
377 },
378 ]
379 );
380 }
381
382 if ( $settings['js_defer'] ) {
383 register_post_meta(
384 $post_type,
385 POST_META_DISABLE_JS_DELAY,
386 [
387 'show_in_rest' => true,
388 'single' => true,
389 'default' => false,
390 'type' => 'boolean',
391 'auth_callback' => function () {
392 return current_user_can( 'edit_others_posts' );
393 },
394 ]
395 );
396 }
397 }
398
399 }
400
401 /**
402 * Save meta info
403 *
404 * @param int $post_id Post ID
405 * @param Object $post \WP_Post
406 */
407 public function save_post_meta( $post_id, $post ) {
408 $post_id = absint( $post_id );
409
410 if ( empty( $post_id ) || empty( $post ) ) {
411 return;
412 }
413
414 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
415 return;
416 }
417
418 // saved in a separate request in the block editor
419 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
420 return;
421 }
422
423 // Check user has permission to edit.
424 if ( ! current_user_can( 'edit_others_posts' ) ) {
425 return;
426 }
427
428 if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) {
429 return;
430 }
431
432 // nonce check
433 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ) ), 'powered_cache_post_meta' ) ) {
434 return;
435 }
436
437 $meta_keys = self::get_meta_keys();
438
439 foreach ( $meta_keys as $meta_key ) {
440 if ( isset( $_POST[ $meta_key ] ) ) {
441 $value = (bool) $_POST[ $meta_key ];
442 update_post_meta( $post_id, $meta_key, $value );
443 } else {
444 // don't need to store default value in meta
445 delete_post_meta( $post_id, $meta_key );
446 }
447 }
448
449 }
450
451 /**
452 * Get powered cache meta keys
453 *
454 * @return array
455 * @since 2.1
456 */
457 private static function get_meta_keys() {
458 return [
459 POST_META_DISABLE_CACHE_KEY,
460 POST_META_DISABLE_LAZYLOAD_KEY,
461 POST_META_DISABLE_CRITICAL_CSS_KEY,
462 POST_META_SPECIFIC_CRITICAL_CSS_KEY,
463 POST_META_DISABLE_UCSS_KEY,
464 POST_META_SPECIFIC_UCSS_KEY,
465 POST_META_DISABLE_CSS_OPTIMIZATION,
466 POST_META_DISABLE_JS_OPTIMIZATION,
467 POST_META_DISABLE_JS_DEFER,
468 POST_META_DISABLE_JS_DELAY,
469 ];
470 }
471
472 /**
473 * Don't keep postmeta with default value. Block editor saves them?
474 *
475 * @param Object $post \WP_Post
476 * @param Object $request \WP_REST_Request
477 *
478 * @since 2.1
479 */
480 public function maybe_remove_default_meta( $post, $request ) {
481 if ( ! current_user_can( 'edit_others_posts' ) ) {
482 return;
483 }
484
485 $powered_cache_meta_keys = self::get_meta_keys();
486
487 foreach ( $powered_cache_meta_keys as $meta_key ) {
488 $meta_status = (bool) get_post_meta( $post->ID, $meta_key, true );
489 if ( ! $meta_status ) {
490 delete_post_meta( $post->ID, $meta_key );
491 }
492 }
493 }
494
495
496 }
497