From f3d9eeeef3020775cdc15e50986ad61acf5c7dec Mon Sep 17 00:00:00 2001 From: Gerhard Date: Thu, 17 Jan 2019 08:04:57 +0200 Subject: [PATCH] Introduce wc_reviews_enabled, wc_review_ratings_enabled and wc_review_ratings_required as helper functions and update all places where options are accessed directly. --- includes/class-wc-comments.php | 2 +- includes/class-wc-frontend-scripts.php | 2 +- includes/class-wc-structured-data.php | 2 +- includes/wc-conditional-functions.php | 30 ++++++++++++++++++++++ includes/wc-template-functions.php | 2 +- templates/loop/rating.php | 2 +- templates/single-product-reviews.php | 6 ++--- templates/single-product/rating.php | 8 +++--- templates/single-product/review-rating.php | 4 +-- 9 files changed, 44 insertions(+), 14 deletions(-) diff --git a/includes/class-wc-comments.php b/includes/class-wc-comments.php index 2b47e1e7ca1..6f1dcfa436a 100644 --- a/includes/class-wc-comments.php +++ b/includes/class-wc-comments.php @@ -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; } diff --git a/includes/class-wc-frontend-scripts.php b/includes/class-wc-frontend-scripts.php index 5bf920be4ae..13624c5b62d 100644 --- a/includes/class-wc-frontend-scripts.php +++ b/includes/class-wc-frontend-scripts.php @@ -471,7 +471,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( 'rtl' => is_rtl(), diff --git a/includes/class-wc-structured-data.php b/includes/class-wc-structured-data.php index 4d3911c963a..ac743c68462 100644 --- a/includes/class-wc-structured-data.php +++ b/includes/class-wc-structured-data.php @@ -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(), diff --git a/includes/wc-conditional-functions.php b/includes/wc-conditional-functions.php index abd62d8b2c1..ac85ebc86de 100644 --- a/includes/wc-conditional-functions.php +++ b/includes/wc-conditional-functions.php @@ -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.5.4 + * @return bool + */ +function wc_reviews_enabled() { + return 'yes' === get_option( 'woocommerce_enable_reviews' ); +} + +/** + * Check if reviews ratings are enabled. + * + * @since 3.5.4 + * @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.5.4 + * @return bool + */ +function wc_review_ratings_required() { + return 'yes' === get_option( 'woocommerce_review_rating_required' ); +} diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index ff71246af43..dbcd778e371 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -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'] ); } diff --git a/templates/loop/rating.php b/templates/loop/rating.php index bed177bb612..9c6231ca2e3 100644 --- a/templates/loop/rating.php +++ b/templates/loop/rating.php @@ -21,7 +21,7 @@ if ( ! defined( 'ABSPATH' ) ) { global $product; -if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) || 'no' === get_option( 'woocommerce_enable_reviews' ) ) { +if ( ! wc_review_ratings_enabled() ) { return; } diff --git a/templates/single-product-reviews.php b/templates/single-product-reviews.php index bdc96a30677..863b2334765 100644 --- a/templates/single-product-reviews.php +++ b/templates/single-product-reviews.php @@ -12,7 +12,7 @@ * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce/Templates - * @version 3.6.0 + * @version 3.5.4 */ if ( ! defined( 'ABSPATH' ) ) { @@ -31,7 +31,7 @@ if ( ! comments_open() ) {

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 ), '' . get_the_title() . '' ); 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'] = ''; } - if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) { + if ( wc_review_ratings_enabled() ) { $comment_form['comment_field'] = '