| @@ -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 | */ |
| @@ -79,8 +83,13 @@ | ||
| 79 | 83 | /** |
| 80 | 84 | * Add metabox |
| 81 | 85 | */ |
| 82 | 86 | public function add_meta_boxes() { |
| 87 | + | |
| 88 | + if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 83 | 92 | $is_block_editor_compatible = false; |
| 84 | 93 | $back_compat = true; |
| 85 | 94 | |
| 86 | 95 | if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) { |
| @@ -87,8 +96,33 @@ | ||
| 87 | 96 | $is_block_editor_compatible = true; |
| 88 | 97 | $back_compat = false; |
| 89 | 98 | } |
| 90 | 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 | + | |
| 91 | 125 | add_meta_box( |
| 92 | 126 | 'powered_cache_post_meta', |
| 93 | 127 | esc_html__( 'Powered Cache', 'powered-cache' ), |
| 94 | 128 | [ $this, 'meta_box' ], |
| @@ -110,11 +144,8 @@ | ||
| 110 | 144 | public function meta_box( $post ) { |
| 111 | 145 | $settings = \PoweredCache\Utils\get_settings(); |
| 112 | 146 | $is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true ); |
| 113 | 147 | |
| 114 | - if ( ! $settings['enable_page_cache'] && ! $settings['enable_lazy_load'] ) { | |
| 115 | - return; // nothing to control post specific | |
| 116 | - } | |
| 117 | 148 | ?> |
| 118 | 149 | <div id="powered-cache-meta-box"> |
| 119 | 150 | <?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?> |
| 120 | 151 | <?php if ( $settings['enable_page_cache'] ) : ?> |
| @@ -147,8 +178,24 @@ | ||
| 147 | 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"> |
| 148 | 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> |
| 149 | 180 | </fieldset> |
| 150 | 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; ?> | |
| 151 | 198 | |
| 152 | 199 | |
| 153 | 200 | <?php if ( $settings['critical_css'] ) : ?> |
| 154 | 201 | <?php $disable_critical = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CRITICAL_CSS_KEY, true ); ?> |
| @@ -165,8 +212,24 @@ | ||
| 165 | 212 | <label for="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></label> |
| 166 | 213 | </fieldset> |
| 167 | 214 | <?php endif; ?> |
| 168 | 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 | + | |
| 169 | 232 | </div> |
| 170 | 233 | <?php |
| 171 | 234 | } |
| 172 | 235 | |
| @@ -186,9 +249,9 @@ | ||
| 186 | 249 | 'show_in_rest' => true, |
| 187 | 250 | 'single' => true, |
| 188 | 251 | 'type' => 'boolean', |
| 189 | 252 | 'auth_callback' => function () { |
| 190 | - return current_user_can( 'edit_posts' ); | |
| 253 | + return current_user_can( 'edit_others_posts' ); | |
| 191 | 254 | }, |
| 192 | 255 | ] |
| 193 | 256 | ); |
| 194 | 257 | } |
| @@ -201,9 +264,9 @@ | ||
| 201 | 264 | 'show_in_rest' => true, |
| 202 | 265 | 'single' => true, |
| 203 | 266 | 'type' => 'boolean', |
| 204 | 267 | 'auth_callback' => function () { |
| 205 | - return current_user_can( 'edit_posts' ); | |
| 268 | + return current_user_can( 'edit_others_posts' ); | |
| 206 | 269 | }, |
| 207 | 270 | ] |
| 208 | 271 | ); |
| 209 | 272 | } |
| @@ -217,9 +280,9 @@ | ||
| 217 | 280 | 'single' => true, |
| 218 | 281 | 'default' => false, |
| 219 | 282 | 'type' => 'boolean', |
| 220 | 283 | 'auth_callback' => function () { |
| 221 | - return current_user_can( 'edit_posts' ); | |
| 284 | + return current_user_can( 'edit_others_posts' ); | |
| 222 | 285 | }, |
| 223 | 286 | ] |
| 224 | 287 | ); |
| 225 | 288 | |
| @@ -231,14 +294,44 @@ | ||
| 231 | 294 | 'single' => true, |
| 232 | 295 | 'default' => false, |
| 233 | 296 | 'type' => 'boolean', |
| 234 | 297 | 'auth_callback' => function () { |
| 235 | - return current_user_can( 'edit_posts' ); | |
| 298 | + return current_user_can( 'edit_others_posts' ); | |
| 236 | 299 | }, |
| 237 | 300 | ] |
| 238 | 301 | ); |
| 239 | 302 | } |
| 240 | 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 | + | |
| 241 | 334 | if ( $settings['minify_css'] || $settings['combine_css'] ) { |
| 242 | 335 | register_post_meta( |
| 243 | 336 | $post_type, |
| 244 | 337 | POST_META_DISABLE_CSS_OPTIMIZATION, |
| @@ -247,9 +340,9 @@ | ||
| 247 | 340 | 'single' => true, |
| 248 | 341 | 'default' => false, |
| 249 | 342 | 'type' => 'boolean', |
| 250 | 343 | 'auth_callback' => function () { |
| 251 | - return current_user_can( 'edit_posts' ); | |
| 344 | + return current_user_can( 'edit_others_posts' ); | |
| 252 | 345 | }, |
| 253 | 346 | ] |
| 254 | 347 | ); |
| 255 | 348 | } |
| @@ -263,13 +356,45 @@ | ||
| 263 | 356 | 'single' => true, |
| 264 | 357 | 'default' => false, |
| 265 | 358 | 'type' => 'boolean', |
| 266 | 359 | 'auth_callback' => function () { |
| 267 | - return current_user_can( 'edit_posts' ); | |
| 360 | + return current_user_can( 'edit_others_posts' ); | |
| 268 | 361 | }, |
| 269 | 362 | ] |
| 270 | 363 | ); |
| 271 | 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 | + } | |
| 272 | 397 | } |
| 273 | 398 | |
| 274 | 399 | } |
| 275 | 400 | |
| @@ -295,9 +420,9 @@ | ||
| 295 | 420 | return; |
| 296 | 421 | } |
| 297 | 422 | |
| 298 | 423 | // Check user has permission to edit. |
| 299 | - if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
| 424 | + if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 300 | 425 | return; |
| 301 | 426 | } |
| 302 | 427 | |
| 303 | 428 | if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) { |
| @@ -304,9 +429,9 @@ | ||
| 304 | 429 | return; |
| 305 | 430 | } |
| 306 | 431 | |
| 307 | 432 | // nonce check |
| 308 | - 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' ) ) { | |
| 309 | 434 | return; |
| 310 | 435 | } |
| 311 | 436 | |
| 312 | 437 | $meta_keys = self::get_meta_keys(); |
| @@ -334,10 +459,14 @@ | ||
| 334 | 459 | POST_META_DISABLE_CACHE_KEY, |
| 335 | 460 | POST_META_DISABLE_LAZYLOAD_KEY, |
| 336 | 461 | POST_META_DISABLE_CRITICAL_CSS_KEY, |
| 337 | 462 | POST_META_SPECIFIC_CRITICAL_CSS_KEY, |
| 463 | + POST_META_DISABLE_UCSS_KEY, | |
| 464 | + POST_META_SPECIFIC_UCSS_KEY, | |
| 338 | 465 | POST_META_DISABLE_CSS_OPTIMIZATION, |
| 339 | 466 | POST_META_DISABLE_JS_OPTIMIZATION, |
| 467 | + POST_META_DISABLE_JS_DEFER, | |
| 468 | + POST_META_DISABLE_JS_DELAY, | |
| 340 | 469 | ]; |
| 341 | 470 | } |
| 342 | 471 | |
| 343 | 472 | /** |
| @@ -348,9 +477,9 @@ | ||
| 348 | 477 | * |
| 349 | 478 | * @since 2.1 |
| 350 | 479 | */ |
| 351 | 480 | public function maybe_remove_default_meta( $post, $request ) { |
| 352 | - if ( ! current_user_can( 'edit_post', $post->ID ) ) { | |
| 481 | + if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 353 | 482 | return; |
| 354 | 483 | } |
| 355 | 484 | |
| 356 | 485 | $powered_cache_meta_keys = self::get_meta_keys(); |