Reviews.php
4 months ago
ReviewsCommentsOverrides.php
1 month ago
ReviewsListTable.php
4 months ago
ReviewsUtil.php
3 months ago
ReviewsUtil.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Admin\ProductReviews; |
| 4 | |
| 5 | /** |
| 6 | * A utility class for handling comments that are product reviews. |
| 7 | */ |
| 8 | class ReviewsUtil { |
| 9 | |
| 10 | /** |
| 11 | * Modifies the moderation URLs in the email notifications for product reviews. |
| 12 | * |
| 13 | * @param string $message The email notification message. |
| 14 | * @param int $comment_id The comment ID. |
| 15 | * @return string The modified email notification message. |
| 16 | */ |
| 17 | public static function modify_product_review_moderation_urls( $message, $comment_id ) { |
| 18 | $comment = get_comment( $comment_id ); |
| 19 | |
| 20 | // Only modify URLs for product reviews. |
| 21 | if ( ! $comment || get_post_type( $comment->comment_post_ID ) !== 'product' ) { |
| 22 | return $message; |
| 23 | } |
| 24 | |
| 25 | // Replace the WordPress comment moderation URLs with WooCommerce product review URLs. |
| 26 | $product_reviews_url = admin_url( 'edit.php?post_type=product&page=product-reviews' ); |
| 27 | |
| 28 | // Replace the moderation panel URL (this is the "show all reviews pending" link). |
| 29 | $message = str_replace( |
| 30 | admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ), |
| 31 | $product_reviews_url . '&comment_status=moderated', |
| 32 | $message |
| 33 | ); |
| 34 | |
| 35 | return $message; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Removes product reviews from the edit-comments page to fix the "Mine" tab counter. |
| 40 | * |
| 41 | * @param array|mixed $clauses A compacted array of comment query clauses. |
| 42 | * @param \WP_Comment_Query $comment_query The WP_Comment_Query instance being filtered. |
| 43 | * |
| 44 | * @return array|mixed |
| 45 | */ |
| 46 | public static function comments_clauses_without_product_reviews( $clauses, $comment_query ) { |
| 47 | global $wpdb; |
| 48 | |
| 49 | if ( ! empty( $comment_query->query_vars['post_type'] ) ) { |
| 50 | $post_type = $comment_query->query_vars['post_type']; |
| 51 | if ( ! is_array( $post_type ) ) { |
| 52 | $post_type = explode( ',', $post_type ); |
| 53 | } |
| 54 | if ( in_array( 'product', $post_type, true ) ) { |
| 55 | return $clauses; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Any comment queries with these values are likely to be custom handling where we don't want to change default behavior. |
| 61 | * This may change for the `type` query vars in the future if we break out review replies as their own type. |
| 62 | */ |
| 63 | foreach ( array( 'ID', 'parent', 'parent__in', 'post_author__in', 'post_author', 'post_name', 'type', 'type__in', 'type__not_in', 'post_type__in', 'comment__in', 'comment__not_in' ) as $arg ) { |
| 64 | if ( ! empty( $comment_query->query_vars[ $arg ] ) ) { |
| 65 | return $clauses; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if ( ! empty( $comment_query->query_vars['post_id'] ) && absint( $comment_query->query_vars['post_id'] ) > 0 ) { |
| 70 | if ( 'product' === get_post_type( absint( $comment_query->query_vars['post_id'] ) ) ) { |
| 71 | return $clauses; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if ( ! empty( $comment_query->query_vars['post__in'] ) ) { |
| 76 | $post_ids = wp_parse_id_list( $comment_query->query_vars['post__in'] ); |
| 77 | // Prime caches to reduce future queries. |
| 78 | _prime_post_caches( $post_ids, false, false ); |
| 79 | foreach ( $post_ids as $post_id ) { |
| 80 | if ( 'product' === get_post_type( $post_id ) ) { |
| 81 | return $clauses; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $clauses['join'] .= " LEFT JOIN {$wpdb->posts} AS wp_posts_to_exclude_reviews ON comment_post_ID = wp_posts_to_exclude_reviews.ID "; |
| 87 | $clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " wp_posts_to_exclude_reviews.post_type NOT IN ('product') "; |
| 88 | |
| 89 | return $clauses; |
| 90 | } |
| 91 | } |
| 92 |