Commit c4080896950 for woocommerce
commit c4080896950633afa47dae39bda3c1d74e0aedb9
Author: James Kemp <me@jckemp.com>
Date: Thu Nov 19 17:25:32 2026 +0000
Fix product reviews screen error on out-of-range ratings (#70701)
* Fix product reviews screen error on out-of-range ratings
* Add changefile(s) from automation for the following project(s): woocommerce
* Fix failing CI: Lint - @woocommerce/plugin-woocommerce, Evaluate Project Job Statuses
* Address review feedback
* In `class-wc-rest-product-reviews-controller.php`, replace the…
* Validate review ratings against the REST schema in create and update
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/70701-fix-70620-wordpress-dashboard-products-reviews b/plugins/woocommerce/changelog/70701-fix-70620-wordpress-dashboard-products-reviews
new file mode 100644
index 00000000000..a6dc128a46e
--- /dev/null
+++ b/plugins/woocommerce/changelog/70701-fix-70620-wordpress-dashboard-products-reviews
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix a critical error on the Products > Reviews screen when a review has a rating outside 0 to 5, and stop the REST API from accepting such ratings.
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php
index c3f491cc2eb..72f2e017520 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller.php
@@ -369,6 +369,34 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
return $response;
}
+ /**
+ * Validate and normalize the request rating against the item schema.
+ *
+ * @param WP_REST_Request<array<string, mixed>> $request Full details about the request.
+ * @return WP_Error|null Validation error, or null if the rating is absent or valid.
+ */
+ private function validate_and_sanitize_rating( $request ) {
+ if ( ! isset( $request['rating'] ) ) {
+ return null;
+ }
+
+ $schema = $this->get_item_schema();
+ if ( empty( $schema['properties']['rating'] ) ) {
+ return null;
+ }
+
+ $rating_schema = $schema['properties']['rating'];
+ $valid = rest_validate_value_from_schema( $request['rating'], $rating_schema, 'rating' );
+ if ( is_wp_error( $valid ) ) {
+ $valid->add_data( array( 'status' => 400 ) );
+ return $valid;
+ }
+
+ $request['rating'] = rest_sanitize_value_from_schema( $request['rating'], $rating_schema, 'rating' );
+
+ return null;
+ }
+
/**
* Create a single review.
*
@@ -386,6 +414,12 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
+ // Batch requests call this method directly, so validate the rating against the schema here.
+ $rating_error = $this->validate_and_sanitize_rating( $request );
+ if ( $rating_error ) {
+ return $rating_error;
+ }
+
$prepared_review = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_review ) ) {
return $prepared_review;
@@ -556,6 +590,12 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
return new WP_Error( 'woocommerce_rest_review_invalid_type', __( 'Sorry, you are not allowed to change the comment type.', 'woocommerce' ), array( 'status' => 404 ) );
}
+ // Batch requests call this method directly, so validate the rating against the schema here.
+ $rating_error = $this->validate_and_sanitize_rating( $request );
+ if ( $rating_error ) {
+ return $rating_error;
+ }
+
$prepared_args = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_args ) ) {
return $prepared_args;
@@ -955,6 +995,8 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
'rating' => array(
'description' => __( 'Review rating (0 to 5).', 'woocommerce' ),
'type' => 'integer',
+ 'minimum' => 0,
+ 'maximum' => 5,
'context' => array( 'view', 'edit' ),
),
'verified' => array(
diff --git a/plugins/woocommerce/src/Internal/Admin/ProductReviews/ReviewsListTable.php b/plugins/woocommerce/src/Internal/Admin/ProductReviews/ReviewsListTable.php
index dcda375f788..e4b5885c961 100644
--- a/plugins/woocommerce/src/Internal/Admin/ProductReviews/ReviewsListTable.php
+++ b/plugins/woocommerce/src/Internal/Admin/ProductReviews/ReviewsListTable.php
@@ -1226,7 +1226,7 @@ class ReviewsListTable extends WP_List_Table {
ob_start();
if ( ! empty( $rating ) && is_numeric( $rating ) ) {
- $rating = (int) $rating;
+ $rating = max( 0, min( 5, (int) $rating ) );
$accessibility_label = sprintf(
/* translators: 1: number representing a rating */
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller-tests.php
index 2695cdc3b4b..509e04e541d 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-product-reviews-controller-tests.php
@@ -666,6 +666,143 @@ class WC_REST_Product_Reviews_Controller_Tests extends WC_REST_Unit_Test_Case {
$this->assertSame( '1', get_comment_meta( $response->get_data()['id'], 'verified', true ), 'The verified meta must be populated eagerly at creation time.' );
}
+ /**
+ * Provides ratings the API must reject.
+ *
+ * @return array<string, array{mixed}> Invalid ratings.
+ */
+ public function data_provider_invalid_ratings(): array {
+ return array(
+ 'above maximum' => array( 6 ),
+ 'negative' => array( -1 ),
+ 'fractional' => array( 4.5 ),
+ 'boolean' => array( true ),
+ );
+ }
+
+ /**
+ * @testdox Creating a review with an invalid rating is rejected.
+ * @dataProvider data_provider_invalid_ratings
+ *
+ * @param mixed $rating Invalid rating.
+ */
+ public function test_create_item_rejects_invalid_rating( $rating ): void {
+ wp_set_current_user( $this->shop_manager_id );
+ $product_id = ProductHelper::create_simple_product()->get_id();
+
+ $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
+ $request->set_body_params(
+ array(
+ 'product_id' => $product_id,
+ 'review' => 'Invalid rating.',
+ 'reviewer' => 'Jane Smith',
+ 'reviewer_email' => 'jane.smith@example.org',
+ 'rating' => $rating,
+ )
+ );
+
+ $response = $this->sut->create_item( $request );
+
+ $this->assertWPError( $response );
+ $this->assertSame( 400, $response->get_error_data()['status'] );
+ $this->assertSame( 0, wc_get_product( $product_id )->get_review_count() );
+ }
+
+ /**
+ * @testdox Updating a review with an invalid rating is rejected.
+ * @dataProvider data_provider_invalid_ratings
+ *
+ * @param mixed $rating Invalid rating.
+ */
+ public function test_update_item_rejects_invalid_rating( $rating ): void {
+ wp_set_current_user( $this->shop_manager_id );
+ $product_id = ProductHelper::create_simple_product()->get_id();
+ $review_id = $this->create_review( $product_id, 'Still five stars.', 5 )->get_data()['id'];
+
+ $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $review_id );
+ $request->set_param( 'id', $review_id );
+ $request->set_param( 'rating', $rating );
+
+ $response = $this->sut->update_item( $request );
+
+ $this->assertWPError( $response );
+ $this->assertSame( 400, $response->get_error_data()['status'] );
+ $this->assertSame( 5, (int) get_comment_meta( $review_id, 'rating', true ) );
+ $this->assertEquals( 5, wc_get_product( $product_id )->get_average_rating() );
+ }
+
+ /**
+ * @testdox Batch creating and updating reviews with an invalid rating returns a 400 error per item.
+ * @dataProvider data_provider_invalid_ratings
+ *
+ * @param mixed $rating Invalid rating.
+ */
+ public function test_batch_rejects_invalid_rating( $rating ): void {
+ wp_set_current_user( $this->shop_manager_id );
+ $product_id = ProductHelper::create_simple_product()->get_id();
+ $review_id = $this->create_review( $product_id, 'Still five stars.', 5 )->get_data()['id'];
+
+ $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' );
+ $request->set_body_params(
+ array(
+ 'create' => array(
+ array(
+ 'product_id' => $product_id,
+ 'review' => 'Invalid rating.',
+ 'reviewer' => 'Jane Smith',
+ 'reviewer_email' => 'jane.smith@example.org',
+ 'rating' => $rating,
+ ),
+ ),
+ 'update' => array(
+ array(
+ 'id' => $review_id,
+ 'rating' => $rating,
+ ),
+ ),
+ )
+ );
+
+ $data = $this->sut->batch_items( $request );
+
+ foreach ( array( 'create', 'update' ) as $operation ) {
+ $this->assertSame( 400, $data[ $operation ][0]['error']['data']['status'] );
+ }
+ $this->assertSame( 5, (int) get_comment_meta( $review_id, 'rating', true ) );
+ $this->assertEquals( 1, wc_get_product( $product_id )->get_review_count() );
+ }
+
+ /**
+ * @testdox A numeric string rating is normalized to an integer for single and batch creation.
+ */
+ public function test_numeric_string_rating_is_normalized_for_single_and_batch(): void {
+ wp_set_current_user( $this->shop_manager_id );
+ $product_id = ProductHelper::create_simple_product()->get_id();
+ $body = array(
+ 'product_id' => $product_id,
+ 'review' => 'Four stars.',
+ 'reviewer' => 'Jane Smith',
+ 'reviewer_email' => 'jane.smith@example.org',
+ 'rating' => '04',
+ );
+
+ $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
+ $request->set_body_params( $body );
+ $single = $this->sut->create_item( $request );
+
+ $this->assertNotWPError( $single );
+ $this->assertSame( '4', get_comment_meta( $single->get_data()['id'], 'rating', true ) );
+
+ $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' );
+ // Use different content so WordPress does not reject the review as a duplicate of the first one.
+ $body['review'] = 'Four stars, in a batch.';
+ $request->set_body_params( array( 'create' => array( $body ) ) );
+ $data = $this->sut->batch_items( $request );
+
+ $this->assertArrayNotHasKey( 'error', $data['create'][0] );
+ $this->assertSame( '4', get_comment_meta( $data['create'][0]['id'], 'rating', true ) );
+ }
+
/**
* Creates two products and a review that can be moved between them.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/ProductReviews/ReviewsListTableTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/ProductReviews/ReviewsListTableTest.php
index 776b4734022..244ac4f975b 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/ProductReviews/ReviewsListTableTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/ProductReviews/ReviewsListTableTest.php
@@ -514,9 +514,33 @@ class ReviewsListTableTest extends WC_Unit_Test_Case {
'4 stars' => array( '4', '<span aria-label="4 out of 5">★★★★☆</span>' ),
'5 stars' => array( '5', '<span aria-label="5 out of 5">★★★★★</span>' ),
'2.5 stars (rounds down)' => array( '2.5', '<span aria-label="2 out of 5">★★☆☆☆</span>' ),
+ 'negative rating' => array( '-1', '<span aria-label="0 out of 5">☆☆☆☆☆</span>' ),
);
}
+ /**
+ * @testdox `column_rating` clamps a stored rating above 5 to 5 stars instead of failing.
+ *
+ * @covers \Automattic\WooCommerce\Internal\Admin\ProductReviews\ReviewsListTable::column_rating()
+ *
+ * @return void
+ * @throws ReflectionException If the method does not exist.
+ */
+ public function test_column_rating_clamps_rating_above_maximum(): void {
+ $list_table = $this->get_reviews_list_table();
+ $method = ( new ReflectionClass( $list_table ) )->getMethod( 'column_rating' );
+ $method->setAccessible( true );
+
+ $review = $this->factory()->comment->create_and_get();
+ update_comment_meta( $review->comment_ID, 'rating', '6' );
+
+ ob_start();
+ $method->invokeArgs( $list_table, array( $review ) );
+ $output = trim( ob_get_clean() );
+
+ $this->assertSame( '<span aria-label="5 out of 5">★★★★★</span>', $output );
+ }
+
/**
* @testdox `column_author` outputs the HTML column content displaying the review author in the product reviews list table.
*