| @@ -7,13 +7,17 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace PoweredCache; |
| 9 | 9 | |
| 10 | 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; | |
| 11 | 13 | use const PoweredCache\Constants\POST_META_DISABLE_JS_OPTIMIZATION; |
| 14 | +use const PoweredCache\Constants\POST_META_DISABLE_UCSS_KEY; | |
| 12 | 15 | use const PoweredCache\Constants\POST_META_SPECIFIC_CRITICAL_CSS_KEY; |
| 13 | 16 | use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY; |
| 14 | 17 | use const PoweredCache\Constants\POST_META_DISABLE_CRITICAL_CSS_KEY; |
| 15 | 18 | use const PoweredCache\Constants\POST_META_DISABLE_LAZYLOAD_KEY; |
| 19 | +use const PoweredCache\Constants\POST_META_SPECIFIC_UCSS_KEY; | |
| 16 | 20 | |
| 17 | 21 | /** |
| 18 | 22 | * Class MetaBox |
| 19 | 23 | */ |
| @@ -92,8 +96,33 @@ | ||
| 92 | 96 | $is_block_editor_compatible = true; |
| 93 | 97 | $back_compat = false; |
| 94 | 98 | } |
| 95 | 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 | + | |
| 96 | 125 | add_meta_box( |
| 97 | 126 | 'powered_cache_post_meta', |
| 98 | 127 | esc_html__( 'Powered Cache', 'powered-cache' ), |
| 99 | 128 | [ $this, 'meta_box' ], |
| @@ -115,11 +144,8 @@ | ||
| 115 | 144 | public function meta_box( $post ) { |
| 116 | 145 | $settings = \PoweredCache\Utils\get_settings(); |
| 117 | 146 | $is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true ); |
| 118 | 147 | |
| 119 | - if ( ! $settings['enable_page_cache'] && ! $settings['enable_lazy_load'] ) { | |
| 120 | - return; // nothing to control post specific | |
| 121 | - } | |
| 122 | 148 | ?> |
| 123 | 149 | <div id="powered-cache-meta-box"> |
| 124 | 150 | <?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?> |
| 125 | 151 | <?php if ( $settings['enable_page_cache'] ) : ?> |
| @@ -152,8 +178,24 @@ | ||
| 152 | 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"> |
| 153 | 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> |
| 154 | 180 | </fieldset> |
| 155 | 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; ?> | |
| 156 | 198 | |
| 157 | 199 | |
| 158 | 200 | <?php if ( $settings['critical_css'] ) : ?> |
| 159 | 201 | <?php $disable_critical = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CRITICAL_CSS_KEY, true ); ?> |
| @@ -170,8 +212,24 @@ | ||
| 170 | 212 | <label for="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></label> |
| 171 | 213 | </fieldset> |
| 172 | 214 | <?php endif; ?> |
| 173 | 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 | + | |
| 174 | 232 | </div> |
| 175 | 233 | <?php |
| 176 | 234 | } |
| 177 | 235 | |
| @@ -242,8 +300,38 @@ | ||
| 242 | 300 | ] |
| 243 | 301 | ); |
| 244 | 302 | } |
| 245 | 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 | + | |
| 246 | 334 | if ( $settings['minify_css'] || $settings['combine_css'] ) { |
| 247 | 335 | register_post_meta( |
| 248 | 336 | $post_type, |
| 249 | 337 | POST_META_DISABLE_CSS_OPTIMIZATION, |
| @@ -273,8 +361,40 @@ | ||
| 273 | 361 | }, |
| 274 | 362 | ] |
| 275 | 363 | ); |
| 276 | 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 | + } | |
| 277 | 397 | } |
| 278 | 398 | |
| 279 | 399 | } |
| 280 | 400 | |
| @@ -309,9 +429,9 @@ | ||
| 309 | 429 | return; |
| 310 | 430 | } |
| 311 | 431 | |
| 312 | 432 | // nonce check |
| 313 | - 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' ) ) { | |
| 314 | 434 | return; |
| 315 | 435 | } |
| 316 | 436 | |
| 317 | 437 | $meta_keys = self::get_meta_keys(); |
| @@ -339,10 +459,14 @@ | ||
| 339 | 459 | POST_META_DISABLE_CACHE_KEY, |
| 340 | 460 | POST_META_DISABLE_LAZYLOAD_KEY, |
| 341 | 461 | POST_META_DISABLE_CRITICAL_CSS_KEY, |
| 342 | 462 | POST_META_SPECIFIC_CRITICAL_CSS_KEY, |
| 463 | + POST_META_DISABLE_UCSS_KEY, | |
| 464 | + POST_META_SPECIFIC_UCSS_KEY, | |
| 343 | 465 | POST_META_DISABLE_CSS_OPTIMIZATION, |
| 344 | 466 | POST_META_DISABLE_JS_OPTIMIZATION, |
| 467 | + POST_META_DISABLE_JS_DEFER, | |
| 468 | + POST_META_DISABLE_JS_DELAY, | |
| 345 | 469 | ]; |
| 346 | 470 | } |
| 347 | 471 | |
| 348 | 472 | /** |