PluginProbe
Property Hive / 1.4.53
Property Hive v1.4.53
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 1.4.62 All 260 releases
propertyhive / includes / admin / class-ph-admin-matching-properties.php

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

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