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 / class-ph-comments.php

class-ph-comments.php in Property Hive 1.4.53, at includes/class-ph-comments.php

388 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_filter( 'comments_clauses', array( __CLASS__, 'related_to_or_post_id' ), 20, 1 );
28
29 // Count comments
30 add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 99, 2 );
31
32 // Delete comments count cache whenever there is a new comment or a comment status changes
33 add_action( 'wp_insert_comment', array( __CLASS__, 'delete_comments_count_cache' ) );
34 add_action( 'wp_set_comment_status', array( __CLASS__, 'delete_comments_count_cache' ) );
35
36 add_action( 'add_post_meta', array( __CLASS__, 'check_on_market_add' ), 10, 3 );
37 add_action( 'update_post_meta', array( __CLASS__, 'check_on_market_update' ), 10, 4 );
38
39 add_action( 'update_post_meta', array( __CLASS__, 'check_price_change' ), 10, 4 );
40 }
41
42 public static function related_to_or_post_id( $clauses )
43 {
44 global $wpdb, $post;
45
46 if ( strpos($clauses['where'], 'related_to') !== FALSE )
47 {
48 $clauses['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $clauses['join'] );
49
50 // we're searching for related_to so should check where main post is comment_post_ID
51 $clauses['where'] = str_replace( $wpdb->prefix . 'commentmeta.meta_key', '( ' . $wpdb->prefix . 'commentmeta.meta_key', $clauses['where'] );
52 $clauses['where'] .= ' OR comment_post_ID = "' . $post->ID . '" ) ';
53 }
54
55
56 return $clauses;
57 }
58
59 public static function insert_note( $post_id, $comment )
60 {
61 $current_user = wp_get_current_user();
62
63 $post_type = get_post_type( $post_id );
64
65 $related_to = array( $post_id );
66
67 switch ( $post_type )
68 {
69 case "property": {
70 // get property owner
71 $owner_contact_ids = get_post_meta( $post_id, '_owner_contact_id', TRUE );
72 if ( !empty($owner_contact_ids) )
73 {
74 if ( !is_array($owner_contact_ids) )
75 {
76 $owner_contact_ids = array( $owner_contact_ids );
77 }
78 foreach ( $owner_contact_ids as $owner_contact_id )
79 {
80 $related_to[] = $owner_contact_id;
81 }
82 }
83 break;
84 }
85 case "contact": {
86 // check contact type, then add to property if owner
87 $contact_types = get_post_meta( $post_id, '_contact_types', TRUE );
88 if ( in_array('owner', $contact_types) )
89 {
90 // this contact is an owner
91 // get properties
92 $args = array(
93 'post_type' => 'property',
94 'nopaging' => true,
95 'fields' => 'ids',
96 'meta_query' => array(
97 'relation' => 'OR',
98 array(
99 'key' => '_owner_contact_id',
100 'value' => $post_id,
101 'compare' => '=',
102 ),
103 array(
104 'key' => '_owner_contact_id',
105 'value' => '"' . $post_id . '"',
106 'compare' => 'LIKE',
107 ),
108 )
109 );
110
111 $property_query = new WP_Query( $args );
112
113 if ( $property_query->have_posts() )
114 {
115 while ( $property_query->have_posts() )
116 {
117 $property_query->the_post();
118
119 $related_to[] = get_the_ID();
120 }
121 }
122 wp_reset_postdata();
123 }
124 break;
125 }
126 case "appraisal": {
127 // get potential owner
128 $property_owner_contact_id = get_post_meta( $post_id, '_property_owner_contact_id', TRUE );
129 if ( $property_owner_contact_id != '' )
130 {
131 $related_to[] = $property_owner_contact_id;
132 }
133 break;
134 }
135 case "viewing":
136 case "offer":
137 case "sale": {
138 // get property
139 $property_id = get_post_meta( $post_id, '_property_id', TRUE );
140 if ( $property_id != '' )
141 {
142 $related_to[] = $property_id;
143
144 // get property owner
145 $owner_contact_ids = get_post_meta( $property_id, '_owner_contact_id', TRUE );
146 if ( !empty($owner_contact_ids) )
147 {
148 if ( !is_array($owner_contact_ids) )
149 {
150 $owner_contact_ids = array( $owner_contact_ids );
151 }
152 foreach ( $owner_contact_ids as $owner_contact_id )
153 {
154 $related_to[] = $owner_contact_id;
155 }
156 }
157 }
158
159 // get applicant
160 $applicant_ids = get_post_meta( $post_id, '_applicant_contact_id', TRUE );
161 if ( !empty($applicant_ids) )
162 {
163 if ( !is_array($applicant_ids) )
164 {
165 $applicant_ids = array( $applicant_ids );
166 }
167 foreach ( $applicant_ids as $applicant_id )
168 {
169 $related_to[] = $applicant_id;
170 }
171 }
172 break;
173 }
174 }
175
176 $related_to = apply_filters( 'property_insert_note_related_to', $related_to, $post_id );
177
178 $related_to = array_filter( $related_to );
179
180 // Ensure they all go in as strings to allow LIKE query to work when querying related_to
181 $new_related_to = array();
182 foreach ( $related_to as $related_to_value )
183 {
184 $new_related_to[] = (string)$related_to_value;
185 }
186
187 $data = array(
188 'comment_post_ID' => $post_id,
189 'comment_author' => $current_user->display_name,
190 'comment_author_email' => 'propertyhive@noreply.com',
191 'comment_author_url' => '',
192 'comment_date' => date("Y-m-d H:i:s"),
193 'comment_content' => serialize($comment),
194 'comment_approved' => 1,
195 'comment_type' => 'propertyhive_note',
196 'comment_meta' => array(
197 'related_to' => $new_related_to,
198 ),
199 );
200 $comment_id = wp_insert_comment( $data );
201
202 return $comment_id;
203 }
204
205 public static function check_price_change( $meta_id, $object_id, $meta_key, $meta_value )
206 {
207 if ( get_post_type($object_id) == 'property' && ( $meta_key == '_price' || $meta_key == '_rent' ) )
208 {
209 $original_value = get_post_meta( $object_id, $meta_key, TRUE );
210
211 if ( $original_value != $meta_value )
212 {
213 $current_user = wp_get_current_user();
214
215 // Add note/comment to property
216 $comment = array(
217 'note_type' => 'action',
218 'action' => 'property_price_change',
219 'original_value' => $original_value,
220 'new_value' => $meta_value
221 );
222
223 $data = array(
224 'comment_post_ID' => (int)$object_id,
225 'comment_author' => $current_user->display_name,
226 'comment_author_email' => 'propertyhive@noreply.com',
227 'comment_author_url' => '',
228 'comment_date' => date("Y-m-d H:i:s"),
229 'comment_content' => serialize($comment),
230 'comment_approved' => 1,
231 'comment_type' => 'propertyhive_note',
232 );
233 $comment_id = wp_insert_comment( $data );
234
235 update_post_meta( $object_id, '_price_change_date', date("Y-m-d H:i:s") );
236 }
237 }
238 }
239
240 public static function check_on_market_add( $object_id, $meta_key, $meta_value )
241 {
242 if ( get_post_type($object_id) == 'property' && $meta_key == '_on_market' )
243 {
244 if ( $meta_value == 'yes' )
245 {
246 $note_action = 'property_on_market';
247
248 $current_user = wp_get_current_user();
249
250 // Add note/comment to property
251 $comment = array(
252 'note_type' => 'action',
253 'action' => $note_action,
254 );
255
256 $data = array(
257 'comment_post_ID' => (int)$object_id,
258 'comment_author' => $current_user->display_name,
259 'comment_author_email' => 'propertyhive@noreply.com',
260 'comment_author_url' => '',
261 'comment_date' => date("Y-m-d H:i:s"),
262 'comment_content' => serialize($comment),
263 'comment_approved' => 1,
264 'comment_type' => 'propertyhive_note',
265 );
266 $comment_id = wp_insert_comment( $data );
267
268 update_post_meta( $object_id, '_on_market_change_date', date("Y-m-d H:i:s") );
269 }
270 }
271 }
272
273 public static function check_on_market_update( $meta_id, $object_id, $meta_key, $meta_value )
274 {
275 if ( get_post_type($object_id) == 'property' && $meta_key == '_on_market' )
276 {
277 $original_value = get_post_meta( $object_id, $meta_key, TRUE );
278
279 if ( $original_value != $meta_value )
280 {
281 $note_action = 'property_off_market';
282 if ($meta_value == 'yes')
283 {
284 $note_action = 'property_on_market';
285 }
286
287 $current_user = wp_get_current_user();
288
289 // Add note/comment to property
290 $comment = array(
291 'note_type' => 'action',
292 'action' => $note_action,
293 );
294
295 $data = array(
296 'comment_post_ID' => (int)$object_id,
297 'comment_author' => $current_user->display_name,
298 'comment_author_email' => 'propertyhive@noreply.com',
299 'comment_author_url' => '',
300 'comment_date' => date("Y-m-d H:i:s"),
301 'comment_content' => serialize($comment),
302 'comment_approved' => 1,
303 'comment_type' => 'propertyhive_note',
304 );
305 $comment_id = wp_insert_comment( $data );
306
307 update_post_meta( $object_id, '_on_market_change_date', date("Y-m-d H:i:s") );
308 }
309 }
310 }
311
312 /**
313 * Exclude propertyhive notes from queries and RSS.
314 *
315 * This code should exclude propertyhive_note comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
316 * @param array $clauses
317 * @return array
318 */
319 public static function exclude_note_comments( $clauses ) {
320 //global $wpdb, $typenow;
321
322 if ( is_admin() && function_exists( 'get_current_screen' ) )
323 {
324 $screen = get_current_screen();
325
326 if ( isset($screen->id) && in_array( $screen->id, apply_filters( 'propertyhive_post_types_with_notes', array( 'property', 'contact', 'enquiry', 'appraisal', 'viewing', 'offer', 'sale' ) ) ) )
327 {
328 return $clauses; // Don't hide when viewing Property Hive record
329 }
330 }
331
332 $clauses['where'] .= ' AND comment_type != "propertyhive_note"';
333
334 return $clauses;
335 }
336
337 /**
338 * Delete comments count cache whenever there is
339 * new comment or the status of a comment changes. Cache
340 * will be regenerated next time PH_Comments::wp_count_comments()
341 * is called.
342 *
343 * @return void
344 */
345 public static function delete_comments_count_cache() {
346 delete_transient( 'ph_count_comments' );
347 }
348 /**
349 * Remove propertyhive notes from wp_count_comments().
350 * @since 1.0.0
351 * @param object $stats
352 * @param int $post_id
353 * @return object
354 */
355 public static function wp_count_comments( $stats, $post_id ) {
356 global $wpdb;
357 if ( 0 === $post_id ) {
358 $stats = get_transient( 'ph_count_comments' );
359 if ( ! $stats ) {
360 $stats = array();
361 $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 );
362 $total = 0;
363 $approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' );
364 foreach ( (array) $count as $row ) {
365 // Don't count post-trashed toward totals
366 if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
367 $total += $row['num_comments'];
368 }
369 if ( isset( $approved[ $row['comment_approved'] ] ) ) {
370 $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
371 }
372 }
373 $stats['total_comments'] = $total;
374 $stats['all'] = $total;
375 foreach ( $approved as $key ) {
376 if ( empty( $stats[ $key ] ) ) {
377 $stats[ $key ] = 0;
378 }
379 }
380 $stats = (object) $stats;
381 set_transient( 'ph_count_comments', $stats );
382 }
383 }
384 return $stats;
385 }
386 }
387
388 PH_Comments::init();