PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.2
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.2
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 2.2, at includes/classes/MetaBox.php

373 lines 11.5 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_OPTIMIZATION;
12 use const PoweredCache\Constants\POST_META_SPECIFIC_CRITICAL_CSS_KEY;
13 use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY;
14 use const PoweredCache\Constants\POST_META_DISABLE_CRITICAL_CSS_KEY;
15 use const PoweredCache\Constants\POST_META_DISABLE_LAZYLOAD_KEY;
16
17 /**
18 * Class MetaBox
19 */
20 class MetaBox {
21
22 /**
23 * Placeholder constructor
24 */
25 public function __construct() {
26 }
27
28 /**
29 * Return an instance of the current class
30 *
31 * @return MetaBox
32 * @since 2.0
33 */
34 public static function factory() {
35 static $instance = false;
36
37 if ( ! $instance ) {
38 $instance = new self();
39 $instance->setup();
40 }
41
42 return $instance;
43 }
44
45 /**
46 * Setup hooks
47 */
48 public function setup() {
49 add_action( 'init', [ $this, 'register_meta_field' ] );
50 add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
51 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 }
57
58 /**
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 * Add metabox
81 */
82 public function add_meta_boxes() {
83
84 if ( ! current_user_can( 'edit_others_posts' ) ) {
85 return;
86 }
87
88 $is_block_editor_compatible = false;
89 $back_compat = true;
90
91 if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) {
92 $is_block_editor_compatible = true;
93 $back_compat = false;
94 }
95
96 add_meta_box(
97 'powered_cache_post_meta',
98 esc_html__( 'Powered Cache', 'powered-cache' ),
99 [ $this, 'meta_box' ],
100 '',
101 'side',
102 'high',
103 [
104 '__block_editor_compatible_meta_box' => $is_block_editor_compatible,
105 '__back_compat_meta_box' => $back_compat,
106 ]
107 );
108 }
109
110 /**
111 * Display metabox output for classic editor
112 *
113 * @param Object $post \WP_Post
114 */
115 public function meta_box( $post ) {
116 $settings = \PoweredCache\Utils\get_settings();
117 $is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true );
118
119 if ( ! $settings['enable_page_cache'] && ! $settings['enable_lazy_load'] ) {
120 return; // nothing to control post specific
121 }
122 ?>
123 <div id="powered-cache-meta-box">
124 <?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?>
125 <?php if ( $settings['enable_page_cache'] ) : ?>
126 <fieldset>
127 <legend class="screen-reader-text"><?php esc_html_e( 'Powered Cache Status', 'powered-cache' ); ?></legend>
128 <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>
130 </fieldset>
131 <?php endif; ?>
132 <?php if ( $settings['enable_lazy_load'] ) : ?>
133 <?php $is_lazyload_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_LAZYLOAD_KEY, true ); ?>
134 <fieldset>
135 <legend class="screen-reader-text"><?php esc_html_e( 'Disable Lazy Load', 'powered-cache' ); ?></legend>
136 <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>
138 </fieldset>
139 <?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 </div>
175 <?php
176 }
177
178
179 /**
180 * Register meta fields for block editor
181 */
182 public function register_meta_field() {
183 $settings = \PoweredCache\Utils\get_settings();
184
185 foreach ( self::get_available_post_types() as $post_type ) {
186 if ( $settings['enable_page_cache'] ) {
187 register_post_meta(
188 $post_type,
189 POST_META_DISABLE_CACHE_KEY,
190 [
191 'show_in_rest' => true,
192 'single' => true,
193 'type' => 'boolean',
194 'auth_callback' => function () {
195 return current_user_can( 'edit_others_posts' );
196 },
197 ]
198 );
199 }
200
201 if ( $settings['enable_lazy_load'] ) {
202 register_post_meta(
203 $post_type,
204 POST_META_DISABLE_LAZYLOAD_KEY,
205 [
206 'show_in_rest' => true,
207 'single' => true,
208 'type' => 'boolean',
209 'auth_callback' => function () {
210 return current_user_can( 'edit_others_posts' );
211 },
212 ]
213 );
214 }
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 }
278
279 }
280
281 /**
282 * Save meta info
283 *
284 * @param int $post_id Post ID
285 * @param Object $post \WP_Post
286 */
287 public function save_post_meta( $post_id, $post ) {
288 $post_id = absint( $post_id );
289
290 if ( empty( $post_id ) || empty( $post ) ) {
291 return;
292 }
293
294 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
295 return;
296 }
297
298 // saved in a separate request in the block editor
299 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
300 return;
301 }
302
303 // Check user has permission to edit.
304 if ( ! current_user_can( 'edit_others_posts' ) ) {
305 return;
306 }
307
308 if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) {
309 return;
310 }
311
312 // nonce check
313 if ( ! wp_verify_nonce( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ), 'powered_cache_post_meta' ) ) {
314 return;
315 }
316
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 }
327 }
328
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;
359 }
360
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 }
370
371
372 }
373