Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../includes
File: class-wc-comments.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Comments
[2] Fix | Delete
*
[3] Fix | Delete
* Handle comments (reviews and order notes).
[4] Fix | Delete
*
[5] Fix | Delete
* @package WooCommerce\Classes\Products
[6] Fix | Delete
* @version 2.3.0
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\ProductReviews\ReviewsUtil;
[10] Fix | Delete
[11] Fix | Delete
defined( 'ABSPATH' ) || exit;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Comments class.
[15] Fix | Delete
*/
[16] Fix | Delete
class WC_Comments {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* The cache group to use for comment counts.
[20] Fix | Delete
*
[21] Fix | Delete
* @var string
[22] Fix | Delete
*/
[23] Fix | Delete
private const COMMENT_COUNT_CACHE_GROUP = 'wc_comment_counts';
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The cache key to use for pending product reviews counts.
[27] Fix | Delete
*
[28] Fix | Delete
* @var string
[29] Fix | Delete
*/
[30] Fix | Delete
private const PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY = 'woocommerce_product_reviews_pending_count';
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Hook in methods.
[34] Fix | Delete
*/
[35] Fix | Delete
public static function init() {
[36] Fix | Delete
// Rating posts.
[37] Fix | Delete
add_filter( 'comments_open', array( __CLASS__, 'comments_open' ), 10, 2 );
[38] Fix | Delete
add_filter( 'preprocess_comment', array( __CLASS__, 'check_comment_rating' ), 0 );
[39] Fix | Delete
add_action( 'comment_post', array( __CLASS__, 'add_comment_rating' ), 1 );
[40] Fix | Delete
add_action( 'comment_moderation_recipients', array( __CLASS__, 'comment_moderation_recipients' ), 10, 2 );
[41] Fix | Delete
[42] Fix | Delete
// Clear transients.
[43] Fix | Delete
add_action( 'wp_update_comment_count', array( __CLASS__, 'clear_transients' ) );
[44] Fix | Delete
[45] Fix | Delete
// Secure order notes.
[46] Fix | Delete
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
[47] Fix | Delete
add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
[48] Fix | Delete
[49] Fix | Delete
// Secure webhook comments.
[50] Fix | Delete
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
[51] Fix | Delete
add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_webhook_comments_from_feed_where' ) );
[52] Fix | Delete
[53] Fix | Delete
// Secure potential remaining Action Logs.
[54] Fix | Delete
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_action_log_comments' ), 10, 2 );
[55] Fix | Delete
add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_action_log_comments_from_feed_where' ) );
[56] Fix | Delete
[57] Fix | Delete
// Exclude product reviews from general comments.
[58] Fix | Delete
add_filter( 'comments_clauses', array( ReviewsUtil::class, 'comments_clauses_without_product_reviews' ), 10, 2 );
[59] Fix | Delete
[60] Fix | Delete
// Modifies the moderation URLs in the email notifications for product reviews.
[61] Fix | Delete
add_filter( 'comment_moderation_text', array( ReviewsUtil::class, 'modify_product_review_moderation_urls' ), 10, 2 );
[62] Fix | Delete
[63] Fix | Delete
// Count comments.
[64] Fix | Delete
add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 10, 2 );
[65] Fix | Delete
[66] Fix | Delete
// Delete comments count cache whenever there is a new comment or a comment status changes.
[67] Fix | Delete
add_action( 'wp_insert_comment', array( __CLASS__, 'increment_comments_count_cache_on_wp_insert_comment' ), 10, 2 );
[68] Fix | Delete
add_action( 'transition_comment_status', array( __CLASS__, 'update_comments_count_cache_on_comment_status_change' ), 10, 3 );
[69] Fix | Delete
[70] Fix | Delete
// Count product reviews that pending moderation.
[71] Fix | Delete
add_action( 'wp_insert_comment', array( __CLASS__, 'maybe_bump_products_reviews_pending_moderation_counter' ), 10, 2 );
[72] Fix | Delete
add_action( 'transition_comment_status', array( __CLASS__, 'maybe_adjust_products_reviews_pending_moderation_counter' ), 10, 3 );
[73] Fix | Delete
[74] Fix | Delete
// Support avatars for `review` comment type.
[75] Fix | Delete
add_filter( 'get_avatar_comment_types', array( __CLASS__, 'add_avatar_for_review_comment_type' ) );
[76] Fix | Delete
[77] Fix | Delete
// Add Product Reviews filter for `review` comment type.
[78] Fix | Delete
add_filter( 'admin_comment_types_dropdown', array( __CLASS__, 'add_review_comment_filter' ) );
[79] Fix | Delete
[80] Fix | Delete
// Review of verified purchase.
[81] Fix | Delete
add_action( 'comment_post', array( __CLASS__, 'add_comment_purchase_verification' ) );
[82] Fix | Delete
[83] Fix | Delete
// Set comment type.
[84] Fix | Delete
add_action( 'preprocess_comment', array( __CLASS__, 'update_comment_type' ), 1 );
[85] Fix | Delete
[86] Fix | Delete
// Validate product reviews if requires verified owners.
[87] Fix | Delete
add_action( 'pre_comment_on_post', array( __CLASS__, 'validate_product_review_verified_owners' ) );
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* See if comments are open.
[92] Fix | Delete
*
[93] Fix | Delete
* @since 3.1.0
[94] Fix | Delete
* @param bool $open Whether the current post is open for comments.
[95] Fix | Delete
* @param int $post_id Post ID.
[96] Fix | Delete
* @return bool
[97] Fix | Delete
*/
[98] Fix | Delete
public static function comments_open( $open, $post_id ) {
[99] Fix | Delete
if ( 'product' === get_post_type( $post_id ) && ! post_type_supports( 'product', 'comments' ) ) {
[100] Fix | Delete
$open = false;
[101] Fix | Delete
}
[102] Fix | Delete
return $open;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Exclude order comments from queries and RSS.
[107] Fix | Delete
*
[108] Fix | Delete
* This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
[109] Fix | Delete
* and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and.
[110] Fix | Delete
* shop managers can view orders anyway.
[111] Fix | Delete
*
[112] Fix | Delete
* The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
[113] Fix | Delete
*
[114] Fix | Delete
* @param array $clauses A compacted array of comment query clauses.
[115] Fix | Delete
* @return array
[116] Fix | Delete
*/
[117] Fix | Delete
public static function exclude_order_comments( $clauses ) {
[118] Fix | Delete
$clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " comment_type != 'order_note' ";
[119] Fix | Delete
return $clauses;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Exclude order comments from feed.
[124] Fix | Delete
*
[125] Fix | Delete
* @deprecated 3.1
[126] Fix | Delete
* @param mixed $join Deprecated.
[127] Fix | Delete
*/
[128] Fix | Delete
public static function exclude_order_comments_from_feed_join( $join ) {
[129] Fix | Delete
wc_deprecated_function( 'WC_Comments::exclude_order_comments_from_feed_join', '3.1' );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Exclude order comments from queries and RSS.
[134] Fix | Delete
*
[135] Fix | Delete
* @param string $where The WHERE clause of the query.
[136] Fix | Delete
* @return string
[137] Fix | Delete
*/
[138] Fix | Delete
public static function exclude_order_comments_from_feed_where( $where ) {
[139] Fix | Delete
return $where . ( trim( $where ) ? ' AND ' : '' ) . " comment_type != 'order_note' ";
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Exclude webhook comments from queries and RSS.
[144] Fix | Delete
*
[145] Fix | Delete
* @since 2.2
[146] Fix | Delete
* @param array $clauses A compacted array of comment query clauses.
[147] Fix | Delete
* @return array
[148] Fix | Delete
*/
[149] Fix | Delete
public static function exclude_webhook_comments( $clauses ) {
[150] Fix | Delete
$clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
[151] Fix | Delete
return $clauses;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Exclude webhooks comments from feed.
[156] Fix | Delete
*
[157] Fix | Delete
* @deprecated 3.1
[158] Fix | Delete
* @param mixed $join Deprecated.
[159] Fix | Delete
*/
[160] Fix | Delete
public static function exclude_webhook_comments_from_feed_join( $join ) {
[161] Fix | Delete
wc_deprecated_function( 'WC_Comments::exclude_webhook_comments_from_feed_join', '3.1' );
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Exclude webhook comments from queries and RSS.
[166] Fix | Delete
*
[167] Fix | Delete
* @since 2.1
[168] Fix | Delete
* @param string $where The WHERE clause of the query.
[169] Fix | Delete
* @return string
[170] Fix | Delete
*/
[171] Fix | Delete
public static function exclude_webhook_comments_from_feed_where( $where ) {
[172] Fix | Delete
return $where . ( trim( $where ) ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Exclude action_log comments from queries and RSS.
[177] Fix | Delete
*
[178] Fix | Delete
* @since 9.9
[179] Fix | Delete
* @param string $where The WHERE clause of the query.
[180] Fix | Delete
* @return string
[181] Fix | Delete
*/
[182] Fix | Delete
public static function exclude_action_log_comments_from_feed_where( $where ) {
[183] Fix | Delete
return $where . ( trim( $where ) ? ' AND ' : '' ) . " comment_type != 'action_log' ";
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
/**
[187] Fix | Delete
* Exclude action_log comments from queries.
[188] Fix | Delete
*
[189] Fix | Delete
* @param array $clauses A compacted array of comment query clauses.
[190] Fix | Delete
* @param WP_Comment_Query $comment_query The WP_Comment_Query being filtered.
[191] Fix | Delete
*
[192] Fix | Delete
* @return array
[193] Fix | Delete
* @since 9.9
[194] Fix | Delete
*/
[195] Fix | Delete
public static function exclude_action_log_comments( $clauses, $comment_query ) {
[196] Fix | Delete
if ( 'action_log' !== $comment_query->query_vars['type'] ) {
[197] Fix | Delete
$clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " comment_type != 'action_log' ";
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $clauses;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Validate the comment ratings.
[205] Fix | Delete
*
[206] Fix | Delete
* @param array $comment_data Comment data.
[207] Fix | Delete
* @return array
[208] Fix | Delete
*/
[209] Fix | Delete
public static function check_comment_rating( $comment_data ) {
[210] Fix | Delete
// If posting a comment (not trackback etc) and not logged in.
[211] Fix | Delete
if ( ! is_admin() && isset( $_POST['comment_post_ID'], $_POST['rating'], $comment_data['comment_type'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) && empty( $_POST['rating'] ) && self::is_default_comment_type( $comment_data['comment_type'] ) && wc_review_ratings_enabled() && wc_review_ratings_required() ) { // WPCS: input var ok, CSRF ok.
[212] Fix | Delete
wp_die( esc_html__( 'Please rate the product.', 'woocommerce' ) );
[213] Fix | Delete
exit;
[214] Fix | Delete
}
[215] Fix | Delete
return $comment_data;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
/**
[219] Fix | Delete
* Rating field for comments.
[220] Fix | Delete
*
[221] Fix | Delete
* @param int $comment_id Comment ID.
[222] Fix | Delete
*/
[223] Fix | Delete
public static function add_comment_rating( $comment_id ) {
[224] Fix | Delete
if ( isset( $_POST['rating'], $_POST['comment_post_ID'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
[225] Fix | Delete
if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
[226] Fix | Delete
return;
[227] Fix | Delete
}
[228] Fix | Delete
add_comment_meta( $comment_id, 'rating', intval( $_POST['rating'] ), true ); // WPCS: input var ok, CSRF ok.
[229] Fix | Delete
[230] Fix | Delete
$post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // WPCS: input var ok, CSRF ok.
[231] Fix | Delete
if ( $post_id ) {
[232] Fix | Delete
self::clear_transients( $post_id );
[233] Fix | Delete
}
[234] Fix | Delete
}
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Modify recipient of review email.
[239] Fix | Delete
*
[240] Fix | Delete
* @param array $emails Emails.
[241] Fix | Delete
* @param int $comment_id Comment ID.
[242] Fix | Delete
* @return array
[243] Fix | Delete
*/
[244] Fix | Delete
public static function comment_moderation_recipients( $emails, $comment_id ) {
[245] Fix | Delete
$comment = get_comment( $comment_id );
[246] Fix | Delete
[247] Fix | Delete
if ( $comment && 'product' === get_post_type( $comment->comment_post_ID ) ) {
[248] Fix | Delete
$emails = array( get_option( 'admin_email' ) );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
return $emails;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
/**
[255] Fix | Delete
* Ensure product average rating and review count is kept up to date.
[256] Fix | Delete
*
[257] Fix | Delete
* @param int $post_id Post ID.
[258] Fix | Delete
*/
[259] Fix | Delete
public static function clear_transients( $post_id ) {
[260] Fix | Delete
if ( 'product' === get_post_type( $post_id ) ) {
[261] Fix | Delete
$product = wc_get_product( $post_id );
[262] Fix | Delete
$product->set_rating_counts( self::get_rating_counts_for_product( $product ) );
[263] Fix | Delete
$product->set_average_rating( self::get_average_rating_for_product( $product ) );
[264] Fix | Delete
$product->set_review_count( self::get_review_count_for_product( $product ) );
[265] Fix | Delete
$product->save();
[266] Fix | Delete
}
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
/**
[270] Fix | Delete
* Callback for 'wp_insert_comment' to delete the comment count cache if the comment is included in the count.
[271] Fix | Delete
*
[272] Fix | Delete
* @param int $comment_id The comment ID.
[273] Fix | Delete
* @param WP_Comment $comment Comment object.
[274] Fix | Delete
*
[275] Fix | Delete
* @return void
[276] Fix | Delete
*/
[277] Fix | Delete
public static function increment_comments_count_cache_on_wp_insert_comment( $comment_id, $comment ) {
[278] Fix | Delete
if ( ! self::is_comment_excluded_from_wp_comment_counts( $comment ) ) {
[279] Fix | Delete
$comment_status = wp_get_comment_status( $comment );
[280] Fix | Delete
if ( false !== $comment_status ) {
[281] Fix | Delete
wp_cache_incr( 'wc_count_comments_' . $comment_status, 1, self::COMMENT_COUNT_CACHE_GROUP );
[282] Fix | Delete
}
[283] Fix | Delete
}
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
/**
[287] Fix | Delete
* Callback for 'comment_status_change' to delete the comment count cache if the comment is included in the count.
[288] Fix | Delete
*
[289] Fix | Delete
* @param int|string $new_status The new comment status.
[290] Fix | Delete
* @param int|string $old_status The old comment status.
[291] Fix | Delete
* @param WP_Comment $comment Comment object.
[292] Fix | Delete
*
[293] Fix | Delete
* @return void
[294] Fix | Delete
*/
[295] Fix | Delete
public static function update_comments_count_cache_on_comment_status_change( $new_status, $old_status, $comment ) {
[296] Fix | Delete
if ( ! self::is_comment_excluded_from_wp_comment_counts( $comment ) ) {
[297] Fix | Delete
wp_cache_incr( 'wc_count_comments_' . $new_status, 1, self::COMMENT_COUNT_CACHE_GROUP );
[298] Fix | Delete
wp_cache_decr( 'wc_count_comments_' . $old_status, 1, self::COMMENT_COUNT_CACHE_GROUP );
[299] Fix | Delete
}
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Determines whether the given comment should be included in the core WP comment counts that are displayed in the
[304] Fix | Delete
* WordPress admin.
[305] Fix | Delete
*
[306] Fix | Delete
* @param WP_Comment $comment Comment object.
[307] Fix | Delete
*
[308] Fix | Delete
* @return bool
[309] Fix | Delete
*/
[310] Fix | Delete
private static function is_comment_excluded_from_wp_comment_counts( $comment ) {
[311] Fix | Delete
return in_array( $comment->comment_type, array( 'action_log', 'order_note', 'webhook_delivery' ), true )
[312] Fix | Delete
|| get_post_type( $comment->comment_post_ID ) === 'product';
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Delete comments count cache whenever there is
[317] Fix | Delete
* new comment or the status of a comment changes. Cache
[318] Fix | Delete
* will be regenerated next time WC_Comments::wp_count_comments()
[319] Fix | Delete
* is called.
[320] Fix | Delete
*/
[321] Fix | Delete
public static function delete_comments_count_cache() {
[322] Fix | Delete
$comment_status_keys = array(
[323] Fix | Delete
'wc_count_comments_approved',
[324] Fix | Delete
'wc_count_comments_unapproved',
[325] Fix | Delete
'wc_count_comments_spam',
[326] Fix | Delete
'wc_count_comments_trash',
[327] Fix | Delete
'wc_count_comments_post-trashed',
[328] Fix | Delete
);
[329] Fix | Delete
wp_cache_delete_multiple( $comment_status_keys, self::COMMENT_COUNT_CACHE_GROUP );
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/**
[333] Fix | Delete
* Fetches (and populates if needed) the counter.
[334] Fix | Delete
*
[335] Fix | Delete
* @return int
[336] Fix | Delete
*/
[337] Fix | Delete
public static function get_products_reviews_pending_moderation_counter(): int {
[338] Fix | Delete
$count = wp_cache_get( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, self::COMMENT_COUNT_CACHE_GROUP );
[339] Fix | Delete
if ( false === $count ) {
[340] Fix | Delete
$count = (int) get_comments(
[341] Fix | Delete
array(
[342] Fix | Delete
'type__in' => array( 'review', 'comment' ),
[343] Fix | Delete
'status' => '0',
[344] Fix | Delete
'post_type' => 'product',
[345] Fix | Delete
'count' => true,
[346] Fix | Delete
)
[347] Fix | Delete
);
[348] Fix | Delete
wp_cache_set( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, $count, self::COMMENT_COUNT_CACHE_GROUP, DAY_IN_SECONDS );
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
return $count;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
/**
[355] Fix | Delete
* Handles `wp_insert_comment` hook processing and actualizes the counter.
[356] Fix | Delete
*
[357] Fix | Delete
* @param int $comment_id Comment ID.
[358] Fix | Delete
* @param \WP_Comment $comment Comment object.
[359] Fix | Delete
* @return void
[360] Fix | Delete
*/
[361] Fix | Delete
public static function maybe_bump_products_reviews_pending_moderation_counter( $comment_id, $comment ): void {
[362] Fix | Delete
$needs_bump = '0' === $comment->comment_approved;
[363] Fix | Delete
if ( $needs_bump && in_array( $comment->comment_type, array( 'review', 'comment', '' ), true ) ) {
[364] Fix | Delete
$is_product = 'product' === get_post_type( $comment->comment_post_ID );
[365] Fix | Delete
if ( $is_product ) {
[366] Fix | Delete
wp_cache_incr( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, 1, self::COMMENT_COUNT_CACHE_GROUP );
[367] Fix | Delete
}
[368] Fix | Delete
}
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* Handles `transition_comment_status` hook processing and actualizes the counter.
[373] Fix | Delete
*
[374] Fix | Delete
* @param int|string $new_status New status.
[375] Fix | Delete
* @param int|string $old_status Old status.
[376] Fix | Delete
* @param \WP_Comment $comment Comment object.
[377] Fix | Delete
* @return void
[378] Fix | Delete
*/
[379] Fix | Delete
public static function maybe_adjust_products_reviews_pending_moderation_counter( $new_status, $old_status, $comment ): void {
[380] Fix | Delete
$needs_adjustments = 'unapproved' === $new_status || 'unapproved' === $old_status;
[381] Fix | Delete
if ( $needs_adjustments && in_array( $comment->comment_type, array( 'review', 'comment', '' ), true ) ) {
[382] Fix | Delete
$is_product = 'product' === get_post_type( $comment->comment_post_ID );
[383] Fix | Delete
if ( $is_product ) {
[384] Fix | Delete
if ( '0' === $comment->comment_approved ) {
[385] Fix | Delete
wp_cache_incr( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, 1, self::COMMENT_COUNT_CACHE_GROUP );
[386] Fix | Delete
} else {
[387] Fix | Delete
wp_cache_decr( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, 1, self::COMMENT_COUNT_CACHE_GROUP );
[388] Fix | Delete
}
[389] Fix | Delete
}
[390] Fix | Delete
}
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
/**
[394] Fix | Delete
* Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().
[395] Fix | Delete
*
[396] Fix | Delete
* @param array|object $stats Comment stats.
[397] Fix | Delete
* @param int $post_id Post ID.
[398] Fix | Delete
*
[399] Fix | Delete
* @return object
[400] Fix | Delete
* @since 2.2
[401] Fix | Delete
*/
[402] Fix | Delete
public static function wp_count_comments( $stats, $post_id ) {
[403] Fix | Delete
if ( 0 !== $post_id || ! empty( $stats ) ) {
[404] Fix | Delete
// If $stats isn't empty, another plugin may have already made changes to the values that we can't account for, so we don't attempt to modify it.
[405] Fix | Delete
return $stats;
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
$comment_counts = array();
[409] Fix | Delete
[410] Fix | Delete
// WordPress is inconsistent in the names it uses for approved/unapproved comment statuses, so we need to remap the names.
[411] Fix | Delete
$stat_key_to_comment_query_status_mapping = array(
[412] Fix | Delete
'approved' => 'approve',
[413] Fix | Delete
'moderated' => 'hold',
[414] Fix | Delete
'spam' => 'spam',
[415] Fix | Delete
'trash' => 'trash',
[416] Fix | Delete
'post-trashed' => 'post-trashed',
[417] Fix | Delete
);
[418] Fix | Delete
[419] Fix | Delete
$comment_query_status_to_comment_status_mapping = array(
[420] Fix | Delete
'approve' => 'approved',
[421] Fix | Delete
'hold' => 'unapproved',
[422] Fix | Delete
'spam' => 'spam',
[423] Fix | Delete
'trash' => 'trash',
[424] Fix | Delete
'post-trashed' => 'post-trashed',
[425] Fix | Delete
);
[426] Fix | Delete
[427] Fix | Delete
$args = array(
[428] Fix | Delete
'count' => true,
[429] Fix | Delete
'update_comment_meta_cache' => false,
[430] Fix | Delete
'orderby' => 'none',
[431] Fix | Delete
);
[432] Fix | Delete
[433] Fix | Delete
foreach ( $stat_key_to_comment_query_status_mapping as $stat_key => $query_status ) {
[434] Fix | Delete
// For simplicity, the cache key is by the comment status returned by wp_get_comment_status() and used by wp_transition_comment_status().
[435] Fix | Delete
$cache_key = 'wc_count_comments_' . $comment_query_status_to_comment_status_mapping[ $query_status ];
[436] Fix | Delete
$count = wp_cache_get( $cache_key, self::COMMENT_COUNT_CACHE_GROUP );
[437] Fix | Delete
if ( false === $count ) {
[438] Fix | Delete
$count = (int) get_comments( array_merge( $args, array( 'status' => $query_status ) ) );
[439] Fix | Delete
wp_cache_set( $cache_key, $count, self::COMMENT_COUNT_CACHE_GROUP, 3 * DAY_IN_SECONDS );
[440] Fix | Delete
}
[441] Fix | Delete
$comment_counts[ $stat_key ] = (int) $count;
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
$comment_counts['all'] = $comment_counts['approved'] + $comment_counts['moderated'];
[445] Fix | Delete
$comment_counts['total_comments'] = $comment_counts['all'] + $comment_counts['spam'];
[446] Fix | Delete
[447] Fix | Delete
return (object) $comment_counts;
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
/**
[451] Fix | Delete
* Make sure WP displays avatars for comments with the `review` type.
[452] Fix | Delete
*
[453] Fix | Delete
* @since 2.3
[454] Fix | Delete
* @param array $comment_types Comment types.
[455] Fix | Delete
* @return array
[456] Fix | Delete
*/
[457] Fix | Delete
public static function add_avatar_for_review_comment_type( $comment_types ) {
[458] Fix | Delete
return array_merge( $comment_types, array( 'review' ) );
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
/**
[462] Fix | Delete
* Add Product Reviews filter for `review` comment type.
[463] Fix | Delete
*
[464] Fix | Delete
* @since 6.0.0
[465] Fix | Delete
*
[466] Fix | Delete
* @param array $comment_types Array of comment type labels keyed by their name.
[467] Fix | Delete
*
[468] Fix | Delete
* @return array
[469] Fix | Delete
*/
[470] Fix | Delete
public static function add_review_comment_filter( array $comment_types ): array {
[471] Fix | Delete
$comment_types['review'] = __( 'Product Reviews', 'woocommerce' );
[472] Fix | Delete
return $comment_types;
[473] Fix | Delete
}
[474] Fix | Delete
[475] Fix | Delete
/**
[476] Fix | Delete
* Determine if a review is from a verified owner at submission.
[477] Fix | Delete
*
[478] Fix | Delete
* @param int $comment_id Comment ID.
[479] Fix | Delete
* @return bool
[480] Fix | Delete
*/
[481] Fix | Delete
public static function add_comment_purchase_verification( $comment_id ) {
[482] Fix | Delete
$comment = get_comment( $comment_id );
[483] Fix | Delete
$verified = false;
[484] Fix | Delete
if ( 'product' === get_post_type( $comment->comment_post_ID ) ) {
[485] Fix | Delete
// When possible, narrow down wc_customer_bought_product inputs for better performance.
[486] Fix | Delete
$email = $comment->user_id ? '' : $comment->comment_author_email;
[487] Fix | Delete
$verified = wc_customer_bought_product( $email, $comment->user_id, $comment->comment_post_ID );
[488] Fix | Delete
add_comment_meta( $comment_id, 'verified', (int) $verified, true );
[489] Fix | Delete
}
[490] Fix | Delete
return $verified;
[491] Fix | Delete
}
[492] Fix | Delete
[493] Fix | Delete
/**
[494] Fix | Delete
* Get product rating for a product. Please note this is not cached.
[495] Fix | Delete
*
[496] Fix | Delete
* @since 3.0.0
[497] Fix | Delete
* @param WC_Product $product Product instance.
[498] Fix | Delete
* @return float
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function