| 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($_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 = $_GET['contact_id']; |
| 32 |
|
| 33 |
$email_address = get_post_meta( $contact_id, '_email_address', TRUE ); |
| 34 |
|
| 35 |
$applicant_profile_id = $_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 |
// Handle properties to email |
| 54 |
if ( isset($_POST['email_property_id']) && !empty($_POST['email_property_id']) ) |
| 55 |
{ |
| 56 |
$subject = get_option( 'propertyhive_property_match_default_email_subject', '' ); |
| 57 |
$body = get_option( 'propertyhive_property_match_default_email_body', '' ); |
| 58 |
|
| 59 |
// We've got emails to send |
| 60 |
include 'views/html-admin-matching-properties-email.php'; |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
echo '<script>window.location.href = "' . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=2";</script>'; |
| 65 |
|
| 66 |
//header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=2' ); // properties marked as not interested |
| 67 |
//die(); |
| 68 |
} |
| 69 |
|
| 70 |
break; |
| 71 |
} |
| 72 |
case "two": |
| 73 |
{ |
| 74 |
if ( isset($_POST['email_property_id']) && !empty($_POST['email_property_id']) ) |
| 75 |
{ |
| 76 |
// Email info entered. Time to send emails |
| 77 |
$this->send_emails( |
| 78 |
$_GET['contact_id'], |
| 79 |
$_GET['applicant_profile'], |
| 80 |
explode(",", $_POST['email_property_id']), |
| 81 |
$_POST['from_name'], |
| 82 |
$_POST['from_email_address'], |
| 83 |
$_POST['subject'], |
| 84 |
$_POST['body'], |
| 85 |
$_POST['to_email_address'] |
| 86 |
); |
| 87 |
|
| 88 |
echo '<script>window.location.href = "' . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=1";</script>'; |
| 89 |
|
| 90 |
//header("Location: " . get_edit_post_link( $contact_id, 'url' ) . '&ph_message=1' ); // email sent |
| 91 |
//die(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
$applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile_id . '_match_history', TRUE ); |
| 99 |
|
| 100 |
$properties = $this->get_matching_properties( $_GET['contact_id'], $_GET['applicant_profile'] ); |
| 101 |
|
| 102 |
$do_not_email = false; |
| 103 |
$forbidden_contact_methods = get_post_meta( $_GET['contact_id'], '_forbidden_contact_methods', TRUE ); |
| 104 |
if ( is_array($forbidden_contact_methods) && in_array('email', $forbidden_contact_methods) ) |
| 105 |
{ |
| 106 |
$do_not_email = true; |
| 107 |
} |
| 108 |
|
| 109 |
include 'views/html-admin-matching-properties.php'; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private function dismiss_properties() |
| 114 |
{ |
| 115 |
$contact_id = $_GET['contact_id']; |
| 116 |
$applicant_profile_id = $_GET['applicant_profile']; |
| 117 |
|
| 118 |
// Get currently dismissed properties for this contact to decide if we need to add or remove it |
| 119 |
$dismissed_properties = get_post_meta( $contact_id, '_dismissed_properties', TRUE ); |
| 120 |
|
| 121 |
if ( !is_array($dismissed_properties) ) |
| 122 |
{ |
| 123 |
$dismissed_properties = array(); |
| 124 |
} |
| 125 |
|
| 126 |
if ( isset($_POST['not_interested_property_id']) && !empty($_POST['not_interested_property_id']) ) |
| 127 |
{ |
| 128 |
foreach ( $_POST['not_interested_property_id'] as $property_id ) |
| 129 |
{ |
| 130 |
if ( in_array($property_id, $dismissed_properties) ) |
| 131 |
{ |
| 132 |
// Already dismissed. Need to remove from array |
| 133 |
if( ($key = array_search($property_id, $dismissed_properties)) !== false ) |
| 134 |
{ |
| 135 |
unset($dismissed_properties[$key]); |
| 136 |
} |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
// Not dismissed. Add to array |
| 141 |
$dismissed_properties[] = $property_id; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$dismissed_properties = array_unique($dismissed_properties); |
| 146 |
|
| 147 |
update_post_meta( $contact_id, '_dismissed_properties', $dismissed_properties ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public function get_matching_properties( $contact_id, $applicant_profile_id, $date_added_from = '' ) |
| 152 |
{ |
| 153 |
global $post; |
| 154 |
|
| 155 |
$properties = array(); |
| 156 |
|
| 157 |
$contact = get_post($contact_id); |
| 158 |
|
| 159 |
if ( !is_null( $contact ) ) |
| 160 |
{ |
| 161 |
$dismissed_properties = get_post_meta( $contact_id, '_dismissed_properties', TRUE ); |
| 162 |
if ( !is_array($dismissed_properties) ) |
| 163 |
{ |
| 164 |
$dismissed_properties = array(); |
| 165 |
} |
| 166 |
|
| 167 |
$applicant_profile = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile_id, TRUE ); |
| 168 |
|
| 169 |
$args = array( |
| 170 |
'post_type' => 'property', |
| 171 |
'nopaging' => true, |
| 172 |
'post__not_in' => $dismissed_properties |
| 173 |
); |
| 174 |
|
| 175 |
if ( $date_added_from != '' ) |
| 176 |
{ |
| 177 |
$args['date_query'] = array( |
| 178 |
array( |
| 179 |
'after' => $date_added_from, |
| 180 |
'inclusive' => true, |
| 181 |
) |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
// Meta query |
| 186 |
$meta_query = array('relation' => 'AND'); |
| 187 |
$meta_query[] = array( |
| 188 |
'key' => '_on_market', |
| 189 |
'value' => 'yes' |
| 190 |
); |
| 191 |
if ( isset($applicant_profile['department']) && $applicant_profile['department'] == 'residential-sales' ) |
| 192 |
{ |
| 193 |
$meta_query[] = array( |
| 194 |
'key' => '_department', |
| 195 |
'value' => $applicant_profile['department'] |
| 196 |
); |
| 197 |
if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' && $applicant_profile['max_price_actual'] != 0 ) |
| 198 |
{ |
| 199 |
$meta_query[] = array( |
| 200 |
'key' => '_price_actual', |
| 201 |
'value' => $applicant_profile['max_price_actual'], |
| 202 |
'compare' => '<=', |
| 203 |
'type' => 'NUMERIC' |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
elseif ( isset($applicant_profile['department']) && $applicant_profile['department'] == 'residential-lettings' ) |
| 208 |
{ |
| 209 |
$meta_query[] = array( |
| 210 |
'key' => '_department', |
| 211 |
'value' => $applicant_profile['department'] |
| 212 |
); |
| 213 |
if ( isset($applicant_profile['max_price_actual']) && $applicant_profile['max_price_actual'] != '' && $applicant_profile['max_price_actual'] != 0 ) |
| 214 |
{ |
| 215 |
$meta_query[] = array( |
| 216 |
'key' => '_price_actual', |
| 217 |
'value' => $applicant_profile['max_price_actual'], |
| 218 |
'compare' => '<=', |
| 219 |
'type' => 'NUMERIC' |
| 220 |
); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if ( isset($applicant_profile['department']) && ( $applicant_profile['department'] == 'residential-sales' || $applicant_profile['department'] == 'residential-lettings' ) ) |
| 225 |
{ |
| 226 |
if ( isset($applicant_profile['min_beds']) && $applicant_profile['min_beds'] != '' && $applicant_profile['min_beds'] != 0 ) |
| 227 |
{ |
| 228 |
$meta_query[] = array( |
| 229 |
'key' => '_bedrooms', |
| 230 |
'value' => $applicant_profile['min_beds'], |
| 231 |
'compare' => '>=', |
| 232 |
'type' => 'NUMERIC' |
| 233 |
); |
| 234 |
} |
| 235 |
} |
| 236 |
if ( isset($applicant_profile['department']) && $applicant_profile['department'] == 'commercial' ) |
| 237 |
{ |
| 238 |
if ( isset($applicant_profile['available_as']) && is_array($applicant_profile['available_as']) && !empty($applicant_profile['available_as']) ) |
| 239 |
{ |
| 240 |
if ( in_array('sale', $applicant_profile['available_as']) && !in_array('rent', $applicant_profile['available_as']) ) |
| 241 |
{ |
| 242 |
$meta_query[] = array( |
| 243 |
'key' => '_for_sale', |
| 244 |
'value' => 'yes', |
| 245 |
); |
| 246 |
} |
| 247 |
if ( !in_array('sale', $applicant_profile['available_as']) && in_array('rent', $applicant_profile['available_as']) ) |
| 248 |
{ |
| 249 |
$meta_query[] = array( |
| 250 |
'key' => '_to_rent', |
| 251 |
'value' => 'yes', |
| 252 |
); |
| 253 |
} |
| 254 |
if ( in_array('sale', $applicant_profile['available_as']) && in_array('rent', $applicant_profile['available_as']) ) |
| 255 |
{ |
| 256 |
// Do nothing as both are ticked |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
$args['meta_query'] = $meta_query; |
| 261 |
|
| 262 |
// Term query |
| 263 |
$tax_query = array('relation' => 'AND'); |
| 264 |
if ( isset($applicant_profile['department']) && ( $applicant_profile['department'] == 'residential-sales' || $applicant_profile['department'] == 'residential-lettings' ) ) |
| 265 |
{ |
| 266 |
if ( isset($applicant_profile['property_types']) && is_array($applicant_profile['property_types']) && !empty($applicant_profile['property_types']) ) |
| 267 |
{ |
| 268 |
$tax_query[] = array( |
| 269 |
'taxonomy' => 'property_type', |
| 270 |
'field' => 'term_id', |
| 271 |
'terms' => $applicant_profile['property_types'], |
| 272 |
'operator' => 'IN', |
| 273 |
); |
| 274 |
} |
| 275 |
} |
| 276 |
if ( isset($applicant_profile['department']) && $applicant_profile['department'] == 'commercial' ) |
| 277 |
{ |
| 278 |
if ( isset($applicant_profile['commercial_property_types']) && is_array($applicant_profile['commercial_property_types']) && !empty($applicant_profile['commercial_property_types']) ) |
| 279 |
{ |
| 280 |
$tax_query[] = array( |
| 281 |
'taxonomy' => 'commercial_property_type', |
| 282 |
'field' => 'term_id', |
| 283 |
'terms' => $applicant_profile['commercial_property_types'], |
| 284 |
'operator' => 'IN', |
| 285 |
); |
| 286 |
} |
| 287 |
} |
| 288 |
if ( isset($applicant_profile['locations']) && is_array($applicant_profile['locations']) && !empty($applicant_profile['locations']) ) |
| 289 |
{ |
| 290 |
$tax_query[] = array( |
| 291 |
'taxonomy' => 'location', |
| 292 |
'field' => 'term_id', |
| 293 |
'terms' => $applicant_profile['locations'], |
| 294 |
'operator' => 'IN', |
| 295 |
); |
| 296 |
} |
| 297 |
$args['tax_query'] = $tax_query; |
| 298 |
|
| 299 |
$properties_query = new WP_Query( $args ); |
| 300 |
|
| 301 |
if ( $properties_query->have_posts() ) |
| 302 |
{ |
| 303 |
//echo '<h2>' . $properties_query->found_posts . ' matching propert' . ( ( $properties_query->found_posts != 1 ) ? 'ies' : 'y') . ' found</h2>'; |
| 304 |
|
| 305 |
while ( $properties_query->have_posts() ) |
| 306 |
{ |
| 307 |
$properties_query->the_post(); |
| 308 |
|
| 309 |
$property = new PH_Property($post->ID); |
| 310 |
|
| 311 |
$properties[] = $property; |
| 312 |
} |
| 313 |
} |
| 314 |
wp_reset_postdata(); |
| 315 |
} |
| 316 |
|
| 317 |
return $properties; |
| 318 |
} |
| 319 |
|
| 320 |
public function send_emails( $contact_id, $applicant_profile, $email_property_ids, $from_name, $from_email_address, $subject, $body, $to_email_address = '' ) |
| 321 |
{ |
| 322 |
global $wpdb; |
| 323 |
|
| 324 |
$current_user = wp_get_current_user(); |
| 325 |
|
| 326 |
if ( $to_email_address == '' ) |
| 327 |
{ |
| 328 |
$to_email_address = get_post_meta( $contact_id, '_email_address', TRUE ); |
| 329 |
} |
| 330 |
|
| 331 |
$subject = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $subject); |
| 332 |
|
| 333 |
$body = str_replace("[contact_name]", get_the_title($contact_id), $body); |
| 334 |
$body = str_replace("[property_count]", count($email_property_ids) . ' propert' . ( ( count($email_property_ids) != 1 ) ? 'ies' : 'y' ), $body); |
| 335 |
|
| 336 |
if ( strpos($body, '[properties]') !== FALSE ) |
| 337 |
{ |
| 338 |
ob_start(); |
| 339 |
if ( !empty($email_property_ids) ) |
| 340 |
{ |
| 341 |
foreach ( $email_property_ids as $email_property_id ) |
| 342 |
{ |
| 343 |
|
| 344 |
$property = new PH_Property((int)$email_property_id); |
| 345 |
ph_get_template( 'emails/applicant-match-property.php', array( 'property' => $property ) ); |
| 346 |
} |
| 347 |
} |
| 348 |
$body = str_replace("[properties]", ob_get_clean(), $body); |
| 349 |
} |
| 350 |
|
| 351 |
// Insert into email log |
| 352 |
$insert = $wpdb->insert( |
| 353 |
$wpdb->prefix . 'ph_email_log', |
| 354 |
array( |
| 355 |
'contact_id' => $contact_id, |
| 356 |
'property_ids' => serialize($email_property_ids), |
| 357 |
'applicant_profile_id' => $applicant_profile, |
| 358 |
'to_email_address' => $to_email_address, |
| 359 |
'from_name' => $from_name, |
| 360 |
'from_email_address' => $from_email_address, |
| 361 |
'subject' => stripslashes($subject), |
| 362 |
'body' => stripslashes($body), |
| 363 |
'status' => '', |
| 364 |
'send_at' => date("Y-m-d H:i:s"), |
| 365 |
'sent_by' => $current_user->ID, |
| 366 |
), |
| 367 |
array( |
| 368 |
'%d', |
| 369 |
'%s', |
| 370 |
'%d', |
| 371 |
'%s', |
| 372 |
'%s', |
| 373 |
'%s', |
| 374 |
'%s', |
| 375 |
'%s', |
| 376 |
'%s', |
| 377 |
'%s', |
| 378 |
'%d', |
| 379 |
) |
| 380 |
); |
| 381 |
|
| 382 |
if ( $insert !== FALSE ) |
| 383 |
{ |
| 384 |
$email_log_id = $wpdb->insert_id; |
| 385 |
|
| 386 |
// Insert properties into applicant match history |
| 387 |
$applicant_profile_match_history = get_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', TRUE ); |
| 388 |
if ( !is_array($applicant_profile_match_history) ) |
| 389 |
{ |
| 390 |
$applicant_profile_match_history = array(); |
| 391 |
} |
| 392 |
|
| 393 |
if ( is_array($email_property_ids) && !empty($email_property_ids) ) |
| 394 |
{ |
| 395 |
foreach ( $email_property_ids as $email_property_id ) |
| 396 |
{ |
| 397 |
if ( !isset($applicant_profile_match_history[$email_property_id]) ) |
| 398 |
{ |
| 399 |
$applicant_profile_match_history[$email_property_id] = array(); |
| 400 |
} |
| 401 |
|
| 402 |
$applicant_profile_match_history[$email_property_id][] = array( |
| 403 |
'date' => date("Y-m-d H:i:s"), |
| 404 |
'method' => 'email', |
| 405 |
'email_log_id' => $email_log_id, |
| 406 |
); |
| 407 |
|
| 408 |
// Add note/comment to property |
| 409 |
$comment = array( |
| 410 |
'note_type' => 'mailout', |
| 411 |
'method' => 'email', |
| 412 |
'email_log_id' => $email_log_id |
| 413 |
); |
| 414 |
|
| 415 |
$data = array( |
| 416 |
'comment_post_ID' => $email_property_id, |
| 417 |
'comment_author' => $current_user->display_name, |
| 418 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 419 |
'comment_author_url' => '', |
| 420 |
'comment_date' => date("Y-m-d H:i:s"), |
| 421 |
'comment_content' => serialize($comment), |
| 422 |
'comment_approved' => 1, |
| 423 |
'comment_type' => 'propertyhive_note', |
| 424 |
); |
| 425 |
wp_insert_comment( $data ); |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
update_post_meta( $contact_id, '_applicant_profile_' . $applicant_profile . '_match_history', $applicant_profile_match_history ); |
| 430 |
|
| 431 |
// Add note/comment to contact |
| 432 |
$comment = array( |
| 433 |
'note_type' => 'mailout', |
| 434 |
'method' => 'email', |
| 435 |
'email_log_id' => $email_log_id |
| 436 |
); |
| 437 |
|
| 438 |
$data = array( |
| 439 |
'comment_post_ID' => $contact_id, |
| 440 |
'comment_author' => $current_user->display_name, |
| 441 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 442 |
'comment_author_url' => '', |
| 443 |
'comment_date' => date("Y-m-d H:i:s"), |
| 444 |
'comment_content' => serialize($comment), |
| 445 |
'comment_approved' => 1, |
| 446 |
'comment_type' => 'propertyhive_note', |
| 447 |
); |
| 448 |
wp_insert_comment( $data ); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
endif; |