PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.1
2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / lib / action-scheduler / classes / data-stores / ActionScheduler_wpCommentLogger.php
sureforms / inc / lib / action-scheduler / classes / data-stores Last commit date
ActionScheduler_DBLogger.php 2 years ago ActionScheduler_DBStore.php 2 years ago ActionScheduler_HybridStore.php 2 years ago ActionScheduler_wpCommentLogger.php 2 years ago ActionScheduler_wpPostStore.php 2 years ago ActionScheduler_wpPostStore_PostStatusRegistrar.php 2 years ago ActionScheduler_wpPostStore_PostTypeRegistrar.php 2 years ago ActionScheduler_wpPostStore_TaxonomyRegistrar.php 2 years ago
ActionScheduler_wpCommentLogger.php
249 lines
1 <?php
2
3 /**
4 * Class ActionScheduler_wpCommentLogger
5 */
6 class ActionScheduler_wpCommentLogger extends ActionScheduler_Logger {
7 const AGENT = 'ActionScheduler';
8 const TYPE = 'action_log';
9
10 /**
11 * @param string $action_id
12 * @param string $message
13 * @param DateTime $date
14 *
15 * @return string The log entry ID
16 */
17 public function log( $action_id, $message, DateTime $date = null ) {
18 if ( empty( $date ) ) {
19 $date = as_get_datetime_object();
20 } else {
21 $date = as_get_datetime_object( clone $date );
22 }
23 $comment_id = $this->create_wp_comment( $action_id, $message, $date );
24 return $comment_id;
25 }
26
27 protected function create_wp_comment( $action_id, $message, DateTime $date ) {
28
29 $comment_date_gmt = $date->format( 'Y-m-d H:i:s' );
30 ActionScheduler_TimezoneHelper::set_local_timezone( $date );
31 $comment_data = array(
32 'comment_post_ID' => $action_id,
33 'comment_date' => $date->format( 'Y-m-d H:i:s' ),
34 'comment_date_gmt' => $comment_date_gmt,
35 'comment_author' => self::AGENT,
36 'comment_content' => $message,
37 'comment_agent' => self::AGENT,
38 'comment_type' => self::TYPE,
39 );
40 return wp_insert_comment( $comment_data );
41 }
42
43 /**
44 * @param string $entry_id
45 *
46 * @return ActionScheduler_LogEntry
47 */
48 public function get_entry( $entry_id ) {
49 $comment = $this->get_comment( $entry_id );
50 if ( empty( $comment ) || $comment->comment_type != self::TYPE ) {
51 return new ActionScheduler_NullLogEntry();
52 }
53
54 $date = as_get_datetime_object( $comment->comment_date_gmt );
55 ActionScheduler_TimezoneHelper::set_local_timezone( $date );
56 return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date );
57 }
58
59 /**
60 * @param string $action_id
61 *
62 * @return ActionScheduler_LogEntry[]
63 */
64 public function get_logs( $action_id ) {
65 $status = 'all';
66 if ( get_post_status( $action_id ) == 'trash' ) {
67 $status = 'post-trashed';
68 }
69 $comments = get_comments(
70 array(
71 'post_id' => $action_id,
72 'orderby' => 'comment_date_gmt',
73 'order' => 'ASC',
74 'type' => self::TYPE,
75 'status' => $status,
76 )
77 );
78 $logs = array();
79 foreach ( $comments as $c ) {
80 $entry = $this->get_entry( $c );
81 if ( ! empty( $entry ) ) {
82 $logs[] = $entry;
83 }
84 }
85 return $logs;
86 }
87
88 protected function get_comment( $comment_id ) {
89 return get_comment( $comment_id );
90 }
91
92
93
94 /**
95 * @param WP_Comment_Query $query
96 */
97 public function filter_comment_queries( $query ) {
98 foreach ( array( 'ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID' ) as $key ) {
99 if ( ! empty( $query->query_vars[ $key ] ) ) {
100 return; // don't slow down queries that wouldn't include action_log comments anyway
101 }
102 }
103 $query->query_vars['action_log_filter'] = true;
104 add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 );
105 }
106
107 /**
108 * @param array $clauses
109 * @param WP_Comment_Query $query
110 *
111 * @return array
112 */
113 public function filter_comment_query_clauses( $clauses, $query ) {
114 if ( ! empty( $query->query_vars['action_log_filter'] ) ) {
115 $clauses['where'] .= $this->get_where_clause();
116 }
117 return $clauses;
118 }
119
120 /**
121 * Make sure Action Scheduler logs are excluded from comment feeds, which use WP_Query, not
122 * the WP_Comment_Query class handled by @see self::filter_comment_queries().
123 *
124 * @param string $where
125 * @param WP_Query $query
126 *
127 * @return string
128 */
129 public function filter_comment_feed( $where, $query ) {
130 if ( is_comment_feed() ) {
131 $where .= $this->get_where_clause();
132 }
133 return $where;
134 }
135
136 /**
137 * Return a SQL clause to exclude Action Scheduler comments.
138 *
139 * @return string
140 */
141 protected function get_where_clause() {
142 global $wpdb;
143 return sprintf( " AND {$wpdb->comments}.comment_type != '%s'", self::TYPE );
144 }
145
146 /**
147 * Remove action log entries from wp_count_comments()
148 *
149 * @param array $stats
150 * @param int $post_id
151 *
152 * @return object
153 */
154 public function filter_comment_count( $stats, $post_id ) {
155 global $wpdb;
156
157 if ( 0 === $post_id ) {
158 $stats = $this->get_comment_count();
159 }
160
161 return $stats;
162 }
163
164 /**
165 * Retrieve the comment counts from our cache, or the database if the cached version isn't set.
166 *
167 * @return object
168 */
169 protected function get_comment_count() {
170 global $wpdb;
171
172 $stats = get_transient( 'as_comment_count' );
173
174 if ( ! $stats ) {
175 $stats = array();
176
177 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A );
178
179 $total = 0;
180 $stats = array();
181 $approved = array(
182 '0' => 'moderated',
183 '1' => 'approved',
184 'spam' => 'spam',
185 'trash' => 'trash',
186 'post-trashed' => 'post-trashed',
187 );
188
189 foreach ( (array) $count as $row ) {
190 // Don't count post-trashed toward totals
191 if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
192 $total += $row['num_comments'];
193 }
194 if ( isset( $approved[ $row['comment_approved'] ] ) ) {
195 $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
196 }
197 }
198
199 $stats['total_comments'] = $total;
200 $stats['all'] = $total;
201
202 foreach ( $approved as $key ) {
203 if ( empty( $stats[ $key ] ) ) {
204 $stats[ $key ] = 0;
205 }
206 }
207
208 $stats = (object) $stats;
209 set_transient( 'as_comment_count', $stats );
210 }
211
212 return $stats;
213 }
214
215 /**
216 * Delete comment count cache whenever there is new comment or the status of a comment changes. Cache
217 * will be regenerated next time ActionScheduler_wpCommentLogger::filter_comment_count() is called.
218 */
219 public function delete_comment_count_cache() {
220 delete_transient( 'as_comment_count' );
221 }
222
223 /**
224 * @codeCoverageIgnore
225 */
226 public function init() {
227 add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 );
228 add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 );
229
230 parent::init();
231
232 add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 );
233 add_action( 'wp_count_comments', array( $this, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs
234 add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 );
235
236 // Delete comments count cache whenever there is a new comment or a comment status changes
237 add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) );
238 add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) );
239 }
240
241 public function disable_comment_counting() {
242 wp_defer_comment_counting( true );
243 }
244 public function enable_comment_counting() {
245 wp_defer_comment_counting( false );
246 }
247
248 }
249