PluginProbe
Property Hive / 1.4.49
Property Hive v1.4.49
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-applicants.php

class-ph-admin-matching-applicants.php in Property Hive 1.4.49, at includes/admin/class-ph-admin-matching-applicants.php

657 lines 28.9 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 Applicants 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_Applicants' ) ) :
14
15 /**
16 * PH_Admin_Matching_Applicants
17 */
18 class PH_Admin_Matching_Applicants {
19
20 public function output()
21 {
22 if ( !isset($_GET['property_id']) || (isset($_GET['property_id']) && get_post_type((int)$_GET['property_id']) != 'property') )
23 {
24 die('Invalid property_id passed');
25 }
26
27 $property_id = (int)$_GET['property_id'];
28
29 $property = new PH_Property($property_id);
30
31 if ( isset($_POST['step']) )
32 {
33 if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'propertyhive-matching-applicants' ) )
34 die( __( 'Action failed. Please refresh the page and retry.', 'propertyhive' ) );
35
36 switch ( $_POST['step'] )
37 {
38 case "one":
39 {
40 // Properties have been selected to email or dismiss
41
42 // Handle dismissed properties
43 $this->dismiss_properties();
44
45 $nothing_to_send = true;
46
47 // Handle properties to email
48 if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) )
49 {
50 $nothing_to_send = false;
51
52 $subject = get_option( 'propertyhive_property_match_default_email_subject', '' );
53 $body = get_option( 'propertyhive_property_match_default_email_body', '' );
54 }
55
56 $nothing_to_send = apply_filters( 'propertyhive_applicant_match_nothing_to_send', $nothing_to_send );
57
58 if ( $nothing_to_send != true )
59 {
60 ?>
61 <div class="wrap propertyhive">
62
63 <div id="poststuff">
64
65 <form method="post" id="mainform" action="" enctype="multipart/form-data">
66 <?php
67 if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) )
68 {
69 // We've got emails to send
70 include 'views/html-admin-matching-applicants-email.php';
71 }
72
73 do_action( 'propertyhive_applicant_match_step_two', $property_id );
74 ?>
75 <p class="submit">
76
77 <input name="save" class="button-primary" type="submit" value="<?php echo __( 'Send Matches', 'propertyhive' ); ?>" />
78 <?php if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) ) { ?>
79 <input name="preview" id="preview_email" class="button" type="button" value="<?php echo __( 'Preview Email', 'propertyhive' ); ?>" />
80 <?php } ?>
81
82 <input type="hidden" name="step" value="two" />
83 <input type="hidden" name="email_contact_applicant_profile_id" value="<?php echo ( isset($_POST['email_contact_applicant_profile_id']) && is_array($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) ) ? implode(",", ph_clean($_POST['email_contact_applicant_profile_id'])) : ''; ?>" />
84 <?php do_action( 'propertyhive_applicant_match_step_two_hidden_fields' ); ?>
85 <?php wp_nonce_field( 'propertyhive-matching-applicants' ); ?>
86
87 </p>
88
89 <p>
90 <?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' );
91 ?>
92 </p>
93
94 </form>
95
96 </div>
97
98 </div>
99
100 <script>
101
102 jQuery(document).ready(function()
103 {
104 jQuery('#preview_email').click(function(e)
105 {
106 e.preventDefault();
107
108 showPreview();
109 });
110 });
111
112 function showPreview()
113 {
114 jQuery('#mainform').attr('target', '_blank');
115 jQuery('#mainform').attr('action', '<?php echo admin_url( '?preview_propertyhive_email=true&property_id=' . (int)$_GET['property_id']); ?>');
116
117 jQuery('#mainform').submit();
118 jQuery('#mainform').attr('target', '_self');
119 jQuery('#mainform').attr('action', '');
120 }
121
122 </script>
123 <?php
124 }
125
126 if ( $nothing_to_send == true )
127 {
128 echo '<script>window.location.href = "' . get_edit_post_link( $property_id, 'url' ) . '&ph_message=2";</script>';
129
130 //header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=2' ); // properties marked as not interested
131 //die();
132 }
133
134 break;
135 }
136 case "two":
137 {
138 if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) )
139 {
140 $email_contact_applicant_profile_id = explode(",", $_POST['email_contact_applicant_profile_id']);
141
142 foreach ( $email_contact_applicant_profile_id as $contact_applicant_profile_id )
143 {
144 $explode_contact_applicant_profile_id = explode("|", $contact_applicant_profile_id);
145 $contact_id = $explode_contact_applicant_profile_id[0];
146 $applicant_profile_id = $explode_contact_applicant_profile_id[1];
147
148 $email_address = get_post_meta( (int)$contact_id, '_email_address', TRUE );
149
150 $to_email_addresses = explode(",", $email_address);
151 $new_to_email_addresses = array();
152 foreach ( $to_email_addresses as $to_email_address)
153 {
154 $new_to_email_addresses[] = sanitize_email($to_email_address);
155 }
156
157 // Email info entered. Time to send emails
158 $this->send_emails(
159 (int)$contact_id,
160 (int)$applicant_profile_id,
161 array($property_id),
162 ph_clean($_POST['from_name']),
163 sanitize_email($_POST['from_email_address']),
164 ph_clean($_POST['subject']),
165 sanitize_textarea_field($_POST['body']),
166 implode(",", $new_to_email_addresses)
167 );
168 }
169
170 //header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=1' ); // email sent
171 //die();
172 }
173
174 do_action( 'propertyhive_applicant_match_step_send', $property_id );
175
176 echo '<script>window.location.href = "' . get_edit_post_link( $property_id, 'url' ) . '&ph_message=1";</script>';
177 }
178 }
179 }
180 else
181 {
182 $applicants = $this->get_matching_applicants( (int)$_GET['property_id'] );
183
184 $on_market_change_date = $property->_on_market_change_date;
185 $price_change_date = $property->_price_change_date;
186
187 include 'views/html-admin-matching-applicants.php';
188 }
189 }
190
191 private function dismiss_properties()
192 {
193 $property_id = (int)$_GET['property_id'];
194
195 if ( isset($_POST['not_interested_contact_applicant_profile_id']) && !empty($_POST['not_interested_contact_applicant_profile_id']) )
196 {
197 foreach ( $_POST['not_interested_contact_applicant_profile_id'] as $contact_applicant_profile_id )
198 {
199 $explode_contact_applicant_profile_id = explode("|", $contact_applicant_profile_id);
200
201 $contact_id = $explode_contact_applicant_profile_id[0];
202 $applicant_profile_id = $explode_contact_applicant_profile_id[1];
203
204 // Get currently dismissed properties for this contact to decide if we need to add or remove it
205 $dismissed_properties = get_post_meta( $contact_id, '_dismissed_properties', TRUE );
206
207 if ( !is_array($dismissed_properties) )
208 {
209 $dismissed_properties = array();
210 }
211
212 if ( in_array((int)$property_id, $dismissed_properties) )
213 {
214 // Already dismissed. Need to remove from array
215 if( ($key = array_search((int)$property_id, $dismissed_properties)) !== false )
216 {
217 unset($dismissed_properties[$key]);
218 }
219 }
220 else
221 {
222 // Not dismissed. Add to array
223 $dismissed_properties[] = (int)$property_id;
224 }
225
226 $dismissed_properties = array_unique($dismissed_properties);
227
228 update_post_meta( $contact_id, '_dismissed_properties', $dismissed_properties );
229 }
230 }
231 }
232
233 public function get_matching_applicants( $property_id )
234 {
235 global $post;
236
237 $applicants = array();
238
239 $property = new PH_Property((int)$property_id);
240
241 if ( $property !== FALSE )
242 {
243 $property_types = array();
244 $prefix = $property->department == 'commercial' ? 'commercial_' : '';
245 $term_list = wp_get_post_terms($property_id, $prefix . 'property_type', array("fields" => "all"));
246 if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) )
247 {
248 foreach ( $term_list as $term )
249 {
250 $property_types[] = $term->term_id;
251
252 if ( $term->parent != 0 )
253 {
254 $parent = get_term_by( 'id', $term->parent , $prefix . 'property_type' );
255 $property_types[] = $parent->term_id;
256
257 if ( $parent->parent != 0 )
258 {
259 $parent = get_term_by( 'id', $parent->parent , $prefix . 'property_type' );
260 $property_types[] = $parent->term_id;
261 }
262 }
263 }
264 }
265
266 $locations = array();
267 $term_list = wp_get_post_terms($property_id, 'location', array("fields" => "all"));
268 if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) )
269 {
270 foreach ( $term_list as $term )
271 {
272 $locations[] = $term->term_id;
273
274 if ( $term->parent != 0 )
275 {
276 $parent = get_term_by( 'id', $term->parent , 'location' );
277 $locations[] = $parent->term_id;
278
279 if ( $parent->parent != 0 )
280 {
281 $parent = get_term_by( 'id', $parent->parent , 'location' );
282 $locations[] = $parent->term_id;
283 }
284 }
285 }
286 }
287
288 $floor_area_from = $property->floor_area_from_sqft;
289 if ( $floor_area_from === '' )
290 {
291 $floor_area_from = 0;
292 }
293 $floor_area_to = $property->floor_area_to_sqft;
294 if ( $floor_area_to === '' )
295 {
296 $floor_area_to = 99999999999;
297 }
298
299 $percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' );
300 $percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' );
301
302 $args = array(
303 'post_type' => 'contact',
304 'nopaging' => true,
305 );
306
307 // Meta query
308 $meta_query = array();
309 $meta_query[] = array(
310 'key' => '_contact_types',
311 'value' => 'applicant',
312 'compare' => 'LIKE'
313 );
314
315 $args['meta_query'] = $meta_query;
316
317 $contacts_query = new WP_Query( $args );
318
319 if ( $contacts_query->have_posts() )
320 {
321 while ( $contacts_query->have_posts() )
322 {
323 $contacts_query->the_post();
324
325 $contact = new PH_Contact(get_the_ID());
326
327 $dismissed_properties = get_post_meta( get_the_ID(), '_dismissed_properties', TRUE );
328
329 if ( is_array($dismissed_properties) && in_array($property_id, $dismissed_properties) )
330 {
331 // This property is dismissed
332 }
333 else
334 {
335 $num_applicant_profiles = get_post_meta( get_the_ID(), '_applicant_profiles', TRUE );
336
337 for ( $i = 0; $i < $num_applicant_profiles; ++$i )
338 {
339 $applicant_profile = get_post_meta( get_the_ID(), '_applicant_profile_' . $i, TRUE );
340
341 if ( isset($applicant_profile['send_matching_properties']) && $applicant_profile['send_matching_properties'] == 'yes' )
342 {
343 $elements_checked = 0;
344 $matching_elements = 0;
345
346 ++$elements_checked;
347 if ( $applicant_profile['department'] != $property->department )
348 {
349
350 }
351 else
352 {
353 ++$matching_elements;
354
355 if ( $property->department != 'commercial' )
356 {
357 if ( $property->department == 'residential-sales' )
358 {
359 if ( $percentage_lower != '' && $percentage_higher != '' )
360 {
361 $match_price_range_lower = '';
362 if ( !isset($applicant_profile['match_price_range_lower_actual']) || ( isset($applicant_profile['match_price_range_lower_actual']) && $applicant_profile['match_price_range_lower_actual'] == '' ) )
363 {
364 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' )
365 {
366 if ( $percentage_lower != '' )
367 {
368 $match_price_range_lower = $applicant_profile['max_price_actual'] - ( $applicant_profile['max_price_actual'] * ( $percentage_lower / 100 ) );
369 }
370 }
371 }
372 else
373 {
374 $match_price_range_lower = $applicant_profile['match_price_range_lower_actual'];
375 }
376
377 $match_price_range_higher = '';
378 if ( !isset($applicant_profile['match_price_range_higher_actual']) || ( isset($applicant_profile['match_price_range_higher_actual']) && $applicant_profile['match_price_range_higher_actual'] == '' ) )
379 {
380 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' )
381 {
382 if ( $percentage_higher != '' )
383 {
384 $match_price_range_higher = $applicant_profile['max_price_actual'] + ( $applicant_profile['max_price_actual'] * ( $percentage_higher / 100 ) );
385 }
386 }
387 }
388 else
389 {
390 $match_price_range_higher = $applicant_profile['match_price_range_higher_actual'];
391 }
392
393 if (
394 ( $match_price_range_lower == '' && $match_price_range_higher == '' ) ||
395 (
396 $property->_price_actual >= $match_price_range_lower &&
397 $property->_price_actual <= $match_price_range_higher
398 )
399 )
400 {
401 ++$matching_elements;
402 }
403 ++$elements_checked;
404 }
405 else
406 {
407 if (
408 $applicant_profile['max_price_actual'] == '' ||
409 $property->_price_actual <= $applicant_profile['max_price_actual']
410 )
411 {
412 ++$matching_elements;
413 }
414 ++$elements_checked;
415 }
416 }
417 else
418 {
419 if (
420 $applicant_profile['max_price_actual'] == '' ||
421 $property->_price_actual <= $applicant_profile['max_price_actual']
422 )
423 {
424 ++$matching_elements;
425 }
426 ++$elements_checked;
427 }
428
429 if (
430 $applicant_profile['min_beds'] == '' ||
431 $property->_bedrooms >= $applicant_profile['min_beds']
432 )
433 {
434 ++$matching_elements;
435 }
436 ++$elements_checked;
437 }
438 else
439 {
440 if ( $applicant_profile['min_floor_area'] === '' )
441 {
442 $applicant_profile['min_floor_area'] = 0;
443 }
444 if ( $applicant_profile['max_floor_area'] === '' )
445 {
446 $applicant_profile['max_floor_area'] = 99999999999;
447 }
448
449 if (
450 $applicant_profile['min_floor_area'] <= $floor_area_to &&
451 $applicant_profile['max_floor_area'] >= $floor_area_from
452 )
453 {
454 ++$matching_elements;
455 }
456 ++$elements_checked;
457 }
458
459 if (
460 !isset($applicant_profile[$prefix . 'property_types']) ||
461 ( isset($applicant_profile[$prefix . 'property_types']) && empty($applicant_profile[$prefix . 'property_types']) )
462 )
463 {
464 ++$matching_elements;
465 }
466 elseif ( isset($applicant_profile[$prefix . 'property_types']) && !empty($applicant_profile[$prefix . 'property_types']) )
467 {
468 foreach ( $applicant_profile[$prefix . 'property_types'] as $applicant_property_type )
469 {
470 if ( in_array($applicant_property_type, $property_types) )
471 {
472 ++$matching_elements;
473 break;
474 }
475 }
476 }
477 ++$elements_checked;
478
479 if (
480 !isset($applicant_profile['locations']) ||
481 ( isset($applicant_profile['locations']) && empty($applicant_profile['locations']) )
482 )
483 {
484 ++$matching_elements;
485 }
486 elseif ( isset($applicant_profile['locations']) && !empty($applicant_profile['locations']) )
487 {
488 foreach ( $applicant_profile['locations'] as $applicant_location )
489 {
490 if ( in_array($applicant_location, $locations) )
491 {
492 ++$matching_elements;
493 break;
494 }
495 }
496 }
497 ++$elements_checked;
498 }
499
500 if ( $matching_elements == $elements_checked )
501 {
502 $applicant_profile['applicant_profile_id'] = $i;
503
504 // Matched all criteria
505 $applicants[] = array(
506 'contact_id' => get_the_ID(),
507 'applicant_profile' => $applicant_profile,
508 );
509 }
510 }
511 }
512 }
513 }
514 }
515
516 wp_reset_postdata();
517 }
518
519 return $applicants;
520 }
521
522 public function send_emails( $contact_id, $applicant_profile, $email_property_ids, $from_name, $from_email_address, $subject, $body, $to_email_address = '' )
523 {
524 global $wpdb;
525
526 $current_user = wp_get_current_user();
527
528 if ( $to_email_address == '' )
529 {
530 $to_email_address = get_post_meta( $contact_id, '_email_address', TRUE );
531 }
532
533 $subject = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $subject);
534
535 $body = str_replace("[contact_name]", get_the_title($contact_id), $body);
536 $body = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $body);
537
538 if ( strpos($body, '[properties]') !== FALSE )
539 {
540 ob_start();
541 if ( !empty($email_property_ids) )
542 {
543 foreach ( $email_property_ids as $email_property_id )
544 {
545
546 $property = new PH_Property((int)$email_property_id);
547 ph_get_template( 'emails/applicant-match-property.php', array( 'property' => $property ) );
548 }
549 }
550 $body = str_replace("[properties]", ob_get_clean(), $body);
551 }
552
553 // Insert into email log
554 $insert = $wpdb->insert(
555 $wpdb->prefix . 'ph_email_log',
556 array(
557 'contact_id' => $contact_id,
558 'property_ids' => serialize($email_property_ids),
559 'applicant_profile_id' => $applicant_profile,
560 'to_email_address' => $to_email_address,
561 'from_name' => $from_name,
562 'from_email_address' => $from_email_address,
563 'subject' => stripslashes($subject),
564 'body' => stripslashes($body),
565 'status' => '',
566 'send_at' => date("Y-m-d H:i:s"),
567 'sent_by' => $current_user->ID,
568 ),
569 array(
570 '%d',
571 '%s',
572 '%d',
573 '%s',
574 '%s',
575 '%s',
576 '%s',
577 '%s',
578 '%s',
579 '%s',
580 '%d',
581 )
582 );
583
584 if ( $insert !== FALSE )
585 {
586 $email_log_id = $wpdb->insert_id;
587
588 // Insert properties into applicant match history
589 $applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', TRUE );
590 if ( !is_array($applicant_profile_match_history) )
591 {
592 $applicant_profile_match_history = array();
593 }
594
595 if ( is_array($email_property_ids) && !empty($email_property_ids) )
596 {
597 foreach ( $email_property_ids as $email_property_id )
598 {
599 if ( !isset($applicant_profile_match_history[$email_property_id]) )
600 {
601 $applicant_profile_match_history[$email_property_id] = array();
602 }
603
604 $applicant_profile_match_history[$email_property_id][] = array(
605 'date' => date("Y-m-d H:i:s"),
606 'method' => 'email',
607 'email_log_id' => $email_log_id,
608 );
609
610 // Add note/comment to property
611 $comment = array(
612 'note_type' => 'mailout',
613 'method' => 'email',
614 'email_log_id' => $email_log_id
615 );
616
617 $data = array(
618 'comment_post_ID' => $email_property_id,
619 'comment_author' => $current_user->display_name,
620 'comment_author_email' => 'propertyhive@noreply.com',
621 'comment_author_url' => '',
622 'comment_date' => date("Y-m-d H:i:s"),
623 'comment_content' => serialize($comment),
624 'comment_approved' => 1,
625 'comment_type' => 'propertyhive_note',
626 );
627 wp_insert_comment( $data );
628
629 }
630
631 update_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', $applicant_profile_match_history );
632
633 // Add note/comment to contact
634 $comment = array(
635 'note_type' => 'mailout',
636 'method' => 'email',
637 'email_log_id' => $email_log_id
638 );
639
640 $data = array(
641 'comment_post_ID' => $contact_id,
642 'comment_author' => $current_user->display_name,
643 'comment_author_email' => 'propertyhive@noreply.com',
644 'comment_author_url' => '',
645 'comment_date' => date("Y-m-d H:i:s"),
646 'comment_content' => serialize($comment),
647 'comment_approved' => 1,
648 'comment_type' => 'propertyhive_note',
649 );
650 wp_insert_comment( $data );
651 }
652 }
653 }
654
655 }
656
657 endif;