| @@ -6,13 +6,9 @@ | ||
| 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_OPTIMIZATION; | |
| 12 | -use const PoweredCache\Constants\POST_META_SPECIFIC_CRITICAL_CSS_KEY; | |
| 13 | 10 | use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY; |
| 14 | -use const PoweredCache\Constants\POST_META_DISABLE_CRITICAL_CSS_KEY; | |
| 15 | 11 | use const PoweredCache\Constants\POST_META_DISABLE_LAZYLOAD_KEY; |
| 16 | 12 | |
| 17 | 13 | /** |
| 18 | 14 | * Class MetaBox |
| @@ -48,44 +44,14 @@ | ||
| 48 | 44 | public function setup() { |
| 49 | 45 | add_action( 'init', [ $this, 'register_meta_field' ] ); |
| 50 | 46 | add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 51 | 47 | add_action( 'save_post', [ $this, 'save_post_meta' ], 10, 2 ); |
| 52 | - | |
| 53 | - foreach ( self::get_available_post_types() as $post_type ) { | |
| 54 | - add_action( 'rest_after_insert_' . $post_type, [ $this, 'maybe_remove_default_meta' ], 10, 2 ); | |
| 55 | - } | |
| 56 | 48 | } |
| 57 | 49 | |
| 58 | 50 | /** |
| 59 | - * Get available post types for meta register | |
| 60 | - * | |
| 61 | - * @return array | |
| 62 | - * @since 2.1 | |
| 63 | - */ | |
| 64 | - private static function get_available_post_types() { | |
| 65 | - $public_posts_types = get_post_types( | |
| 66 | - [ | |
| 67 | - 'public' => true, | |
| 68 | - 'publicly_queryable' => true, | |
| 69 | - ], | |
| 70 | - 'names', | |
| 71 | - 'or' | |
| 72 | - ); | |
| 73 | - | |
| 74 | - unset( $public_posts_types['attachment'] ); | |
| 75 | - | |
| 76 | - return (array) $public_posts_types; | |
| 77 | - } | |
| 78 | - | |
| 79 | - /** | |
| 80 | 51 | * Add metabox |
| 81 | 52 | */ |
| 82 | 53 | public function add_meta_boxes() { |
| 83 | - | |
| 84 | - if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 85 | - return; | |
| 86 | - } | |
| 87 | - | |
| 88 | 54 | $is_block_editor_compatible = false; |
| 89 | 55 | $back_compat = true; |
| 90 | 56 | |
| 91 | 57 | if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) { |
| @@ -96,9 +62,9 @@ | ||
| 96 | 62 | add_meta_box( |
| 97 | 63 | 'powered_cache_post_meta', |
| 98 | 64 | esc_html__( 'Powered Cache', 'powered-cache' ), |
| 99 | 65 | [ $this, 'meta_box' ], |
| 100 | - '', | |
| 66 | + 'post', | |
| 101 | 67 | 'side', |
| 102 | 68 | 'high', |
| 103 | 69 | [ |
| 104 | 70 | '__block_editor_compatible_meta_box' => $is_block_editor_compatible, |
| @@ -125,9 +91,9 @@ | ||
| 125 | 91 | <?php if ( $settings['enable_page_cache'] ) : ?> |
| 126 | 92 | <fieldset> |
| 127 | 93 | <legend class="screen-reader-text"><?php esc_html_e( 'Powered Cache Status', 'powered-cache' ); ?></legend> |
| 128 | 94 | <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"> |
| 129 | - <label for="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>"><?php esc_html_e( 'Don\'t cache this post', 'powered-cache' ); ?></label> | |
| 95 | + <label for="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>"><?php esc_html_e( 'Don\'t cache this page', 'powered-cache' ); ?></label> | |
| 130 | 96 | </fieldset> |
| 131 | 97 | <?php endif; ?> |
| 132 | 98 | <?php if ( $settings['enable_lazy_load'] ) : ?> |
| 133 | 99 | <?php $is_lazyload_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_LAZYLOAD_KEY, true ); ?> |
| @@ -133,45 +99,11 @@ | ||
| 133 | 99 | <?php $is_lazyload_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_LAZYLOAD_KEY, true ); ?> |
| 134 | 100 | <fieldset> |
| 135 | 101 | <legend class="screen-reader-text"><?php esc_html_e( 'Disable Lazy Load', 'powered-cache' ); ?></legend> |
| 136 | 102 | <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"> |
| 137 | - <label for="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>"><?php esc_html_e( 'Disable lazy loading for this post', 'powered-cache' ); ?></label> | |
| 103 | + <label for="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>"><?php esc_html_e( 'Skip lazy loading for this post', 'powered-cache' ); ?></label> | |
| 138 | 104 | </fieldset> |
| 139 | 105 | <?php endif; ?> |
| 140 | - <?php if ( $settings['minify_css'] || $settings['combine_css'] ) : ?> | |
| 141 | - <?php $is_css_optimization_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CSS_OPTIMIZATION, true ); ?> | |
| 142 | - <fieldset> | |
| 143 | - <legend class="screen-reader-text"><?php esc_html_e( 'Disable CSS optimization (minify/concat) for this post', 'powered-cache' ); ?></legend> | |
| 144 | - <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"> | |
| 145 | - <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> | |
| 146 | - </fieldset> | |
| 147 | - <?php endif; ?> | |
| 148 | - <?php if ( $settings['minify_js'] || $settings['combine_js'] ) : ?> | |
| 149 | - <?php $is_js_optimization_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_JS_OPTIMIZATION, true ); ?> | |
| 150 | - <fieldset> | |
| 151 | - <legend class="screen-reader-text"><?php esc_html_e( 'Disable JS optimization (minify/concat) for this post', 'powered-cache' ); ?></legend> | |
| 152 | - <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 | - <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 | - </fieldset> | |
| 155 | - <?php endif; ?> | |
| 156 | - | |
| 157 | - | |
| 158 | - <?php if ( $settings['critical_css'] ) : ?> | |
| 159 | - <?php $disable_critical = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CRITICAL_CSS_KEY, true ); ?> | |
| 160 | - <?php $generate_post_specific_critical = (bool) get_post_meta( $post->ID, POST_META_SPECIFIC_CRITICAL_CSS_KEY, true ); ?> | |
| 161 | - <fieldset> | |
| 162 | - <legend class="screen-reader-text"><?php esc_html_e( 'Disable Critical CSS on this post', 'powered-cache' ); ?></legend> | |
| 163 | - <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"> | |
| 164 | - <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> | |
| 165 | - </fieldset> | |
| 166 | - | |
| 167 | - <fieldset> | |
| 168 | - <legend class="screen-reader-text"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></legend> | |
| 169 | - <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"> | |
| 170 | - <label for="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></label> | |
| 171 | - </fieldset> | |
| 172 | - <?php endif; ?> | |
| 173 | - | |
| 174 | 106 | </div> |
| 175 | 107 | <?php |
| 176 | 108 | } |
| 177 | 109 | |
| @@ -176,14 +108,22 @@ | ||
| 176 | 108 | } |
| 177 | 109 | |
| 178 | 110 | |
| 179 | 111 | /** |
| 180 | - * Register meta fields for block editor | |
| 112 | + * Register meta field for block editor | |
| 181 | 113 | */ |
| 182 | 114 | public function register_meta_field() { |
| 183 | - $settings = \PoweredCache\Utils\get_settings(); | |
| 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 | + ); | |
| 184 | 124 | |
| 185 | - foreach ( self::get_available_post_types() as $post_type ) { | |
| 125 | + foreach ( (array) $public_posts_types as $post_type ) { | |
| 186 | 126 | if ( $settings['enable_page_cache'] ) { |
| 187 | 127 | register_post_meta( |
| 188 | 128 | $post_type, |
| 189 | 129 | POST_META_DISABLE_CACHE_KEY, |
| @@ -191,9 +131,9 @@ | ||
| 191 | 131 | 'show_in_rest' => true, |
| 192 | 132 | 'single' => true, |
| 193 | 133 | 'type' => 'boolean', |
| 194 | 134 | 'auth_callback' => function () { |
| 195 | - return current_user_can( 'edit_others_posts' ); | |
| 135 | + return current_user_can( 'edit_posts' ); | |
| 196 | 136 | }, |
| 197 | 137 | ] |
| 198 | 138 | ); |
| 199 | 139 | } |
| @@ -206,75 +146,13 @@ | ||
| 206 | 146 | 'show_in_rest' => true, |
| 207 | 147 | 'single' => true, |
| 208 | 148 | 'type' => 'boolean', |
| 209 | 149 | 'auth_callback' => function () { |
| 210 | - return current_user_can( 'edit_others_posts' ); | |
| 150 | + return current_user_can( 'edit_posts' ); | |
| 211 | 151 | }, |
| 212 | 152 | ] |
| 213 | 153 | ); |
| 214 | 154 | } |
| 215 | - | |
| 216 | - if ( $settings['critical_css'] ) { | |
| 217 | - register_post_meta( | |
| 218 | - $post_type, | |
| 219 | - POST_META_DISABLE_CRITICAL_CSS_KEY, | |
| 220 | - [ | |
| 221 | - 'show_in_rest' => true, | |
| 222 | - 'single' => true, | |
| 223 | - 'default' => false, | |
| 224 | - 'type' => 'boolean', | |
| 225 | - 'auth_callback' => function () { | |
| 226 | - return current_user_can( 'edit_others_posts' ); | |
| 227 | - }, | |
| 228 | - ] | |
| 229 | - ); | |
| 230 | - | |
| 231 | - register_post_meta( | |
| 232 | - $post_type, | |
| 233 | - POST_META_SPECIFIC_CRITICAL_CSS_KEY, | |
| 234 | - [ | |
| 235 | - 'show_in_rest' => true, | |
| 236 | - 'single' => true, | |
| 237 | - 'default' => false, | |
| 238 | - 'type' => 'boolean', | |
| 239 | - 'auth_callback' => function () { | |
| 240 | - return current_user_can( 'edit_others_posts' ); | |
| 241 | - }, | |
| 242 | - ] | |
| 243 | - ); | |
| 244 | - } | |
| 245 | - | |
| 246 | - if ( $settings['minify_css'] || $settings['combine_css'] ) { | |
| 247 | - register_post_meta( | |
| 248 | - $post_type, | |
| 249 | - POST_META_DISABLE_CSS_OPTIMIZATION, | |
| 250 | - [ | |
| 251 | - 'show_in_rest' => true, | |
| 252 | - 'single' => true, | |
| 253 | - 'default' => false, | |
| 254 | - 'type' => 'boolean', | |
| 255 | - 'auth_callback' => function () { | |
| 256 | - return current_user_can( 'edit_others_posts' ); | |
| 257 | - }, | |
| 258 | - ] | |
| 259 | - ); | |
| 260 | - } | |
| 261 | - | |
| 262 | - if ( $settings['minify_js'] || $settings['combine_js'] ) { | |
| 263 | - register_post_meta( | |
| 264 | - $post_type, | |
| 265 | - POST_META_DISABLE_JS_OPTIMIZATION, | |
| 266 | - [ | |
| 267 | - 'show_in_rest' => true, | |
| 268 | - 'single' => true, | |
| 269 | - 'default' => false, | |
| 270 | - 'type' => 'boolean', | |
| 271 | - 'auth_callback' => function () { | |
| 272 | - return current_user_can( 'edit_others_posts' ); | |
| 273 | - }, | |
| 274 | - ] | |
| 275 | - ); | |
| 276 | - } | |
| 277 | 155 | } |
| 278 | 156 | |
| 279 | 157 | } |
| 280 | 158 | |
| @@ -300,9 +178,9 @@ | ||
| 300 | 178 | return; |
| 301 | 179 | } |
| 302 | 180 | |
| 303 | 181 | // Check user has permission to edit. |
| 304 | - if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 182 | + if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
| 305 | 183 | return; |
| 306 | 184 | } |
| 307 | 185 | |
| 308 | 186 | if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) { |
| @@ -313,60 +191,24 @@ | ||
| 313 | 191 | if ( ! wp_verify_nonce( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ), 'powered_cache_post_meta' ) ) { |
| 314 | 192 | return; |
| 315 | 193 | } |
| 316 | 194 | |
| 317 | - $meta_keys = self::get_meta_keys(); | |
| 318 | - | |
| 319 | - foreach ( $meta_keys as $meta_key ) { | |
| 320 | - if ( isset( $_POST[ $meta_key ] ) ) { | |
| 321 | - $value = (bool) $_POST[ $meta_key ]; | |
| 322 | - update_post_meta( $post_id, $meta_key, $value ); | |
| 323 | - } else { | |
| 324 | - // don't need to store default value in meta | |
| 325 | - delete_post_meta( $post_id, $meta_key ); | |
| 326 | - } | |
| 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 ); | |
| 327 | 201 | } |
| 328 | 202 | |
| 329 | - } | |
| 330 | - | |
| 331 | - /** | |
| 332 | - * Get powered cache meta keys | |
| 333 | - * | |
| 334 | - * @return array | |
| 335 | - * @since 2.1 | |
| 336 | - */ | |
| 337 | - private static function get_meta_keys() { | |
| 338 | - return [ | |
| 339 | - POST_META_DISABLE_CACHE_KEY, | |
| 340 | - POST_META_DISABLE_LAZYLOAD_KEY, | |
| 341 | - POST_META_DISABLE_CRITICAL_CSS_KEY, | |
| 342 | - POST_META_SPECIFIC_CRITICAL_CSS_KEY, | |
| 343 | - POST_META_DISABLE_CSS_OPTIMIZATION, | |
| 344 | - POST_META_DISABLE_JS_OPTIMIZATION, | |
| 345 | - ]; | |
| 346 | - } | |
| 347 | - | |
| 348 | - /** | |
| 349 | - * Don't keep postmeta with default value. Block editor saves them? | |
| 350 | - * | |
| 351 | - * @param Object $post \WP_Post | |
| 352 | - * @param Object $request \WP_REST_Request | |
| 353 | - * | |
| 354 | - * @since 2.1 | |
| 355 | - */ | |
| 356 | - public function maybe_remove_default_meta( $post, $request ) { | |
| 357 | - if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 358 | - return; | |
| 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 ); | |
| 359 | 209 | } |
| 360 | 210 | |
| 361 | - $powered_cache_meta_keys = self::get_meta_keys(); | |
| 362 | - | |
| 363 | - foreach ( $powered_cache_meta_keys as $meta_key ) { | |
| 364 | - $meta_status = (bool) get_post_meta( $post->ID, $meta_key, true ); | |
| 365 | - if ( ! $meta_status ) { | |
| 366 | - delete_post_meta( $post->ID, $meta_key ); | |
| 367 | - } | |
| 368 | - } | |
| 369 | 211 | } |
| 370 | 212 | |
| 371 | 213 | |
| 372 | 214 | } |