PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / data-stores / ActionScheduler_wpCommentLogger.php

ActionScheduler_wpCommentLogger.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at vendor/woocommerce/action-scheduler/classes/data-stores/ActionScheduler_wpCommentLogger.php

272 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $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 return wp_insert_comment($comment_data);
50 }
51
52 /**
53 * Get single log entry for action.
54 *
55 * @param string $entry_id Entry ID.
56 *
57 * @return ActionScheduler_LogEntry
58 */
59 public function get_entry( $entry_id ) {
60 $comment = $this->get_comment( $entry_id );
61 if ( empty($comment) || $comment->comment_type != self::TYPE ) {
62 return new ActionScheduler_NullLogEntry();
63 }
64
65 $date = as_get_datetime_object( $comment->comment_date_gmt );
66 ActionScheduler_TimezoneHelper::set_local_timezone( $date );
67 return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date );
68 }
69
70 /**
71 * Get action's logs.
72 *
73 * @param string $action_id Action ID.
74 *
75 * @return ActionScheduler_LogEntry[]
76 */
77 public function get_logs( $action_id ) {
78 $status = 'all';
79 if ( get_post_status($action_id) == 'trash' ) {
80 $status = 'post-trashed';
81 }
82 $comments = get_comments(array(
83 'post_id' => $action_id,
84 'orderby' => 'comment_date_gmt',
85 'order' => 'ASC',
86 'type' => self::TYPE,
87 'status' => $status,
88 ));
89 $logs = array();
90 foreach ( $comments as $c ) {
91 $entry = $this->get_entry( $c );
92 if ( !empty($entry) ) {
93 $logs[] = $entry;
94 }
95 }
96 return $logs;
97 }
98
99 /**
100 * Get comment.
101 *
102 * @param int $comment_id Comment ID.
103 */
104 protected function get_comment( $comment_id ) {
105 return get_comment( $comment_id );
106 }
107
108
109
110 /**
111 * Filter comment queries.
112 *
113 * @param WP_Comment_Query $query Comment query object.
114 */
115 public function filter_comment_queries( $query ) {
116 foreach ( array('ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID') as $key ) {
117 if ( !empty($query->query_vars[$key]) ) {
118 return; // don't slow down queries that wouldn't include action_log comments anyway.
119 }
120 }
121 $query->query_vars['action_log_filter'] = TRUE;
122 add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 );
123 }
124
125 /**
126 * Filter comment queries.
127 *
128 * @param array $clauses Query's clauses.
129 * @param WP_Comment_Query $query Query object.
130 *
131 * @return array
132 */
133 public function filter_comment_query_clauses( $clauses, $query ) {
134 if ( !empty($query->query_vars['action_log_filter']) ) {
135 $clauses['where'] .= $this->get_where_clause();
136 }
137 return $clauses;
138 }
139
140 /**
141 * Make sure Action Scheduler logs are excluded from comment feeds, which use WP_Query, not
142 * the WP_Comment_Query class handled by @see self::filter_comment_queries().
143 *
144 * @param string $where Query's `where` clause.
145 * @param WP_Query $query Query object.
146 *
147 * @return string
148 */
149 public function filter_comment_feed( $where, $query ) {
150 if ( is_comment_feed() ) {
151 $where .= $this->get_where_clause();
152 }
153 return $where;
154 }
155
156 /**
157 * Return a SQL clause to exclude Action Scheduler comments.
158 *
159 * @return string
160 */
161 protected function get_where_clause() {
162 global $wpdb;
163 return sprintf( " AND {$wpdb->comments}.comment_type != '%s'", self::TYPE );
164 }
165
166 /**
167 * Remove action log entries from wp_count_comments()
168 *
169 * @param array $stats Comment count.
170 * @param int $post_id Post ID.
171 *
172 * @return object
173 */
174 public function filter_comment_count( $stats, $post_id ) {
175 global $wpdb;
176
177 if ( 0 === $post_id ) {
178 $stats = $this->get_comment_count();
179 }
180
181 return $stats;
182 }
183
184 /**
185 * Retrieve the comment counts from our cache, or the database if the cached version isn't set.
186 *
187 * @return object
188 */
189 protected function get_comment_count() {
190 global $wpdb;
191
192 $stats = get_transient( 'as_comment_count' );
193
194 if ( ! $stats ) {
195 $stats = array();
196
197 $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 );
198
199 $total = 0;
200 $stats = array();
201 $approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' );
202
203 foreach ( (array) $count as $row ) {
204 // Don't count post-trashed toward totals.
205 if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
206 $total += $row['num_comments'];
207 }
208 if ( isset( $approved[ $row['comment_approved'] ] ) ) {
209 $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
210 }
211 }
212
213 $stats['total_comments'] = $total;
214 $stats['all'] = $total;
215
216 foreach ( $approved as $key ) {
217 if ( empty( $stats[ $key ] ) ) {
218 $stats[ $key ] = 0;
219 }
220 }
221
222 $stats = (object) $stats;
223 set_transient( 'as_comment_count', $stats );
224 }
225
226 return $stats;
227 }
228
229 /**
230 * Delete comment count cache whenever there is new comment or the status of a comment changes. Cache
231 * will be regenerated next time ActionScheduler_wpCommentLogger::filter_comment_count() is called.
232 */
233 public function delete_comment_count_cache() {
234 delete_transient( 'as_comment_count' );
235 }
236
237 /**
238 * Initialize.
239 *
240 * @codeCoverageIgnore
241 */
242 public function init() {
243 add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 );
244 add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 );
245
246 parent::init();
247
248 add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 );
249 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.
250 add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 );
251
252 // Delete comments count cache whenever there is a new comment or a comment status changes.
253 add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) );
254 add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) );
255 }
256
257 /**
258 * Defer comment counting.
259 */
260 public function disable_comment_counting() {
261 wp_defer_comment_counting(true);
262 }
263
264 /**
265 * Enable comment counting.
266 */
267 public function enable_comment_counting() {
268 wp_defer_comment_counting(false);
269 }
270
271 }
272