woocommerce
/
packages
/
action-scheduler
/
classes
/
data-stores
/
ActionScheduler_wpCommentLogger.php
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 |