Fixed includes/class-wc-comments.php PHPCS violations
This commit is contained in:
parent
73b1c9c603
commit
f81b90efe6
|
@ -1,19 +1,17 @@
|
|||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Comments
|
||||
*
|
||||
* Handle comments (reviews and order notes).
|
||||
*
|
||||
* @class WC_Comments
|
||||
* @version 2.3.0
|
||||
* @package WooCommerce/Classes/Products
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
* @package WooCommerce/Classes/Products
|
||||
* @version 2.3.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Comments class.
|
||||
*/
|
||||
class WC_Comments {
|
||||
|
||||
|
@ -21,34 +19,34 @@ class WC_Comments {
|
|||
* Hook in methods.
|
||||
*/
|
||||
public static function init() {
|
||||
// Rating posts
|
||||
// Rating posts.
|
||||
add_filter( 'comments_open', array( __CLASS__, 'comments_open' ), 10, 2 );
|
||||
add_filter( 'preprocess_comment', array( __CLASS__, 'check_comment_rating' ), 0 );
|
||||
add_action( 'comment_post', array( __CLASS__, 'add_comment_rating' ), 1 );
|
||||
add_action( 'comment_moderation_recipients', array( __CLASS__, 'comment_moderation_recipients' ), 10, 2 );
|
||||
|
||||
// Clear transients
|
||||
// Clear transients.
|
||||
add_action( 'wp_update_comment_count', array( __CLASS__, 'clear_transients' ) );
|
||||
|
||||
// Secure order notes
|
||||
// Secure order notes.
|
||||
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
|
||||
add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
|
||||
|
||||
// Secure webhook comments
|
||||
// Secure webhook comments.
|
||||
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
|
||||
add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_webhook_comments_from_feed_where' ) );
|
||||
|
||||
// Count comments
|
||||
// Count comments.
|
||||
add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 10, 2 );
|
||||
|
||||
// Delete comments count cache whenever there is a new comment or a comment status changes
|
||||
// Delete comments count cache whenever there is a new comment or a comment status changes.
|
||||
add_action( 'wp_insert_comment', array( __CLASS__, 'delete_comments_count_cache' ) );
|
||||
add_action( 'wp_set_comment_status', array( __CLASS__, 'delete_comments_count_cache' ) );
|
||||
|
||||
// Support avatars for `review` comment type
|
||||
// Support avatars for `review` comment type.
|
||||
add_filter( 'get_avatar_comment_types', array( __CLASS__, 'add_avatar_for_review_comment_type' ) );
|
||||
|
||||
// Review of verified purchase
|
||||
// Review of verified purchase.
|
||||
add_action( 'comment_post', array( __CLASS__, 'add_comment_purchase_verification' ) );
|
||||
}
|
||||
|
||||
|
@ -56,8 +54,8 @@ class WC_Comments {
|
|||
* See if comments are open.
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @param bool $open
|
||||
* @param int $post_id
|
||||
* @param bool $open Whether the current post is open for comments.
|
||||
* @param int $post_id Post ID.
|
||||
* @return bool
|
||||
*/
|
||||
public static function comments_open( $open, $post_id ) {
|
||||
|
@ -75,7 +73,8 @@ class WC_Comments {
|
|||
* shop managers can view orders anyway.
|
||||
*
|
||||
* The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
|
||||
* @param array $clauses
|
||||
*
|
||||
* @param array $clauses A compacted array of comment query clauses.
|
||||
* @return array
|
||||
*/
|
||||
public static function exclude_order_comments( $clauses ) {
|
||||
|
@ -84,7 +83,10 @@ class WC_Comments {
|
|||
}
|
||||
|
||||
/**
|
||||
* Exclude order comments from feed.
|
||||
*
|
||||
* @deprecated 3.1
|
||||
* @param mixed $join Deprecated.
|
||||
*/
|
||||
public static function exclude_order_comments_from_feed_join( $join ) {
|
||||
wc_deprecated_function( 'WC_Comments::exclude_order_comments_from_feed_join', '3.1' );
|
||||
|
@ -93,7 +95,7 @@ class WC_Comments {
|
|||
/**
|
||||
* Exclude order comments from queries and RSS.
|
||||
*
|
||||
* @param string $where
|
||||
* @param string $where The WHERE clause of the query.
|
||||
* @return string
|
||||
*/
|
||||
public static function exclude_order_comments_from_feed_where( $where ) {
|
||||
|
@ -102,8 +104,9 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Exclude webhook comments from queries and RSS.
|
||||
*
|
||||
* @since 2.2
|
||||
* @param array $clauses
|
||||
* @param array $clauses A compacted array of comment query clauses.
|
||||
* @return array
|
||||
*/
|
||||
public static function exclude_webhook_comments( $clauses ) {
|
||||
|
@ -112,7 +115,10 @@ class WC_Comments {
|
|||
}
|
||||
|
||||
/**
|
||||
* Exclude webhooks comments from feed.
|
||||
*
|
||||
* @deprecated 3.1
|
||||
* @param mixed $join Deprecated.
|
||||
*/
|
||||
public static function exclude_webhook_comments_from_feed_join( $join ) {
|
||||
wc_deprecated_function( 'WC_Comments::exclude_webhook_comments_from_feed_join', '3.1' );
|
||||
|
@ -120,8 +126,9 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Exclude webhook comments from queries and RSS.
|
||||
*
|
||||
* @since 2.1
|
||||
* @param string $where
|
||||
* @param string $where The WHERE clause of the query.
|
||||
* @return string
|
||||
*/
|
||||
public static function exclude_webhook_comments_from_feed_where( $where ) {
|
||||
|
@ -131,13 +138,13 @@ class WC_Comments {
|
|||
/**
|
||||
* Validate the comment ratings.
|
||||
*
|
||||
* @param array $comment_data
|
||||
* @param array $comment_data Comment data.
|
||||
* @return array
|
||||
*/
|
||||
public static function check_comment_rating( $comment_data ) {
|
||||
// If posting a comment (not trackback etc) and not logged in
|
||||
if ( ! is_admin() && isset( $_POST['comment_post_ID'], $_POST['rating'], $comment_data['comment_type'] ) && 'product' === get_post_type( $_POST['comment_post_ID'] ) && empty( $_POST['rating'] ) && '' === $comment_data['comment_type'] && 'yes' === get_option( 'woocommerce_enable_review_rating' ) && 'yes' === get_option( 'woocommerce_review_rating_required' ) ) {
|
||||
wp_die( __( 'Please rate the product.', 'woocommerce' ) );
|
||||
// If posting a comment (not trackback etc) and not logged in.
|
||||
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'] ) && '' === $comment_data['comment_type'] && 'yes' === get_option( 'woocommerce_enable_review_rating' ) && 'yes' === get_option( 'woocommerce_review_rating_required' ) ) { // WPCS: input var ok, CSRF ok.
|
||||
wp_die( esc_html__( 'Please rate the product.', 'woocommerce' ) );
|
||||
exit;
|
||||
}
|
||||
return $comment_data;
|
||||
|
@ -145,16 +152,17 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Rating field for comments.
|
||||
* @param int $comment_id
|
||||
*
|
||||
* @param int $comment_id Comment ID.
|
||||
*/
|
||||
public static function add_comment_rating( $comment_id ) {
|
||||
if ( isset( $_POST['rating'] ) && 'product' === get_post_type( $_POST['comment_post_ID'] ) ) {
|
||||
if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) {
|
||||
if ( isset( $_POST['rating'], $_POST['comment_post_ID'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
|
||||
if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
|
||||
return;
|
||||
}
|
||||
add_comment_meta( $comment_id, 'rating', (int) esc_attr( $_POST['rating'] ), true );
|
||||
add_comment_meta( $comment_id, 'rating', intval( $_POST['rating'] ), true ); // WPCS: input var ok, CSRF ok.
|
||||
|
||||
$post_id = isset( $_POST['comment_post_ID'] ) ? (int) $_POST['comment_post_ID'] : 0;
|
||||
$post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // WPCS: input var ok, CSRF ok.
|
||||
if ( $post_id ) {
|
||||
self::clear_transients( $post_id );
|
||||
}
|
||||
|
@ -163,8 +171,9 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Modify recipient of review email.
|
||||
* @param array $emails
|
||||
* @param int $comment_id
|
||||
*
|
||||
* @param array $emails Emails.
|
||||
* @param int $comment_id Comment ID.
|
||||
* @return array
|
||||
*/
|
||||
public static function comment_moderation_recipients( $emails, $comment_id ) {
|
||||
|
@ -179,7 +188,8 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Ensure product average rating and review count is kept up to date.
|
||||
* @param int $post_id
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
*/
|
||||
public static function clear_transients( $post_id ) {
|
||||
|
||||
|
@ -196,8 +206,6 @@ class WC_Comments {
|
|||
* new comment or the status of a comment changes. Cache
|
||||
* will be regenerated next time WC_Comments::wp_count_comments()
|
||||
* is called.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function delete_comments_count_cache() {
|
||||
delete_transient( 'wc_count_comments' );
|
||||
|
@ -223,12 +231,14 @@ class WC_Comments {
|
|||
'all' => 0,
|
||||
);
|
||||
|
||||
$count = $wpdb->get_results( "
|
||||
$count = $wpdb->get_results(
|
||||
"
|
||||
SELECT comment_approved, COUNT(*) AS num_comments
|
||||
FROM {$wpdb->comments}
|
||||
WHERE comment_type NOT IN ('order_note', 'webhook_delivery')
|
||||
GROUP BY comment_approved
|
||||
", ARRAY_A );
|
||||
", ARRAY_A
|
||||
);
|
||||
|
||||
$approved = array(
|
||||
'0' => 'moderated',
|
||||
|
@ -267,8 +277,9 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Make sure WP displays avatars for comments with the `review` type.
|
||||
*
|
||||
* @since 2.3
|
||||
* @param array $comment_types
|
||||
* @param array $comment_types Comment types.
|
||||
* @return array
|
||||
*/
|
||||
public static function add_avatar_for_review_comment_type( $comment_types ) {
|
||||
|
@ -277,7 +288,8 @@ class WC_Comments {
|
|||
|
||||
/**
|
||||
* Determine if a review is from a verified owner at submission.
|
||||
* @param int $comment_id
|
||||
*
|
||||
* @param int $comment_id Comment ID.
|
||||
* @return bool
|
||||
*/
|
||||
public static function add_comment_purchase_verification( $comment_id ) {
|
||||
|
@ -294,7 +306,7 @@ class WC_Comments {
|
|||
* Get product rating for a product. Please note this is not cached.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param WC_Product $product
|
||||
* @param WC_Product $product Product instance.
|
||||
* @return float
|
||||
*/
|
||||
public static function get_average_rating_for_product( &$product ) {
|
||||
|
@ -303,14 +315,18 @@ class WC_Comments {
|
|||
$count = $product->get_rating_count();
|
||||
|
||||
if ( $count ) {
|
||||
$ratings = $wpdb->get_var( $wpdb->prepare("
|
||||
$ratings = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"
|
||||
SELECT SUM(meta_value) FROM $wpdb->commentmeta
|
||||
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
|
||||
WHERE meta_key = 'rating'
|
||||
AND comment_post_ID = %d
|
||||
AND comment_approved = '1'
|
||||
AND meta_value > 0
|
||||
", $product->get_id() ) );
|
||||
", $product->get_id()
|
||||
)
|
||||
);
|
||||
$average = number_format( $ratings / $count, 2, '.', '' );
|
||||
} else {
|
||||
$average = 0;
|
||||
|
@ -328,18 +344,22 @@ class WC_Comments {
|
|||
* Get product review count for a product (not replies). Please note this is not cached.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param WC_Product $product
|
||||
* @param WC_Product $product Product instance.
|
||||
* @return int
|
||||
*/
|
||||
public static function get_review_count_for_product( &$product ) {
|
||||
global $wpdb;
|
||||
|
||||
$count = $wpdb->get_var( $wpdb->prepare("
|
||||
$count = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"
|
||||
SELECT COUNT(*) FROM $wpdb->comments
|
||||
WHERE comment_parent = 0
|
||||
AND comment_post_ID = %d
|
||||
AND comment_approved = '1'
|
||||
", $product->get_id() ) );
|
||||
", $product->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
$product->set_review_count( $count );
|
||||
|
||||
|
@ -353,14 +373,16 @@ class WC_Comments {
|
|||
* Get product rating count for a product. Please note this is not cached.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param WC_Product $product
|
||||
* @return array of integers
|
||||
* @param WC_Product $product Product instance.
|
||||
* @return int[]
|
||||
*/
|
||||
public static function get_rating_counts_for_product( &$product ) {
|
||||
global $wpdb;
|
||||
|
||||
$counts = array();
|
||||
$raw_counts = $wpdb->get_results( $wpdb->prepare( "
|
||||
$raw_counts = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"
|
||||
SELECT meta_value, COUNT( * ) as meta_value_count FROM $wpdb->commentmeta
|
||||
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
|
||||
WHERE meta_key = 'rating'
|
||||
|
@ -368,10 +390,12 @@ class WC_Comments {
|
|||
AND comment_approved = '1'
|
||||
AND meta_value > 0
|
||||
GROUP BY meta_value
|
||||
", $product->get_id() ) );
|
||||
", $product->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $raw_counts as $count ) {
|
||||
$counts[ $count->meta_value ] = absint( $count->meta_value_count );
|
||||
$counts[ $count->meta_value ] = absint( $count->meta_value_count ); // WPCS: slow query ok.
|
||||
}
|
||||
|
||||
$product->set_rating_counts( $counts );
|
||||
|
|
Loading…
Reference in New Issue