| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. |
| 2 |
/** |
| 3 |
* |
| 4 |
* Metabox Class |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
* |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'CSF_Metabox' ) ) { |
| 11 |
class CSF_Metabox extends CSF_Abstract{ |
| 12 |
|
| 13 |
// constans |
| 14 |
public $unique = ''; |
| 15 |
public $abstract = 'metabox'; |
| 16 |
public $sections = array(); |
| 17 |
public $pre_fields = array(); |
| 18 |
public $post_type = array(); |
| 19 |
public $post_formats = array(); |
| 20 |
public $page_templates = array(); |
| 21 |
public $args = array( |
| 22 |
'title' => '', |
| 23 |
'post_type' => 'post', |
| 24 |
'data_type' => 'serialize', |
| 25 |
'context' => 'advanced', |
| 26 |
'priority' => 'default', |
| 27 |
'exclude_post_types' => array(), |
| 28 |
'page_templates' => '', |
| 29 |
'post_formats' => '', |
| 30 |
'show_reset' => false, |
| 31 |
'show_restore' => false, |
| 32 |
'enqueue_webfont' => true, |
| 33 |
'async_webfont' => false, |
| 34 |
'output_css' => true, |
| 35 |
'nav' => 'normal', |
| 36 |
'theme' => 'dark', |
| 37 |
'class' => '', |
| 38 |
'defaults' => array(), |
| 39 |
); |
| 40 |
|
| 41 |
// run metabox construct |
| 42 |
public function __construct( $key, $params = array() ) { |
| 43 |
|
| 44 |
$this->unique = $key; |
| 45 |
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this ); |
| 46 |
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this ); |
| 47 |
$this->post_type = ( is_array( $this->args['post_type'] ) ) ? $this->args['post_type'] : array_filter( (array) $this->args['post_type'] ); |
| 48 |
$this->post_formats = ( is_array( $this->args['post_formats'] ) ) ? $this->args['post_formats'] : array_filter( (array) $this->args['post_formats'] ); |
| 49 |
$this->page_templates = ( is_array( $this->args['page_templates'] ) ) ? $this->args['page_templates'] : array_filter( (array) $this->args['page_templates'] ); |
| 50 |
$this->pre_fields = $this->pre_fields( $this->sections ); |
| 51 |
|
| 52 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); |
| 53 |
add_action( 'save_post', array( $this, 'save_meta_box' ) ); |
| 54 |
add_action( 'edit_attachment', array( $this, 'save_meta_box' ) ); |
| 55 |
|
| 56 |
if ( ! empty( $this->page_templates ) || ! empty( $this->post_formats ) || ! empty( $this->args['class'] ) ) { |
| 57 |
foreach ( $this->post_type as $post_type ) { |
| 58 |
add_filter( 'postbox_classes_'. $post_type .'_'. $this->unique, array( $this, 'add_metabox_classes' ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// wp enqeueu for typography and output css |
| 63 |
parent::__construct(); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
// instance |
| 68 |
public static function instance( $key, $params = array() ) { |
| 69 |
return new self( $key, $params ); |
| 70 |
} |
| 71 |
|
| 72 |
public function add_metabox_classes( $classes ) { |
| 73 |
|
| 74 |
global $post; |
| 75 |
|
| 76 |
if ( ! empty( $this->post_formats ) ) { |
| 77 |
|
| 78 |
$saved_post_format = ( is_object( $post ) ) ? get_post_format( $post ) : false; |
| 79 |
$saved_post_format = ( ! empty( $saved_post_format ) ) ? $saved_post_format : 'default'; |
| 80 |
|
| 81 |
$classes[] = 'csf-post-formats'; |
| 82 |
|
| 83 |
// Sanitize post format for standard to default |
| 84 |
if ( ( $key = array_search( 'standard', $this->post_formats ) ) !== false ) { |
| 85 |
$this->post_formats[$key] = 'default'; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ( $this->post_formats as $format ) { |
| 89 |
$classes[] = 'csf-post-format-'. $format; |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! in_array( $saved_post_format, $this->post_formats ) ) { |
| 93 |
$classes[] = 'csf-metabox-hide'; |
| 94 |
} else { |
| 95 |
$classes[] = 'csf-metabox-show'; |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
if ( ! empty( $this->page_templates ) ) { |
| 101 |
|
| 102 |
$saved_template = ( is_object( $post ) && ! empty( $post->page_template ) ) ? $post->page_template : 'default'; |
| 103 |
|
| 104 |
$classes[] = 'csf-page-templates'; |
| 105 |
|
| 106 |
foreach ( $this->page_templates as $template ) { |
| 107 |
$classes[] = 'csf-page-'. preg_replace( '/[^a-zA-Z0-9]+/', '-', strtolower( $template ) ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! in_array( $saved_template, $this->page_templates ) ) { |
| 111 |
$classes[] = 'csf-metabox-hide'; |
| 112 |
} else { |
| 113 |
$classes[] = 'csf-metabox-show'; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
if ( ! empty( $this->args['class'] ) ) { |
| 119 |
$classes[] = $this->args['class']; |
| 120 |
} |
| 121 |
|
| 122 |
return $classes; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
// add metabox |
| 127 |
public function add_meta_box( $post_type ) { |
| 128 |
|
| 129 |
if ( ! in_array( $post_type, $this->args['exclude_post_types'] ) ) { |
| 130 |
add_meta_box( $this->unique, $this->args['title'], array( $this, 'add_meta_box_content' ), $this->post_type, $this->args['context'], $this->args['priority'], $this->args ); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
// get default value |
| 136 |
public function get_default( $field ) { |
| 137 |
|
| 138 |
$default = ( isset( $field['default'] ) ) ? $field['default'] : ''; |
| 139 |
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default; |
| 140 |
|
| 141 |
return $default; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
// get meta value |
| 146 |
public function get_meta_value( $field ) { |
| 147 |
|
| 148 |
global $post; |
| 149 |
|
| 150 |
$value = null; |
| 151 |
|
| 152 |
if ( is_object( $post ) && ! empty( $field['id'] ) ) { |
| 153 |
|
| 154 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 155 |
$meta = get_post_meta( $post->ID, $field['id'] ); |
| 156 |
$value = ( isset( $meta[0] ) ) ? $meta[0] : null; |
| 157 |
} else { |
| 158 |
$meta = get_post_meta( $post->ID, $this->unique, true ); |
| 159 |
$value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null; |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; |
| 165 |
$value = ( isset( $value ) ) ? $value : $default; |
| 166 |
|
| 167 |
return $value; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
// add metabox content |
| 172 |
public function add_meta_box_content( $post, $callback ) { |
| 173 |
|
| 174 |
global $post; |
| 175 |
|
| 176 |
$has_nav = ( count( $this->sections ) > 1 && $this->args['context'] !== 'side' ) ? true : false; |
| 177 |
$show_all = ( ! $has_nav ) ? ' csf-show-all' : ''; |
| 178 |
$post_type = ( is_object ( $post ) ) ? $post->post_type : ''; |
| 179 |
$errors = ( is_object ( $post ) ) ? get_post_meta( $post->ID, '_csf_errors_'. $this->unique, true ) : array(); |
| 180 |
$errors = ( ! empty( $errors ) ) ? $errors : array(); |
| 181 |
$theme = ( $this->args['theme'] ) ? ' csf-theme-'. $this->args['theme'] : ''; |
| 182 |
$nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal'; |
| 183 |
|
| 184 |
if ( is_object ( $post ) && ! empty( $errors ) ) { |
| 185 |
delete_post_meta( $post->ID, '_csf_errors_'. $this->unique ); |
| 186 |
} |
| 187 |
|
| 188 |
wp_nonce_field( 'csf_metabox_nonce', 'csf_metabox_nonce'. $this->unique ); |
| 189 |
|
| 190 |
echo '<div class="csf csf-metabox'. esc_attr( $theme ) .'">'; |
| 191 |
|
| 192 |
echo '<div class="csf-wrapper'. esc_attr( $show_all ) .'">'; |
| 193 |
|
| 194 |
if ( $has_nav ) { |
| 195 |
|
| 196 |
echo '<div class="csf-nav csf-nav-'. esc_attr( $nav_type ) .' csf-nav-metabox">'; |
| 197 |
|
| 198 |
echo '<ul>'; |
| 199 |
|
| 200 |
$tab_key = 0; |
| 201 |
|
| 202 |
foreach ( $this->sections as $section ) { |
| 203 |
|
| 204 |
if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
$tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="csf-label-error csf-error">!</i>' : ''; |
| 209 |
$tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : ''; |
| 210 |
|
| 211 |
echo '<li><a href="#">'. $tab_icon . $section['title'] . $tab_error .'</a></li>'; |
| 212 |
|
| 213 |
$tab_key++; |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
echo '</ul>'; |
| 218 |
|
| 219 |
echo '</div>'; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
echo '<div class="csf-content">'; |
| 224 |
|
| 225 |
echo '<div class="csf-sections">'; |
| 226 |
|
| 227 |
$section_key = 0; |
| 228 |
|
| 229 |
foreach ( $this->sections as $section ) { |
| 230 |
|
| 231 |
if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
$section_onload = ( ! $has_nav ) ? ' csf-onload' : ''; |
| 236 |
$section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : ''; |
| 237 |
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; |
| 238 |
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : ''; |
| 239 |
|
| 240 |
echo '<div class="csf-section hidden'. esc_attr( $section_onload . $section_class ) .'">'; |
| 241 |
|
| 242 |
echo ( $section_title || $section_icon ) ? '<div class="csf-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : ''; |
| 243 |
echo ( ! empty( $section['description'] ) ) ? '<div class="csf-field csf-section-description">'. $section['description'] .'</div>' : ''; |
| 244 |
|
| 245 |
if ( ! empty( $section['fields'] ) ) { |
| 246 |
|
| 247 |
foreach ( $section['fields'] as $field ) { |
| 248 |
|
| 249 |
if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) { |
| 250 |
$field['_error'] = $errors['fields'][$field['id']]; |
| 251 |
} |
| 252 |
|
| 253 |
if ( ! empty( $field['id'] ) ) { |
| 254 |
$field['default'] = $this->get_default( $field ); |
| 255 |
} |
| 256 |
|
| 257 |
CSF::field( $field, $this->get_meta_value( $field ), $this->unique, 'metabox' ); |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
} else { |
| 262 |
|
| 263 |
echo '<div class="csf-no-option">'. esc_html__( 'No data available.', 'csf' ) .'</div>'; |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
echo '</div>'; |
| 268 |
|
| 269 |
$section_key++; |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
echo '</div>'; |
| 274 |
|
| 275 |
if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) { |
| 276 |
|
| 277 |
echo '<div class="csf-sections-reset">'; |
| 278 |
echo '<label>'; |
| 279 |
echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />'; |
| 280 |
echo '<span class="button csf-button-reset">'. esc_html__( 'Reset', 'csf' ) .'</span>'; |
| 281 |
echo '<span class="button csf-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'csf' ), esc_html__( 'Cancel', 'csf' ) ) .'</span>'; |
| 282 |
echo '</label>'; |
| 283 |
echo '</div>'; |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
echo '</div>'; |
| 288 |
|
| 289 |
echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="csf-nav-background"></div>' : ''; |
| 290 |
|
| 291 |
echo '<div class="clear"></div>'; |
| 292 |
|
| 293 |
echo '</div>'; |
| 294 |
|
| 295 |
echo '</div>'; |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
// save metabox |
| 300 |
public function save_meta_box( $post_id ) { |
| 301 |
|
| 302 |
$count = 1; |
| 303 |
$data = array(); |
| 304 |
$errors = array(); |
| 305 |
$noncekey = 'csf_metabox_nonce'. $this->unique; |
| 306 |
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : ''; |
| 307 |
|
| 308 |
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'csf_metabox_nonce' ) ) { |
| 309 |
return $post_id; |
| 310 |
} |
| 311 |
|
| 312 |
// XSS ok. |
| 313 |
// No worries, This "POST" requests is sanitizing in the below foreach. |
| 314 |
$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array(); |
| 315 |
|
| 316 |
if ( ! empty( $request ) ) { |
| 317 |
|
| 318 |
foreach ( $this->sections as $section ) { |
| 319 |
|
| 320 |
if ( ! empty( $section['fields'] ) ) { |
| 321 |
|
| 322 |
foreach ( $section['fields'] as $field ) { |
| 323 |
|
| 324 |
if ( ! empty( $field['id'] ) ) { |
| 325 |
|
| 326 |
$field_id = $field['id']; |
| 327 |
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : ''; |
| 328 |
|
| 329 |
// Sanitize "post" request of field. |
| 330 |
if ( ! isset( $field['sanitize'] ) ) { |
| 331 |
|
| 332 |
if( is_array( $field_value ) ) { |
| 333 |
$data[$field_id] = wp_kses_post_deep( $field_value ); |
| 334 |
} else { |
| 335 |
$data[$field_id] = wp_kses_post( $field_value ); |
| 336 |
} |
| 337 |
|
| 338 |
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { |
| 339 |
|
| 340 |
$data[$field_id] = call_user_func( $field['sanitize'], $field_value ); |
| 341 |
|
| 342 |
} else { |
| 343 |
|
| 344 |
$data[$field_id] = $field_value; |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
// Validate "post" request of field. |
| 349 |
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { |
| 350 |
|
| 351 |
$has_validated = call_user_func( $field['validate'], $field_value ); |
| 352 |
|
| 353 |
if ( ! empty( $has_validated ) ) { |
| 354 |
|
| 355 |
$errors['sections'][$count] = true; |
| 356 |
$errors['fields'][$field_id] = $has_validated; |
| 357 |
$data[$field_id] = $this->get_meta_value( $field ); |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
$count++; |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
$data = apply_filters( "csf_{$this->unique}_save", $data, $post_id, $this ); |
| 376 |
|
| 377 |
do_action( "csf_{$this->unique}_save_before", $data, $post_id, $this ); |
| 378 |
|
| 379 |
if ( empty( $data ) || ! empty( $request['_reset'] ) ) { |
| 380 |
|
| 381 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 382 |
foreach ( $this->pre_fields as $field ) { |
| 383 |
if ( ! empty( $field['id'] ) ) { |
| 384 |
delete_post_meta( $post_id, $field['id'] ); |
| 385 |
} |
| 386 |
} |
| 387 |
} else { |
| 388 |
delete_post_meta( $post_id, $this->unique ); |
| 389 |
} |
| 390 |
|
| 391 |
} else { |
| 392 |
|
| 393 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 394 |
foreach ( $data as $key => $value ) { |
| 395 |
update_post_meta( $post_id, $key, $value ); |
| 396 |
} |
| 397 |
} else { |
| 398 |
update_post_meta( $post_id, $this->unique, $data ); |
| 399 |
} |
| 400 |
|
| 401 |
if ( ! empty( $errors ) ) { |
| 402 |
update_post_meta( $post_id, '_csf_errors_'. $this->unique, $errors ); |
| 403 |
} |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
do_action( "csf_{$this->unique}_saved", $data, $post_id, $this ); |
| 408 |
|
| 409 |
do_action( "csf_{$this->unique}_save_after", $data, $post_id, $this ); |
| 410 |
|
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|