PluginProbe
Property Hive / 1.4.48
Property Hive v1.4.48
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.48, at includes/admin/class-ph-admin-matching-applicants.php

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