PluginProbe
Property Hive / 1.4.52
Property Hive v1.4.52
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.52, at includes/admin/class-ph-admin-matching-applicants.php

668 lines 29.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 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 $hot_applicants = array();
238 $applicants = array();
239
240 $property = new PH_Property((int)$property_id);
241
242 if ( $property !== FALSE )
243 {
244 $property_types = array();
245 $prefix = $property->department == 'commercial' ? 'commercial_' : '';
246 $term_list = wp_get_post_terms($property_id, $prefix . 'property_type', array("fields" => "all"));
247 if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) )
248 {
249 foreach ( $term_list as $term )
250 {
251 $property_types[] = $term->term_id;
252
253 if ( $term->parent != 0 )
254 {
255 $parent = get_term_by( 'id', $term->parent , $prefix . 'property_type' );
256 $property_types[] = $parent->term_id;
257
258 if ( $parent->parent != 0 )
259 {
260 $parent = get_term_by( 'id', $parent->parent , $prefix . 'property_type' );
261 $property_types[] = $parent->term_id;
262 }
263 }
264 }
265 }
266
267 $locations = array();
268 $term_list = wp_get_post_terms($property_id, 'location', array("fields" => "all"));
269 if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) )
270 {
271 foreach ( $term_list as $term )
272 {
273 $locations[] = $term->term_id;
274
275 if ( $term->parent != 0 )
276 {
277 $parent = get_term_by( 'id', $term->parent , 'location' );
278 $locations[] = $parent->term_id;
279
280 if ( $parent->parent != 0 )
281 {
282 $parent = get_term_by( 'id', $parent->parent , 'location' );
283 $locations[] = $parent->term_id;
284 }
285 }
286 }
287 }
288
289 $floor_area_from = $property->floor_area_from_sqft;
290 if ( $floor_area_from === '' )
291 {
292 $floor_area_from = 0;
293 }
294 $floor_area_to = $property->floor_area_to_sqft;
295 if ( $floor_area_to === '' )
296 {
297 $floor_area_to = 99999999999;
298 }
299
300 $percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' );
301 $percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' );
302
303 $args = array(
304 'post_type' => 'contact',
305 'nopaging' => true,
306 );
307
308 // Meta query
309 $meta_query = array();
310 $meta_query[] = array(
311 'key' => '_contact_types',
312 'value' => 'applicant',
313 'compare' => 'LIKE'
314 );
315
316 $args['meta_query'] = $meta_query;
317
318 $contacts_query = new WP_Query( $args );
319
320 if ( $contacts_query->have_posts() )
321 {
322 while ( $contacts_query->have_posts() )
323 {
324 $contacts_query->the_post();
325
326 $contact = new PH_Contact(get_the_ID());
327
328 $dismissed_properties = get_post_meta( get_the_ID(), '_dismissed_properties', TRUE );
329
330 if ( is_array($dismissed_properties) && in_array($property_id, $dismissed_properties) )
331 {
332 // This property is dismissed
333 }
334 else
335 {
336 $num_applicant_profiles = get_post_meta( get_the_ID(), '_applicant_profiles', TRUE );
337
338 for ( $i = 0; $i < $num_applicant_profiles; ++$i )
339 {
340 $applicant_profile = get_post_meta( get_the_ID(), '_applicant_profile_' . $i, TRUE );
341
342 if ( isset($applicant_profile['send_matching_properties']) && $applicant_profile['send_matching_properties'] == 'yes' )
343 {
344 $elements_checked = 0;
345 $matching_elements = 0;
346
347 ++$elements_checked;
348 if ( $applicant_profile['department'] != $property->department )
349 {
350
351 }
352 else
353 {
354 ++$matching_elements;
355
356 if ( $property->department != 'commercial' )
357 {
358 if ( $property->department == 'residential-sales' )
359 {
360 if ( $percentage_lower != '' && $percentage_higher != '' )
361 {
362 $match_price_range_lower = '';
363 if ( !isset($applicant_profile['match_price_range_lower_actual']) || ( isset($applicant_profile['match_price_range_lower_actual']) && $applicant_profile['match_price_range_lower_actual'] == '' ) )
364 {
365 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' )
366 {
367 if ( $percentage_lower != '' )
368 {
369 $match_price_range_lower = $applicant_profile['max_price_actual'] - ( $applicant_profile['max_price_actual'] * ( $percentage_lower / 100 ) );
370 }
371 }
372 }
373 else
374 {
375 $match_price_range_lower = $applicant_profile['match_price_range_lower_actual'];
376 }
377
378 $match_price_range_higher = '';
379 if ( !isset($applicant_profile['match_price_range_higher_actual']) || ( isset($applicant_profile['match_price_range_higher_actual']) && $applicant_profile['match_price_range_higher_actual'] == '' ) )
380 {
381 if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' )
382 {
383 if ( $percentage_higher != '' )
384 {
385 $match_price_range_higher = $applicant_profile['max_price_actual'] + ( $applicant_profile['max_price_actual'] * ( $percentage_higher / 100 ) );
386 }
387 }
388 }
389 else
390 {
391 $match_price_range_higher = $applicant_profile['match_price_range_higher_actual'];
392 }
393
394 if (
395 ( $match_price_range_lower == '' && $match_price_range_higher == '' ) ||
396 (
397 $property->_price_actual >= $match_price_range_lower &&
398 $property->_price_actual <= $match_price_range_higher
399 )
400 )
401 {
402 ++$matching_elements;
403 }
404 ++$elements_checked;
405 }
406 else
407 {
408 if (
409 $applicant_profile['max_price_actual'] == '' ||
410 $property->_price_actual <= $applicant_profile['max_price_actual']
411 )
412 {
413 ++$matching_elements;
414 }
415 ++$elements_checked;
416 }
417 }
418 else
419 {
420 if (
421 $applicant_profile['max_price_actual'] == '' ||
422 $property->_price_actual <= $applicant_profile['max_price_actual']
423 )
424 {
425 ++$matching_elements;
426 }
427 ++$elements_checked;
428 }
429
430 if (
431 $applicant_profile['min_beds'] == '' ||
432 $property->_bedrooms >= $applicant_profile['min_beds']
433 )
434 {
435 ++$matching_elements;
436 }
437 ++$elements_checked;
438 }
439 else
440 {
441 if ( $applicant_profile['min_floor_area'] === '' )
442 {
443 $applicant_profile['min_floor_area'] = 0;
444 }
445 if ( $applicant_profile['max_floor_area'] === '' )
446 {
447 $applicant_profile['max_floor_area'] = 99999999999;
448 }
449
450 if (
451 $applicant_profile['min_floor_area'] <= $floor_area_to &&
452 $applicant_profile['max_floor_area'] >= $floor_area_from
453 )
454 {
455 ++$matching_elements;
456 }
457 ++$elements_checked;
458 }
459
460 if (
461 !isset($applicant_profile[$prefix . 'property_types']) ||
462 ( isset($applicant_profile[$prefix . 'property_types']) && empty($applicant_profile[$prefix . 'property_types']) )
463 )
464 {
465 ++$matching_elements;
466 }
467 elseif ( isset($applicant_profile[$prefix . 'property_types']) && !empty($applicant_profile[$prefix . 'property_types']) )
468 {
469 foreach ( $applicant_profile[$prefix . 'property_types'] as $applicant_property_type )
470 {
471 if ( in_array($applicant_property_type, $property_types) )
472 {
473 ++$matching_elements;
474 break;
475 }
476 }
477 }
478 ++$elements_checked;
479
480 if (
481 !isset($applicant_profile['locations']) ||
482 ( isset($applicant_profile['locations']) && empty($applicant_profile['locations']) )
483 )
484 {
485 ++$matching_elements;
486 }
487 elseif ( isset($applicant_profile['locations']) && !empty($applicant_profile['locations']) )
488 {
489 foreach ( $applicant_profile['locations'] as $applicant_location )
490 {
491 if ( in_array($applicant_location, $locations) )
492 {
493 ++$matching_elements;
494 break;
495 }
496 }
497 }
498 ++$elements_checked;
499 }
500
501 if ( $matching_elements == $elements_checked )
502 {
503 $applicant_profile['applicant_profile_id'] = $i;
504
505 // Matched all criteria
506 if ( isset($applicant_profile['grading']) && $applicant_profile['grading'] == 'hot' )
507 {
508 $hot_applicants[] = array(
509 'contact_id' => get_the_ID(),
510 'applicant_profile' => $applicant_profile,
511 );
512 }
513 else
514 {
515 $applicants[] = array(
516 'contact_id' => get_the_ID(),
517 'applicant_profile' => $applicant_profile,
518 );
519 }
520 }
521 }
522 }
523 }
524 }
525 }
526
527 wp_reset_postdata();
528 }
529
530 return array_merge($hot_applicants, $applicants);
531 }
532
533 public function send_emails( $contact_id, $applicant_profile, $email_property_ids, $from_name, $from_email_address, $subject, $body, $to_email_address = '' )
534 {
535 global $wpdb;
536
537 $current_user = wp_get_current_user();
538
539 if ( $to_email_address == '' )
540 {
541 $to_email_address = get_post_meta( $contact_id, '_email_address', TRUE );
542 }
543
544 $subject = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $subject);
545
546 $body = str_replace("[contact_name]", get_the_title($contact_id), $body);
547 $body = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $body);
548
549 if ( strpos($body, '[properties]') !== FALSE )
550 {
551 ob_start();
552 if ( !empty($email_property_ids) )
553 {
554 foreach ( $email_property_ids as $email_property_id )
555 {
556
557 $property = new PH_Property((int)$email_property_id);
558 ph_get_template( 'emails/applicant-match-property.php', array( 'property' => $property ) );
559 }
560 }
561 $body = str_replace("[properties]", ob_get_clean(), $body);
562 }
563
564 // Insert into email log
565 $insert = $wpdb->insert(
566 $wpdb->prefix . 'ph_email_log',
567 array(
568 'contact_id' => $contact_id,
569 'property_ids' => serialize($email_property_ids),
570 'applicant_profile_id' => $applicant_profile,
571 'to_email_address' => $to_email_address,
572 'from_name' => $from_name,
573 'from_email_address' => $from_email_address,
574 'subject' => stripslashes($subject),
575 'body' => stripslashes($body),
576 'status' => '',
577 'send_at' => date("Y-m-d H:i:s"),
578 'sent_by' => $current_user->ID,
579 ),
580 array(
581 '%d',
582 '%s',
583 '%d',
584 '%s',
585 '%s',
586 '%s',
587 '%s',
588 '%s',
589 '%s',
590 '%s',
591 '%d',
592 )
593 );
594
595 if ( $insert !== FALSE )
596 {
597 $email_log_id = $wpdb->insert_id;
598
599 // Insert properties into applicant match history
600 $applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', TRUE );
601 if ( !is_array($applicant_profile_match_history) )
602 {
603 $applicant_profile_match_history = array();
604 }
605
606 if ( is_array($email_property_ids) && !empty($email_property_ids) )
607 {
608 foreach ( $email_property_ids as $email_property_id )
609 {
610 if ( !isset($applicant_profile_match_history[$email_property_id]) )
611 {
612 $applicant_profile_match_history[$email_property_id] = array();
613 }
614
615 $applicant_profile_match_history[$email_property_id][] = array(
616 'date' => date("Y-m-d H:i:s"),
617 'method' => 'email',
618 'email_log_id' => $email_log_id,
619 );
620
621 // Add note/comment to property
622 $comment = array(
623 'note_type' => 'mailout',
624 'method' => 'email',
625 'email_log_id' => $email_log_id
626 );
627
628 $data = array(
629 'comment_post_ID' => $email_property_id,
630 'comment_author' => $current_user->display_name,
631 'comment_author_email' => 'propertyhive@noreply.com',
632 'comment_author_url' => '',
633 'comment_date' => date("Y-m-d H:i:s"),
634 'comment_content' => serialize($comment),
635 'comment_approved' => 1,
636 'comment_type' => 'propertyhive_note',
637 );
638 wp_insert_comment( $data );
639
640 }
641
642 update_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', $applicant_profile_match_history );
643
644 // Add note/comment to contact
645 $comment = array(
646 'note_type' => 'mailout',
647 'method' => 'email',
648 'email_log_id' => $email_log_id
649 );
650
651 $data = array(
652 'comment_post_ID' => $contact_id,
653 'comment_author' => $current_user->display_name,
654 'comment_author_email' => 'propertyhive@noreply.com',
655 'comment_author_url' => '',
656 'comment_date' => date("Y-m-d H:i:s"),
657 'comment_content' => serialize($comment),
658 'comment_approved' => 1,
659 'comment_type' => 'propertyhive_note',
660 );
661 wp_insert_comment( $data );
662 }
663 }
664 }
665
666 }
667
668 endif;