| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Comments |
| 9 |
* |
| 10 |
* Handle comments (reviews and order notes). |
| 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 |
|
| 35 |
/** |
| 36 |
* Exclude propertyhive notes from queries and RSS. |
| 37 |
* |
| 38 |
* This code should exclude propertyhive_note comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded. |
| 39 |
* @param array $clauses |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public static function exclude_note_comments( $clauses ) { |
| 43 |
//global $wpdb, $typenow; |
| 44 |
|
| 45 |
if ( is_admin() && function_exists( 'get_current_screen' ) ) |
| 46 |
{ |
| 47 |
$screen = get_current_screen(); |
| 48 |
|
| 49 |
if ( isset($screen->id) && in_array( $screen->id, array( 'property', 'contact', 'enquiry', 'viewing', 'offer', 'sale' ) ) ) |
| 50 |
{ |
| 51 |
return $clauses; // Don't hide when viewing Property Hive record |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
$clauses['where'] .= ' AND comment_type != "propertyhive_note"'; |
| 56 |
|
| 57 |
return $clauses; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Delete comments count cache whenever there is |
| 62 |
* new comment or the status of a comment changes. Cache |
| 63 |
* will be regenerated next time PH_Comments::wp_count_comments() |
| 64 |
* is called. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public static function delete_comments_count_cache() { |
| 69 |
delete_transient( 'ph_count_comments' ); |
| 70 |
} |
| 71 |
/** |
| 72 |
* Remove propertyhive notes from wp_count_comments(). |
| 73 |
* @since 1.0.0 |
| 74 |
* @param object $stats |
| 75 |
* @param int $post_id |
| 76 |
* @return object |
| 77 |
*/ |
| 78 |
public static function wp_count_comments( $stats, $post_id ) { |
| 79 |
global $wpdb; |
| 80 |
if ( 0 === $post_id ) { |
| 81 |
$stats = get_transient( 'ph_count_comments' ); |
| 82 |
if ( ! $stats ) { |
| 83 |
$stats = array(); |
| 84 |
$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 ); |
| 85 |
$total = 0; |
| 86 |
$approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' ); |
| 87 |
foreach ( (array) $count as $row ) { |
| 88 |
// Don't count post-trashed toward totals |
| 89 |
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) { |
| 90 |
$total += $row['num_comments']; |
| 91 |
} |
| 92 |
if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
| 93 |
$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
| 94 |
} |
| 95 |
} |
| 96 |
$stats['total_comments'] = $total; |
| 97 |
$stats['all'] = $total; |
| 98 |
foreach ( $approved as $key ) { |
| 99 |
if ( empty( $stats[ $key ] ) ) { |
| 100 |
$stats[ $key ] = 0; |
| 101 |
} |
| 102 |
} |
| 103 |
$stats = (object) $stats; |
| 104 |
set_transient( 'ph_count_comments', $stats ); |
| 105 |
} |
| 106 |
} |
| 107 |
return $stats; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
PH_Comments::init(); |