| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Comments |
| 9 |
* |
| 10 |
* Handle comments. |
| 11 |
* |
| 12 |
* @class PH_Comments |
| 13 |
* @version 1.0.0 |
| 14 |
* @package PropertyHive/Classes/Products |
| 15 |
* @category Class |
| 16 |
* @author PropertyHive |
| 17 |
*/ |
| 18 |
class PH_Comments { |
| 19 |
|
| 20 |
/** |
| 21 |
* Hook in methods. |
| 22 |
*/ |
| 23 |
public static function init() { |
| 24 |
// Secure propertyhive notes |
| 25 |
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_note_comments' ), 10, 1 ); |
| 26 |
|
| 27 |
// Count comments |
| 28 |
add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 99, 2 ); |
| 29 |
|
| 30 |
// Delete comments count cache whenever there is a new comment or a comment status changes |
| 31 |
add_action( 'wp_insert_comment', array( __CLASS__, 'delete_comments_count_cache' ) ); |
| 32 |
add_action( 'wp_set_comment_status', array( __CLASS__, 'delete_comments_count_cache' ) ); |
| 33 |
|
| 34 |
add_action( 'add_post_meta', array( __CLASS__, 'check_on_market_add' ), 10, 3 ); |
| 35 |
add_action( 'update_post_meta', array( __CLASS__, 'check_on_market_update' ), 10, 4 ); |
| 36 |
|
| 37 |
add_action( 'update_post_meta', array( __CLASS__, 'check_price_change' ), 10, 4 ); |
| 38 |
} |
| 39 |
|
| 40 |
public static function check_price_change( $meta_id, $object_id, $meta_key, $meta_value ) |
| 41 |
{ |
| 42 |
if ( get_post_type($object_id) == 'property' && ( $meta_key == '_price' || $meta_key == '_rent' ) ) |
| 43 |
{ |
| 44 |
$original_value = get_post_meta( $object_id, $meta_key, TRUE ); |
| 45 |
|
| 46 |
if ( $original_value != $meta_value ) |
| 47 |
{ |
| 48 |
$current_user = wp_get_current_user(); |
| 49 |
|
| 50 |
// Add note/comment to property |
| 51 |
$comment = array( |
| 52 |
'note_type' => 'action', |
| 53 |
'action' => 'property_price_change', |
| 54 |
'original_value' => $original_value, |
| 55 |
'new_value' => $meta_value |
| 56 |
); |
| 57 |
|
| 58 |
$data = array( |
| 59 |
'comment_post_ID' => (int)$object_id, |
| 60 |
'comment_author' => $current_user->display_name, |
| 61 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 62 |
'comment_author_url' => '', |
| 63 |
'comment_date' => date("Y-m-d H:i:s"), |
| 64 |
'comment_content' => serialize($comment), |
| 65 |
'comment_approved' => 1, |
| 66 |
'comment_type' => 'propertyhive_note', |
| 67 |
); |
| 68 |
$comment_id = wp_insert_comment( $data ); |
| 69 |
|
| 70 |
update_post_meta( $object_id, '_price_change_date', date("Y-m-d H:i:s") ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public static function check_on_market_add( $object_id, $meta_key, $meta_value ) |
| 76 |
{ |
| 77 |
if ( get_post_type($object_id) == 'property' && $meta_key == '_on_market' ) |
| 78 |
{ |
| 79 |
if ( $meta_value == 'yes' ) |
| 80 |
{ |
| 81 |
$note_action = 'property_on_market'; |
| 82 |
|
| 83 |
$current_user = wp_get_current_user(); |
| 84 |
|
| 85 |
// Add note/comment to property |
| 86 |
$comment = array( |
| 87 |
'note_type' => 'action', |
| 88 |
'action' => $note_action, |
| 89 |
); |
| 90 |
|
| 91 |
$data = array( |
| 92 |
'comment_post_ID' => (int)$object_id, |
| 93 |
'comment_author' => $current_user->display_name, |
| 94 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 95 |
'comment_author_url' => '', |
| 96 |
'comment_date' => date("Y-m-d H:i:s"), |
| 97 |
'comment_content' => serialize($comment), |
| 98 |
'comment_approved' => 1, |
| 99 |
'comment_type' => 'propertyhive_note', |
| 100 |
); |
| 101 |
$comment_id = wp_insert_comment( $data ); |
| 102 |
|
| 103 |
update_post_meta( $object_id, '_on_market_change_date', date("Y-m-d H:i:s") ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public static function check_on_market_update( $meta_id, $object_id, $meta_key, $meta_value ) |
| 109 |
{ |
| 110 |
if ( get_post_type($object_id) == 'property' && $meta_key == '_on_market' ) |
| 111 |
{ |
| 112 |
$original_value = get_post_meta( $object_id, $meta_key, TRUE ); |
| 113 |
|
| 114 |
if ( $original_value != $meta_value ) |
| 115 |
{ |
| 116 |
$note_action = 'property_off_market'; |
| 117 |
if ($meta_value == 'yes') |
| 118 |
{ |
| 119 |
$note_action = 'property_on_market'; |
| 120 |
} |
| 121 |
|
| 122 |
$current_user = wp_get_current_user(); |
| 123 |
|
| 124 |
// Add note/comment to property |
| 125 |
$comment = array( |
| 126 |
'note_type' => 'action', |
| 127 |
'action' => $note_action, |
| 128 |
); |
| 129 |
|
| 130 |
$data = array( |
| 131 |
'comment_post_ID' => (int)$object_id, |
| 132 |
'comment_author' => $current_user->display_name, |
| 133 |
'comment_author_email' => 'propertyhive@noreply.com', |
| 134 |
'comment_author_url' => '', |
| 135 |
'comment_date' => date("Y-m-d H:i:s"), |
| 136 |
'comment_content' => serialize($comment), |
| 137 |
'comment_approved' => 1, |
| 138 |
'comment_type' => 'propertyhive_note', |
| 139 |
); |
| 140 |
$comment_id = wp_insert_comment( $data ); |
| 141 |
|
| 142 |
update_post_meta( $object_id, '_on_market_change_date', date("Y-m-d H:i:s") ); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Exclude propertyhive notes from queries and RSS. |
| 149 |
* |
| 150 |
* This code should exclude propertyhive_note comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded. |
| 151 |
* @param array $clauses |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public static function exclude_note_comments( $clauses ) { |
| 155 |
//global $wpdb, $typenow; |
| 156 |
|
| 157 |
if ( is_admin() && function_exists( 'get_current_screen' ) ) |
| 158 |
{ |
| 159 |
$screen = get_current_screen(); |
| 160 |
|
| 161 |
if ( isset($screen->id) && in_array( $screen->id, apply_filters( 'propertyhive_post_types_with_notes', array( 'property', 'contact', 'enquiry', 'appraisal', 'viewing', 'offer', 'sale' ) ) ) ) |
| 162 |
{ |
| 163 |
return $clauses; // Don't hide when viewing Property Hive record |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$clauses['where'] .= ' AND comment_type != "propertyhive_note"'; |
| 168 |
|
| 169 |
return $clauses; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Delete comments count cache whenever there is |
| 174 |
* new comment or the status of a comment changes. Cache |
| 175 |
* will be regenerated next time PH_Comments::wp_count_comments() |
| 176 |
* is called. |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
public static function delete_comments_count_cache() { |
| 181 |
delete_transient( 'ph_count_comments' ); |
| 182 |
} |
| 183 |
/** |
| 184 |
* Remove propertyhive notes from wp_count_comments(). |
| 185 |
* @since 1.0.0 |
| 186 |
* @param object $stats |
| 187 |
* @param int $post_id |
| 188 |
* @return object |
| 189 |
*/ |
| 190 |
public static function wp_count_comments( $stats, $post_id ) { |
| 191 |
global $wpdb; |
| 192 |
if ( 0 === $post_id ) { |
| 193 |
$stats = get_transient( 'ph_count_comments' ); |
| 194 |
if ( ! $stats ) { |
| 195 |
$stats = array(); |
| 196 |
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type != 'propertyhive_note' GROUP BY comment_approved", ARRAY_A ); |
| 197 |
$total = 0; |
| 198 |
$approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' ); |
| 199 |
foreach ( (array) $count as $row ) { |
| 200 |
// Don't count post-trashed toward totals |
| 201 |
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) { |
| 202 |
$total += $row['num_comments']; |
| 203 |
} |
| 204 |
if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
| 205 |
$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
| 206 |
} |
| 207 |
} |
| 208 |
$stats['total_comments'] = $total; |
| 209 |
$stats['all'] = $total; |
| 210 |
foreach ( $approved as $key ) { |
| 211 |
if ( empty( $stats[ $key ] ) ) { |
| 212 |
$stats[ $key ] = 0; |
| 213 |
} |
| 214 |
} |
| 215 |
$stats = (object) $stats; |
| 216 |
set_transient( 'ph_count_comments', $stats ); |
| 217 |
} |
| 218 |
} |
| 219 |
return $stats; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
PH_Comments::init(); |