| 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: | function epl_register_default_contact_views( $views ) { |
| 25: | |
| 26: | $default_views = array( |
| 27: | 'overview' => 'epl_contacts_view', |
| 28: | 'delete' => 'epl_contacts_delete_view', |
| 29: | 'notes' => 'epl_contact_notes_view', |
| 30: | 'meta' => 'epl_contact_meta_view', |
| 31: | 'listings' => 'epl_contacts_listing_view', |
| 32: | 'new-contact' => 'epl_new_contact_view', |
| 33: | 'all_tags' => 'epl_all_tags_view', |
| 34: | ); |
| 35: | |
| 36: | return array_merge( $views, $default_views ); |
| 37: | } |
| 38: | add_filter( 'epl_contact_views', 'epl_register_default_contact_views', 1, 1 ); |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | function epl_register_default_contact_tabs( $tabs ) { |
| 48: | |
| 49: | $default_tabs = array( |
| 50: | 'overview' => array( |
| 51: | 'dashicon' => 'dashicons-admin-users', |
| 52: | 'title' => __( 'Contact Profile', 'easy-property-listings' ), |
| 53: | ), |
| 54: | 'meta' => array( |
| 55: | 'dashicon' => 'dashicons-edit', |
| 56: | 'title' => __( 'Contact Info', 'easy-property-listings' ), |
| 57: | ), |
| 58: | 'listings' => array( |
| 59: | 'dashicon' => 'dashicons-admin-home', |
| 60: | 'title' => __( 'Listings', 'easy-property-listings' ), |
| 61: | ), |
| 62: | 'notes' => array( |
| 63: | 'dashicon' => 'dashicons-admin-comments', |
| 64: | 'title' => __( 'Contact Notes', 'easy-property-listings' ), |
| 65: | ), |
| 66: | ); |
| 67: | |
| 68: | return array_merge( $tabs, $default_tabs ); |
| 69: | } |
| 70: | add_filter( 'epl_contact_tabs', 'epl_register_default_contact_tabs', 1, 1 ); |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | function epl_register_delete_contact_tab( $tabs ) { |
| 80: | |
| 81: | $tabs['delete'] = array( |
| 82: | 'dashicon' => 'dashicons-trash', |
| 83: | 'title' => __( 'Delete Contact', 'easy-property-listings' ), |
| 84: | ); |
| 85: | |
| 86: | return $tabs; |
| 87: | } |
| 88: | add_filter( 'epl_contact_tabs', 'epl_register_delete_contact_tab', PHP_INT_MAX, 1 ); |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | function epl_get_next_contact_link( $contact_id ) { |
| 100: | if ( absint( $contact_id ) < 1 ) { |
| 101: | return; |
| 102: | } |
| 103: | |
| 104: | $contact_id = absint( $contact_id ); |
| 105: | global $wpdb; |
| 106: | |
| 107: | $where = " `post_type` = 'epl_contact' |
| 108: | AND `post_status` = 'publish' |
| 109: | AND `ID` > {$contact_id} "; |
| 110: | |
| 111: | $where = apply_filters( 'epl_get_next_contact_link_query', $where ); |
| 112: | |
| 113: | $query = " |
| 114: | SELECT `ID` |
| 115: | FROM {$wpdb->prefix}posts |
| 116: | WHERE " . $where . ' |
| 117: | ORDER BY `ID` ASC |
| 118: | LIMIT 1 |
| 119: | '; |
| 120: | |
| 121: | $next = $wpdb->get_col( $query ); |
| 122: | |
| 123: | if ( ! empty( $next ) ) { |
| 124: | return admin_url( 'admin.php?page=epl-contacts&view=overview&id=' . $next[0] ); |
| 125: | } |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | function epl_get_prev_contact_link( $contact_id ) { |
| 138: | |
| 139: | if ( absint( $contact_id ) < 1 ) { |
| 140: | return; |
| 141: | } |
| 142: | |
| 143: | $contact_id = absint( $contact_id ); |
| 144: | global $wpdb; |
| 145: | |
| 146: | $where = " `post_type` = 'epl_contact' |
| 147: | AND `post_status` = 'publish' |
| 148: | AND `ID` < {$contact_id} "; |
| 149: | |
| 150: | $where = apply_filters( 'epl_get_prev_contact_link_query', $where ); |
| 151: | |
| 152: | $query = " |
| 153: | SELECT `ID` |
| 154: | FROM {$wpdb->prefix}posts |
| 155: | WHERE " . $where . ' |
| 156: | ORDER BY `ID` DESC |
| 157: | LIMIT 1 |
| 158: | '; |
| 159: | |
| 160: | $next = $wpdb->get_col( $query ); |
| 161: | |
| 162: | if ( ! empty( $next ) ) { |
| 163: | return admin_url( 'admin.php?page=epl-contacts&view=overview&id=' . $next[0] ); |
| 164: | } |
| 165: | } |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | function epl_contact_contact_fields( $contact_fields, $contact ) { |
| 177: | $fields = array(); |
| 178: | $phones = (array) $contact->get_meta( 'contact_phones' ); |
| 179: | $phones = array_filter( $phones ); |
| 180: | if ( ! empty( $phones ) ) { |
| 181: | foreach ( $phones as $phone_name => $phone_value ) { |
| 182: | $label = ucwords( str_replace( '_', ' ', $phone_name ) ); |
| 183: | $fields[] = array( |
| 184: | 'name' => "contact_phones[$phone_name]", |
| 185: | 'label' => $label, |
| 186: | 'type' => 'text', |
| 187: | 'class' => 'epl-contact-addable', |
| 188: | 'maxlength' => '60', |
| 189: | 'value' => $phone_value, |
| 190: | ); |
| 191: | } |
| 192: | } |
| 193: | $emails = (array) $contact->get_meta( 'contact_emails' ); |
| 194: | $emails = array_filter( $emails ); |
| 195: | if ( ! empty( $emails ) ) { |
| 196: | foreach ( $emails as $mail_name => $mail_value ) { |
| 197: | $label = ucwords( str_replace( '_', ' ', $mail_name ) ); |
| 198: | $fields[] = array( |
| 199: | 'name' => "contact_emails[$mail_name]", |
| 200: | 'label' => $label, |
| 201: | 'type' => 'email', |
| 202: | 'class' => 'epl-contact-addable-email', |
| 203: | 'maxlength' => '60', |
| 204: | 'value' => $mail_value, |
| 205: | ); |
| 206: | } |
| 207: | } |
| 208: | $merged_fields = array_merge( $fields, $contact_fields ); |
| 209: | return array_intersect_key( $merged_fields, array_unique( array_map( 'serialize', $merged_fields ) ) ); |
| 210: | } |
| 211: | add_filter( 'epl_contact_contact_fields', 'epl_contact_contact_fields', 10, 2 ); |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | function epl_contact_access() { |
| 219: | $allowed = epl_get_option( 'min_contact_access' ); |
| 220: | $allowed = empty( $allowed ) ? 'level_10' : $allowed; |
| 221: | $return = current_user_can( $allowed ) ? true : false; |
| 222: | return apply_filters( 'epl_contact_access', $return ); |
| 223: | } |
| 224: | |