Merge pull request #9318 from woothemes/issue/8458-refactor-verified-purchase-review-meta

Refactor "verified owner" logic to reduce queries and transients.
This commit is contained in:
Mike Jolley 2015-10-22 09:00:38 -06:00
commit d46e57d9d6
7 changed files with 56 additions and 22 deletions

View File

@ -463,7 +463,7 @@ class WC_API_Products extends WC_API_Resource {
'rating' => get_comment_meta( $comment->comment_ID, 'rating', true ),
'reviewer_name' => $comment->comment_author,
'reviewer_email' => $comment->comment_author_email,
'verified' => (bool) wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $id ),
'verified' => wc_review_is_from_verified_owner( $comment->comment_ID ),
);
}

View File

@ -216,7 +216,7 @@ class WC_API_Products extends WC_API_Resource {
'rating' => get_comment_meta( $comment->comment_ID, 'rating', true ),
'reviewer_name' => $comment->comment_author,
'reviewer_email' => $comment->comment_author_email,
'verified' => (bool) wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $id ),
'verified' => wc_review_is_from_verified_owner( $comment->comment_ID ),
);
}

View File

@ -431,7 +431,7 @@ class WC_API_Products extends WC_API_Resource {
'rating' => get_comment_meta( $comment->comment_ID, 'rating', true ),
'reviewer_name' => $comment->comment_author,
'reviewer_email' => $comment->comment_author_email,
'verified' => (bool) wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $id ),
'verified' => wc_review_is_from_verified_owner( $comment->comment_ID ),
);
}

View File

@ -44,6 +44,9 @@ class WC_Comments {
// Support avatars for `review` comment type
add_filter( 'get_avatar_comment_types', array( __CLASS__, 'add_avatar_for_review_comment_type' ) );
// Review of verified purchase
add_action( 'comment_post', array( __CLASS__, 'add_comment_purchase_verification' ) );
}
/**
@ -289,6 +292,21 @@ class WC_Comments {
public static function add_avatar_for_review_comment_type( $comment_types ) {
return array_merge( $comment_types, array( 'review' ) );
}
/**
* Determine if a review is from a verified owner at submission.
* @param int $comment_id
* @return bool
*/
public static function add_comment_purchase_verification( $comment_id ) {
$comment = get_comment( $comment_id );
$verified = false;
if ( 'product' === get_post_type( $comment->comment_post_ID ) ) {
$verified = wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $comment->comment_post_ID );
add_comment_meta( $comment_id, 'verified', (int) $verified, true );
}
return $verified;
}
}
WC_Comments::init();

View File

@ -539,7 +539,7 @@ class WC_CLI_Product extends WC_CLI_Command {
'rating' => get_comment_meta( $comment->comment_ID, 'rating', true ),
'reviewer_name' => $comment->comment_author,
'reviewer_email' => $comment->comment_author_email,
'verified' => (bool) wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $id ),
'verified' => (bool) get_comment_meta( $comment->comment_ID, 'verified', true ),
);
}

View File

@ -218,7 +218,7 @@ add_action( 'woocommerce_order_status_completed', 'wc_paying_customer' );
function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
global $wpdb;
$transient_name = 'wc_cbp_' . md5( $customer_email . $user_id . $product_id . WC_Cache_Helper::get_transient_version( 'orders' ) );
$transient_name = 'wc_cbp_' . md5( $customer_email . $user_id . WC_Cache_Helper::get_transient_version( 'orders' ) );
if ( false === ( $result = get_transient( $transient_name ) ) ) {
$customer_data = array( $user_id );
@ -241,23 +241,22 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
return false;
}
$result = $wpdb->get_var(
$wpdb->prepare( "
SELECT COUNT( i.order_item_id ) FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
WHERE p.post_status IN ( 'wc-completed', 'wc-processing' )
AND pm.meta_key IN ( '_billing_email', '_customer_user' )
AND im.meta_key IN ( '_product_id', '_variation_id' )
AND im.meta_value = %d
", $product_id
) . " AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )"
);
$result = $wpdb->get_col( "
SELECT im.meta_value FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
WHERE p.post_status IN ( 'wc-completed', 'wc-processing' )
AND pm.meta_key IN ( '_billing_email', '_customer_user' )
AND im.meta_key IN ( '_product_id', '_variation_id' )
AND im.meta_value != 0
AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
" );
$result = array_map( 'intval', $result );
set_transient( $transient_name, $result ? 1 : 0, DAY_IN_SECONDS * 30 );
set_transient( $transient_name, $result, DAY_IN_SECONDS * 30 );
}
return (bool) $result;
return in_array( (int) $product_id, $result );
}
/**
@ -549,3 +548,19 @@ function wc_reset_order_customer_id_on_deleted_user( $user_id ) {
}
add_action( 'deleted_user', 'wc_reset_order_customer_id_on_deleted_user' );
/**
* Get review verification status.
* @param int $comment_id
* @return bool
*/
function wc_review_is_from_verified_owner( $comment_id ) {
$verified = get_comment_meta( $comment_id, 'verified', true );
// If no "verified" meta is present, generate it (if this is a product review).
if ( '' === $verified ) {
$verified = WC_Comments::add_comment_purchase_verification( $comment_id );
}
return (bool) $verified;
}

View File

@ -21,7 +21,8 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$verified = wc_review_is_from_verified_owner( $comment->comment_ID );
?>
<li itemprop="review" itemscope itemtype="http://schema.org/Review" <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
@ -50,7 +51,7 @@ $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
<strong itemprop="author"><?php comment_author(); ?></strong> <?php
if ( get_option( 'woocommerce_review_rating_verification_label' ) === 'yes' )
if ( wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $comment->comment_post_ID ) )
if ( $verified )
echo '<em class="verified">(' . __( 'verified owner', 'woocommerce' ) . ')</em> ';
?>&ndash; <time itemprop="datePublished" datetime="<?php echo get_comment_date( 'c' ); ?>"><?php echo get_comment_date( wc_date_format() ); ?></time>: