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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / packages / action-scheduler / classes / data-stores / ActionScheduler_wpCommentLogger.php
woocommerce / packages / action-scheduler / classes / data-stores Last commit date
ActionScheduler_DBLogger.php 1 year ago ActionScheduler_DBStore.php 11 months ago ActionScheduler_HybridStore.php 11 months ago ActionScheduler_wpCommentLogger.php 1 year ago ActionScheduler_wpPostStore.php 11 months ago ActionScheduler_wpPostStore_PostStatusRegistrar.php 1 year ago ActionScheduler_wpPostStore_PostTypeRegistrar.php 1 year ago ActionScheduler_wpPostStore_TaxonomyRegistrar.php 1 year ago
ActionScheduler_wpCommentLogger.php
283 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 * Create log entry.
12 *
13 * @param string $action_id Action ID.
14 * @param string $message Action log's message.
15 * @param DateTime|null $date Action log's timestamp.
16 *
17 * @return string The log entry ID
18 */
19 public function log( $action_id, $message, ?DateTime $date = null ) {
20 if ( empty( $date ) ) {
21 $date = as_get_datetime_object();
22 } else {
23 $date = as_get_datetime_object( clone $date );
24 }
25 $comment_id = $this->create_wp_comment( $action_id, $message, $date );
26 return $comment_id;
27 }
28
29 /**
30 * Create comment.
31 *
32 * @param int $action_id Action ID.
33 * @param string $message Action log's message.
34 * @param DateTime $date Action log entry's timestamp.
35 */
36 protected function create_wp_comment( $action_id, $message, DateTime $date ) {
37
38 $comment_date_gmt = $date->format( 'Y-m-d H:i:s' );
39 ActionScheduler_TimezoneHelper::set_local_timezone( $date );
40 $comment_data = array(
41 'comment_post_ID' => $action_id,
42 'comment_date' => $date->format( 'Y-m-d H:i:s' ),
43 'comment_date_gmt' => $comment_date_gmt,
44 'comment_author' => self::AGENT,
45 'comment_content' => $message,
46 'comment_agent' => self::AGENT,
47 'comment_type' => self::TYPE,
48 );
49
50 return wp_insert_comment( $comment_data );
51 }
52
53 /**
54 * Get single log entry for action.
55 *
56 * @param string $entry_id Entry ID.
57 *
58 * @return ActionScheduler_LogEntry
59 */
60 public function get_entry( $entry_id ) {
61 $comment = $this->get_comment( $entry_id );
62
63 if ( empty( $comment ) || self::TYPE !== $comment->comment_type ) {
64 return new ActionScheduler_NullLogEntry();
65 }
66
67 $date = as_get_datetime_object( $comment->comment_date_gmt );
68 ActionScheduler_TimezoneHelper::set_local_timezone( $date );
69 return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date );
70 }
71
72 /**
73 * Get action's logs.
74 *
75 * @param string $action_id Action ID.
76 *
77 * @return ActionScheduler_LogEntry[]
78 */
79 public function get_logs( $action_id ) {
80 $status = 'all';
81 $logs = array();
82
83 if ( get_post_status( $action_id ) === 'trash' ) {
84 $status = 'post-trashed';
85 }
86
87 $comments = get_comments(
88 array(
89 'post_id' => $action_id,
90 'orderby' => 'comment_date_gmt',
91 'order' => 'ASC',
92 'type' => self::TYPE,
93 'status' => $status,
94 )
95 );
96
97 foreach ( $comments as $c ) {
98 $entry = $this->get_entry( $c );
99
100 if ( ! empty( $entry ) ) {
101 $logs[] = $entry;
102 }
103 }
104
105 return $logs;
106 }
107
108 /**
109 * Get comment.
110 *
111 * @param int $comment_id Comment ID.
112 */
113 protected function get_comment( $comment_id ) {
114 return get_comment( $comment_id );
115 }
116
117 /**
118 * Filter comment queries.
119 *
120 * @param WP_Comment_Query $query Comment query object.
121 */
122 public function filter_comment_queries( $query ) {
123 foreach ( array( 'ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID' ) as $key ) {
124 if ( ! empty( $query->query_vars[ $key ] ) ) {
125 return; // don't slow down queries that wouldn't include action_log comments anyway.
126 }
127 }
128 $query->query_vars['action_log_filter'] = true;
129 add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 );
130 }
131
132 /**
133 * Filter comment queries.
134 *
135 * @param array $clauses Query's clauses.
136 * @param WP_Comment_Query $query Query object.
137 *
138 * @return array
139 */
140 public function filter_comment_query_clauses( $clauses, $query ) {
141 if ( ! empty( $query->query_vars['action_log_filter'] ) ) {
142 $clauses['where'] .= $this->get_where_clause();
143 }
144 return $clauses;
145 }
146
147 /**
148 * Make sure Action Scheduler logs are excluded from comment feeds, which use WP_Query, not
149 * the WP_Comment_Query class handled by @see self::filter_comment_queries().
150 *
151 * @param string $where Query's `where` clause.
152 * @param WP_Query $query Query object.
153 *
154 * @return string
155 */
156 public function filter_comment_feed( $where, $query ) {
157 if ( is_comment_feed() ) {
158 $where .= $this->get_where_clause();
159 }
160 return $where;
161 }
162
163 /**
164 * Return a SQL clause to exclude Action Scheduler comments.
165 *
166 * @return string
167 */
168 protected function get_where_clause() {
169 global $wpdb;
170 return sprintf( " AND {$wpdb->comments}.comment_type != '%s'", self::TYPE );
171 }
172
173 /**
174 * Remove action log entries from wp_count_comments()
175 *
176 * @param array $stats Comment count.
177 * @param int $post_id Post ID.
178 *
179 * @return object
180 */
181 public function filter_comment_count( $stats, $post_id ) {
182 global $wpdb;
183
184 if ( 0 === $post_id ) {
185 $stats = $this->get_comment_count();
186 }
187
188 return $stats;
189 }
190
191 /**
192 * Retrieve the comment counts from our cache, or the database if the cached version isn't set.
193 *
194 * @return object
195 */
196 protected function get_comment_count() {
197 global $wpdb;
198
199 $stats = get_transient( 'as_comment_count' );
200
201 if ( ! $stats ) {
202 $stats = array();
203 $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 );
204 $total = 0;
205 $stats = array();
206 $approved = array(
207 '0' => 'moderated',
208 '1' => 'approved',
209 'spam' => 'spam',
210 'trash' => 'trash',
211 'post-trashed' => 'post-trashed',
212 );
213
214 foreach ( (array) $count as $row ) {
215 // Don't count post-trashed toward totals.
216 if ( 'post-trashed' !== $row['comment_approved'] && 'trash' !== $row['comment_approved'] ) {
217 $total += $row['num_comments'];
218 }
219 if ( isset( $approved[ $row['comment_approved'] ] ) ) {
220 $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
221 }
222 }
223
224 $stats['total_comments'] = $total;
225 $stats['all'] = $total;
226
227 foreach ( $approved as $key ) {
228 if ( empty( $stats[ $key ] ) ) {
229 $stats[ $key ] = 0;
230 }
231 }
232
233 $stats = (object) $stats;
234 set_transient( 'as_comment_count', $stats );
235 }
236
237 return $stats;
238 }
239
240 /**
241 * Delete comment count cache whenever there is new comment or the status of a comment changes. Cache
242 * will be regenerated next time ActionScheduler_wpCommentLogger::filter_comment_count() is called.
243 */
244 public function delete_comment_count_cache() {
245 delete_transient( 'as_comment_count' );
246 }
247
248 /**
249 * Initialize.
250 *
251 * @codeCoverageIgnore
252 */
253 public function init() {
254 add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 );
255 add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 );
256
257 parent::init();
258
259 add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 );
260 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.
261 add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 );
262
263 // Delete comments count cache whenever there is a new comment or a comment status changes.
264 add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) );
265 add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) );
266 }
267
268 /**
269 * Defer comment counting.
270 */
271 public function disable_comment_counting() {
272 wp_defer_comment_counting( true );
273 }
274
275 /**
276 * Enable comment counting.
277 */
278 public function enable_comment_counting() {
279 wp_defer_comment_counting( false );
280 }
281
282 }
283