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

368 lines 11.4 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 $is_block_editor_compatible = false;
84 $back_compat = true;
85
86 if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) {
87 $is_block_editor_compatible = true;
88 $back_compat = false;
89 }
90
91 add_meta_box(
92 'powered_cache_post_meta',
93 esc_html__( 'Powered Cache', 'powered-cache' ),
94 [ $this, 'meta_box' ],
95 '',
96 'side',
97 'high',
98 [
99 '__block_editor_compatible_meta_box' => $is_block_editor_compatible,
100 '__back_compat_meta_box' => $back_compat,
101 ]
102 );
103 }
104
105 /**
106 * Display metabox output for classic editor
107 *
108 * @param Object $post \WP_Post
109 */
110 public function meta_box( $post ) {
111 $settings = \PoweredCache\Utils\get_settings();
112 $is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true );
113
114 if ( ! $settings['enable_page_cache'] && ! $settings['enable_lazy_load'] ) {
115 return; // nothing to control post specific
116 }
117 ?>
118 <div id="powered-cache-meta-box">
119 <?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?>
120 <?php if ( $settings['enable_page_cache'] ) : ?>
121 <fieldset>
122 <legend class="screen-reader-text"><?php esc_html_e( 'Powered Cache Status', 'powered-cache' ); ?></legend>
123 <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">
124 <label for="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>"><?php esc_html_e( 'Don\'t cache this post', 'powered-cache' ); ?></label>
125 </fieldset>
126 <?php endif; ?>
127 <?php if ( $settings['enable_lazy_load'] ) : ?>
128 <?php $is_lazyload_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_LAZYLOAD_KEY, true ); ?>
129 <fieldset>
130 <legend class="screen-reader-text"><?php esc_html_e( 'Disable Lazy Load', 'powered-cache' ); ?></legend>
131 <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">
132 <label for="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>"><?php esc_html_e( 'Disable lazy loading for this post', 'powered-cache' ); ?></label>
133 </fieldset>
134 <?php endif; ?>
135 <?php if ( $settings['minify_css'] || $settings['combine_css'] ) : ?>
136 <?php $is_css_optimization_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CSS_OPTIMIZATION, true ); ?>
137 <fieldset>
138 <legend class="screen-reader-text"><?php esc_html_e( 'Disable CSS optimization (minify/concat) for this post', 'powered-cache' ); ?></legend>
139 <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">
140 <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>
141 </fieldset>
142 <?php endif; ?>
143 <?php if ( $settings['minify_js'] || $settings['combine_js'] ) : ?>
144 <?php $is_js_optimization_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_JS_OPTIMIZATION, true ); ?>
145 <fieldset>
146 <legend class="screen-reader-text"><?php esc_html_e( 'Disable JS optimization (minify/concat) for this post', 'powered-cache' ); ?></legend>
147 <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 <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 </fieldset>
150 <?php endif; ?>
151
152
153 <?php if ( $settings['critical_css'] ) : ?>
154 <?php $disable_critical = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CRITICAL_CSS_KEY, true ); ?>
155 <?php $generate_post_specific_critical = (bool) get_post_meta( $post->ID, POST_META_SPECIFIC_CRITICAL_CSS_KEY, true ); ?>
156 <fieldset>
157 <legend class="screen-reader-text"><?php esc_html_e( 'Disable Critical CSS on this post', 'powered-cache' ); ?></legend>
158 <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">
159 <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>
160 </fieldset>
161
162 <fieldset>
163 <legend class="screen-reader-text"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></legend>
164 <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">
165 <label for="<?php echo esc_attr( POST_META_SPECIFIC_CRITICAL_CSS_KEY ); ?>"><?php esc_html_e( 'Generate specific Critical CSS', 'powered-cache' ); ?></label>
166 </fieldset>
167 <?php endif; ?>
168
169 </div>
170 <?php
171 }
172
173
174 /**
175 * Register meta fields for block editor
176 */
177 public function register_meta_field() {
178 $settings = \PoweredCache\Utils\get_settings();
179
180 foreach ( self::get_available_post_types() as $post_type ) {
181 if ( $settings['enable_page_cache'] ) {
182 register_post_meta(
183 $post_type,
184 POST_META_DISABLE_CACHE_KEY,
185 [
186 'show_in_rest' => true,
187 'single' => true,
188 'type' => 'boolean',
189 'auth_callback' => function () {
190 return current_user_can( 'edit_posts' );
191 },
192 ]
193 );
194 }
195
196 if ( $settings['enable_lazy_load'] ) {
197 register_post_meta(
198 $post_type,
199 POST_META_DISABLE_LAZYLOAD_KEY,
200 [
201 'show_in_rest' => true,
202 'single' => true,
203 'type' => 'boolean',
204 'auth_callback' => function () {
205 return current_user_can( 'edit_posts' );
206 },
207 ]
208 );
209 }
210
211 if ( $settings['critical_css'] ) {
212 register_post_meta(
213 $post_type,
214 POST_META_DISABLE_CRITICAL_CSS_KEY,
215 [
216 'show_in_rest' => true,
217 'single' => true,
218 'default' => false,
219 'type' => 'boolean',
220 'auth_callback' => function () {
221 return current_user_can( 'edit_posts' );
222 },
223 ]
224 );
225
226 register_post_meta(
227 $post_type,
228 POST_META_SPECIFIC_CRITICAL_CSS_KEY,
229 [
230 'show_in_rest' => true,
231 'single' => true,
232 'default' => false,
233 'type' => 'boolean',
234 'auth_callback' => function () {
235 return current_user_can( 'edit_posts' );
236 },
237 ]
238 );
239 }
240
241 if ( $settings['minify_css'] || $settings['combine_css'] ) {
242 register_post_meta(
243 $post_type,
244 POST_META_DISABLE_CSS_OPTIMIZATION,
245 [
246 'show_in_rest' => true,
247 'single' => true,
248 'default' => false,
249 'type' => 'boolean',
250 'auth_callback' => function () {
251 return current_user_can( 'edit_posts' );
252 },
253 ]
254 );
255 }
256
257 if ( $settings['minify_js'] || $settings['combine_js'] ) {
258 register_post_meta(
259 $post_type,
260 POST_META_DISABLE_JS_OPTIMIZATION,
261 [
262 'show_in_rest' => true,
263 'single' => true,
264 'default' => false,
265 'type' => 'boolean',
266 'auth_callback' => function () {
267 return current_user_can( 'edit_posts' );
268 },
269 ]
270 );
271 }
272 }
273
274 }
275
276 /**
277 * Save meta info
278 *
279 * @param int $post_id Post ID
280 * @param Object $post \WP_Post
281 */
282 public function save_post_meta( $post_id, $post ) {
283 $post_id = absint( $post_id );
284
285 if ( empty( $post_id ) || empty( $post ) ) {
286 return;
287 }
288
289 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
290 return;
291 }
292
293 // saved in a separate request in the block editor
294 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
295 return;
296 }
297
298 // Check user has permission to edit.
299 if ( ! current_user_can( 'edit_post', $post_id ) ) {
300 return;
301 }
302
303 if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) {
304 return;
305 }
306
307 // nonce check
308 if ( ! wp_verify_nonce( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ), 'powered_cache_post_meta' ) ) {
309 return;
310 }
311
312 $meta_keys = self::get_meta_keys();
313
314 foreach ( $meta_keys as $meta_key ) {
315 if ( isset( $_POST[ $meta_key ] ) ) {
316 $value = (bool) $_POST[ $meta_key ];
317 update_post_meta( $post_id, $meta_key, $value );
318 } else {
319 // don't need to store default value in meta
320 delete_post_meta( $post_id, $meta_key );
321 }
322 }
323
324 }
325
326 /**
327 * Get powered cache meta keys
328 *
329 * @return array
330 * @since 2.1
331 */
332 private static function get_meta_keys() {
333 return [
334 POST_META_DISABLE_CACHE_KEY,
335 POST_META_DISABLE_LAZYLOAD_KEY,
336 POST_META_DISABLE_CRITICAL_CSS_KEY,
337 POST_META_SPECIFIC_CRITICAL_CSS_KEY,
338 POST_META_DISABLE_CSS_OPTIMIZATION,
339 POST_META_DISABLE_JS_OPTIMIZATION,
340 ];
341 }
342
343 /**
344 * Don't keep postmeta with default value. Block editor saves them?
345 *
346 * @param Object $post \WP_Post
347 * @param Object $request \WP_REST_Request
348 *
349 * @since 2.1
350 */
351 public function maybe_remove_default_meta( $post, $request ) {
352 if ( ! current_user_can( 'edit_post', $post->ID ) ) {
353 return;
354 }
355
356 $powered_cache_meta_keys = self::get_meta_keys();
357
358 foreach ( $powered_cache_meta_keys as $meta_key ) {
359 $meta_status = (bool) get_post_meta( $post->ID, $meta_key, true );
360 if ( ! $meta_status ) {
361 delete_post_meta( $post->ID, $meta_key );
362 }
363 }
364 }
365
366
367 }
368