From f9731bb2ed7b9b8cc620d043480d2415ab294045 Mon Sep 17 00:00:00 2001 From: Jonathan Sadowski Date: Mon, 8 Jun 2020 17:36:34 -0500 Subject: [PATCH] Update saving of comment_type for product reviews to account for WP 5.5 default comment_type --- includes/class-wc-comments.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-comments.php b/includes/class-wc-comments.php index d39bb7b0a85..ab4e6945f01 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'] && wc_review_ratings_enabled() && wc_review_ratings_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'] ) && self::is_default_comment_type( $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; } @@ -406,12 +406,26 @@ class WC_Comments { * @return array */ public static function update_comment_type( $comment_data ) { - if ( ! is_admin() && isset( $_POST['comment_post_ID'], $comment_data['comment_type'] ) && '' === $comment_data['comment_type'] && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok. + if ( ! is_admin() && isset( $_POST['comment_post_ID'], $comment_data['comment_type'] ) && self::is_default_comment_type( $comment_data['comment_type'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok. $comment_data['comment_type'] = 'review'; } return $comment_data; } + + /** + * Determines if a comment is of the default type. + * + * Prior to WordPress 5.5, '' was the default comment type. + * As of 5.5, the default type is 'comment'. + * + * @since 4.3.0 + * @param string $comment_type Comment type. + * @return bool + */ + private static function is_default_comment_type( $comment_type ) { + return ( '' === $comment_type || 'comment' === $comment_type ); + } } WC_Comments::init();