forked from gocodebox/lifterlms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.llms.comments.php
155 lines (135 loc) · 4.35 KB
/
class.llms.comments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; }
/**
* Custom filters & actions for LifterLMS Comments
*
* @since 3.0.0
* @version 3.0.0
*/
class LLMS_Comments {
public function __construct() {
// secure order notes
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
add_action( 'comment_feed_join', array( __CLASS__, 'exclude_order_comments_from_feed_join' ) );
add_action( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
// remove order notes when counting comments
add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 777, 2 );
// Delete comments count cache whenever there is a new comment or a comment status changes
add_action( 'wp_insert_comment', array( __CLASS__, 'delete_comments_count_cache' ) );
add_action( 'wp_set_comment_status', array( __CLASS__, 'delete_comments_count_cache' ) );
}
/**
* Delete transient data when inserting new comments or updating comment status
* Next time wp_count_comments is called it'll be automatically regenerated
*
* @return void
* @since 3.0.0
* @version 3.0.0
* @note thanks WooCommerce :-D
*/
public static function delete_comments_count_cache() {
delete_transient( 'llms_count_comments' );
}
/**
* Exclude order comments from queries and RSS.
*
* @param array $clauses
* @return array
* @since 3.0.0
* @version 3.0.0
* @see thanks WooCommerce :-D
*/
public static function exclude_order_comments( $clauses ) {
global $wpdb, $typenow;
// allow queries when in the admin
if ( is_admin() && in_array( $typenow, array( 'llms_order' ) ) && current_user_can( apply_filters( 'lifterlms_admin_order_access', 'manage_options' ) ) ) {
return $clauses;
}
if ( ! $clauses['join'] ) {
$clauses['join'] = '';
}
if ( ! strstr( $clauses['join'], "JOIN $wpdb->posts" ) ) {
$clauses['join'] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts.ID ";
}
if ( $clauses['where'] ) {
$clauses['where'] .= ' AND ';
}
$clauses['where'] .= " $wpdb->posts.post_type NOT IN ('" . implode( "','", array( 'llms_order' ) ) . "') ";
return $clauses;
}
/**
* Exclude order comments from queries and RSS.
*
* @param string $join
* @return string
* @since 3.0.0
* @version 3.0.0
* @see thanks WooCommerce :-D
*/
public static function exclude_order_comments_from_feed_join( $join ) {
global $wpdb;
if ( ! strstr( $join, $wpdb->posts ) ) {
$join = " LEFT JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID ";
}
return $join;
}
/**
* Exclude order comments from queries and RSS.
*
* @param string $where
* @return string
* @since 3.0.0
* @version 3.0.0
* @see thanks WooCommerce :-D
*/
public static function exclude_order_comments_from_feed_where( $where ) {
global $wpdb;
if ( $where ) {
$where .= ' AND ';
}
$where .= " $wpdb->posts.post_type NOT IN ('" . implode( "','", array( 'llms_order' ) ) . "') ";
return $where;
}
/**
* Remove order notes from the count when counting comments
*
* @param object $stats original comment stats
* @param int $post_id WP Post ID
* @return object
* @since 3.0.0
* @version 3.0.0
* @see thanks WooCommerce :-D
*/
public static function wp_count_comments( $stats, $post_id ) {
global $wpdb;
if ( 0 === $post_id ) {
$trans = get_transient( 'llms_count_comments' );
if ( ! $trans ) {
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type = 'llms_order_note' GROUP BY comment_approved;", ARRAY_A );
$approved = array(
'0' => 'moderated',
'1' => 'approved',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);
foreach ( $count as $row ) {
// Don't count post-trashed toward totals
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
$stats->total_comments -= $row['num_comments'];
}
if ( isset( $approved[ $row['comment_approved'] ] ) ) {
$var = $approved[ $row['comment_approved'] ];
$stats->$var -= $row['num_comments'];
}
}
set_transient( 'llms_count_comments', $stats );
} else {
$stats = $trans;
}
}
return $stats;
}
}
return new LLMS_Comments();