PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
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
← All changes | includes/classes/MetaBox.php +313 -31 2.0.1trunk View file →
@@ -6,10 +6,18 @@
6 6 */
7 7
8 8 namespace PoweredCache;
9 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;
10 16 use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY;
17 +use const PoweredCache\Constants\POST_META_DISABLE_CRITICAL_CSS_KEY;
11 18 use const PoweredCache\Constants\POST_META_DISABLE_LAZYLOAD_KEY;
19 +use const PoweredCache\Constants\POST_META_SPECIFIC_UCSS_KEY;
12 20
13 21 /**
14 22 * Class MetaBox
15 23 */
@@ -44,14 +52,44 @@
44 52 public function setup() {
45 53 add_action( 'init', [ $this, 'register_meta_field' ] );
46 54 add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
47 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 + }
48 60 }
49 61
50 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 + /**
51 84 * Add metabox
52 85 */
53 86 public function add_meta_boxes() {
87 +
88 + if ( ! current_user_can( 'edit_others_posts' ) ) {
89 + return;
90 + }
91 +
54 92 $is_block_editor_compatible = false;
55 93 $back_compat = true;
56 94
57 95 if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) {
@@ -58,13 +96,38 @@
58 96 $is_block_editor_compatible = true;
59 97 $back_compat = false;
60 98 }
61 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 +
62 125 add_meta_box(
63 126 'powered_cache_post_meta',
64 127 esc_html__( 'Powered Cache', 'powered-cache' ),
65 128 [ $this, 'meta_box' ],
66 - 'post',
129 + '',
67 130 'side',
68 131 'high',
69 132 [
70 133 '__block_editor_compatible_meta_box' => $is_block_editor_compatible,
@@ -81,11 +144,8 @@
81 144 public function meta_box( $post ) {
82 145 $settings = \PoweredCache\Utils\get_settings();
83 146 $is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true );
84 147
85 - if ( ! $settings['enable_page_cache'] && ! $settings['enable_lazy_load'] ) {
86 - return; // nothing to control post specific
87 - }
88 148 ?>
89 149 <div id="powered-cache-meta-box">
90 150 <?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?>
91 151 <?php if ( $settings['enable_page_cache'] ) : ?>
@@ -102,8 +162,74 @@
102 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">
103 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>
104 164 </fieldset>
105 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 +
106 232 </div>
107 233 <?php
108 234 }
109 235
@@ -108,22 +234,14 @@
108 234 }
109 235
110 236
111 237 /**
112 - * Register meta field for block editor
238 + * Register meta fields for block editor
113 239 */
114 240 public function register_meta_field() {
115 - $settings = \PoweredCache\Utils\get_settings();
116 - $public_posts_types = get_post_types(
117 - [
118 - 'public' => true,
119 - 'publicly_queryable' => true,
120 - ],
121 - 'names',
122 - 'or'
123 - );
241 + $settings = \PoweredCache\Utils\get_settings();
124 242
125 - foreach ( (array) $public_posts_types as $post_type ) {
243 + foreach ( self::get_available_post_types() as $post_type ) {
126 244 if ( $settings['enable_page_cache'] ) {
127 245 register_post_meta(
128 246 $post_type,
129 247 POST_META_DISABLE_CACHE_KEY,
@@ -131,9 +249,9 @@
131 249 'show_in_rest' => true,
132 250 'single' => true,
133 251 'type' => 'boolean',
134 252 'auth_callback' => function () {
135 - return current_user_can( 'edit_posts' );
253 + return current_user_can( 'edit_others_posts' );
136 254 },
137 255 ]
138 256 );
139 257 }
@@ -146,13 +264,137 @@
146 264 'show_in_rest' => true,
147 265 'single' => true,
148 266 'type' => 'boolean',
149 267 'auth_callback' => function () {
150 - return current_user_can( 'edit_posts' );
268 + return current_user_can( 'edit_others_posts' );
151 269 },
152 270 ]
153 271 );
154 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 + }
155 397 }
156 398
157 399 }
158 400
@@ -178,9 +420,9 @@
178 420 return;
179 421 }
180 422
181 423 // Check user has permission to edit.
182 - if ( ! current_user_can( 'edit_post', $post_id ) ) {
424 + if ( ! current_user_can( 'edit_others_posts' ) ) {
183 425 return;
184 426 }
185 427
186 428 if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) {
@@ -187,28 +429,68 @@
187 429 return;
188 430 }
189 431
190 432 // nonce check
191 - if ( ! wp_verify_nonce( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ), 'powered_cache_post_meta' ) ) {
433 + if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ) ), 'powered_cache_post_meta' ) ) {
192 434 return;
193 435 }
194 436
195 - if ( isset( $_POST[ POST_META_DISABLE_CACHE_KEY ] ) ) {
196 - $cache_status = (bool) $_POST[ POST_META_DISABLE_CACHE_KEY ];
197 - update_post_meta( $post_id, POST_META_DISABLE_CACHE_KEY, $cache_status );
198 - } else {
199 - // don't need to store default value in meta
200 - delete_post_meta( $post_id, POST_META_DISABLE_CACHE_KEY );
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 + }
201 447 }
202 448
203 - if ( isset( $_POST[ POST_META_DISABLE_LAZYLOAD_KEY ] ) ) {
204 - $lazyload_status = (bool) $_POST[ POST_META_DISABLE_LAZYLOAD_KEY ];
205 - update_post_meta( $post_id, POST_META_DISABLE_LAZYLOAD_KEY, $lazyload_status );
206 - } else {
207 - // don't need to store default value in meta
208 - delete_post_meta( $post_id, POST_META_DISABLE_LAZYLOAD_KEY );
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;
209 483 }
210 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 + }
211 493 }
212 494
213 495
214 496 }