← All changes
|
vendor/woocommerce/action-scheduler/classes/data-stores/ActionScheduler_wpCommentLogger.php
+96
-54
1.5.0
→
trunk
View file →
| @@ -4,19 +4,21 @@ | ||
| 4 | 4 | * Class ActionScheduler_wpCommentLogger |
| 5 | 5 | */ |
| 6 | 6 | class ActionScheduler_wpCommentLogger extends ActionScheduler_Logger { |
| 7 | 7 | const AGENT = 'ActionScheduler'; |
| 8 | - const TYPE = 'action_log'; | |
| 8 | + const TYPE = 'action_log'; | |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | - * @param string $action_id | |
| 12 | - * @param string $message | |
| 13 | - * @param DateTime $date | |
| 11 | + * Create log entry. | |
| 14 | 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 | + * | |
| 15 | 17 | * @return string The log entry ID |
| 16 | 18 | */ |
| 17 | - public function log( $action_id, $message, DateTime $date = NULL ) { | |
| 18 | - if ( empty($date) ) { | |
| 19 | + public function log( $action_id, $message, ?DateTime $date = null ) { | |
| 20 | + if ( empty( $date ) ) { | |
| 19 | 21 | $date = as_get_datetime_object(); |
| 20 | 22 | } else { |
| 21 | 23 | $date = as_get_datetime_object( clone $date ); |
| 22 | 24 | } |
| @@ -23,32 +25,43 @@ | ||
| 23 | 25 | $comment_id = $this->create_wp_comment( $action_id, $message, $date ); |
| 24 | 26 | return $comment_id; |
| 25 | 27 | } |
| 26 | 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 | + */ | |
| 27 | 36 | protected function create_wp_comment( $action_id, $message, DateTime $date ) { |
| 28 | 37 | |
| 29 | - $comment_date_gmt = $date->format('Y-m-d H:i:s'); | |
| 38 | + $comment_date_gmt = $date->format( 'Y-m-d H:i:s' ); | |
| 30 | 39 | ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
| 31 | 40 | $comment_data = array( |
| 32 | - 'comment_post_ID' => $action_id, | |
| 33 | - 'comment_date' => $date->format('Y-m-d H:i:s'), | |
| 41 | + 'comment_post_ID' => $action_id, | |
| 42 | + 'comment_date' => $date->format( 'Y-m-d H:i:s' ), | |
| 34 | 43 | '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, | |
| 44 | + 'comment_author' => self::AGENT, | |
| 45 | + 'comment_content' => $message, | |
| 46 | + 'comment_agent' => self::AGENT, | |
| 47 | + 'comment_type' => self::TYPE, | |
| 39 | 48 | ); |
| 40 | - return wp_insert_comment($comment_data); | |
| 49 | + | |
| 50 | + return wp_insert_comment( $comment_data ); | |
| 41 | 51 | } |
| 42 | 52 | |
| 43 | 53 | /** |
| 44 | - * @param string $entry_id | |
| 54 | + * Get single log entry for action. | |
| 45 | 55 | * |
| 56 | + * @param string $entry_id Entry ID. | |
| 57 | + * | |
| 46 | 58 | * @return ActionScheduler_LogEntry |
| 47 | 59 | */ |
| 48 | 60 | public function get_entry( $entry_id ) { |
| 49 | 61 | $comment = $this->get_comment( $entry_id ); |
| 50 | - if ( empty($comment) || $comment->comment_type != self::TYPE ) { | |
| 62 | + | |
| 63 | + if ( empty( $comment ) || self::TYPE !== $comment->comment_type ) { | |
| 51 | 64 | return new ActionScheduler_NullLogEntry(); |
| 52 | 65 | } |
| 53 | 66 | |
| 54 | 67 | $date = as_get_datetime_object( $comment->comment_date_gmt ); |
| @@ -56,61 +69,77 @@ | ||
| 56 | 69 | return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date ); |
| 57 | 70 | } |
| 58 | 71 | |
| 59 | 72 | /** |
| 60 | - * @param string $action_id | |
| 73 | + * Get action's logs. | |
| 61 | 74 | * |
| 75 | + * @param string $action_id Action ID. | |
| 76 | + * | |
| 62 | 77 | * @return ActionScheduler_LogEntry[] |
| 63 | 78 | */ |
| 64 | 79 | public function get_logs( $action_id ) { |
| 65 | 80 | $status = 'all'; |
| 66 | - if ( get_post_status($action_id) == 'trash' ) { | |
| 81 | + $logs = array(); | |
| 82 | + | |
| 83 | + if ( get_post_status( $action_id ) === 'trash' ) { | |
| 67 | 84 | $status = 'post-trashed'; |
| 68 | 85 | } |
| 69 | - $comments = get_comments(array( | |
| 70 | - 'post_id' => $action_id, | |
| 71 | - 'orderby' => 'comment_date_gmt', | |
| 72 | - 'order' => 'ASC', | |
| 73 | - 'type' => self::TYPE, | |
| 74 | - 'status' => $status, | |
| 75 | - )); | |
| 76 | - $logs = array(); | |
| 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 | + | |
| 77 | 97 | foreach ( $comments as $c ) { |
| 78 | 98 | $entry = $this->get_entry( $c ); |
| 79 | - if ( !empty($entry) ) { | |
| 99 | + | |
| 100 | + if ( ! empty( $entry ) ) { | |
| 80 | 101 | $logs[] = $entry; |
| 81 | 102 | } |
| 82 | 103 | } |
| 104 | + | |
| 83 | 105 | return $logs; |
| 84 | 106 | } |
| 85 | 107 | |
| 108 | + /** | |
| 109 | + * Get comment. | |
| 110 | + * | |
| 111 | + * @param int $comment_id Comment ID. | |
| 112 | + */ | |
| 86 | 113 | protected function get_comment( $comment_id ) { |
| 87 | 114 | return get_comment( $comment_id ); |
| 88 | 115 | } |
| 89 | 116 | |
| 90 | - | |
| 91 | - | |
| 92 | 117 | /** |
| 93 | - * @param WP_Comment_Query $query | |
| 118 | + * Filter comment queries. | |
| 119 | + * | |
| 120 | + * @param WP_Comment_Query $query Comment query object. | |
| 94 | 121 | */ |
| 95 | 122 | public function filter_comment_queries( $query ) { |
| 96 | - foreach ( array('ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID') as $key ) { | |
| 97 | - if ( !empty($query->query_vars[$key]) ) { | |
| 98 | - return; // don't slow down queries that wouldn't include action_log comments anyway | |
| 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. | |
| 99 | 126 | } |
| 100 | 127 | } |
| 101 | - $query->query_vars['action_log_filter'] = TRUE; | |
| 128 | + $query->query_vars['action_log_filter'] = true; | |
| 102 | 129 | add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 ); |
| 103 | 130 | } |
| 104 | 131 | |
| 105 | 132 | /** |
| 106 | - * @param array $clauses | |
| 107 | - * @param WP_Comment_Query $query | |
| 133 | + * Filter comment queries. | |
| 108 | 134 | * |
| 135 | + * @param array $clauses Query's clauses. | |
| 136 | + * @param WP_Comment_Query $query Query object. | |
| 137 | + * | |
| 109 | 138 | * @return array |
| 110 | 139 | */ |
| 111 | 140 | public function filter_comment_query_clauses( $clauses, $query ) { |
| 112 | - if ( !empty($query->query_vars['action_log_filter']) ) { | |
| 141 | + if ( ! empty( $query->query_vars['action_log_filter'] ) ) { | |
| 113 | 142 | $clauses['where'] .= $this->get_where_clause(); |
| 114 | 143 | } |
| 115 | 144 | return $clauses; |
| 116 | 145 | } |
| @@ -118,10 +147,10 @@ | ||
| 118 | 147 | /** |
| 119 | 148 | * Make sure Action Scheduler logs are excluded from comment feeds, which use WP_Query, not |
| 120 | 149 | * the WP_Comment_Query class handled by @see self::filter_comment_queries(). |
| 121 | 150 | * |
| 122 | - * @param string $where | |
| 123 | - * @param WP_Query $query | |
| 151 | + * @param string $where Query's `where` clause. | |
| 152 | + * @param WP_Query $query Query object. | |
| 124 | 153 | * |
| 125 | 154 | * @return string |
| 126 | 155 | */ |
| 127 | 156 | public function filter_comment_feed( $where, $query ) { |
| @@ -143,10 +172,10 @@ | ||
| 143 | 172 | |
| 144 | 173 | /** |
| 145 | 174 | * Remove action log entries from wp_count_comments() |
| 146 | 175 | * |
| 147 | - * @param array $stats | |
| 148 | - * @param int $post_id | |
| 176 | + * @param array $stats Comment count. | |
| 177 | + * @param int $post_id Post ID. | |
| 149 | 178 | * |
| 150 | 179 | * @return object |
| 151 | 180 | */ |
| 152 | 181 | public function filter_comment_count( $stats, $post_id ) { |
| @@ -169,19 +198,23 @@ | ||
| 169 | 198 | |
| 170 | 199 | $stats = get_transient( 'as_comment_count' ); |
| 171 | 200 | |
| 172 | 201 | if ( ! $stats ) { |
| 173 | - $stats = array(); | |
| 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 | + ); | |
| 174 | 213 | |
| 175 | - $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 ); | |
| 176 | - | |
| 177 | - $total = 0; | |
| 178 | - $stats = array(); | |
| 179 | - $approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' ); | |
| 180 | - | |
| 181 | 214 | foreach ( (array) $count as $row ) { |
| 182 | - // Don't count post-trashed toward totals | |
| 183 | - if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) { | |
| 215 | + // Don't count post-trashed toward totals. | |
| 216 | + if ( 'post-trashed' !== $row['comment_approved'] && 'trash' !== $row['comment_approved'] ) { | |
| 184 | 217 | $total += $row['num_comments']; |
| 185 | 218 | } |
| 186 | 219 | if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
| 187 | 220 | $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
| @@ -212,8 +245,10 @@ | ||
| 212 | 245 | delete_transient( 'as_comment_count' ); |
| 213 | 246 | } |
| 214 | 247 | |
| 215 | 248 | /** |
| 249 | + * Initialize. | |
| 250 | + * | |
| 216 | 251 | * @codeCoverageIgnore |
| 217 | 252 | */ |
| 218 | 253 | public function init() { |
| 219 | 254 | add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 ); |
| @@ -221,20 +256,27 @@ | ||
| 221 | 256 | |
| 222 | 257 | parent::init(); |
| 223 | 258 | |
| 224 | 259 | add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 ); |
| 225 | - 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 | |
| 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. | |
| 226 | 261 | add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 ); |
| 227 | 262 | |
| 228 | - // Delete comments count cache whenever there is a new comment or a comment status changes | |
| 263 | + // Delete comments count cache whenever there is a new comment or a comment status changes. | |
| 229 | 264 | add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) ); |
| 230 | 265 | add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) ); |
| 231 | 266 | } |
| 232 | 267 | |
| 268 | + /** | |
| 269 | + * Defer comment counting. | |
| 270 | + */ | |
| 233 | 271 | public function disable_comment_counting() { |
| 234 | - wp_defer_comment_counting(true); | |
| 272 | + wp_defer_comment_counting( true ); | |
| 235 | 273 | } |
| 274 | + | |
| 275 | + /** | |
| 276 | + * Enable comment counting. | |
| 277 | + */ | |
| 236 | 278 | public function enable_comment_counting() { |
| 237 | - wp_defer_comment_counting(false); | |
| 279 | + wp_defer_comment_counting( false ); | |
| 238 | 280 | } |
| 239 | 281 | |
| 240 | 282 | } |