| 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( esc_html(__( '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 |
$from_email_option = get_option( 'propertyhive_property_match_default_from', '' ); |
| 56 |
if( $from_email_option == 'default_from_email' ) |
| 57 |
{ |
| 58 |
$from_email_address = get_option('propertyhive_email_from_address', ''); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
$current_user = wp_get_current_user(); |
| 63 |
$from_email_address = $current_user->user_email; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
$nothing_to_send = apply_filters( 'propertyhive_applicant_match_nothing_to_send', $nothing_to_send ); |
| 68 |
|
| 69 |
if ( $nothing_to_send != true ) |
| 70 |
{ |
| 71 |
?> |
| 72 |
<div class="wrap propertyhive"> |
| 73 |
|
| 74 |
<div id="poststuff"> |
| 75 |
|
| 76 |
<form method="post" id="mainform" action="" enctype="multipart/form-data"> |
| 77 |
<?php |
| 78 |
if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) ) |
| 79 |
{ |
| 80 |
// We've got emails to send |
| 81 |
include 'views/html-admin-matching-applicants-email.php'; |
| 82 |
} |
| 83 |
|
| 84 |
do_action( 'propertyhive_applicant_match_step_two', $property_id ); |
| 85 |
?> |
| 86 |
<p class="submit"> |
| 87 |
|
| 88 |
<input name="save" class="button-primary" type="submit" value="<?php echo esc_attr(__( 'Send Matches', 'propertyhive' )); ?>" /> |
| 89 |
<?php if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) ) { ?> |
| 90 |
<input name="preview" id="preview_email" class="button" type="button" value="<?php echo esc_attr(__( 'Preview Email', 'propertyhive' )); ?>" /> |
| 91 |
<?php } ?> |
| 92 |
|
| 93 |
<input type="hidden" name="step" value="two" /> |
| 94 |
<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']) ) ? esc_attr(implode(",", ph_clean($_POST['email_contact_applicant_profile_id']))) : ''; ?>" /> |
| 95 |
<?php do_action( 'propertyhive_applicant_match_step_two_hidden_fields' ); ?> |
| 96 |
<?php wp_nonce_field( 'propertyhive-matching-applicants' ); ?> |
| 97 |
|
| 98 |
</p> |
| 99 |
|
| 100 |
<p> |
| 101 |
<?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' ); |
| 102 |
?> |
| 103 |
</p> |
| 104 |
|
| 105 |
</form> |
| 106 |
|
| 107 |
</div> |
| 108 |
|
| 109 |
</div> |
| 110 |
|
| 111 |
<script> |
| 112 |
|
| 113 |
jQuery(document).ready(function() |
| 114 |
{ |
| 115 |
jQuery('#preview_email').click(function(e) |
| 116 |
{ |
| 117 |
e.preventDefault(); |
| 118 |
|
| 119 |
showPreview(); |
| 120 |
}); |
| 121 |
}); |
| 122 |
|
| 123 |
function showPreview() |
| 124 |
{ |
| 125 |
jQuery('#mainform').attr('target', '_blank'); |
| 126 |
jQuery('#mainform').attr('action', '<?php echo admin_url( '?preview_propertyhive_email=true&property_id=' . (int)$_GET['property_id']); ?>'); |
| 127 |
|
| 128 |
jQuery('#mainform').submit(); |
| 129 |
jQuery('#mainform').attr('target', '_self'); |
| 130 |
jQuery('#mainform').attr('action', ''); |
| 131 |
} |
| 132 |
|
| 133 |
</script> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
if ( $nothing_to_send == true ) |
| 138 |
{ |
| 139 |
echo '<script>window.location.href = "' . get_edit_post_link( $property_id, 'url' ) . '&ph_message=2";</script>'; |
| 140 |
|
| 141 |
//header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=2' ); // properties marked as not interested |
| 142 |
//die(); |
| 143 |
} |
| 144 |
|
| 145 |
break; |
| 146 |
} |
| 147 |
case "two": |
| 148 |
{ |
| 149 |
if ( isset($_POST['email_contact_applicant_profile_id']) && !empty($_POST['email_contact_applicant_profile_id']) ) |
| 150 |
{ |
| 151 |
$email_contact_applicant_profile_id = explode(",", sanitize_text_field($_POST['email_contact_applicant_profile_id'])); |
| 152 |
|
| 153 |
foreach ( $email_contact_applicant_profile_id as $contact_applicant_profile_id ) |
| 154 |
{ |
| 155 |
$explode_contact_applicant_profile_id = explode("|", $contact_applicant_profile_id); |
| 156 |
$contact_id = $explode_contact_applicant_profile_id[0]; |
| 157 |
$applicant_profile_id = $explode_contact_applicant_profile_id[1]; |
| 158 |
|
| 159 |
$email_address = get_post_meta( (int)$contact_id, '_email_address', TRUE ); |
| 160 |
|
| 161 |
$to_email_addresses = explode(",", $email_address); |
| 162 |
$new_to_email_addresses = array(); |
| 163 |
foreach ( $to_email_addresses as $to_email_address) |
| 164 |
{ |
| 165 |
$new_to_email_addresses[] = sanitize_email($to_email_address); |
| 166 |
} |
| 167 |
|
| 168 |
$cc_email_addresses = explode(",", $_POST['cc_email_address']); |
| 169 |
$new_cc_email_addresses = array(); |
| 170 |
foreach ( $cc_email_addresses as $cc_email_address ) |
| 171 |
{ |
| 172 |
$new_cc_email_addresses[] = sanitize_email($cc_email_address); |
| 173 |
} |
| 174 |
|
| 175 |
$bcc_email_addresses = explode(",", $_POST['bcc_email_address']); |
| 176 |
$new_bcc_email_addresses = array(); |
| 177 |
foreach ( $bcc_email_addresses as $bcc_email_address ) |
| 178 |
{ |
| 179 |
$new_bcc_email_addresses[] = sanitize_email($bcc_email_address); |
| 180 |
} |
| 181 |
|
| 182 |
$allowed_tags = array( |
| 183 |
'strong' => array(), |
| 184 |
'span' => array(), |
| 185 |
'em' => array(), |
| 186 |
'h1' => array(), |
| 187 |
'h2' => array(), |
| 188 |
'h3' => array(), |
| 189 |
'h4' => array(), |
| 190 |
'h5' => array(), |
| 191 |
'h6' => array(), |
| 192 |
'i' => array(), |
| 193 |
'u' => array(), |
| 194 |
'b' => array(), |
| 195 |
'a' => array( |
| 196 |
'href' => array(), |
| 197 |
'target' => array(), |
| 198 |
), |
| 199 |
); |
| 200 |
$allowed_tags = apply_filters( 'propertyhive_match_email_allowed_tags', $allowed_tags ); |
| 201 |
|
| 202 |
$body = wp_kses(wp_unslash($_POST['body']), $allowedposttags); |
| 203 |
|
| 204 |
// Email info entered. Time to send emails |
| 205 |
$this->send_emails( |
| 206 |
(int)$contact_id, |
| 207 |
(int)$applicant_profile_id, |
| 208 |
array($property_id), |
| 209 |
ph_clean(wp_unslash($_POST['from_name'])), |
| 210 |
sanitize_email(wp_unslash($_POST['from_email_address'])), |
| 211 |
ph_clean(wp_unslash($_POST['subject'])), |
| 212 |
$body, |
| 213 |
implode(",", $new_to_email_addresses), |
| 214 |
implode(",", $new_cc_email_addresses), |
| 215 |
implode(",", $new_bcc_email_addresses) |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
//header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=1' ); // email sent |
| 220 |
//die(); |
| 221 |
} |
| 222 |
|
| 223 |
do_action( 'propertyhive_applicant_match_step_send', $property_id ); |
| 224 |
|
| 225 |
echo '<script>window.location.href = "' . get_edit_post_link( $property_id, 'url' ) . '&ph_message=1";</script>'; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
$applicants = $this->get_matching_applicants( (int)$_GET['property_id'] ); |
| 232 |
|
| 233 |
$on_market_change_date = $property->_on_market_change_date; |
| 234 |
$price_change_date = $property->_price_change_date; |
| 235 |
|
| 236 |
include 'views/html-admin-matching-applicants.php'; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
private function dismiss_properties() |
| 241 |
{ |
| 242 |
$property_id = (int)$_GET['property_id']; |
| 243 |
|
| 244 |
if ( isset($_POST['not_interested_contact_applicant_profile_id']) && !empty($_POST['not_interested_contact_applicant_profile_id']) ) |
| 245 |
{ |
| 246 |
foreach ( $_POST['not_interested_contact_applicant_profile_id'] as $contact_applicant_profile_id ) |
| 247 |
{ |
| 248 |
$explode_contact_applicant_profile_id = explode("|", $contact_applicant_profile_id); |
| 249 |
|
| 250 |
$contact_id = $explode_contact_applicant_profile_id[0]; |
| 251 |
$applicant_profile_id = $explode_contact_applicant_profile_id[1]; |
| 252 |
|
| 253 |
// Get currently dismissed properties for this contact to decide if we need to add or remove it |
| 254 |
$dismissed_properties = get_post_meta( $contact_id, '_dismissed_properties', TRUE ); |
| 255 |
|
| 256 |
if ( !is_array($dismissed_properties) ) |
| 257 |
{ |
| 258 |
$dismissed_properties = array(); |
| 259 |
} |
| 260 |
|
| 261 |
if ( in_array((int)$property_id, $dismissed_properties) ) |
| 262 |
{ |
| 263 |
// Already dismissed. Need to remove from array |
| 264 |
if( ($key = array_search((int)$property_id, $dismissed_properties)) !== false ) |
| 265 |
{ |
| 266 |
unset($dismissed_properties[$key]); |
| 267 |
} |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
// Not dismissed. Add to array |
| 272 |
$dismissed_properties[] = (int)$property_id; |
| 273 |
} |
| 274 |
|
| 275 |
$dismissed_properties = array_unique($dismissed_properties); |
| 276 |
|
| 277 |
update_post_meta( $contact_id, '_dismissed_properties', $dismissed_properties ); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
public function get_matching_applicants( $property_id ) |
| 283 |
{ |
| 284 |
global $post; |
| 285 |
|
| 286 |
$hot_applicants = array(); |
| 287 |
$applicants = array(); |
| 288 |
|
| 289 |
$property = new PH_Property((int)$property_id); |
| 290 |
|
| 291 |
if ( $property !== FALSE ) |
| 292 |
{ |
| 293 |
$property_types = array(); |
| 294 |
$prefix = $property->department == 'commercial' || ph_get_custom_department_based_on($property->department) == 'commercial' ? 'commercial_' : ''; |
| 295 |
$term_list = wp_get_post_terms($property_id, $prefix . 'property_type', array("fields" => "all")); |
| 296 |
if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) ) |
| 297 |
{ |
| 298 |
foreach ( $term_list as $term ) |
| 299 |
{ |
| 300 |
$property_types[] = $term->term_id; |
| 301 |
|
| 302 |
if ( $term->parent != 0 ) |
| 303 |
{ |
| 304 |
$parent = get_term_by( 'id', $term->parent , $prefix . 'property_type' ); |
| 305 |
$property_types[] = $parent->term_id; |
| 306 |
|
| 307 |
if ( $parent->parent != 0 ) |
| 308 |
{ |
| 309 |
$parent = get_term_by( 'id', $parent->parent , $prefix . 'property_type' ); |
| 310 |
$property_types[] = $parent->term_id; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if ( get_option('propertyhive_applicant_locations_type') != 'text' ) |
| 317 |
{ |
| 318 |
$locations = array(); |
| 319 |
$term_list = wp_get_post_terms($property_id, 'location', array("fields" => "all")); |
| 320 |
if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) ) |
| 321 |
{ |
| 322 |
foreach ( $term_list as $term ) |
| 323 |
{ |
| 324 |
$locations[] = $term->term_id; |
| 325 |
|
| 326 |
if ( $term->parent != 0 ) |
| 327 |
{ |
| 328 |
$parent = get_term_by( 'id', $term->parent , 'location' ); |
| 329 |
$locations[] = $parent->term_id; |
| 330 |
|
| 331 |
if ( $parent->parent != 0 ) |
| 332 |
{ |
| 333 |
$parent = get_term_by( 'id', $parent->parent , 'location' ); |
| 334 |
$locations[] = $parent->term_id; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
$floor_area_from = $property->floor_area_from_sqft; |
| 342 |
if ( $floor_area_from === '' ) |
| 343 |
{ |
| 344 |
$floor_area_from = 0; |
| 345 |
} |
| 346 |
$floor_area_to = $property->floor_area_to_sqft; |
| 347 |
if ( $floor_area_to === '' ) |
| 348 |
{ |
| 349 |
$floor_area_to = 99999999999; |
| 350 |
} |
| 351 |
|
| 352 |
$percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' ); |
| 353 |
$percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' ); |
| 354 |
|
| 355 |
$args = array( |
| 356 |
'post_type' => 'contact', |
| 357 |
'nopaging' => true, |
| 358 |
); |
| 359 |
|
| 360 |
// Meta query |
| 361 |
$meta_query = array(); |
| 362 |
$meta_query[] = array( |
| 363 |
'key' => '_contact_types', |
| 364 |
'value' => 'applicant', |
| 365 |
'compare' => 'LIKE' |
| 366 |
); |
| 367 |
|
| 368 |
$args['meta_query'] = $meta_query; |
| 369 |
|
| 370 |
$contacts_query = new WP_Query( $args ); |
| 371 |
|
| 372 |
if ( $contacts_query->have_posts() ) |
| 373 |
{ |
| 374 |
while ( $contacts_query->have_posts() ) |
| 375 |
{ |
| 376 |
$contacts_query->the_post(); |
| 377 |
|
| 378 |
$contact = new PH_Contact(get_the_ID()); |
| 379 |
|
| 380 |
$dismissed_properties = get_post_meta( get_the_ID(), '_dismissed_properties', TRUE ); |
| 381 |
|
| 382 |
if ( is_array($dismissed_properties) && in_array($property_id, $dismissed_properties) ) |
| 383 |
{ |
| 384 |
// This property is dismissed |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
$num_applicant_profiles = get_post_meta( get_the_ID(), '_applicant_profiles', TRUE ); |
| 389 |
|
| 390 |
for ( $i = 0; $i < $num_applicant_profiles; ++$i ) |
| 391 |
{ |
| 392 |
$applicant_profile = get_post_meta( get_the_ID(), '_applicant_profile_' . $i, TRUE ); |
| 393 |
|
| 394 |
if ( isset($applicant_profile['send_matching_properties']) && $applicant_profile['send_matching_properties'] == 'yes' ) |
| 395 |
{ |
| 396 |
$elements_checked = 0; |
| 397 |
$matching_elements = 0; |
| 398 |
|
| 399 |
++$elements_checked; |
| 400 |
if ( $applicant_profile['department'] != $property->department ) |
| 401 |
{ |
| 402 |
|
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
++$matching_elements; |
| 407 |
|
| 408 |
if ( $property->department != 'commercial' && ph_get_custom_department_based_on($property->department) != 'commercial' ) |
| 409 |
{ |
| 410 |
if ( $property->department == 'residential-sales' || ph_get_custom_department_based_on($property->department) == 'residential-sales' ) |
| 411 |
{ |
| 412 |
if ( $percentage_lower != '' && $percentage_higher != '' ) |
| 413 |
{ |
| 414 |
$match_price_range_lower = ''; |
| 415 |
if ( !isset($applicant_profile['match_price_range_lower_actual']) || ( isset($applicant_profile['match_price_range_lower_actual']) && $applicant_profile['match_price_range_lower_actual'] == '' ) ) |
| 416 |
{ |
| 417 |
if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' ) |
| 418 |
{ |
| 419 |
if ( $percentage_lower != '' ) |
| 420 |
{ |
| 421 |
$match_price_range_lower = $applicant_profile['max_price_actual'] - ( $applicant_profile['max_price_actual'] * ( $percentage_lower / 100 ) ); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
else |
| 426 |
{ |
| 427 |
$match_price_range_lower = $applicant_profile['match_price_range_lower_actual']; |
| 428 |
} |
| 429 |
|
| 430 |
$match_price_range_higher = ''; |
| 431 |
if ( !isset($applicant_profile['match_price_range_higher_actual']) || ( isset($applicant_profile['match_price_range_higher_actual']) && $applicant_profile['match_price_range_higher_actual'] == '' ) ) |
| 432 |
{ |
| 433 |
if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' ) |
| 434 |
{ |
| 435 |
if ( $percentage_higher != '' ) |
| 436 |
{ |
| 437 |
$match_price_range_higher = $applicant_profile['max_price_actual'] + ( $applicant_profile['max_price_actual'] * ( $percentage_higher / 100 ) ); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
else |
| 442 |
{ |
| 443 |
$match_price_range_higher = $applicant_profile['match_price_range_higher_actual']; |
| 444 |
} |
| 445 |
|
| 446 |
if ( |
| 447 |
( $match_price_range_lower == '' && $match_price_range_higher == '' ) || |
| 448 |
( |
| 449 |
$property->_price_actual >= $match_price_range_lower && |
| 450 |
$property->_price_actual <= $match_price_range_higher |
| 451 |
) |
| 452 |
) |
| 453 |
{ |
| 454 |
++$matching_elements; |
| 455 |
} |
| 456 |
++$elements_checked; |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
if ( |
| 461 |
$applicant_profile['max_price_actual'] == '' || |
| 462 |
$property->_price_actual <= $applicant_profile['max_price_actual'] |
| 463 |
) |
| 464 |
{ |
| 465 |
++$matching_elements; |
| 466 |
} |
| 467 |
++$elements_checked; |
| 468 |
} |
| 469 |
} |
| 470 |
else |
| 471 |
{ |
| 472 |
if ( |
| 473 |
$applicant_profile['max_price_actual'] == '' || |
| 474 |
$property->_price_actual <= $applicant_profile['max_price_actual'] |
| 475 |
) |
| 476 |
{ |
| 477 |
++$matching_elements; |
| 478 |
} |
| 479 |
++$elements_checked; |
| 480 |
} |
| 481 |
|
| 482 |
if ( |
| 483 |
$applicant_profile['min_beds'] == '' || |
| 484 |
$property->_bedrooms >= $applicant_profile['min_beds'] |
| 485 |
) |
| 486 |
{ |
| 487 |
++$matching_elements; |
| 488 |
} |
| 489 |
++$elements_checked; |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
if ( $applicant_profile['min_floor_area'] === '' ) |
| 494 |
{ |
| 495 |
$applicant_profile['min_floor_area'] = 0; |
| 496 |
} |
| 497 |
if ( $applicant_profile['max_floor_area'] === '' ) |
| 498 |
{ |
| 499 |
$applicant_profile['max_floor_area'] = 99999999999; |
| 500 |
} |
| 501 |
|
| 502 |
if ( |
| 503 |
$applicant_profile['min_floor_area'] <= $floor_area_to && |
| 504 |
$applicant_profile['max_floor_area'] >= $floor_area_from |
| 505 |
) |
| 506 |
{ |
| 507 |
++$matching_elements; |
| 508 |
} |
| 509 |
++$elements_checked; |
| 510 |
} |
| 511 |
|
| 512 |
if ( |
| 513 |
!isset($applicant_profile[$prefix . 'property_types']) || |
| 514 |
( isset($applicant_profile[$prefix . 'property_types']) && empty($applicant_profile[$prefix . 'property_types']) ) |
| 515 |
) |
| 516 |
{ |
| 517 |
++$matching_elements; |
| 518 |
} |
| 519 |
elseif ( isset($applicant_profile[$prefix . 'property_types']) && !empty($applicant_profile[$prefix . 'property_types']) ) |
| 520 |
{ |
| 521 |
foreach ( $applicant_profile[$prefix . 'property_types'] as $applicant_property_type ) |
| 522 |
{ |
| 523 |
if ( in_array($applicant_property_type, $property_types) ) |
| 524 |
{ |
| 525 |
++$matching_elements; |
| 526 |
break; |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
++$elements_checked; |
| 531 |
|
| 532 |
if ( apply_filters( 'propertyhive_location_used_when_matching_applicants', TRUE, $applicant_profile ) === TRUE ) |
| 533 |
{ |
| 534 |
if ( get_option('propertyhive_applicant_locations_type') != 'text' ) |
| 535 |
{ |
| 536 |
if ( |
| 537 |
!isset($applicant_profile['locations']) || |
| 538 |
( isset($applicant_profile['locations']) && empty($applicant_profile['locations']) ) |
| 539 |
) |
| 540 |
{ |
| 541 |
++$matching_elements; |
| 542 |
} |
| 543 |
elseif ( isset($applicant_profile['locations']) && !empty($applicant_profile['locations']) ) |
| 544 |
{ |
| 545 |
foreach ( $applicant_profile['locations'] as $applicant_location ) |
| 546 |
{ |
| 547 |
if ( in_array($applicant_location, $locations) ) |
| 548 |
{ |
| 549 |
++$matching_elements; |
| 550 |
break; |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
else |
| 556 |
{ |
| 557 |
if ( !isset($applicant_profile['location_text']) || trim($applicant_profile['location_text']) == '' ) |
| 558 |
{ |
| 559 |
++$matching_elements; |
| 560 |
} |
| 561 |
else |
| 562 |
{ |
| 563 |
if ( propertyhive_is_location_in_address($property, $applicant_profile['location_text']) === true ) |
| 564 |
{ |
| 565 |
++$matching_elements; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
++$elements_checked; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
$additional_checks = apply_filters( 'propertyhive_matching_applicants_check', true, $property, get_the_ID(), $applicant_profile ); |
| 574 |
|
| 575 |
if ( $additional_checks === true && $matching_elements == $elements_checked ) |
| 576 |
{ |
| 577 |
$applicant_profile['applicant_profile_id'] = $i; |
| 578 |
|
| 579 |
// Matched all criteria |
| 580 |
if ( isset($applicant_profile['grading']) && $applicant_profile['grading'] == 'hot' ) |
| 581 |
{ |
| 582 |
$hot_applicants[] = array( |
| 583 |
'contact_id' => get_the_ID(), |
| 584 |
'applicant_profile' => $applicant_profile, |
| 585 |
); |
| 586 |
} |
| 587 |
else |
| 588 |
{ |
| 589 |
$applicants[] = array( |
| 590 |
'contact_id' => get_the_ID(), |
| 591 |
'applicant_profile' => $applicant_profile, |
| 592 |
); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
wp_reset_postdata(); |
| 602 |
} |
| 603 |
|
| 604 |
return array_merge($hot_applicants, $applicants); |
| 605 |
} |
| 606 |
|
| 607 |
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 = '' ) |
| 608 |
{ |
| 609 |
global $wpdb; |
| 610 |
|
| 611 |
$current_user = wp_get_current_user(); |
| 612 |
|
| 613 |
$applicant_profile_details = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile, TRUE ); |
| 614 |
|
| 615 |
$contact = new PH_Contact($contact_id); |
| 616 |
if ( $to_email_address == '' ) |
| 617 |
{ |
| 618 |
$to_email_address = $contact->email_address; |
| 619 |
} |
| 620 |
|
| 621 |
$subject = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $subject); |
| 622 |
|
| 623 |
$body = str_replace("[contact_name]", $contact->post_title, $body); |
| 624 |
$body = str_replace("[contact_dear]", $contact->dear(), $body); |
| 625 |
$body = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $body); |
| 626 |
|
| 627 |
$office_counts = array(); |
| 628 |
|
| 629 |
if ( strpos($body, '[properties]') !== FALSE ) |
| 630 |
{ |
| 631 |
ob_start(); |
| 632 |
|
| 633 |
if ( !empty($email_property_ids) ) |
| 634 |
{ |
| 635 |
foreach ( $email_property_ids as $email_property_id ) |
| 636 |
{ |
| 637 |
$property = new PH_Property((int)$email_property_id); |
| 638 |
|
| 639 |
if ( $property->office_id != '' && $property->office_id != 0 ) |
| 640 |
{ |
| 641 |
if ( !isset($office_counts[$property->office_id]) ) { $office_counts[$property->office_id] = 0; } |
| 642 |
++$office_counts[$property->office_id]; |
| 643 |
} |
| 644 |
|
| 645 |
ph_get_template( 'emails/applicant-match-property.php', array( 'property' => $property ) ); |
| 646 |
} |
| 647 |
} |
| 648 |
$body = str_replace("[properties]", ob_get_clean(), $body); |
| 649 |
} |
| 650 |
|
| 651 |
$office_name = ''; |
| 652 |
$office_email_address = ''; |
| 653 |
|
| 654 |
$office_id = get_user_meta($current_user->ID, 'office_id', TRUE); |
| 655 |
if ($office_id == '') |
| 656 |
{ |
| 657 |
// No office against user. Use email address of office with most properties |
| 658 |
if ( !empty($office_counts) ) |
| 659 |
{ |
| 660 |
arsort($office_counts); |
| 661 |
reset($office_counts); |
| 662 |
$office_id = key($office_counts); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
if ( !empty($office_id) ) |
| 667 |
{ |
| 668 |
$office_name = get_the_title($office_id); |
| 669 |
$office_email_address = get_post_meta( $office_id, '_office_email_address_' . str_replace("residential-", "", $applicant_profile_details['department']), TRUE ); |
| 670 |
} |
| 671 |
|
| 672 |
$body = str_replace("[office_name]", $office_name, $body); |
| 673 |
$body = str_replace("[office_email_address]", $office_email_address, $body); |
| 674 |
|
| 675 |
$body = str_replace("[negotiator_name]", $current_user->display_name, $body); |
| 676 |
$body = str_replace("[negotiator_email_address]", $current_user->user_email, $body); |
| 677 |
|
| 678 |
$body = stripslashes($body); |
| 679 |
|
| 680 |
if (extension_loaded('zlib')) |
| 681 |
{ |
| 682 |
$compressed_body = @gzcompress($body); |
| 683 |
if ( $compressed_body !== false ) |
| 684 |
{ |
| 685 |
$body = $compressed_body; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
// Insert into email log |
| 690 |
$insert = $wpdb->insert( |
| 691 |
$wpdb->prefix . 'ph_email_log', |
| 692 |
array( |
| 693 |
'contact_id' => $contact_id, |
| 694 |
'property_ids' => serialize($email_property_ids), |
| 695 |
'applicant_profile_id' => $applicant_profile, |
| 696 |
'to_email_address' => $to_email_address, |
| 697 |
'cc_email_address' => $cc_email_address, |
| 698 |
'bcc_email_address' => $bcc_email_address, |
| 699 |
'from_name' => $from_name, |
| 700 |
'from_email_address' => $from_email_address, |
| 701 |
'subject' => stripslashes($subject), |
| 702 |
'body' => $body, |
| 703 |
'status' => '', |
| 704 |
'send_at' => date("Y-m-d H:i:s"), |
| 705 |
'sent_by' => $current_user->ID, |
| 706 |
), |
| 707 |
array( |
| 708 |
'%d', |
| 709 |
'%s', |
| 710 |
'%d', |
| 711 |
'%s', |
| 712 |
'%s', |
| 713 |
'%s', |
| 714 |
'%s', |
| 715 |
'%s', |
| 716 |
'%s', |
| 717 |
'%s', |
| 718 |
'%s', |
| 719 |
'%s', |
| 720 |
'%d', |
| 721 |
) |
| 722 |
); |
| 723 |
|
| 724 |
if ( $insert !== FALSE ) |
| 725 |
{ |
| 726 |
$email_log_id = $wpdb->insert_id; |
| 727 |
|
| 728 |
// Insert properties into applicant match history |
| 729 |
$applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', TRUE ); |
| 730 |
if ( !is_array($applicant_profile_match_history) ) |
| 731 |
{ |
| 732 |
$applicant_profile_match_history = array(); |
| 733 |
} |
| 734 |
|
| 735 |
if ( is_array($email_property_ids) && !empty($email_property_ids) ) |
| 736 |
{ |
| 737 |
foreach ( $email_property_ids as $email_property_id ) |
| 738 |
{ |
| 739 |
if ( !isset($applicant_profile_match_history[$email_property_id]) ) |
| 740 |
{ |
| 741 |
$applicant_profile_match_history[$email_property_id] = array(); |
| 742 |
} |
| 743 |
|
| 744 |
$applicant_profile_match_history[$email_property_id][] = array( |
| 745 |
'date' => date("Y-m-d H:i:s"), |
| 746 |
'method' => 'email', |
| 747 |
'email_log_id' => $email_log_id, |
| 748 |
); |
| 749 |
|
| 750 |
// Add note/comment to property |
| 751 |
$comment = array( |
| 752 |
'note_type' => 'mailout', |
| 753 |
'method' => 'email', |
| 754 |
'email_log_id' => $email_log_id |
| 755 |
); |
| 756 |
|
| 757 |
$data = array( |
| 758 |
'comment_post_ID' => $email_property_id, |
| 759 |
'comment_author' => $current_user->display_name, |
| 760 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 761 |
'comment_author_url' => '', |
| 762 |
'comment_date' => date("Y-m-d H:i:s"), |
| 763 |
'comment_content' => serialize($comment), |
| 764 |
'comment_approved' => 1, |
| 765 |
'comment_type' => 'propertyhive_note', |
| 766 |
); |
| 767 |
wp_insert_comment( $data ); |
| 768 |
|
| 769 |
} |
| 770 |
|
| 771 |
update_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', $applicant_profile_match_history ); |
| 772 |
|
| 773 |
// Add note/comment to contact |
| 774 |
$comment = array( |
| 775 |
'note_type' => 'mailout', |
| 776 |
'method' => 'email', |
| 777 |
'email_log_id' => $email_log_id |
| 778 |
); |
| 779 |
|
| 780 |
$data = array( |
| 781 |
'comment_post_ID' => $contact_id, |
| 782 |
'comment_author' => $current_user->display_name, |
| 783 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 784 |
'comment_author_url' => '', |
| 785 |
'comment_date' => date("Y-m-d H:i:s"), |
| 786 |
'comment_content' => serialize($comment), |
| 787 |
'comment_approved' => 1, |
| 788 |
'comment_type' => 'propertyhive_note', |
| 789 |
); |
| 790 |
wp_insert_comment( $data ); |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
} |
| 796 |
|
| 797 |
endif; |