PluginProbe
Property Hive / 2.2.3
Property Hive v2.2.3
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / admin / class-ph-admin-matching-properties.php

class-ph-admin-matching-properties.php in Property Hive 2.2.3, at includes/admin/class-ph-admin-matching-properties.php

893 lines 37.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Admin Matching Properties Class.
4 *
5 * @author PropertyHive
6 * @category Admin
7 * @package PropertyHive/Admin
8 * @version 1.0.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
12
13 if ( ! class_exists( 'PH_Admin_Matching_Properties' ) ) :
14
15 /**
16 * PH_Admin_Matching_Properties
17 */
18 class PH_Admin_Matching_Properties {
19
20 public function output()
21 {
22 if ( !isset($_GET['contact_id']) || (isset($_GET['contact_id']) && get_post_type((int)$_GET['contact_id']) != 'contact') )
23 {
24 die('Invalid contact_id passed');
25 }
26 if ( !isset($_GET['applicant_profile']) )
27 {
28 die('Invalid applicant_profile passed');
29 }
30
31 $contact_id = (int)$_GET['contact_id'];
32
33 $email_address = get_post_meta( $contact_id, '_email_address', TRUE );
34
35 $applicant_profile_id = (int)$_GET['applicant_profile'];
36
37 $applicant_profile = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
38
39 if ( isset($_POST['step']) )
40 {
41 if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'propertyhive-matching-properties' ) )
42 die( esc_html(__( 'Action failed. Please refresh the page and retry.', 'propertyhive' )) );
43
44 switch ( $_POST['step'] )
45 {
46 case "one":
47 {
48 // Properties have been selected to email or dismiss
49
50 // Handle dismissed properties
51 $this->dismiss_properties();
52
53 $nothing_to_send = true;
54
55 // Handle properties to email
56 if ( isset($_POST['email_property_id']) && !empty($_POST['email_property_id']) )
57 {
58 $nothing_to_send = false;
59
60 $subject = get_option( 'propertyhive_property_match_default_email_subject', '' );
61 $body = get_option( 'propertyhive_property_match_default_email_body', '' );
62
63 $from_email_option = get_option( 'propertyhive_property_match_default_from', '' );
64 if( $from_email_option == 'default_from_email' )
65 {
66 $from_email_address = get_option('propertyhive_email_from_address', '');
67 }
68 else
69 {
70 $current_user = wp_get_current_user();
71 $from_email_address = $current_user->user_email;
72 }
73 }
74
75 $nothing_to_send = apply_filters( 'propertyhive_property_match_nothing_to_send', $nothing_to_send );
76
77 if ( $nothing_to_send != true )
78 {
79 ?>
80 <div class="wrap propertyhive">
81
82 <div id="poststuff">
83
84 <form method="post" id="mainform" action="" enctype="multipart/form-data">
85 <?php
86 if ( isset($_POST['email_property_id']) && !empty($_POST['email_property_id']) )
87 {
88 // We've got emails to send
89 include 'views/html-admin-matching-properties-email.php';
90 }
91
92 do_action( 'propertyhive_property_match_step_two', $contact_id, $applicant_profile_id );
93 ?>
94 <p class="submit">
95
96 <input name="save" class="button-primary" type="submit" value="<?php echo esc_attr(__( 'Send Matches', 'propertyhive' )); ?>" />
97 <?php if ( isset($_POST['email_property_id']) && !empty($_POST['email_property_id']) ) { ?>
98 <input name="preview" id="preview_email" class="button" type="button" value="<?php echo esc_attr(__( 'Preview Email', 'propertyhive' )); ?>" />
99 <?php } ?>
100
101 <input type="hidden" name="step" value="two" />
102 <input type="hidden" name="email_property_id" value="<?php echo ( isset($_POST['email_property_id']) && is_array($_POST['email_property_id']) && !empty($_POST['email_property_id']) ) ? esc_attr(implode(",", ph_clean($_POST['email_property_id']))) : ''; ?>" />
103 <?php do_action( 'propertyhive_property_match_step_two_hidden_fields' ); ?>
104 <?php wp_nonce_field( 'propertyhive-matching-properties' ); ?>
105
106 </p>
107
108 <p>
109 <?php echo __( 'When sending out lots of emails we recommend using <a href="https://en-gb.wordpress.org/plugins/tags/smtp" target="_blank">a plugin</a> to send them out using SMTP. Your web developer or hosting company should be able to advise on this.', 'propertyhive' );
110 ?>
111 </p>
112
113 </form>
114
115 </div>
116
117 </div>
118
119 <script>
120
121 jQuery(document).ready(function()
122 {
123 jQuery('#preview_email').click(function(e)
124 {
125 e.preventDefault();
126
127 showPreview();
128 });
129 });
130
131 function showPreview()
132 {
133 jQuery('#mainform').attr('target', '_blank');
134 jQuery('#mainform').attr('action', '<?php echo admin_url( '?preview_propertyhive_email=true&contact_id=' . (int)$_GET['contact_id'] . '&applicant_profile=' . (int)$_GET['applicant_profile'] ); ?>');
135
136 jQuery('#mainform').submit();
137 jQuery('#mainform').attr('target', '_self');
138 jQuery('#mainform').attr('action', '');
139 }
140
141 </script>
142 <?php
143 }
144
145 if ( $nothing_to_send == true )
146 {
147 echo '<script>window.location.href = "' . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=2";</script>';
148
149 //header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=2' ); // properties marked as not interested
150 //die();
151 }
152
153 break;
154 }
155 case "two":
156 {
157 if ( isset($_POST['email_property_id']) && !empty($_POST['email_property_id']) )
158 {
159 $to_email_addresses = explode(",", $_POST['to_email_address']);
160 $new_to_email_addresses = array();
161 foreach ( $to_email_addresses as $to_email_address )
162 {
163 $new_to_email_addresses[] = sanitize_email($to_email_address);
164 }
165
166 $cc_email_addresses = explode(",", $_POST['cc_email_address']);
167 $new_cc_email_addresses = array();
168 foreach ( $cc_email_addresses as $cc_email_address )
169 {
170 $new_cc_email_addresses[] = sanitize_email($cc_email_address);
171 }
172
173 $bcc_email_addresses = explode(",", $_POST['bcc_email_address']);
174 $new_bcc_email_addresses = array();
175 foreach ( $bcc_email_addresses as $bcc_email_address )
176 {
177 $new_bcc_email_addresses[] = sanitize_email($bcc_email_address);
178 }
179
180 $allowed_tags = array(
181 'strong' => array(),
182 'span' => array(),
183 'em' => array(),
184 'h1' => array(),
185 'h2' => array(),
186 'h3' => array(),
187 'h4' => array(),
188 'h5' => array(),
189 'h6' => array(),
190 'i' => array(),
191 'u' => array(),
192 'b' => array(),
193 'a' => array(
194 'href' => array(),
195 'target' => array(),
196 ),
197 );
198 $allowed_tags = apply_filters( 'propertyhive_match_email_allowed_tags', $allowed_tags );
199
200 $body = wp_kses(wp_unslash($_POST['body']), $allowedposttags);
201
202 // Email info entered. Time to send emails
203 $this->send_emails(
204 (int)$_GET['contact_id'],
205 (int)$_GET['applicant_profile'],
206 explode(",", ph_clean($_POST['email_property_id'])),
207 ph_clean(wp_unslash($_POST['from_name'])),
208 sanitize_email(wp_unslash($_POST['from_email_address'])),
209 ph_clean(wp_unslash($_POST['subject'])),
210 $body,
211 implode(",", $new_to_email_addresses),
212 implode(",", $new_cc_email_addresses),
213 implode(",", $new_bcc_email_addresses)
214 );
215
216 //header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=1' ); // email sent
217 //die();
218 }
219
220 do_action( 'propertyhive_property_match_step_send', $contact_id, $applicant_profile_id );
221
222 echo '<script>window.location.href = "' . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=1";</script>';
223 }
224 }
225 }
226 else
227 {
228 $applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile_id . '_match_history', TRUE );
229
230 $properties = $this->get_matching_properties( (int)$_GET['contact_id'], (int)$_GET['applicant_profile'] );
231
232 $do_not_email = false;
233 $forbidden_contact_methods = get_post_meta( (int)$_GET['contact_id'], '_forbidden_contact_methods', TRUE );
234 if ( is_array($forbidden_contact_methods) && in_array('email', $forbidden_contact_methods) )
235 {
236 $do_not_email = true;
237 }
238
239 include 'views/html-admin-matching-properties.php';
240 }
241 }
242
243 private function dismiss_properties()
244 {
245 $contact_id = (int)$_GET['contact_id'];
246 $applicant_profile_id = (int)$_GET['applicant_profile'];
247
248 // Get currently dismissed properties for this contact to decide if we need to add or remove it
249 $dismissed_properties = get_post_meta( $contact_id, '_dismissed_properties', TRUE );
250
251 if ( !is_array($dismissed_properties) )
252 {
253 $dismissed_properties = array();
254 }
255
256 if ( isset($_POST['not_interested_property_id']) && !empty($_POST['not_interested_property_id']) )
257 {
258 foreach ( $_POST['not_interested_property_id'] as $property_id )
259 {
260 if ( in_array((int)$property_id, $dismissed_properties) )
261 {
262 // Already dismissed. Need to remove from array
263 if( ($key = array_search((int)$property_id, $dismissed_properties)) !== false )
264 {
265 unset($dismissed_properties[$key]);
266 }
267 }
268 else
269 {
270 // Not dismissed. Add to array
271 $dismissed_properties[] = (int)$property_id;
272 }
273 }
274
275 $dismissed_properties = array_unique($dismissed_properties);
276
277 update_post_meta( $contact_id, '_dismissed_properties', $dismissed_properties );
278 }
279 }
280
281 public function get_matching_properties( $contact_id, $applicant_profile_id, $date_added_from = '' )
282 {
283 global $post;
284
285 $properties = array();
286
287 $contact = get_post($contact_id);
288
289 if ( !is_null( $contact ) )
290 {
291 $percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' );
292 $percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' );
293
294 $dismissed_properties = get_post_meta( $contact_id, '_dismissed_properties', TRUE );
295 if ( !is_array($dismissed_properties) )
296 {
297 $dismissed_properties = array();
298 }
299
300 $applicant_profile = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
301
302 $args = array(
303 'post_type' => 'property',
304 'nopaging' => true,
305 'post__not_in' => $dismissed_properties
306 );
307
308 if ( $date_added_from != '' )
309 {
310 // Convert stored UTC date to site timezone
311 $timezone = wp_timezone(); // Returns DateTimeZone object based on site settings
312
313 $datetime = new DateTime($date_added_from, new DateTimeZone('UTC'));
314 $datetime->setTimezone($timezone);
315 $local_date = $datetime->format('Y-m-d H:i:s');
316
317 $args['date_query'] = array(
318 array(
319 'after' => $local_date,
320 'inclusive' => true,
321 )
322 );
323 }
324
325 // Meta query
326 $meta_query = array('relation' => 'AND');
327 $meta_query[] = array(
328 'key' => '_on_market',
329 'value' => 'yes'
330 );
331 if (
332 isset($applicant_profile['department']) &&
333 (
334 $applicant_profile['department'] == 'residential-sales' ||
335 ph_get_custom_department_based_on($applicant_profile['department']) == 'residential-sales'
336 )
337 )
338 {
339 $meta_query[] = array(
340 'key' => '_department',
341 'value' => $applicant_profile['department']
342 );
343
344 if (
345 get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' ) != '' &&
346 get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' ) != ''
347 )
348 {
349 $match_price_range_lower = '';
350 if ( !isset($applicant_profile['match_price_range_lower_actual']) || ( isset($applicant_profile['match_price_range_lower_actual']) && $applicant_profile['match_price_range_lower_actual'] == '' ) )
351 {
352 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' )
353 {
354 if ( $percentage_lower != '' )
355 {
356 $match_price_range_lower = $applicant_profile['max_price_actual'] - ( $applicant_profile['max_price_actual'] * ( $percentage_lower / 100 ) );
357 }
358 }
359 }
360 else
361 {
362 $match_price_range_lower = $applicant_profile['match_price_range_lower_actual'];
363 }
364
365 $match_price_range_higher = '';
366 if ( !isset($applicant_profile['match_price_range_higher_actual']) || ( isset($applicant_profile['match_price_range_higher_actual']) && $applicant_profile['match_price_range_higher_actual'] == '' ) )
367 {
368 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' )
369 {
370 if ( $percentage_higher != '' )
371 {
372 $match_price_range_higher = $applicant_profile['max_price_actual'] + ( $applicant_profile['max_price_actual'] * ( $percentage_higher / 100 ) );
373 }
374 }
375 }
376 else
377 {
378 $match_price_range_higher = $applicant_profile['match_price_range_higher_actual'];
379 }
380
381 if ( $match_price_range_lower != '' && $match_price_range_higher != '' )
382 {
383 $meta_query[] = array(
384 'key' => '_price_actual',
385 'value' => array($match_price_range_lower, $match_price_range_higher),
386 'compare' => 'BETWEEN',
387 'type' => 'NUMERIC'
388 );
389 }
390 }
391 elseif ( isset($applicant_profile['max_price_actual']) && !empty($applicant_profile['max_price_actual']) )
392 {
393 $meta_query[] = array(
394 'key' => '_price_actual',
395 'value' => $applicant_profile['max_price_actual'],
396 'compare' => '<=',
397 'type' => 'NUMERIC'
398 );
399 }
400 }
401 elseif (
402 isset($applicant_profile['department']) &&
403 (
404 $applicant_profile['department'] == 'residential-lettings' ||
405 ph_get_custom_department_based_on($applicant_profile['department']) == 'residential-lettings'
406 )
407 )
408 {
409 $meta_query[] = array(
410 'key' => '_department',
411 'value' => $applicant_profile['department']
412 );
413 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' && $applicant_profile['max_price_actual'] != 0 )
414 {
415 $meta_query[] = array(
416 'key' => '_price_actual',
417 'value' => $applicant_profile['max_price_actual'],
418 'compare' => '<=',
419 'type' => 'NUMERIC'
420 );
421 }
422 }
423
424 if (
425 isset($applicant_profile['department']) &&
426 (
427 $applicant_profile['department'] == 'residential-sales' ||
428 $applicant_profile['department'] == 'residential-lettings' ||
429 ph_get_custom_department_based_on($applicant_profile['department']) == 'residential-sales' ||
430 ph_get_custom_department_based_on($applicant_profile['department']) == 'residential-lettings'
431 )
432 )
433 {
434 if ( isset($applicant_profile['min_beds']) && $applicant_profile['min_beds'] != '' && $applicant_profile['min_beds'] != 0 )
435 {
436 $meta_query[] = array(
437 'key' => '_bedrooms',
438 'value' => $applicant_profile['min_beds'],
439 'compare' => '>=',
440 'type' => 'NUMERIC'
441 );
442 }
443 }
444 if (
445 isset($applicant_profile['department']) &&
446 (
447 $applicant_profile['department'] == 'commercial' ||
448 ph_get_custom_department_based_on($applicant_profile['department']) == 'commercial'
449 )
450 )
451 {
452 if ( isset($applicant_profile['available_as']) && is_array($applicant_profile['available_as']) && !empty($applicant_profile['available_as']) )
453 {
454 if ( in_array('sale', $applicant_profile['available_as']) && !in_array('rent', $applicant_profile['available_as']) )
455 {
456 $meta_query[] = array(
457 'key' => '_for_sale',
458 'value' => 'yes',
459 );
460 }
461 if ( !in_array('sale', $applicant_profile['available_as']) && in_array('rent', $applicant_profile['available_as']) )
462 {
463 $meta_query[] = array(
464 'key' => '_to_rent',
465 'value' => 'yes',
466 );
467 }
468 if ( in_array('sale', $applicant_profile['available_as']) && in_array('rent', $applicant_profile['available_as']) )
469 {
470 // Do nothing as both are ticked
471 }
472 }
473
474 if (
475 !isset($applicant_profile['min_floor_area_actual']) ||
476 ( isset($applicant_profile['min_floor_area_actual']) && $applicant_profile['min_floor_area_actual'] === '')
477 )
478 {
479 $applicant_profile['min_floor_area_actual'] = 0;
480 }
481 if (
482 !isset($applicant_profile['max_floor_area_actual']) ||
483 ( isset($applicant_profile['max_floor_area_actual']) && $applicant_profile['max_floor_area_actual'] === '')
484 )
485 {
486 $applicant_profile['max_floor_area_actual'] = 99999999999;
487 }
488 $meta_query[] = array(
489 'key' => '_floor_area_from_sqft',
490 'value' => $applicant_profile['max_floor_area_actual'],
491 'compare' => '<=',
492 'type' => 'NUMERIC'
493 );
494 $meta_query[] = array(
495 'key' => '_floor_area_to_sqft',
496 'value' => $applicant_profile['min_floor_area_actual'],
497 'compare' => '>=',
498 'type' => 'NUMERIC'
499 );
500 }
501
502 if ( get_option('propertyhive_applicant_locations_type') == 'text' )
503 {
504 if ( isset($applicant_profile['location_text']) && $applicant_profile['location_text'] != '' )
505 {
506 $address_keywords = array( $applicant_profile['location_text'] );
507 if ( strpos( $applicant_profile['location_text'], ' ' ) !== FALSE )
508 {
509 $address_keywords[] = str_replace(" ", "-", ph_clean($applicant_profile['location_text']));
510 }
511 if ( strpos( $applicant_profile['location_text'], '-' ) !== FALSE )
512 {
513 $address_keywords[] = str_replace("-", " ", ph_clean($applicant_profile['location_text']));
514 }
515
516 if ( strpos( $applicant_profile['location_text'], '.' ) !== FALSE )
517 {
518 $address_keywords[] = str_replace(".", "", ph_clean($applicant_profile['location_text']));
519 }
520 if ( stripos( $applicant_profile['location_text'], 'st ' ) !== FALSE )
521 {
522 $address_keywords[] = str_ireplace("st ", "st. ", ph_clean($applicant_profile['location_text']));
523 }
524
525 $location_query = array('relation' => 'OR');
526
527 $address_fields_to_query = array(
528 '_address_street',
529 '_address_two',
530 '_address_three',
531 '_address_four',
532 '_address_postcode'
533 );
534
535 $address_fields_to_query = apply_filters( 'propertyhive_address_fields_to_query', $address_fields_to_query );
536
537 $address_keyword_compare = get_option( 'propertyhive_address_keyword_compare', '=' );
538 if ( $address_keyword_compare == 'polygon' )
539 {
540 $address_keyword_compare = apply_filters('propertyhive_property_match_address_keyword_compare', '=');
541 }
542
543 foreach ( $address_keywords as $address_keyword )
544 {
545 foreach ( $address_fields_to_query as $address_field )
546 {
547 if ( $address_field == '_address_postcode' ) { continue; } // ignore postcode as that is handled differently afterwards
548
549 $location_query[] = array(
550 'key' => $address_field,
551 'value' => $address_keyword,
552 'compare' => $address_keyword_compare
553 );
554 }
555 }
556 if ( in_array('_address_postcode', $address_fields_to_query) )
557 {
558 if ( strlen($applicant_profile['location_text']) <= 4 )
559 {
560 $location_query[] = array(
561 'key' => '_address_postcode',
562 'value' => ph_clean( $applicant_profile['location_text'] ),
563 'compare' => '='
564 );
565 // Run regex match where given keyword is at the start of the postcode ^
566 // followed by one or zero letters (for WC2E-style postcodes) [a-zA-Z]?
567 // then a single space [ ]
568 $location_query[] = array(
569 'key' => '_address_postcode',
570 'value' => '^' . ph_clean( $applicant_profile['location_text'] ) . '[a-zA-Z]?[ ]',
571 'compare' => 'RLIKE'
572 );
573 }
574 else
575 {
576 $postcode = ph_clean( $applicant_profile['location_text'] );
577
578 if ( preg_match('#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW])[0-9][ABD-HJLNP-UW-Z]{2})$#i', $postcode) )
579 {
580 // UK postcode found with no space
581
582 if ( strlen($postcode) == 5 )
583 {
584 $first_part = substr($postcode, 0, 2);
585 $last_part = substr($postcode, 2, 3);
586
587 $postcode = $first_part . ' ' . $last_part;
588 }
589 elseif ( strlen($postcode) == 6 )
590 {
591 $first_part = substr($postcode, 0, 3);
592 $last_part = substr($postcode, 3, 3);
593
594 $postcode = $first_part . ' ' . $last_part;
595 }
596 elseif ( strlen($postcode) == 7 )
597 {
598 $first_part = substr($postcode, 0, 4);
599 $last_part = substr($postcode, 4, 3);
600
601 $postcode = $first_part . ' ' . $last_part;
602 }
603 }
604
605 $location_query[] = array(
606 'key' => '_address_postcode',
607 'value' => ph_clean( $postcode ),
608 'compare' => 'LIKE'
609 );
610 }
611 }
612 $meta_query[] = $location_query;
613 }
614 }
615 $args['meta_query'] = $meta_query;
616
617 // Term query
618 $tax_query = array('relation' => 'AND');
619 if (
620 isset($applicant_profile['department']) &&
621 (
622 $applicant_profile['department'] == 'residential-sales' ||
623 $applicant_profile['department'] == 'residential-lettings' ||
624 ph_get_custom_department_based_on($applicant_profile['department']) == 'residential-sales' ||
625 ph_get_custom_department_based_on($applicant_profile['department']) == 'residential-lettings'
626 )
627 )
628 {
629 if ( isset($applicant_profile['property_types']) && is_array($applicant_profile['property_types']) && !empty($applicant_profile['property_types']) )
630 {
631 $tax_query[] = array(
632 'taxonomy' => 'property_type',
633 'field' => 'term_id',
634 'terms' => $applicant_profile['property_types'],
635 'operator' => 'IN',
636 );
637 }
638 }
639 if (
640 isset($applicant_profile['department']) &&
641 (
642 $applicant_profile['department'] == 'commercial' ||
643 ph_get_custom_department_based_on($applicant_profile['department']) == 'commercial'
644 )
645 )
646 {
647 if ( isset($applicant_profile['commercial_property_types']) && is_array($applicant_profile['commercial_property_types']) && !empty($applicant_profile['commercial_property_types']) )
648 {
649 $tax_query[] = array(
650 'taxonomy' => 'commercial_property_type',
651 'field' => 'term_id',
652 'terms' => $applicant_profile['commercial_property_types'],
653 'operator' => 'IN',
654 );
655 }
656 }
657 if ( get_option('propertyhive_applicant_locations_type') != 'text' )
658 {
659 if ( isset($applicant_profile['locations']) && is_array($applicant_profile['locations']) && !empty($applicant_profile['locations']) )
660 {
661 $tax_query[] = array(
662 'taxonomy' => 'location',
663 'field' => 'term_id',
664 'terms' => $applicant_profile['locations'],
665 'operator' => 'IN',
666 );
667 }
668 }
669 $property_match_statuses = get_option( 'propertyhive_property_match_statuses', '' );
670 if ( $property_match_statuses != '' && is_array($property_match_statuses) && !empty($property_match_statuses) )
671 {
672 $tax_query[] = array(
673 'taxonomy' => 'availability',
674 'field' => 'term_id',
675 'terms' => $property_match_statuses,
676 'operator' => 'IN',
677 );
678 }
679 $args['tax_query'] = $tax_query;
680
681 $args = apply_filters( 'propertyhive_matching_properties_args', $args, $contact_id, $applicant_profile );
682
683 $properties_query = new WP_Query( $args );
684
685 if ( $properties_query->have_posts() )
686 {
687 while ( $properties_query->have_posts() )
688 {
689 $properties_query->the_post();
690
691 $property = new PH_Property($post->ID);
692
693 $properties[] = $property;
694 }
695 }
696 wp_reset_postdata();
697 }
698
699 return $properties;
700 }
701
702 public function send_emails( $contact_id, $applicant_profile, $email_property_ids, $from_name, $from_email_address, $subject, $body, $to_email_address = '', $cc_email_address = '', $bcc_email_address = '' )
703 {
704 global $wpdb;
705
706 $current_user = wp_get_current_user();
707
708 $applicant_profile_details = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile, TRUE );
709
710 $contact = new PH_Contact($contact_id);
711 if ( $to_email_address == '' )
712 {
713 $to_email_address = $contact->email_address;
714 }
715
716 $subject = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $subject);
717
718 $body = str_replace("[contact_name]", $contact->post_title, $body);
719 $body = str_replace("[contact_dear]", $contact->dear(), $body);
720 $body = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $body);
721
722 $office_counts = array();
723
724 if ( strpos($body, '[properties]') !== FALSE )
725 {
726 ob_start();
727 if ( !empty($email_property_ids) )
728 {
729 foreach ( $email_property_ids as $email_property_id )
730 {
731
732 $property = new PH_Property((int)$email_property_id);
733
734 if ( $property->office_id != '' && $property->office_id != 0 )
735 {
736 if ( !isset($office_counts[$property->office_id]) ) { $office_counts[$property->office_id] = 0; }
737 ++$office_counts[$property->office_id];
738 }
739
740 ph_get_template( 'emails/applicant-match-property.php', array( 'property' => $property ) );
741 }
742 }
743 $body = str_replace("[properties]", ob_get_clean(), $body);
744 }
745
746 // Get email address of office with most properties
747 $office_name = '';
748 $office_email_address = '';
749
750 $office_id = get_user_meta($current_user->ID, 'office_id', TRUE);
751 if ($office_id == '')
752 {
753 // No office against user. Use email address of office with most properties
754 if ( !empty($office_counts) )
755 {
756 arsort($office_counts);
757 reset($office_counts);
758 $office_id = key($office_counts);
759 }
760 }
761
762 if ( !empty($office_id) )
763 {
764 $office_name = get_the_title($office_id);
765 $office_email_address = get_post_meta( $office_id, '_office_email_address_' . str_replace("residential-", "", $applicant_profile_details['department']), TRUE );
766 }
767
768 $body = str_replace("[office_name]", $office_name, $body);
769 $body = str_replace("[office_email_address]", $office_email_address, $body);
770
771 $body = str_replace("[negotiator_name]", $current_user->display_name, $body);
772 $body = str_replace("[negotiator_email_address]", $current_user->user_email, $body);
773
774 $body = stripslashes($body);
775
776 if (extension_loaded('zlib'))
777 {
778 $compressed_body = @gzcompress($body);
779 if ( $compressed_body !== false )
780 {
781 $body = $compressed_body;
782 }
783 }
784
785 // Insert into email log
786 $insert = $wpdb->insert(
787 $wpdb->prefix . 'ph_email_log',
788 array(
789 'contact_id' => $contact_id,
790 'property_ids' => serialize($email_property_ids),
791 'applicant_profile_id' => $applicant_profile,
792 'to_email_address' => $to_email_address,
793 'cc_email_address' => $cc_email_address,
794 'bcc_email_address' => $bcc_email_address,
795 'from_name' => $from_name,
796 'from_email_address' => $from_email_address,
797 'subject' => stripslashes($subject),
798 'body' => $body,
799 'status' => '',
800 'send_at' => date("Y-m-d H:i:s"),
801 'sent_by' => $current_user->ID,
802 ),
803 array(
804 '%d',
805 '%s',
806 '%d',
807 '%s',
808 '%s',
809 '%s',
810 '%s',
811 '%s',
812 '%s',
813 '%s',
814 '%s',
815 '%s',
816 '%d',
817 )
818 );
819
820 if ( $insert !== FALSE )
821 {
822 $email_log_id = $wpdb->insert_id;
823
824 // Insert properties into applicant match history
825 $applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', TRUE );
826 if ( !is_array($applicant_profile_match_history) )
827 {
828 $applicant_profile_match_history = array();
829 }
830
831 if ( is_array($email_property_ids) && !empty($email_property_ids) )
832 {
833 foreach ( $email_property_ids as $email_property_id )
834 {
835 if ( !isset($applicant_profile_match_history[$email_property_id]) )
836 {
837 $applicant_profile_match_history[$email_property_id] = array();
838 }
839
840 $applicant_profile_match_history[$email_property_id][] = array(
841 'date' => date("Y-m-d H:i:s"),
842 'method' => 'email',
843 'email_log_id' => $email_log_id,
844 );
845
846 // Add note/comment to property
847 $comment = array(
848 'note_type' => 'mailout',
849 'method' => 'email',
850 'email_log_id' => $email_log_id
851 );
852
853 $data = array(
854 'comment_post_ID' => $email_property_id,
855 'comment_author' => $current_user->display_name,
856 'comment_author_email' => 'propertyhive@noreply.com',
857 'comment_author_url' => '',
858 'comment_date' => date("Y-m-d H:i:s"),
859 'comment_content' => serialize($comment),
860 'comment_approved' => 1,
861 'comment_type' => 'propertyhive_note',
862 );
863 wp_insert_comment( $data );
864
865 }
866
867 update_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', $applicant_profile_match_history );
868
869 // Add note/comment to contact
870 $comment = array(
871 'note_type' => 'mailout',
872 'method' => 'email',
873 'email_log_id' => $email_log_id
874 );
875
876 $data = array(
877 'comment_post_ID' => $contact_id,
878 'comment_author' => $current_user->display_name,
879 'comment_author_email' => 'propertyhive@noreply.com',
880 'comment_author_url' => '',
881 'comment_date' => date("Y-m-d H:i:s"),
882 'comment_content' => serialize($comment),
883 'comment_approved' => 1,
884 'comment_type' => 'propertyhive_note',
885 );
886 wp_insert_comment( $data );
887 }
888 }
889 }
890
891 }
892
893 endif;