Merge pull request #22476 from woocommerce/fix/22470

Hide ratings when reviews are disabled
This commit is contained in:
Mike Jolley 2019-01-22 13:32:22 +00:00 committed by GitHub
commit 084dab4553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 65 additions and 26 deletions

View File

@ -225,7 +225,7 @@ abstract class WC_Widget extends WP_Widget {
case 'text':
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; ?></label><?php // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label>
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" />
</p>
<?php

View File

@ -146,7 +146,7 @@ class WC_Comments {
*/
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( 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.
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'] && wc_review_ratings_enabled() && wc_review_ratings_required() ) { // WPCS: input var ok, CSRF ok.
wp_die( esc_html__( 'Please rate the product.', 'woocommerce' ) );
exit;
}
@ -240,7 +240,8 @@ class WC_Comments {
FROM {$wpdb->comments}
WHERE comment_type NOT IN ('order_note', 'webhook_delivery')
GROUP BY comment_approved
", ARRAY_A
",
ARRAY_A
);
$approved = array(
@ -327,7 +328,8 @@ class WC_Comments {
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, '.', '' );
@ -360,7 +362,8 @@ class WC_Comments {
WHERE comment_parent = 0
AND comment_post_ID = %d
AND comment_approved = '1'
", $product->get_id()
",
$product->get_id()
)
);
@ -393,7 +396,8 @@ class WC_Comments {
AND comment_approved = '1'
AND meta_value > 0
GROUP BY meta_value
", $product->get_id()
",
$product->get_id()
)
);

View File

@ -137,7 +137,8 @@ class WC_Customer extends WC_Legacy_Customer {
public function delete_and_reassign( $reassign = null ) {
if ( $this->data_store ) {
$this->data_store->delete(
$this, array(
$this,
array(
'force_delete' => true,
'reassign' => $reassign,
)

View File

@ -472,7 +472,7 @@ class WC_Frontend_Scripts {
case 'wc-single-product':
$params = array(
'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
'review_rating_required' => wc_review_ratings_required() ? 'yes' : 'no',
'flexslider' => apply_filters(
'woocommerce_single_product_carousel_options',
array(

View File

@ -261,7 +261,7 @@ class WC_Structured_Data {
$markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
}
if ( $product->get_review_count() && 'yes' === get_option( 'woocommerce_enable_review_rating' ) ) {
if ( $product->get_review_count() && wc_review_ratings_enabled() ) {
$markup['aggregateRating'] = array(
'@type' => 'AggregateRating',
'ratingValue' => $product->get_average_rating(),

View File

@ -418,3 +418,33 @@ function wc_post_content_has_shortcode( $tag = '' ) {
return is_singular() && is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
}
/**
* Check if reviews are enabled.
*
* @since 3.6.0
* @return bool
*/
function wc_reviews_enabled() {
return 'yes' === get_option( 'woocommerce_enable_reviews' );
}
/**
* Check if reviews ratings are enabled.
*
* @since 3.6.0
* @return bool
*/
function wc_review_ratings_enabled() {
return wc_reviews_enabled() && 'yes' === get_option( 'woocommerce_enable_review_rating' );
}
/**
* Check if review ratings are required.
*
* @since 3.6.0
* @return bool
*/
function wc_review_ratings_required() {
return 'yes' === get_option( 'woocommerce_review_rating_required' );
}

View File

@ -1324,7 +1324,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
unset( $catalog_orderby_options['menu_order'] );
}
if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) {
if ( ! wc_review_ratings_enabled() ) {
unset( $catalog_orderby_options['rating'] );
}

View File

@ -10,9 +10,9 @@
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.0.0
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -21,8 +21,8 @@ if ( ! defined( 'ABSPATH' ) ) {
global $product;
if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
if ( ! wc_review_ratings_enabled() ) {
return;
}
echo wc_get_rating_html( $product->get_average_rating() );
echo wc_get_rating_html( $product->get_average_rating() ); // WordPress.XSS.EscapeOutput.OutputNotEscaped.

View File

@ -31,7 +31,7 @@ if ( ! comments_open() ) {
<h2 class="woocommerce-Reviews-title">
<?php
$count = $product->get_review_count();
if ( $count && get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
if ( $count && wc_review_ratings_enabled() ) {
/* translators: 1: reviews count 2: product name */
$reviews_title = sprintf( esc_html( _n( '%1$s review for %2$s', '%1$s reviews for %2$s', $count, 'woocommerce' ) ), esc_html( $count ), '<span>' . get_the_title() . '</span>' );
echo apply_filters( 'woocommerce_reviews_title', $reviews_title, $count, $product ); // WPCS: XSS ok.
@ -98,7 +98,7 @@ if ( ! comments_open() ) {
$comment_form['must_log_in'] = '<p class="must-log-in">' . sprintf( esc_html__( 'You must be %slogged in%S to post a review.', 'woocommerce' ), '<a href="' . esc_url( $account_page_url ) . '">', '</a>' ) . '</p>';
}
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
if ( wc_review_ratings_enabled() ) {
$comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating">' . esc_html__( 'Your rating', 'woocommerce' ) . '</label><select name="rating" id="rating" required>
<option value="">' . esc_html__( 'Rate&hellip;', 'woocommerce' ) . '</option>
<option value="5">' . esc_html__( 'Perfect', 'woocommerce' ) . '</option>

View File

@ -12,16 +12,16 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.1.0
* @version 3.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit; // Exit if accessed directly.
}
global $product;
if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) {
if ( ! wc_review_ratings_enabled() ) {
return;
}
@ -32,8 +32,12 @@ $average = $product->get_average_rating();
if ( $rating_count > 0 ) : ?>
<div class="woocommerce-product-rating">
<?php echo wc_get_rating_html( $average, $rating_count ); ?>
<?php if ( comments_open() ) : ?><a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a><?php endif ?>
<?php echo wc_get_rating_html( $average, $rating_count ); // WPCS: XSS ok. ?>
<?php if ( comments_open() ) : ?>
<?php //phpcs:disable ?>
<a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a>
<?php // phpcs:enable ?>
<?php endif ?>
</div>
<?php endif; ?>

View File

@ -12,16 +12,16 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.1.0
* @version 3.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit; // Exit if accessed directly.
}
global $comment;
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
if ( $rating && 'yes' === get_option( 'woocommerce_enable_review_rating' ) ) {
echo wc_get_rating_html( $rating );
if ( $rating && wc_review_ratings_enabled() ) {
echo wc_get_rating_html( $rating ); // WPCS: XSS ok.
}