| 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_CACHE_KEY; |
| 11 |
use const PoweredCache\Constants\POST_META_DISABLE_LAZYLOAD_KEY; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class MetaBox |
| 15 |
*/ |
| 16 |
class MetaBox { |
| 17 |
|
| 18 |
/** |
| 19 |
* Placeholder constructor |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Return an instance of the current class |
| 26 |
* |
| 27 |
* @return MetaBox |
| 28 |
* @since 2.0 |
| 29 |
*/ |
| 30 |
public static function factory() { |
| 31 |
static $instance = false; |
| 32 |
|
| 33 |
if ( ! $instance ) { |
| 34 |
$instance = new self(); |
| 35 |
$instance->setup(); |
| 36 |
} |
| 37 |
|
| 38 |
return $instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Setup hooks |
| 43 |
*/ |
| 44 |
public function setup() { |
| 45 |
add_action( 'init', [ $this, 'register_meta_field' ] ); |
| 46 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 47 |
add_action( 'save_post', [ $this, 'save_post_meta' ], 10, 2 ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Add metabox |
| 52 |
*/ |
| 53 |
public function add_meta_boxes() { |
| 54 |
$is_block_editor_compatible = false; |
| 55 |
$back_compat = true; |
| 56 |
|
| 57 |
if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) { |
| 58 |
$is_block_editor_compatible = true; |
| 59 |
$back_compat = false; |
| 60 |
} |
| 61 |
|
| 62 |
add_meta_box( |
| 63 |
'powered_cache_post_meta', |
| 64 |
esc_html__( 'Powered Cache', 'powered-cache' ), |
| 65 |
[ $this, 'meta_box' ], |
| 66 |
'', |
| 67 |
'side', |
| 68 |
'high', |
| 69 |
[ |
| 70 |
'__block_editor_compatible_meta_box' => $is_block_editor_compatible, |
| 71 |
'__back_compat_meta_box' => $back_compat, |
| 72 |
] |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Display metabox output for classic editor |
| 78 |
* |
| 79 |
* @param Object $post \WP_Post |
| 80 |
*/ |
| 81 |
public function meta_box( $post ) { |
| 82 |
$settings = \PoweredCache\Utils\get_settings(); |
| 83 |
$is_cache_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_CACHE_KEY, true ); |
| 84 |
|
| 85 |
if ( ! $settings['enable_page_cache'] && ! $settings['enable_lazy_load'] ) { |
| 86 |
return; // nothing to control post specific |
| 87 |
} |
| 88 |
?> |
| 89 |
<div id="powered-cache-meta-box"> |
| 90 |
<?php wp_nonce_field( 'powered_cache_post_meta', 'powered_cache_post_meta_nonce' ); ?> |
| 91 |
<?php if ( $settings['enable_page_cache'] ) : ?> |
| 92 |
<fieldset> |
| 93 |
<legend class="screen-reader-text"><?php esc_html_e( 'Powered Cache Status', 'powered-cache' ); ?></legend> |
| 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"> |
| 95 |
<label for="<?php echo esc_attr( POST_META_DISABLE_CACHE_KEY ); ?>"><?php esc_html_e( 'Don\'t cache this post', 'powered-cache' ); ?></label> |
| 96 |
</fieldset> |
| 97 |
<?php endif; ?> |
| 98 |
<?php if ( $settings['enable_lazy_load'] ) : ?> |
| 99 |
<?php $is_lazyload_disabled = (bool) get_post_meta( $post->ID, POST_META_DISABLE_LAZYLOAD_KEY, true ); ?> |
| 100 |
<fieldset> |
| 101 |
<legend class="screen-reader-text"><?php esc_html_e( 'Disable Lazy Load', 'powered-cache' ); ?></legend> |
| 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"> |
| 103 |
<label for="<?php echo esc_attr( POST_META_DISABLE_LAZYLOAD_KEY ); ?>"><?php esc_html_e( 'Disable lazy loading for this post', 'powered-cache' ); ?></label> |
| 104 |
</fieldset> |
| 105 |
<?php endif; ?> |
| 106 |
</div> |
| 107 |
<?php |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Register meta field for block editor |
| 113 |
*/ |
| 114 |
public function register_meta_field() { |
| 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 |
); |
| 124 |
|
| 125 |
foreach ( (array) $public_posts_types as $post_type ) { |
| 126 |
if ( $settings['enable_page_cache'] ) { |
| 127 |
register_post_meta( |
| 128 |
$post_type, |
| 129 |
POST_META_DISABLE_CACHE_KEY, |
| 130 |
[ |
| 131 |
'show_in_rest' => true, |
| 132 |
'single' => true, |
| 133 |
'type' => 'boolean', |
| 134 |
'auth_callback' => function () { |
| 135 |
return current_user_can( 'edit_posts' ); |
| 136 |
}, |
| 137 |
] |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
if ( $settings['enable_lazy_load'] ) { |
| 142 |
register_post_meta( |
| 143 |
$post_type, |
| 144 |
POST_META_DISABLE_LAZYLOAD_KEY, |
| 145 |
[ |
| 146 |
'show_in_rest' => true, |
| 147 |
'single' => true, |
| 148 |
'type' => 'boolean', |
| 149 |
'auth_callback' => function () { |
| 150 |
return current_user_can( 'edit_posts' ); |
| 151 |
}, |
| 152 |
] |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Save meta info |
| 161 |
* |
| 162 |
* @param int $post_id Post ID |
| 163 |
* @param Object $post \WP_Post |
| 164 |
*/ |
| 165 |
public function save_post_meta( $post_id, $post ) { |
| 166 |
$post_id = absint( $post_id ); |
| 167 |
|
| 168 |
if ( empty( $post_id ) || empty( $post ) ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// saved in a separate request in the block editor |
| 177 |
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
// Check user has permission to edit. |
| 182 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
if ( empty( $_POST['powered_cache_post_meta_nonce'] ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
// nonce check |
| 191 |
if ( ! wp_verify_nonce( wp_unslash( $_POST['powered_cache_post_meta_nonce'] ), 'powered_cache_post_meta' ) ) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 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 ); |
| 201 |
} |
| 202 |
|
| 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 ); |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
} |
| 215 |
|