1: <?php
2: /**
3: * Contact Functions
4: *
5: * @package EPL
6: * @subpackage Admin/ContactsFunctions
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Register a view for the single contact view
19: *
20: * @since 3.0
21: * @param array $views An array of existing views.
22: * @return array The altered list of views
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: * Register a tab for the single contact view
42: *
43: * @since 3.0
44: * @param array $tabs An array of existing tabs.
45: * @return array The altered list of tabs
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: * Register the Delete icon as late as possible so it's at the bottom
74: *
75: * @since 3.0
76: * @param array $tabs An array of existing tabs.
77: * @return array The altered list of tabs, with 'delete' at the bottom
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: * Next contact link
92: *
93: * @param [type] $contact_id Contact ID.
94: *
95: * @return string|void
96: * @since 3.0
97: * @since 3.6 Fix: Linked to the newest contact instead of the next one (sorted highest ID first). Selects only the ID.
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: // phpcs:ignore WordPress.DB
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: * Previous contact link
130: *
131: * @param [type] $contact_id Contact ID.
132: *
133: * @return string|void
134: * @since 3.0
135: * @since 3.6 Selects only the ID.
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: // phpcs:ignore WordPress.DB
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: * Contact Fields
169: *
170: * @param array $contact_fields Contact fields.
171: * @param array $contact The contact.
172: *
173: * @return array
174: * @since 3.0
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: * Contact Access Roles
215: *
216: * @since 3.0
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: