| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | add_action( 'init', 'epl_meta_box_init' ); |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | function epl_meta_box_init() { |
| 26: | |
| 27: | global $epl_settings; |
| 28: | global $epl_meta_boxes; |
| 29: | |
| 30: | $opts_property_status = epl_get_property_status_opts(); |
| 31: | $opts_property_authority = epl_get_property_authority_opts(); |
| 32: | $opts_property_exclusivity = epl_get_property_exclusivity_opts(); |
| 33: | $opts_property_com_authority = epl_get_property_com_authority_opts(); |
| 34: | $opts_area_unit = epl_get_property_area_unit_opts(); |
| 35: | $opts_rent_period = epl_get_property_rent_period_opts(); |
| 36: | $opts_property_com_listing_type = epl_get_property_com_listing_type_opts(); |
| 37: | $opts_property_com_tenancy = epl_get_property_com_tenancy_opts(); |
| 38: | $opts_property_com_property_extent = epl_get_property_com_property_extent_opts(); |
| 39: | |
| 40: | $epl_meta_boxes = epl_get_meta_boxes(); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | function epl_add_meta_boxes() { |
| 50: | |
| 51: | global $epl_meta_boxes; |
| 52: | if ( ! empty( $epl_meta_boxes ) ) { |
| 53: | foreach ( $epl_meta_boxes as $epl_meta_box ) { |
| 54: | if ( is_array( $epl_meta_box['post_type'] ) ) { |
| 55: | foreach ( $epl_meta_box['post_type'] as $post_type ) { |
| 56: | add_meta_box( $epl_meta_box['id'], $epl_meta_box['label'], 'epl_meta_box_inner_custom_box', $post_type, $epl_meta_box['context'], $epl_meta_box['priority'], $epl_meta_box ); |
| 57: | } |
| 58: | } else { |
| 59: | add_meta_box( $epl_meta_box['id'], $epl_meta_box['label'], 'epl_meta_box_inner_custom_box', $epl_meta_box['post_type'], $epl_meta_box['context'], $epl_meta_box['priority'], $epl_meta_box ); |
| 60: | } |
| 61: | } |
| 62: | } |
| 63: | } |
| 64: | |
| 65: | add_action( 'add_meta_boxes', 'epl_add_meta_boxes' ); |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | function epl_meta_box_inner_custom_box( $post, $args ) { |
| 76: | |
| 77: | $groups = $args['args']['groups']; |
| 78: | $groups = array_filter( $groups ); |
| 79: | if ( ! empty( $groups ) ) { |
| 80: | wp_nonce_field( 'epl_inner_custom_box', 'epl_inner_custom_box_nonce' ); |
| 81: | foreach ( $groups as $group ) { ?> |
| 82: | <div class="epl-inner-div col-<?php echo esc_attr( $group['columns'] ); ?> table-<?php echo esc_attr( $args['args']['context'] ); ?>"> |
| 83: | <?php |
| 84: | $group['label'] = trim( $group['label'] ); |
| 85: | if ( ! empty( $group['label'] ) ) { |
| 86: | echo '<h3>' . esc_attr( $group['label'] ) . '</h3>'; |
| 87: | } |
| 88: | ?> |
| 89: | <ul class="form-table epl-form-table"> |
| 90: | <?php |
| 91: | $fields = $group['fields']; |
| 92: | $gp_field_width = isset( $group['width'] ) ? $group['width'] : '1'; |
| 93: | $fields = array_filter( $fields ); |
| 94: | if ( ! empty( $fields ) ) { |
| 95: | foreach ( $fields as $field ) { |
| 96: | if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) { |
| 97: | if ( in_array( $post->post_type, $field['exclude'], true ) ) { |
| 98: | continue; |
| 99: | } |
| 100: | } |
| 101: | |
| 102: | if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) { |
| 103: | if ( ! in_array( $post->post_type, $field['include'], true ) ) { |
| 104: | continue; |
| 105: | } |
| 106: | } |
| 107: | $val = get_post_meta( $post->ID, $field['name'], true ); |
| 108: | if ( has_action( 'epl_before_meta_field_' . $field['name'] ) ) { |
| 109: | do_action( 'epl_before_meta_field_' . $field['name'], $post, $val ); |
| 110: | } |
| 111: | |
| 112: | $field_width = isset( $field['width'] ) ? $field['width'] : $gp_field_width; |
| 113: | ?> |
| 114: | <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'] ); ?>"> |
| 115: | |
| 116: | |
| 117: | <?php if ( 'checkbox_single' !== $field['type'] || ( isset( $field['opts'] ) && 1 !== count( $field['opts'] ) ) ) : ?> |
| 118: | <div class="form-field epl-form-field-label"> |
| 119: | <span valign="top" scope="row"> |
| 120: | <label for="<?php echo esc_attr( $field['name'] ); ?>"><?php echo esc_attr( $field['label'] ); ?></label> |
| 121: | </span> |
| 122: | </div> |
| 123: | <?php endif; ?> |
| 124: | |
| 125: | |
| 126: | <div id="epl_<?php echo esc_attr( $field['name'] ); ?>" class="form-field epl-form-field-value epl_<?php echo esc_attr( $field['name'] ); ?>"> |
| 127: | <?php |
| 128: | epl_render_html_fields( $field, $val ); |
| 129: | ?> |
| 130: | </div> |
| 131: | |
| 132: | </li> |
| 133: | |
| 134: | <?php |
| 135: | if ( has_action( 'epl_after_meta_field_' . $field['name'] ) ) { |
| 136: | do_action( 'epl_after_meta_field_' . $field['name'], $post, $val ); |
| 137: | } |
| 138: | ?> |
| 139: | <?php |
| 140: | } |
| 141: | } |
| 142: | ?> |
| 143: | </ul> |
| 144: | </div> |
| 145: | <?php |
| 146: | } |
| 147: | ?> |
| 148: | <input type="hidden" name="epl_meta_box_ids[]" value="<?php echo esc_attr( $args['id'] ); ?>" /> |
| 149: | <div class="epl-clear"></div> |
| 150: | <?php |
| 151: | } |
| 152: | } |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | function epl_save_meta_boxes( $post_ID ) { |
| 165: | |
| 166: | if ( ! isset( $_POST['epl_inner_custom_box_nonce'] ) ) { |
| 167: | return $post_ID; |
| 168: | } |
| 169: | if ( ! wp_verify_nonce( sanitize_key( $_POST['epl_inner_custom_box_nonce'] ), 'epl_inner_custom_box' ) ) { |
| 170: | return $post_ID; |
| 171: | } |
| 172: | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 173: | return $post_ID; |
| 174: | } |
| 175: | if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) { |
| 176: | if ( ! current_user_can( 'edit_page', $post_ID ) ) { |
| 177: | return $post_ID; |
| 178: | } |
| 179: | } else { |
| 180: | if ( ! current_user_can( 'edit_post', $post_ID ) ) { |
| 181: | return $post_ID; |
| 182: | } |
| 183: | } |
| 184: | |
| 185: | $epl_meta_box_ids = ''; |
| 186: | if ( isset( $_POST['epl_meta_box_ids'] ) ) { |
| 187: | $epl_meta_box_ids = array_map( 'sanitize_text_field', wp_unslash( $_POST['epl_meta_box_ids'] ) ); |
| 188: | } |
| 189: | |
| 190: | if ( ! empty( $epl_meta_box_ids ) ) { |
| 191: | global $epl_meta_boxes; |
| 192: | if ( ! empty( $epl_meta_boxes ) ) { |
| 193: | $epl_enable_import_geocode = get_option( 'epl_enable_import_geocode' ); |
| 194: | foreach ( $epl_meta_box_ids as $epl_meta_box_id ) { |
| 195: | foreach ( $epl_meta_boxes as $epl_meta_box ) { |
| 196: | if ( $epl_meta_box['id'] === $epl_meta_box_id ) { |
| 197: | if ( ! empty( $epl_meta_box['groups'] ) ) { |
| 198: | foreach ( $epl_meta_box['groups'] as $group ) { |
| 199: | |
| 200: | $fields = $group['fields']; |
| 201: | if ( ! empty( $fields ) ) { |
| 202: | foreach ( $fields as $field ) { |
| 203: | |
| 204: | if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) { |
| 205: | if ( in_array( $_POST['post_type'], $field['exclude'], true ) ) { |
| 206: | continue; |
| 207: | } |
| 208: | } |
| 209: | |
| 210: | if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) { |
| 211: | if ( ! in_array( $_POST['post_type'], $field['include'], true ) ) { |
| 212: | continue; |
| 213: | } |
| 214: | } |
| 215: | |
| 216: | switch ( $field['type'] ) { |
| 217: | |
| 218: | case 'radio': |
| 219: | if ( ! isset( $_POST[ $field['name'] ] ) ) { |
| 220: | continue 2; |
| 221: | } |
| 222: | |
| 223: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 224: | |
| 225: | break; |
| 226: | |
| 227: | case 'checkbox_single': |
| 228: | if ( ! isset( $_POST[ $field['name'] ] ) ) { |
| 229: | $meta_value = ''; |
| 230: | } else { |
| 231: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 232: | } |
| 233: | |
| 234: | break; |
| 235: | |
| 236: | case 'number': |
| 237: | case 'decimal': |
| 238: | |
| 239: | if ( ! is_numeric( $_POST[ $field['name'] ] ) && ! empty( $_POST[ $field['name'] ] ) ) { |
| 240: | continue 2; |
| 241: | } |
| 242: | |
| 243: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 244: | |
| 245: | break; |
| 246: | |
| 247: | case 'textarea': |
| 248: | $meta_value = sanitize_textarea_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 249: | |
| 250: | break; |
| 251: | |
| 252: | case 'textarea_html': |
| 253: | $meta_value = epl_sanitize_html_tags( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 254: | |
| 255: | break; |
| 256: | |
| 257: | case 'url': |
| 258: | case 'file': |
| 259: | |
| 260: | $meta_value = esc_url_raw( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 261: | |
| 262: | break; |
| 263: | |
| 264: | case 'auction-date': |
| 265: | $meta_value = ''; |
| 266: | |
| 267: | if ( isset( $_POST[ $field['name'] ] ) ) { |
| 268: | |
| 269: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 270: | |
| 271: | if ( false !== strpos( $meta_value, 'T' ) ) { |
| 272: | $meta_value = gmdate( 'Y-m-d\TH:i', strtotime( $meta_value ) ); |
| 273: | } else { |
| 274: | $meta_value = DateTime::createFromFormat( 'Y-m-d-H:i:s', $meta_value ); |
| 275: | |
| 276: | if ( $meta_value ) { |
| 277: | $meta_value = $meta_value->format( 'Y-m-d\TH:i' ); |
| 278: | } |
| 279: | } |
| 280: | } |
| 281: | |
| 282: | break; |
| 283: | |
| 284: | case 'sold-date': |
| 285: | $meta_value = ''; |
| 286: | |
| 287: | if ( isset( $_POST[ $field['name'] ] ) ) { |
| 288: | |
| 289: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 290: | |
| 291: | if ( false !== strpos( $meta_value, 'T' ) ) { |
| 292: | $meta_value = gmdate( 'Y-m-d\TH:i', strtotime( $meta_value ) ); |
| 293: | } else { |
| 294: | $meta_value = DateTime::createFromFormat( 'Y-m-d', $meta_value ); |
| 295: | |
| 296: | if ( $meta_value ) { |
| 297: | $meta_value = $meta_value->format( 'Y-m-d' ); |
| 298: | } |
| 299: | } |
| 300: | } |
| 301: | |
| 302: | break; |
| 303: | |
| 304: | case 'editor': |
| 305: | $meta_value = wp_unslash( $_POST[ $field['name'] ] ) ; |
| 306: | |
| 307: | break; |
| 308: | |
| 309: | case 'checkbox': |
| 310: | case 'select_multiple': |
| 311: | $meta_value = ( array ) wp_unslash( $_POST[ $field['name'] ] ) ; |
| 312: | |
| 313: | break; |
| 314: | |
| 315: | default: |
| 316: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 317: | |
| 318: | break; |
| 319: | } |
| 320: | |
| 321: | if ( empty( $meta_value ) ) { |
| 322: | |
| 323: | if ( is_array( $_POST[ $field['name'] ] ) ) { |
| 324: | $meta_value = array_map( 'sanitize_text_field', wp_unslash( $_POST[ $field['name'] ] ) ); |
| 325: | } else { |
| 326: | $meta_value = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) ); |
| 327: | } |
| 328: | } |
| 329: | $meta_value = apply_filters( 'epl_field_save_meta_' . $field['name'], $meta_value ); |
| 330: | update_post_meta( $post_ID, $field['name'], $meta_value ); |
| 331: | } |
| 332: | } |
| 333: | } |
| 334: | } |
| 335: | } |
| 336: | } |
| 337: | } |
| 338: | } |
| 339: | } |
| 340: | } |
| 341: | add_action( 'save_post', 'epl_save_meta_boxes' ); |
| 342: | |