1: <?php
2: /**
3: * Contact Object
4: *
5: * @package EPL
6: * @subpackage Classes/Contact
7: * @copyright Copyright (c) 2020, 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: // phpcs:disable WordPress.DB.SlowDBQuery
18:
19: /**
20: * EPL_Contact Class
21: *
22: * @since 3.0.0
23: */
24: class EPL_Contact {
25:
26: /**
27: * The contact ID
28: *
29: * @var int $ID Contact ID.
30: * @since 3.0.0
31: */
32: public $ID = 0;
33:
34: /**
35: * The contact ID Fallback
36: *
37: * @var int $id Contact ID.
38: * @since 3.0.0
39: */
40: public $id = 0;
41:
42: /**
43: * The contact's listing count
44: *
45: * @var int $listing_count Contact listing count.
46: * @since 3.0.0
47: */
48: public $listing_count = 0;
49:
50: /**
51: * The contact's email
52: *
53: * @var string $email Contact email address.
54: * @since 3.0.0
55: */
56: public $email;
57:
58: /**
59: * The contact's name
60: *
61: * @var string $name Contact name.
62: * @since 3.0.0
63: */
64: public $name;
65:
66: /**
67: * The contact's creation date
68: *
69: * @var string $date_created Contact date created.
70: * @since 3.0.0
71: */
72: public $date_created;
73:
74: /**
75: * The contact's background info
76: *
77: * @var string $background_info.
78: * @since 3.5.0
79: */
80: public $background_info;
81:
82: /**
83: * The contact's type
84: *
85: * @var string $type.
86: * @since 3.5.0
87: */
88: public $type;
89:
90: /**
91: * The contact's heading
92: *
93: * @var string $heading.
94: * @since 3.5.0
95: */
96: public $heading;
97:
98: /**
99: * The listing IDs associated with the contact
100: *
101: * @var string $listing_ids Contact associated listing ids.
102: * @since 3.0
103: */
104: public $listing_ids;
105:
106: /**
107: * Contact Notes
108: *
109: * @var string notes Contact notes.
110: * @since 3.0
111: */
112: public $notes;
113:
114: /**
115: * Get things going
116: *
117: * @param bool $_id_or_email Contact ID or email.
118: * @since 3.0.0
119: */
120: public function __construct( $_id_or_email = false ) {
121:
122: if ( false === $_id_or_email || ( is_numeric( $_id_or_email ) && absint( $_id_or_email ) !== (int) $_id_or_email ) ) {
123: return false;
124: }
125:
126: if ( is_numeric( $_id_or_email ) ) {
127: $field = 'id';
128: } else {
129: $field = 'email';
130: }
131:
132: $contact = $this->get_contact_by( $field, $_id_or_email );
133:
134: if ( empty( $contact ) || ! is_object( $contact ) ) {
135: return false;
136: }
137: $this->setup_contact( $contact );
138: }
139:
140: /**
141: * Get the primary email of contact / alias of get_primary_email
142: *
143: * @param int $id Contact ID.
144: * @return mixed
145: * @since 3.0
146: */
147: public function get_contact_email( $id ) {
148: return $this->get_primary_email( $id );
149: }
150:
151: /**
152: * Update the primary email of contact
153: *
154: * @param int $id Contact ID.
155: * @param string $value Contact email.
156: * @return bool|int
157: * @since 3.0
158: */
159: public function update_contact_email( $id, $value ) {
160: $emails = (array) get_post_meta( $id, 'contact_emails', true );
161: $emails['email'] = $value;
162: return update_post_meta( $id, 'contact_emails', $emails );
163: }
164:
165: /**
166: * Given the contact data, let's set the variables
167: *
168: * @since 3.0
169: * @param object $contact The contact object.
170: * @return bool If the setup was successful or not.
171: * @since 3.5.0 Fixed case of property ID.
172: */
173: private function setup_contact( $contact ) {
174:
175: if ( ! is_object( $contact ) ) {
176: return false;
177: }
178:
179: foreach ( array( 'ID', 'name', 'email', 'notes', 'listing_ids', 'listing_count', 'date_created', 'background_info', 'type', 'heading' ) as $key ) {
180:
181: switch ( $key ) {
182:
183: case 'ID':
184: $this->$key = $contact->ID;
185: $this->ID = $contact->ID;
186: break;
187:
188: case 'email':
189: $this->$key = $this->get_contact_email( $contact->ID );
190: break;
191:
192: case 'notes':
193: $this->$key = $this->get_notes();
194: break;
195:
196: case 'listing_ids':
197: $this->$key = $this->get_listings();
198: break;
199:
200: case 'listing_count':
201: $this->$key = $this->get_listing_count();
202: break;
203:
204: case 'date_created':
205: $this->$key = $contact->post_date;
206: break;
207:
208: case 'heading':
209: $this->$key = $contact->post_title;
210: break;
211:
212: case 'name':
213: $this->$key = $this->get_meta( 'contact_first_name' ) . ' ' . $this->get_meta( 'contact_last_name' );
214: break;
215:
216: case 'background_info':
217: $this->$key = $contact->post_content;
218: break;
219:
220: case 'type':
221: $this->$key = $this->get_category_label();
222: break;
223: }
224: }
225:
226: // Contact ID and email are the only things that are necessary, make sure they exist.
227: if ( ! empty( $this->ID ) && ! empty( $this->email ) ) {
228: return true;
229: }
230:
231: return false;
232: }
233:
234: /**
235: * Retrieves a single contact from the database
236: *
237: * @access public
238: * @param string $field Type of field option, id or email.
239: * @param mixed $value The Contact ID or email to search.
240: * @return mixed Upon success, an object of the contact. Upon failure, NULL
241: * @since 3.0
242: */
243: public function get_contact_by( $field = 'id', $value = 0 ) {
244:
245: if ( empty( $field ) || empty( $value ) ) {
246: return null;
247: }
248:
249: if ( 'id' === $field ) {
250: // Make sure the value is numeric to avoid casting objects, for example, to int 1.
251: if ( ! is_numeric( $value ) ) {
252: return false;
253: }
254:
255: $value = intval( $value );
256:
257: if ( $value < 1 ) {
258: return false;
259: }
260: } elseif ( 'email' === $field ) {
261:
262: if ( ! is_email( $value ) ) {
263: return false;
264: }
265:
266: $value = trim( $value );
267: }
268:
269: if ( ! $value ) {
270: return false;
271: }
272:
273: $contact = false;
274: switch ( $field ) {
275: case 'id':
276: $contact = get_post( $value );
277: break;
278: case 'email':
279: $matched_contacts = new WP_Query(
280: array(
281: 'post_type' => 'epl_contact',
282: 'post_status' => 'publish',
283: 'paged' => 1,
284: 'offset' => 0,
285: 'meta_query' => array(
286: array(
287: 'key' => 'contact_emails',
288: 'value' => sprintf( ':"%s";', $value ),
289: 'compare' => 'LIKE',
290: ),
291: ),
292: )
293: );
294: if ( ! empty( $matched_contacts->posts ) ) {
295:
296: foreach ( $matched_contacts->posts as $matched_contact ) {
297: $emails = get_post_meta( $matched_contact->ID, 'contact_emails', true );
298:
299: if ( isset( $emails['email'] ) && $emails['email'] === $value ) {
300: $contact = $matched_contacts->post;
301: }
302: }
303: }
304:
305: break;
306: default:
307: return false;
308: }
309:
310: if ( ! $contact ) {
311: return false;
312: }
313:
314: return $contact;
315: }
316:
317: /**
318: * Magic __get function to dispatch a call to retrieve a private property
319: *
320: * @param string $key Meta key.
321: * @return mixed|WP_Error
322: * @since 3.0.0
323: */
324: public function __get( $key ) {
325:
326: if ( method_exists( $this, 'get_' . $key ) ) {
327: return call_user_func( array( $this, 'get_' . $key ) );
328: } else {
329: /* Translators: %s is the meta key. */
330: return new WP_Error( 'epl-contact-invalid-property', sprintf( __( 'Can\'t get property %s', 'easy-property-listings' ), $key ) );
331: }
332: }
333:
334: /**
335: * Creates a contact
336: *
337: * @since 3.0
338: * @param array $data Array of attributes for a contact.
339: * @return mixed False if not a valid creation, Contact ID if user is found or valid creation
340: */
341: public function create( $data = array() ) {
342:
343: if ( 0 !== $this->ID || empty( $data ) ) {
344: return false;
345: }
346:
347: $defaults = array(
348: 'name' => '',
349: 'email' => '',
350: );
351:
352: $args = wp_parse_args( $data, $defaults );
353:
354: if ( empty( $args['email'] ) || ! is_email( $args['email'] ) ) {
355: return false;
356: }
357:
358: do_action( 'epl_contact_pre_create', $args );
359:
360: $created = false;
361:
362: $contact = $this->contact_exists( $args['email'] );
363:
364: if ( $contact ) {
365: wp_update_post(
366: array(
367: 'post_title' => $args['name'],
368: 'post_type' => 'epl_contact',
369: 'ID' => $contact->ID,
370: )
371: );
372:
373: } else {
374:
375: $inserted = wp_insert_post(
376: array(
377: 'post_title' => $args['name'],
378: 'post_type' => 'epl_contact',
379: 'post_status' => 'publish',
380: )
381: );
382:
383: if ( $inserted ) {
384: $this->update_contact_email( $inserted, $args['email'] );
385: $contact = $this->get_contact_by( 'email', $args['email'] );
386: $this->setup_contact( $contact );
387: }
388: }
389: $created = $contact->ID;
390:
391: do_action( 'epl_contact_post_create', $created, $args );
392: return $created;
393: }
394:
395: /**
396: * Checking for contact
397: *
398: * @param null $email Contact email address.
399: * @return bool|WP_Post
400: * @since 3.0
401: */
402: public function contact_exists( $email = null ) {
403: $matched_contacts = new WP_Query(
404: array(
405: 'post_type' => 'epl_contact',
406: 'post_status' => 'publish',
407: 'meta_query' => array(
408: array(
409: 'key' => 'contact_emails',
410: 'value' => sprintf( ':"%s";', $email ),
411: 'compare' => 'LIKE',
412: ),
413: ),
414: )
415: );
416: if ( ! empty( $matched_contacts->posts ) ) {
417:
418: foreach ( $matched_contacts->posts as $matched_contact ) {
419: $emails = get_post_meta( $matched_contact->ID, 'contact_emails', true );
420:
421: if ( isset( $emails['email'] ) && $emails['email'] === $email ) {
422: return $matched_contacts->post;
423: }
424: }
425: }
426: return false;
427: }
428:
429: /**
430: * Update a contact record
431: *
432: * @since 3.0
433: * @param array $data Array of data attributes for a contact (checked via whitelist).
434: * @return bool If the update was successful or not
435: */
436: public function update( $data = array() ) {
437:
438: if ( empty( $data ) ) {
439: return false;
440: }
441:
442: do_action( 'epl_contact_pre_update', $this->ID, $data );
443:
444: $updated = false;
445:
446: $updated = wp_update_post(
447: array(
448: 'post_title' => $data['name'],
449: 'post_type' => 'epl_contact',
450: 'ID' => $this->ID,
451: 'post_status' => 'publish',
452: )
453: );
454:
455: if ( $updated ) {
456:
457: if ( isset( $data['email'] ) && is_email( $data['email'] ) ) {
458: // Make sure email is new & not already attached to some other contact.
459: if ( ! $this->contact_exists( $data['email'] ) && ( $data['email'] !== $this->email ) ) {
460: $this->update_contact_email( $this->ID, $data['email'] );
461: }
462: }
463:
464: $contact = $this->get_contact_by( 'id', $this->ID );
465: $this->setup_contact( $contact );
466:
467: $updated = true;
468: }
469:
470: do_action( 'epl_contact_post_update', $updated, $this->ID, $data );
471:
472: return $updated;
473: }
474:
475: /**
476: * Attach listing to the contact then triggers increasing stats
477: *
478: * @since 3.0
479: * @param int $listing_id The listing ID to attach to the contact.
480: * @return bool If the attachment was successfully
481: */
482: public function attach_listing( $listing_id = 0 ) {
483:
484: if ( empty( $listing_id ) ) {
485: return false;
486: }
487:
488: if ( empty( $this->listing_ids ) ) {
489:
490: $new_listing_ids = array( $listing_id );
491:
492: } else {
493:
494: $this->listing_ids[] = $listing_id;
495:
496: $new_listing_ids = array_unique( array_values( $this->listing_ids ) );
497:
498: }
499:
500: do_action( 'epl_contact_pre_attach_listing', $listing_id, $this->ID );
501:
502: $listing_added = update_post_meta( $this->ID, 'contact_interested_listings', $new_listing_ids );
503:
504: if ( $listing_added ) {
505:
506: // Update the contacts of listing as well.
507: $contact_ids = get_post_meta( $listing_id, 'epl_interested_contacts', true );
508: if ( ! empty( $contact_ids ) ) {
509: $contact_ids[] = $this->ID;
510: } else {
511: $contact_ids = array( $this->ID );
512: }
513: update_post_meta( $listing_id, 'epl_interested_contacts', $contact_ids );
514:
515: }
516:
517: do_action( 'epl_contact_post_attach_listing', $listing_added, $listing_id, $this->ID );
518:
519: return $listing_added;
520: }
521:
522:
523: /**
524: * Remove a listing from this contact, then triggers reducing stats
525: *
526: * @since 3.0
527: * @param integer $listing_id The Payment ID to remove.
528: * @return boolean If the removal was successful
529: */
530: public function remove_listing( $listing_id = 0 ) {
531:
532: if ( empty( $listing_id ) ) {
533: return false;
534: }
535:
536: $new_listing_ids = '';
537:
538: if ( ! empty( $this->listing_ids ) ) {
539:
540: $pos = array_search( $listing_id, $this->listing_ids ); // phpcs:ignore
541: if ( false === $pos ) {
542: return false;
543: }
544:
545: unset( $this->listing_ids[ $pos ] );
546: $listing_ids = array_filter( $this->listing_ids );
547:
548: $new_listing_ids = array_unique( array_values( $listing_ids ) );
549:
550: }
551:
552: do_action( 'epl_contact_pre_remove_listing', $listing_id, $this->ID );
553:
554: $listing_added = update_post_meta( $this->ID, 'contact_interested_listings', $new_listing_ids );
555:
556: $listing_removed = $listing_added;
557:
558: // Update the contacts of listing as well.
559: $contact_ids = get_post_meta( $listing_id, 'epl_interested_contacts', true );
560: if ( ! empty( $contact_ids ) ) {
561: $pos = array_search( $this->ID, $contact_ids ); // phpcs:ignore
562: if ( false !== $pos ) {
563: unset( $contact_ids[ $pos ] );
564: update_post_meta( $listing_id, 'epl_interested_contacts', $contact_ids );
565: }
566: }
567:
568: do_action( 'epl_contact_post_remove_listing', $listing_removed, $listing_id, $this->ID );
569:
570: return $listing_removed;
571: }
572:
573:
574: /**
575: * Get listings of contact
576: *
577: * @since 3.0
578: */
579: public function get_listings() {
580:
581: return get_post_meta( $this->ID, 'contact_interested_listings', true );
582: }
583:
584: /**
585: * Get listings count
586: *
587: * @since 3.0
588: */
589: public function get_listing_count() {
590:
591: $listings = (array) get_post_meta( $this->ID, 'contact_interested_listings', true );
592: $listings = array_filter( $listings );
593: return count( $listings );
594: }
595:
596: /**
597: * Get the parsed notes for a contact as an array
598: *
599: * @param int $number Number of notes per page.
600: * @param int $paged Paged query.
601: * @param string $orderby Order by option.
602: * @param string $order Order option.
603: *
604: * @return array The notes requested
605: * @since 3.5.25 Request the 'note' comment type explicitly for WordPress 6.9+ compatibility.
606: */
607: public function get_notes( $number = 10, $paged = 1, $orderby = 'comment_date', $order = 'DESC' ) {
608:
609: $length = is_numeric( $number ) ? $number : 10;
610: $offset = is_numeric( $paged ) && 1 !== $paged ? ( ( absint( $paged ) - 1 ) * $number ) : 0;
611: $args = array(
612: 'meta_key' => 'epl_contact_id',
613: 'meta_value' => $this->ID,
614: 'offset' => $offset,
615: 'number' => $length,
616: 'orderby' => $orderby,
617: 'order' => $order,
618: // WP 6.9 excludes the reserved 'note' comment type unless 'all'/'note'
619: // is requested; contact activities are stored as 'note', so opt in.
620: 'type' => 'all',
621: );
622:
623: return get_comments( $args );
624: }
625:
626: /**
627: * Get the total number of notes we have after parsing
628: *
629: * @since 3.0
630: * @since 3.5.25 Request the 'note' comment type explicitly for WordPress 6.9+ compatibility.
631: * @return int The number of notes for the contact.
632: */
633: public function get_notes_count() {
634:
635: return get_comments(
636: array(
637: 'meta_key' => 'epl_contact_id',
638: 'meta_value' => $this->ID,
639: 'count' => true,
640: // See get_notes(): WP 6.9 hides the reserved 'note' type by default.
641: 'type' => 'all',
642: )
643: );
644: }
645:
646: /**
647: * Add a note for the contact
648: *
649: * @param string $note The note to add.
650: * @param string $note_type Type of note.
651: * @param int $listing_id Listing ID.
652: * @return string|boolean The new note if added successfully, false otherwise
653: * @since 3.0
654: */
655: public function add_note( $note = '', $note_type = 'note', $listing_id = 0 ) {
656:
657: $note = trim( $note );
658: if ( empty( $note ) ) {
659: return false;
660: }
661:
662: $note = apply_filters( 'epl_contact_add_note_string', $note, $note_type, $listing_id );
663:
664: do_action( 'epl_contact_pre_add_note', $note, $this->ID );
665:
666: $note_data = array(
667: 'comment_post_ID' => absint( $listing_id ),
668: 'comment_author' => $this->name,
669: 'comment_author_email' => $this->email,
670: 'comment_content' => $note,
671: 'comment_type' => $note_type,
672: 'comment_parent' => 0,
673: 'comment_agent' => 'easy-property-listings',
674: );
675:
676: // Insert new note and get the note ID.
677: $note_id = wp_insert_comment( $note_data );
678: if ( $note_id ) {
679: $this->notes = $this->get_notes();
680: update_comment_meta( $note_id, 'epl_contact_id', $this->ID );
681: }
682:
683: do_action( 'epl_contact_post_add_note', $this->notes, $note_data, $this->ID );
684:
685: // Return the note object.
686: return get_comment( $note_id );
687: }
688:
689: /**
690: * Delete the contact
691: *
692: * @since 3.0
693: */
694: public function delete() {
695: return wp_delete_post( $this->ID, true );
696: }
697:
698: /**
699: * Update Contact Meta
700: *
701: * @param string $key Meta key name.
702: * @param string|array $value Meta value.
703: * @return bool|int
704: * @since 3.0
705: */
706: public function update_meta( $key, $value ) {
707: return update_post_meta( $this->ID, $key, $value );
708: }
709:
710: /**
711: * Get Contact Meta
712: *
713: * @param string $key Meta key name.
714: * @return mixed
715: * @since 3.0
716: */
717: public function get_meta( $key ) {
718: return get_post_meta( $this->ID, $key, true );
719: }
720:
721: /**
722: * Returns activity type label for activity type
723: *
724: * @param string $key Meta key name.
725: * @return mixed
726: * @since 3.0
727: */
728: public function get_activity_type( $key ) {
729: $activity_types = $this->get_activity_types();
730: return isset( $activity_types[ $key ] ) ? $activity_types[ $key ] : $activity_types['epl-admin-note'];
731: }
732:
733: /**
734: * Returns all activity types
735: *
736: * @return mixed
737: * @since 3.0.0
738: */
739: public static function get_activity_types() {
740: return apply_filters(
741: 'epl_contact_activity_labels',
742: array(
743: 'note' => __( 'User Activity', 'easy-property-listings' ),
744: 'epl-admin-note' => __( 'Admin Note', 'easy-property-listings' ),
745: 'call' => __( 'Call', 'easy-property-listings' ),
746: 'email' => __( 'Email', 'easy-property-listings' ),
747: 'listing' => __( 'Listing', 'easy-property-listings' ),
748: )
749: );
750: }
751:
752: /**
753: * Returns Contact Address
754: *
755: * @return mixed
756: * @since 3.0.0
757: */
758: public function epl_contact_get_address() {
759:
760: $addr = $this->get_meta( 'contact_street_number' ) . ' ';
761: $addr .= $this->get_meta( 'contact_street_name' ) . ' ';
762: $contact_suburb = $this->get_meta( 'contact_suburb' );
763: if ( ! empty( $contact_suburb ) ) {
764: $addr .= $this->get_meta( 'contact_suburb' ) . ', ';
765: }
766:
767: $addr .= $this->get_meta( 'contact_state' ) . ' ';
768: $addr .= $this->get_meta( 'contact_postcode' ) . ' ';
769: $addr .= $this->get_meta( 'contact_country' ) . ' ';
770:
771: return apply_filters( 'epl_contact_formatted_address', $addr );
772: }
773:
774: /**
775: * Returns Contact Primary Email
776: *
777: * @param int $id User ID.
778: * @return mixed
779: * @since 3.0.0
780: */
781: public function get_primary_email( $id ) {
782: $emails = get_post_meta( $id, 'contact_emails', true );
783: return isset( $emails['email'] ) ? $emails['email'] : false;
784: }
785:
786: /**
787: * Returns Contact Email
788: *
789: * @return mixed
790: * @since 3.0.0
791: * @since 3.4.1 Added value span wrapper.
792: */
793: public function get_emails() {
794:
795: $emails = (array) $this->get_meta( 'contact_emails' );
796: $emails = array_filter( $emails );
797: if ( ! empty( $emails ) ) {
798: ob_start();
799: foreach ( $emails as $mail_name => $mail_value ) {
800: $label = ucwords( str_replace( '_', ' ', $mail_name ) ); ?>
801: <span class="contact-email epl-info-item editable" data-key="email">
802: <span class="dashicons dashicons-email epl-contact-icons"></span>
803: <span class="epl-contact-value"><?php echo esc_attr( $label . ' - ' . $mail_value ); ?></span>
804: </span>
805: <?php
806: }
807: $email_html = ob_get_clean();
808: return apply_filters( 'epl_contact_email_html', $email_html );
809: }
810: }
811:
812: /**
813: * Returns Contact Phone
814: *
815: * @return mixed
816: * @since 3.0.0
817: * @since 3.4.1 Renamed emails to phones and added value span wrapper.
818: */
819: public function get_phones() {
820:
821: $phone_numbers = $this->get_meta( 'contact_phones' );
822: $phone_numbers = array_filter( $phone_numbers );
823: if ( ! empty( $phone_numbers ) ) {
824: ob_start();
825: foreach ( $phone_numbers as $mail_name => $mail_value ) {
826: $label = ucwords( str_replace( '_', ' ', $mail_name ) );
827: ?>
828: <span class="contact-phone epl-info-item editable" data-key="phone">
829: <span class="dashicons dashicons-phone epl-contact-icons"></span>
830: <span class="epl-contact-value"><?php echo esc_attr( $label . ' - ' . $mail_value ); ?></span>
831: </span>
832: <?php
833: }
834: $email_html = ob_get_clean();
835: return apply_filters( 'epl_contact_phone_html', $email_html );
836: }
837: }
838:
839: /**
840: * Returns Contact Category Label
841: *
842: * @return mixed
843: * @since 3.0.0
844: */
845: public function get_category_label() {
846: $cat = $this->get_meta( 'contact_category' );
847: return get_category_label( $cat );
848: }
849:
850: /**
851: * Returns Contact Category Name
852: *
853: * @return mixed
854: * @since 3.0.0
855: */
856: public function get_category_name() {
857: return $this->get_meta( 'contact_category' );
858: }
859: }
860: