From 56940d6bf1eb57807ce59d1b63cb0a176d47e678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Tue, 16 Jul 2019 12:07:34 +0200 Subject: [PATCH] Hide ratings in Reviews by Product if disabled in settings --- assets/js/blocks/reviews-by-product/block.js | 128 ++++++++++++------- assets/js/blocks/reviews-by-product/edit.js | 22 ++-- assets/js/blocks/reviews-by-product/utils.js | 7 +- src/Assets.php | 29 +++-- src/RestApi/Controllers/ProductReviews.php | 3 +- 5 files changed, 113 insertions(+), 76 deletions(-) diff --git a/assets/js/blocks/reviews-by-product/block.js b/assets/js/blocks/reviews-by-product/block.js index 16c5f346280..9159cc4a71e 100644 --- a/assets/js/blocks/reviews-by-product/block.js +++ b/assets/js/blocks/reviews-by-product/block.js @@ -130,21 +130,13 @@ class ReviewsByProduct extends Component { this.getReviews( null, page ); } - render() { + renderOrderBySelect() { + if ( wc_product_block_data.enableReviewRating ) { + return null; + } + const { attributes, instanceId, isPreview } = this.props; - const { orderby, reviews, totalReviews } = this.state; - const { className, showProductRating, showReviewDate, showReviewerName } = attributes; - const showAvatar = wc_product_block_data.showAvatars && attributes.showAvatar; - const classes = classNames( 'wc-block-reviews-by-product', className, { - 'has-avatar': showAvatar, - 'has-date': showReviewDate, - 'has-name': showReviewerName, - 'has-rating': showProductRating, - } ); - const attrs = { - ...attributes, - showAvatar, - }; + const { orderby } = this.state; const selectId = `wc-block-reviews-by-product__orderby__select-${ instanceId }`; const selectProps = isPreview ? { @@ -155,41 +147,83 @@ class ReviewsByProduct extends Component { onBlur: this.onChangeOrderby, }; + return ( +

+ + +

+ ); + } + + renderReviewsList( showAvatar, showProductRating ) { + const { attributes } = this.props; + const { reviews } = this.state; + const attrs = { + ...attributes, + showAvatar, + showProductRating, + }; + + return ( + + ); + } + + renderLoadMoreButton() { + const { isPreview } = this.props; + const { reviews, totalReviews } = this.state; + + if ( totalReviews <= reviews.length ) { + return null; + } + + return ( + + ); + } + + render() { + const { attributes } = this.props; + const { className, showReviewDate, showReviewerName } = attributes; + const showAvatar = wc_product_block_data.showAvatars && attributes.showAvatar; + const showProductRating = wc_product_block_data.enableReviewRating && attributes.showProductRating; + const classes = classNames( 'wc-block-reviews-by-product', className, { + 'has-avatar': showAvatar, + 'has-date': showReviewDate, + 'has-name': showReviewerName, + 'has-rating': showProductRating, + } ); + return (
-

- - -

- - { totalReviews > reviews.length && ( - - ) } + { this.renderOrderBySelect() } + { this.renderReviewsList( showAvatar, showProductRating ) } + { this.renderLoadMoreButton() }
); } diff --git a/assets/js/blocks/reviews-by-product/edit.js b/assets/js/blocks/reviews-by-product/edit.js index 34d005d3b50..08fb00b3afe 100644 --- a/assets/js/blocks/reviews-by-product/edit.js +++ b/assets/js/blocks/reviews-by-product/edit.js @@ -99,16 +99,18 @@ class ReviewsByProductEditor extends Component { /> - setAttributes( { showProductRating: ! attributes.showProductRating } ) } - /> + { wc_product_block_data.enableReviewRating && ( + setAttributes( { showProductRating: ! attributes.showProductRating } ) } + /> + ) } 0; + const showProductRating = Number.isFinite( rating ) && showProductRatingAttr; const classes = classNames( 'wc-block-reviews-by-product__item', { 'has-avatar': showAvatar, @@ -57,9 +58,7 @@ export function renderReview( attributes, review = {} ) { { showProductRating && (
- { Number.isFinite( rating ) && ( - { sprintf( __( 'Rated %d out of 5', 'woo-gutenberg-products-block' ), rating ) } - ) } + { sprintf( __( 'Rated %d out of 5', 'woo-gutenberg-products-block' ), rating ) }
) } diff --git a/src/Assets.php b/src/Assets.php index 374990acdd7..be051f19495 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -127,20 +127,21 @@ public static function print_script_block_data() { // Global settings used in each block. $block_settings = array( - 'min_columns' => wc_get_theme_support( 'product_blocks::min_columns', 1 ), - 'max_columns' => wc_get_theme_support( 'product_blocks::max_columns', 6 ), - 'default_columns' => wc_get_theme_support( 'product_blocks::default_columns', 3 ), - 'min_rows' => wc_get_theme_support( 'product_blocks::min_rows', 1 ), - 'max_rows' => wc_get_theme_support( 'product_blocks::max_rows', 6 ), - 'default_rows' => wc_get_theme_support( 'product_blocks::default_rows', 1 ), - 'thumbnail_size' => wc_get_theme_support( 'thumbnail_image_width', 300 ), - 'placeholderImgSrc' => wc_placeholder_img_src(), - 'min_height' => wc_get_theme_support( 'featured_block::min_height', 500 ), - 'default_height' => wc_get_theme_support( 'featured_block::default_height', 500 ), - 'isLargeCatalog' => $product_counts->publish > 200, - 'productCategories' => $product_categories, - 'homeUrl' => esc_js( home_url( '/' ) ), - 'showAvatars' => '1' === get_option( 'show_avatars' ), + 'min_columns' => wc_get_theme_support( 'product_blocks::min_columns', 1 ), + 'max_columns' => wc_get_theme_support( 'product_blocks::max_columns', 6 ), + 'default_columns' => wc_get_theme_support( 'product_blocks::default_columns', 3 ), + 'min_rows' => wc_get_theme_support( 'product_blocks::min_rows', 1 ), + 'max_rows' => wc_get_theme_support( 'product_blocks::max_rows', 6 ), + 'default_rows' => wc_get_theme_support( 'product_blocks::default_rows', 1 ), + 'thumbnail_size' => wc_get_theme_support( 'thumbnail_image_width', 300 ), + 'placeholderImgSrc' => wc_placeholder_img_src(), + 'min_height' => wc_get_theme_support( 'featured_block::min_height', 500 ), + 'default_height' => wc_get_theme_support( 'featured_block::default_height', 500 ), + 'isLargeCatalog' => $product_counts->publish > 200, + 'productCategories' => $product_categories, + 'homeUrl' => esc_js( home_url( '/' ) ), + 'showAvatars' => '1' === get_option( 'show_avatars' ), + 'enableReviewRating' => 'yes' === get_option( 'woocommerce_enable_review_rating' ), ); ?>