| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
$args = array( |
| 8 |
'post_id' => (int)$post->ID, |
| 9 |
'type' => 'propertyhive_note', |
| 10 |
'meta_query' => array( |
| 11 |
array( |
| 12 |
'key' => 'related_to', |
| 13 |
'value' => '"' . (int)$post->ID . '"', |
| 14 |
'compare' => 'LIKE', |
| 15 |
), |
| 16 |
) |
| 17 |
); |
| 18 |
|
| 19 |
if ( isset($_POST['pinned']) && (int)$_POST['pinned'] == 1 ) |
| 20 |
{ |
| 21 |
$args['search'] = '"pinned";s:1:"1"'; |
| 22 |
} |
| 23 |
|
| 24 |
$notes = get_comments( $args ); |
| 25 |
|
| 26 |
$pinned_notes = array(); |
| 27 |
$unpinned_notes = array(); |
| 28 |
|
| 29 |
if ( !empty($notes) ) |
| 30 |
{ |
| 31 |
foreach( $notes as $note ) |
| 32 |
{ |
| 33 |
$comment_content = @unserialize($note->comment_content, ['allowed_classes' => false]); |
| 34 |
|
| 35 |
if ( $comment_content === false ) |
| 36 |
{ |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$note_body = 'Unknown note type'; |
| 41 |
switch ( $comment_content['note_type'] ) |
| 42 |
{ |
| 43 |
case "mailout": |
| 44 |
{ |
| 45 |
if ( isset($comment_content['method']) && $comment_content['method'] == 'email' && isset($comment_content['email_log_id']) ) |
| 46 |
{ |
| 47 |
$email_log = $wpdb->get_row( "SELECT * FROM " . $wpdb->prefix . "ph_email_log WHERE email_id = '" . $comment_content['email_log_id'] . "'" ); |
| 48 |
|
| 49 |
if ( null !== $email_log ) |
| 50 |
{ |
| 51 |
$next_cron_run = ''; |
| 52 |
$email_status = ''; |
| 53 |
$note_suffix = ''; |
| 54 |
switch ($email_log->status) { |
| 55 |
case '': |
| 56 |
$next_cron_run = $next_cron_run ?: propertyhive_human_time_difference( wp_next_scheduled( 'propertyhive_process_email_log' ) ); |
| 57 |
$email_status = __( 'queued', 'propertyhive' ); |
| 58 |
$note_suffix = '<em>(' . __( 'Due to be sent', 'propertyhive' ) . ' ' . $next_cron_run . ')</em>'; |
| 59 |
break; |
| 60 |
case 'fail1': |
| 61 |
case 'fail2': |
| 62 |
$email_status = '<b>' . __( 'failed', 'propertyhive' ) . '</b>'; |
| 63 |
break; |
| 64 |
} |
| 65 |
$note_body = ''; |
| 66 |
if ($section == 'property') |
| 67 |
{ |
| 68 |
$note_body .= 'Included in ' . $email_status . ' email mailout to ' . get_the_title($email_log->contact_id) . '. ' . $note_suffix; |
| 69 |
} |
| 70 |
elseif ($section == 'contact') |
| 71 |
{ |
| 72 |
$property_ids = @unserialize($email_log->property_ids, ['allowed_classes' => false]); |
| 73 |
if ( $property_ids !== false ) |
| 74 |
{ |
| 75 |
$note_body .= count($property_ids) . ' propert' . ( (count($property_ids) != 1) ? 'ies' : 'y' ) . ' included in ' . $email_status . ' email mailout. ' . $note_suffix; |
| 76 |
} |
| 77 |
} |
| 78 |
$note_body .= ' <a href="' . wp_nonce_url( admin_url('?view_propertyhive_email=' . $comment_content['email_log_id'] . '&email_id=' . $comment_content['email_log_id'] ), 'view-email' ) . '" target="_blank">View Mailout</a>'; |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
$keep_logs_days = (string)apply_filters( 'propertyhive_keep_email_logs_days', '3650' ); // 10 years |
| 83 |
|
| 84 |
// Revert back to 3650 days if anything other than numbers has been passed |
| 85 |
// This prevent SQL injection and errors |
| 86 |
if ( !preg_match("/^\d+$/", $keep_logs_days) ) |
| 87 |
{ |
| 88 |
$keep_logs_days = '3650'; |
| 89 |
} |
| 90 |
|
| 91 |
$note_body = 'The details of the email have since been deleted as we remove details of emails sent more then ' . $keep_logs_days . ' days ago'; |
| 92 |
} |
| 93 |
} |
| 94 |
break; |
| 95 |
} |
| 96 |
case "action": |
| 97 |
{ |
| 98 |
switch ( $comment_content['action'] ) |
| 99 |
{ |
| 100 |
case "property_price_change": |
| 101 |
{ |
| 102 |
$note_body = $comment_content['action'] . '<br>From: ' . $comment_content['original_value'] . '<br>To: ' . $comment_content['new_value']; |
| 103 |
break; |
| 104 |
} |
| 105 |
case "property_availability_change": |
| 106 |
{ |
| 107 |
$note_body = $comment_content['action'] . '<br>From: ' . $comment_content['original_value'] . '<br>To: ' . $comment_content['new_value']; |
| 108 |
break; |
| 109 |
} |
| 110 |
case "viewing_booked": |
| 111 |
{ |
| 112 |
$note_body = '<a href="' . get_edit_post_link($comment_content['viewing_id']) . '">Viewing</a> booked'; |
| 113 |
if ( isset($comment_content['property_id']) ) |
| 114 |
{ |
| 115 |
$property = new PH_Property((int)$comment_content['property_id']); |
| 116 |
$note_body .= ' on <a href="' . get_edit_post_link($comment_content['property_id']) . '">' . $property->get_formatted_full_address() . '</a>'; |
| 117 |
} |
| 118 |
break; |
| 119 |
} |
| 120 |
case "added_to_viewing": |
| 121 |
{ |
| 122 |
$note_body = 'Added to <a href="' . get_edit_post_link($comment_content['viewing_id']) . '">viewing</a>'; |
| 123 |
if ( isset($comment_content['property_id']) ) |
| 124 |
{ |
| 125 |
$property = new PH_Property((int)$comment_content['property_id']); |
| 126 |
$note_body .= ' on <a href="' . get_edit_post_link($comment_content['property_id']) . '">' . $property->get_formatted_full_address() . '</a>'; |
| 127 |
} |
| 128 |
break; |
| 129 |
} |
| 130 |
case "tenancy_booked": |
| 131 |
{ |
| 132 |
$note_body = '<a href="' . get_edit_post_link($comment_content['tenancy_id']) . '">Tenancy</a> created'; |
| 133 |
if ( isset($comment_content['property_id']) ) |
| 134 |
{ |
| 135 |
$property = new PH_Property((int)$comment_content['property_id']); |
| 136 |
$note_body .= ' on <a href="' . get_edit_post_link($comment_content['property_id']) . '">' . $property->get_formatted_full_address() . '</a>'; |
| 137 |
} |
| 138 |
break; |
| 139 |
} |
| 140 |
case "added_to_tenancy": |
| 141 |
{ |
| 142 |
$note_body = 'Added to <a href="' . get_edit_post_link($comment_content['tenancy_id']) . '">tenancy</a>'; |
| 143 |
if ( isset($comment_content['property_id']) ) |
| 144 |
{ |
| 145 |
$property = new PH_Property((int)$comment_content['property_id']); |
| 146 |
$note_body .= ' on <a href="' . get_edit_post_link($comment_content['property_id']) . '">' . $property->get_formatted_full_address() . '</a>'; |
| 147 |
} |
| 148 |
break; |
| 149 |
} |
| 150 |
case "removed_from_tenancy": |
| 151 |
{ |
| 152 |
if (isset($comment_content['tenancy_id'])) |
| 153 |
{ |
| 154 |
$note_body = 'Removed from <a href="' . get_edit_post_link($comment_content['tenancy_id']) . '">tenancy</a>'; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
$note_body = '<a href="' . get_edit_post_link($comment_content['contact_id']) . '">' . get_the_title($comment_content['contact_id']) . '</a> removed from tenancy'; |
| 159 |
} |
| 160 |
break; |
| 161 |
} |
| 162 |
default: |
| 163 |
{ |
| 164 |
$note_body = $comment_content['action']; |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
break; |
| 169 |
} |
| 170 |
case "note": |
| 171 |
{ |
| 172 |
$note_body = $comment_content['note']; |
| 173 |
|
| 174 |
// Regular expression pattern to match {{mention-ID|NAME}} or {{mention-ID}} |
| 175 |
$pattern = '/\{\{mention-(\d+)(?:\|([^}]*))?\}\}/'; |
| 176 |
|
| 177 |
// Callback function to replace the mentions with HTML links |
| 178 |
$callback = function($matches) |
| 179 |
{ |
| 180 |
$post_id = $matches[1]; |
| 181 |
$title = isset($matches[2]) ? $matches[2] : ''; |
| 182 |
$edit_url = get_edit_post_link($post_id); |
| 183 |
$post_title = get_the_title($post_id); |
| 184 |
if ( get_post_type($post_id) == 'property' ) |
| 185 |
{ |
| 186 |
$property = new PH_Property((int)$post_id); |
| 187 |
$post_title = $property->get_formatted_full_address(); |
| 188 |
} |
| 189 |
|
| 190 |
// If the post exists, create the link |
| 191 |
if ( $edit_url && $post_title ) |
| 192 |
{ |
| 193 |
return '<a href="' . esc_url($edit_url) . '">' . esc_html($post_title) . '</a>'; |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
if ( !empty($title) ) |
| 198 |
{ |
| 199 |
return $title; |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
return '{{mention-' . $post_id . '}}'; |
| 204 |
} |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
$note_body = preg_replace_callback($pattern, $callback, $note_body); |
| 209 |
|
| 210 |
/*$pattern = '/\{\{mention-(\d+)(\|.*)?\}\}/'; |
| 211 |
$replacement = function($matches) { |
| 212 |
$post_id = $matches[1]; |
| 213 |
$title = trim(trim($matches[2], '|')); |
| 214 |
$edit_url = get_edit_post_link($post_id); |
| 215 |
$post_title = get_the_title($post_id); |
| 216 |
if ( get_post_type($post_id) == 'property' ) |
| 217 |
{ |
| 218 |
$property = new PH_Property((int)$post_id); |
| 219 |
$post_title = $property->get_formatted_full_address(); |
| 220 |
} |
| 221 |
|
| 222 |
// If the post exists, create the link |
| 223 |
if ( $edit_url && $post_title ) |
| 224 |
{ |
| 225 |
return '<a href="' . esc_url($edit_url) . '">' . esc_html($post_title) . '</a>'; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
if ( !empty($title) ) |
| 230 |
{ |
| 231 |
return $title; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
return '{{mention-' . $post_id . '}}'; |
| 236 |
} |
| 237 |
} |
| 238 |
}; |
| 239 |
$note_body = preg_replace_callback($pattern, $replacement, $note_body);*/ |
| 240 |
|
| 241 |
$note_body = nl2br($note_body); |
| 242 |
|
| 243 |
break; |
| 244 |
} |
| 245 |
case "unsubscribe": |
| 246 |
{ |
| 247 |
$note_body = 'Contact unsubscribed themselves from emails'; |
| 248 |
break; |
| 249 |
} |
| 250 |
case "status_change": // Believe this is only used by maintenance jobs add on |
| 251 |
{ |
| 252 |
$note_body = 'Status changed from ' . $comment_content['previous_status'] . ' to ' . $comment_content['new_status']; |
| 253 |
break; |
| 254 |
} |
| 255 |
default: |
| 256 |
{ |
| 257 |
$note_body = apply_filters( 'propertyhive_note_body', $note_body, $note ); |
| 258 |
} |
| 259 |
} |
| 260 |
$note_content = array( |
| 261 |
'id' => $note->comment_ID, |
| 262 |
'post_id' => $note->comment_post_ID, |
| 263 |
'type' => $comment_content['note_type'], |
| 264 |
'author' => $note->comment_author, |
| 265 |
'body' => $note_body, |
| 266 |
'timestamp' => strtotime($note->comment_date), |
| 267 |
'internal' => true, |
| 268 |
'pinned' => ( isset($comment_content['pinned']) && $comment_content['pinned'] == '1' ) ? '1' : '0', |
| 269 |
); |
| 270 |
|
| 271 |
if ( $note_content['pinned'] == '1' ) |
| 272 |
{ |
| 273 |
$pinned_notes[] = $note_content; |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
$unpinned_notes[] = $note_content; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
$note_output = array_merge($pinned_notes, $unpinned_notes); |
| 283 |
|
| 284 |
if ($section != 'enquiry') |
| 285 |
{ |
| 286 |
$note_output = apply_filters( 'propertyhive_notes', $note_output, $post ); |
| 287 |
$note_output = apply_filters( 'propertyhive_' . $section . '_notes', $note_output, $post ); |
| 288 |
} |
| 289 |
?> |
| 290 |
<ul class="record_notes" style="max-height:300px; overflow-y:auto"> |
| 291 |
<?php |
| 292 |
if ( !empty($note_output) ) |
| 293 |
{ |
| 294 |
foreach ( $note_output as $key => $note ) |
| 295 |
{ |
| 296 |
// Set pinned parameter for any notes added by third party plugins |
| 297 |
if ( !isset($note['pinned']) ) |
| 298 |
{ |
| 299 |
$note_output[$key]['pinned'] = 0; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
$datetime_format = get_option('date_format')." \a\\t ".get_option('time_format'); |
| 304 |
|
| 305 |
// order by date desc. Older PHP versions don't support array_column so just can't order for them |
| 306 |
if ( function_exists('array_column') ) |
| 307 |
{ |
| 308 |
$pinned = array_column($note_output, 'pinned'); |
| 309 |
$timestamp = array_column($note_output, 'timestamp'); |
| 310 |
|
| 311 |
array_multisort($pinned, SORT_DESC, |
| 312 |
$timestamp, SORT_DESC, |
| 313 |
$note_output); |
| 314 |
} |
| 315 |
|
| 316 |
foreach ( $note_output as $note ) |
| 317 |
{ |
| 318 |
$note_classes = array( 'note' ); |
| 319 |
|
| 320 |
$note_classes[] = 'note-type-' . $note['type']; |
| 321 |
?> |
| 322 |
<li rel="<?php echo absint( $note['id'] ) ; ?>" class="<?php echo esc_attr(implode( ' ', $note_classes )); ?>"> |
| 323 |
<div class="note_content<?php echo ($note['pinned'] == '1') ? ' pinned' : '' ?>"> |
| 324 |
<?php echo wp_kses_post( $note['body'] ); ?> |
| 325 |
</div> |
| 326 |
<p class="meta"> |
| 327 |
<abbr class="exact-date" title="<?php echo esc_attr(date("Y-m-d H:i:s", $note['timestamp'])); ?>"> |
| 328 |
<?php |
| 329 |
|
| 330 |
$time_diff = current_time( 'timestamp', 1 ) - $note['timestamp']; |
| 331 |
|
| 332 |
if ($time_diff > 86400) { |
| 333 |
echo esc_html(date( $datetime_format, $note['timestamp'] )); |
| 334 |
} else { |
| 335 |
printf( __( '%s ago', 'propertyhive' ), human_time_diff( $note['timestamp'], current_time( 'timestamp', 1 ) ) ); |
| 336 |
} |
| 337 |
?> |
| 338 |
</abbr> |
| 339 |
<?php |
| 340 |
if ( !empty($note['author']) && $note['author'] !== 'Property Hive' ) |
| 341 |
{ |
| 342 |
printf( |
| 343 |
/* translators: %s: author name */ |
| 344 |
__( 'by %s', 'propertyhive' ), |
| 345 |
esc_html($note['author']) |
| 346 |
); |
| 347 |
} |
| 348 |
?> |
| 349 |
|
| 350 |
<a href="#" data-section="<?php echo esc_attr($section); ?>" class="toggle_note_pinned"><?php echo esc_html(__( ( $note['pinned'] == '0' ) ? 'Pin To Top' : 'Unpin', 'propertyhive' )); ?></a> |
| 351 |
|
| 352 |
<?php if ( $note['type'] == 'note' ) { ?><a href="#" data-section="<?php echo esc_attr($section); ?>" class="delete_note"><?php echo esc_html(__( 'Delete', 'propertyhive' )); ?></a><?php } ?> |
| 353 |
<?php |
| 354 |
if ( $post->ID != $note['post_id'] ) |
| 355 |
{ |
| 356 |
echo '<br>'; |
| 357 |
$post_type_object = get_post_type_object( get_post_type( $note['post_id'] ) ); |
| 358 |
$post_type_label = $post_type_object ? $post_type_object->labels->singular_name : get_post_type( $note['post_id'] ); |
| 359 |
|
| 360 |
printf( |
| 361 |
/* translators: %s: linked post type label, for example "Property" */ |
| 362 |
__( 'Note originally entered on %s', 'propertyhive' ), |
| 363 |
'<a href="' . esc_url( get_edit_post_link( $note['post_id'] ) ) . '" style="color:inherit;">' . esc_html( $post_type_label ) . '</a>' |
| 364 |
); |
| 365 |
} |
| 366 |
?> |
| 367 |
</p> |
| 368 |
</li> |
| 369 |
<?php |
| 370 |
} |
| 371 |
} |
| 372 |
?> |
| 373 |
<li id="no_notes" style="text-align:center;<?php echo (!empty($note_output)) ? 'display:none;' : ''; ?>"><?php if ( isset($_POST['pinned']) && (int)$_POST['pinned'] == 1 ) { echo esc_html(__( 'There are no pinned notes to display', 'propertyhive' )); }else{ echo esc_html(__( 'There are no notes to display', 'propertyhive' )); } ?></li> |
| 374 |
</ul> |
| 375 |
|
| 376 |
<?php if ( !isset($_POST['pinned']) ) { ?> |
| 377 |
<div class="add_note"> |
| 378 |
<h4><?php _e( 'Add Note', 'propertyhive' ); ?></h4> |
| 379 |
<p> |
| 380 |
<textarea type="text" name="note" id="add_note" class="input-text" cols="20" rows="6" placeholder="Enter your note<?php if ( apply_filters('propertyhive_disable_notes_mention', false) === false ) { ?><br>Type <code style='background:#f9f9f9; border:1px solid #DDD; padding:0 2px; border-radius:5px; vertical-align:middle'>@</code> to tag a contact and property<?php } ?>"></textarea> |
| 381 |
<br> |
| 382 |
<input type="checkbox" name="pinned" id="pinned" value="1"> <?php echo esc_html(__( 'Pin Note', 'propertyhive' )); ?> |
| 383 |
</p> |
| 384 |
<p> |
| 385 |
<a href="#" class="add_note button-primary" data-section="<?php echo esc_attr($section); ?>"><?php echo esc_html(__( 'Save Note', 'propertyhive' )); ?></a> |
| 386 |
</p> |
| 387 |
</div> |
| 388 |
<?php } ?> |