| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | class EPL_METABOX { |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | protected $epl_meta_boxes; |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | protected $prefix; |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | protected $text_domain; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | public function __construct( $epl_meta_boxes, $prefix = 'epl_', $text_domain = 'easy-property-listings' ) { |
| 64: | |
| 65: | $this->epl_meta_boxes = $epl_meta_boxes; |
| 66: | |
| 67: | $this->prefix = (string) $prefix; |
| 68: | |
| 69: | $this->text_domain = (string) $text_domain; |
| 70: | |
| 71: | |
| 72: | $this->add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) ); |
| 73: | |
| 74: | |
| 75: | $this->add_action( 'save_post', array( &$this, 'save_meta_box' ) ); |
| 76: | } |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | public function add_action( $action, $callback_function, $priority = 10, $accepted_args = 1 ) { |
| 89: | |
| 90: | |
| 91: | add_action( $action, $callback_function, $priority, $accepted_args ); |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | public function add_filter( $action, $callback_function, $priority = 10, $accepted_args = 1 ) { |
| 107: | |
| 108: | |
| 109: | add_filter( $action, $callback_function, $priority, $accepted_args ); |
| 110: | } |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | public function add_meta_boxes() { |
| 120: | |
| 121: | if ( ! empty( $this->epl_meta_boxes ) ) { |
| 122: | foreach ( $this->epl_meta_boxes as $epl_meta_box ) { |
| 123: | |
| 124: | |
| 125: | if ( isset( $epl_meta_box['id'] ) && is_array( $epl_meta_box ) ) { |
| 126: | |
| 127: | if ( is_array( $epl_meta_box['post_type'] ) ) { |
| 128: | foreach ( $epl_meta_box['post_type'] as $post_type ) { |
| 129: | $this->add_meta_box( |
| 130: | $epl_meta_box['id'], |
| 131: | $epl_meta_box['label'], |
| 132: | 'inner_meta_box', |
| 133: | $post_type, |
| 134: | $epl_meta_box['context'], |
| 135: | $epl_meta_box['priority'], |
| 136: | $epl_meta_box |
| 137: | ); |
| 138: | } |
| 139: | } else { |
| 140: | $this->add_meta_box( |
| 141: | $epl_meta_box['id'], |
| 142: | $epl_meta_box['label'], |
| 143: | 'inner_meta_box', |
| 144: | $epl_meta_box['post_type'], |
| 145: | $epl_meta_box['context'], |
| 146: | $epl_meta_box['priority'], |
| 147: | $epl_meta_box |
| 148: | ); |
| 149: | } |
| 150: | } else { |
| 151: | |
| 152: | $epl_meta_box = $this->epl_meta_boxes; |
| 153: | |
| 154: | if ( is_array( $epl_meta_box['post_type'] ) ) { |
| 155: | foreach ( $epl_meta_box['post_type'] as $post_type ) { |
| 156: | $this->add_meta_box( |
| 157: | $epl_meta_box['id'], |
| 158: | $epl_meta_box['label'], |
| 159: | 'inner_meta_box', |
| 160: | $post_type, |
| 161: | $epl_meta_box['context'], |
| 162: | $epl_meta_box['priority'], |
| 163: | $epl_meta_box |
| 164: | ); |
| 165: | } |
| 166: | } else { |
| 167: | $this->add_meta_box( |
| 168: | $epl_meta_box['id'], |
| 169: | $epl_meta_box['label'], |
| 170: | 'inner_meta_box', |
| 171: | $epl_meta_box['post_type'], |
| 172: | $epl_meta_box['context'], |
| 173: | $epl_meta_box['priority'], |
| 174: | $epl_meta_box |
| 175: | ); |
| 176: | } |
| 177: | break; |
| 178: | } |
| 179: | } |
| 180: | } |
| 181: | } |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | public function add_meta_box( $id = '', $label = '', $func = 'inner_meta_box', $post_type = array(), $context = 'normal', $priority = 'default', $args = null ) { |
| 206: | add_meta_box( |
| 207: | $id, |
| 208: | $label, |
| 209: | array( $this, $func ), |
| 210: | $post_type, |
| 211: | $context, |
| 212: | $priority, |
| 213: | $args |
| 214: | ); |
| 215: | } |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | public function inner_meta_box( $post, $args ) { |
| 226: | $groups = $args['args']['groups']; |
| 227: | $groups = array_filter( $groups ); |
| 228: | $render_as = empty( $args['args']['render_as'] ) ? 'default' : sanitize_text_field( $args['args']['render_as'] ); |
| 229: | |
| 230: | if ( ! empty( $groups ) ) { |
| 231: | wp_nonce_field( $this->prefix . 'inner_custom_box', $this->prefix . 'inner_custom_box_nonce' ); |
| 232: | if ( in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) { |
| 233: | |
| 234: | echo '<div class="epl-group-tab-wrapper epl-' . esc_attr( $render_as ) . '">'; |
| 235: | } |
| 236: | if ( in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) { ?> |
| 237: | <ul class="epl-group-tabs"> |
| 238: | <?php |
| 239: | foreach ( $groups as $group ) { |
| 240: | echo '<li><a href="#epl-field-group-' . esc_attr( $group['id'] ) . '"><h3>' . esc_attr( $group['label'] ) . '</h3></a></li>'; |
| 241: | } |
| 242: | ?> |
| 243: | </ul> |
| 244: | <?php |
| 245: | } |
| 246: | foreach ( $groups as $group ) { |
| 247: | ?> |
| 248: | <div id="epl-field-group-<?php echo esc_attr( $group['id'] ); ?>"> |
| 249: | <div class="epl-inner-div col-<?php echo esc_attr( $group['columns'] ); ?> table-<?php echo esc_attr( $args['args']['context'] ); ?>"> |
| 250: | <?php |
| 251: | $group['label'] = trim( $group['label'] ); |
| 252: | if ( ! empty( $group['label'] ) && ! in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) { |
| 253: | echo '<h3>' . esc_attr( $group['label'] ) . '</h3>'; |
| 254: | } |
| 255: | ?> |
| 256: | <ul class="form-table epl-form-table"> |
| 257: | <?php |
| 258: | $fields = $group['fields']; |
| 259: | $gp_field_width = isset( $group['width'] ) ? $group['width'] : '1'; |
| 260: | $fields = array_filter( $fields ); |
| 261: | if ( ! empty( $fields ) ) { |
| 262: | foreach ( $fields as $field ) { |
| 263: | if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) { |
| 264: | if ( in_array( $post->post_type, $field['exclude'], true ) ) { |
| 265: | continue; |
| 266: | } |
| 267: | } |
| 268: | |
| 269: | if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) { |
| 270: | if ( ! in_array( $post->post_type, $field['include'], true ) ) { |
| 271: | continue; |
| 272: | } |
| 273: | } |
| 274: | $val = get_post_meta( $post->ID, $field['name'], true ); |
| 275: | if ( has_action( 'epl_before_meta_field_' . $field['name'] ) ) { |
| 276: | do_action( 'epl_before_meta_field_' . $field['name'], $post, $val ); |
| 277: | } |
| 278: | |
| 279: | $field_width = isset( $field['width'] ) ? $field['width'] : $gp_field_width; |
| 280: | ?> |
| 281: | <li id="epl_<?php echo esc_attr( $field['name'] ); ?>_wrap" class="epl-form-field-wrap epl-grid-<?php echo esc_attr( $field_width ); ?> epl_<?php echo esc_attr( $field['name'] ); ?>_wrap epl-field-type-<?php echo esc_attr( $field['type'] ); ?>"> |
| 282: | |
| 283: | |
| 284: | <?php if ( 'checkbox_single' !== $field['type'] || ( isset( $field['opts'] ) && 1 !== count( $field['opts'] ) ) ) : ?> |
| 285: | <div class="form-field epl-form-field-label"> |
| 286: | <span valign="top" scope="row"> |
| 287: | <label for="<?php echo esc_attr( $field['name'] ); ?>"><?php echo esc_attr( $field['label'] ); ?></label> |
| 288: | </span> |
| 289: | </div> |
| 290: | <?php endif; ?> |
| 291: | |
| 292: | |
| 293: | <div id="epl_<?php echo esc_attr( $field['name'] ); ?>" class="form-field epl-form-field-value epl_<?php echo esc_attr( $field['name'] ); ?>"> |
| 294: | <?php |
| 295: | epl_render_html_fields( $field, $val ); |
| 296: | ?> |
| 297: | </div> |
| 298: | |
| 299: | </li> |
| 300: | |
| 301: | <?php |
| 302: | if ( has_action( 'epl_after_meta_field_' . $field['name'] ) ) { |
| 303: | do_action( 'epl_after_meta_field_' . $field['name'], $post, $val ); |
| 304: | } |
| 305: | ?> |
| 306: | <?php |
| 307: | } |
| 308: | } |
| 309: | ?> |
| 310: | </ul> |
| 311: | </div> |
| 312: | </div> |
| 313: | <?php |
| 314: | } |
| 315: | ?> |
| 316: | <input type="hidden" name="epl_meta_box_ids[]" value="<?php echo esc_attr( $args['id'] ); ?>" /> |
| 317: | <div class="epl-clear"></div> |
| 318: | <?php |
| 319: | if ( in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) { |
| 320: | |
| 321: | echo '</div>'; |
| 322: | } |
| 323: | } |
| 324: | } |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | public function save_meta_box( $post_ID ) { |
| 338: | |
| 339: | if ( ! isset( $_POST[ $this->prefix . 'inner_custom_box_nonce' ] ) ) { |
| 340: | return $post_ID; |
| 341: | } |
| 342: | |
| 343: | $nonce = $_POST[ $this->prefix . 'inner_custom_box_nonce' ]; |
| 344: | |
| 345: | if ( ! wp_verify_nonce( $nonce, $this->prefix . 'inner_custom_box' ) ) { |
| 346: | return $post_ID; |
| 347: | } |
| 348: | |
| 349: | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 350: | return $post_ID; |
| 351: | } |
| 352: | |
| 353: | if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) { |
| 354: | if ( ! current_user_can( 'edit_page', $post_ID ) ) { |
| 355: | return $post_ID; |
| 356: | } |
| 357: | } else { |
| 358: | if ( ! current_user_can( 'edit_post', $post_ID ) ) { |
| 359: | return $post_ID; |
| 360: | } |
| 361: | } |
| 362: | |
| 363: | $epl_meta_box_ids = ''; |
| 364: | if ( isset( $_POST['epl_meta_box_ids'] ) ) { |
| 365: | $epl_meta_box_ids = wp_unslash( $_POST['epl_meta_box_ids'] ); |
| 366: | } |
| 367: | |
| 368: | if ( ! empty( $epl_meta_box_ids ) ) { |
| 369: | if ( ! empty( $this->epl_meta_boxes ) ) { |
| 370: | foreach ( $epl_meta_box_ids as $epl_meta_box_id ) { |
| 371: | foreach ( $this->epl_meta_boxes as $epl_meta_box ) { |
| 372: | if ( $epl_meta_box['id'] === $epl_meta_box_id ) { |
| 373: | if ( ! empty( $epl_meta_box['groups'] ) ) { |
| 374: | foreach ( $epl_meta_box['groups'] as $group ) { |
| 375: | |
| 376: | $fields = $group['fields']; |
| 377: | if ( ! empty( $fields ) ) { |
| 378: | foreach ( $fields as $field ) { |
| 379: | |
| 380: | |
| 381: | if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) { |
| 382: | if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], $field['exclude'], true ) ) { |
| 383: | continue; |
| 384: | } |
| 385: | } |
| 386: | |
| 387: | |
| 388: | if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) { |
| 389: | if ( ! in_array( $_POST['post_type'], $field['include'] ) ) { |
| 390: | continue; |
| 391: | } |
| 392: | } |
| 393: | |
| 394: | if ( 'radio' === $field['type'] ) { |
| 395: | if ( ! isset( $_POST[ $field['name'] ] ) ) { |
| 396: | continue; |
| 397: | } |
| 398: | } elseif ( 'checkbox_single' === $field['type'] ) { |
| 399: | if ( ! isset( $_POST[ $field['name'] ] ) ) { |
| 400: | $_POST[ $field['name'] ] = ''; |
| 401: | } |
| 402: | } elseif ( in_array( $field['type'], array( 'number', 'decimal' ), true ) ) { |
| 403: | |
| 404: | |
| 405: | if ( ! is_numeric( $_POST[ $field['name'] ] ) && ! empty( $_POST[ $field['name'] ] ) ) { |
| 406: | continue; |
| 407: | } |
| 408: | } elseif ( in_array( $field['type'], array( 'textarea' ), true ) ) { |
| 409: | |
| 410: | if ( function_exists( 'sanitize_textarea_field' ) ) { |
| 411: | $_POST[ $field['name'] ] = |
| 412: | sanitize_textarea_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 413: | } |
| 414: | } elseif ( in_array( $field['type'], array( 'url', 'file' ), true ) ) { |
| 415: | |
| 416: | |
| 417: | $_POST[ $field['name'] ] = esc_url_raw( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 418: | |
| 419: | } elseif ( 'auction-date' === $field['type'] && ! empty( $_POST[ $field['name'] ] ) ) { |
| 420: | $epl_date = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 421: | if ( strpos( $epl_date, 'T' ) !== false ) { |
| 422: | $epl_date = gmdate( 'Y-m-d\TH:i', strtotime( $epl_date ) ); |
| 423: | } else { |
| 424: | $epl_date = DateTime::createFromFormat( 'Y-m-d-H:i:s', $epl_date ); |
| 425: | |
| 426: | if ( $epl_date ) { |
| 427: | $epl_date = $epl_date->format( 'Y-m-d\TH:i' ); |
| 428: | } |
| 429: | } |
| 430: | $_POST[ $field['name'] ] = $epl_date; |
| 431: | } elseif ( 'sold-date' === $field['type'] && ! empty( $_POST[ $field['name'] ] ) ) { |
| 432: | $epl_date = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 433: | if ( strpos( $epl_date, 'T' ) !== false ) { |
| 434: | $epl_date = gmdate( 'Y-m-d\TH:i', strtotime( $epl_date ) ); |
| 435: | } else { |
| 436: | $epl_date = DateTime::createFromFormat( 'Y-m-d', $epl_date ); |
| 437: | |
| 438: | if ( $epl_date ) { |
| 439: | $epl_date = $epl_date->format( 'Y-m-d' ); |
| 440: | } |
| 441: | } |
| 442: | $_POST[ $field['name'] ] = $epl_date; |
| 443: | } |
| 444: | if ( isset( $_POST[ $field['name'] ] ) ) { |
| 445: | $meta_value = wp_unslash( $_POST[ $field['name'] ] ); |
| 446: | update_post_meta( $post_ID, $field['name'], $meta_value ); |
| 447: | } |
| 448: | } |
| 449: | } |
| 450: | } |
| 451: | } |
| 452: | } |
| 453: | } |
| 454: | } |
| 455: | } |
| 456: | } |
| 457: | } |
| 458: | } |
| 459: | |