WC_REST_Product_Reviews_Controller::update_item │ public │ WC 1.0
Updates a review.
Method of the class: WC_REST_Product_Reviews_Controller{}
Hooks from the method
Returns
WP_Error|WP_REST_Response. Response object on success, or error object on failure.
Usage
$WC_REST_Product_Reviews_Controller = new WC_REST_Product_Reviews_Controller(); $WC_REST_Product_Reviews_Controller->update_item( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
WC_REST_Product_Reviews_Controller::update_item() WC REST Product Reviews Controller::update item code WC 11.2.1
public function update_item( $request ) {
$review = $this->get_review( $request['id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
$id = (int) $review->comment_ID;
// Captured before the update: $review still holds the pre-update comment, so this is the
// product the review currently belongs to.
$original_product_id = (int) $review->comment_post_ID;
if ( isset( $request['type'] ) && 'review' !== get_comment_type( $id ) ) {
return new WP_Error( 'woocommerce_rest_review_invalid_type', __( 'Sorry, you are not allowed to change the comment type.', 'woocommerce' ), array( 'status' => 404 ) );
}
$prepared_args = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_args ) ) {
return $prepared_args;
}
if ( ! is_array( $prepared_args ) ) {
return new WP_Error( 'woocommerce_rest_comment_failed_edit', __( 'Updating review failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
if ( isset( $prepared_args['comment_post_ID'] ) ) {
$product_id = (int) $prepared_args['comment_post_ID'];
if ( 0 >= $product_id || 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
}
$core_counted_product_id = (int) ( $prepared_args['comment_post_ID'] ?? $original_product_id );
if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
// Only the comment status is being changed.
$change = $this->handle_status_param( $request['status'], $id );
if ( ! $change ) {
return new WP_Error( 'woocommerce_rest_review_failed_edit', __( 'Updating review status failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
} elseif ( ! empty( $prepared_args ) ) {
if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) {
return new WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) );
}
$prepared_args['comment_ID'] = $id;
$check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_args );
if ( is_wp_error( $check_comment_lengths ) ) {
$error_code = str_replace( array( 'comment_author', 'comment_content' ), array( 'reviewer', 'review_content' ), $check_comment_lengths->get_error_code() );
return new WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) );
}
/*
* Core stores update-side comment meta before it updates the comment count. Keep this value
* as an integer so wp_slash() below cannot change its shape.
*/
if ( ! empty( $request['rating'] ) ) {
if ( array_key_exists( 'comment_meta', $prepared_args ) && ! is_array( $prepared_args['comment_meta'] ) ) {
return new WP_Error( 'woocommerce_rest_comment_failed_edit', __( 'Updating review failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
$prepared_args['comment_meta']['rating'] = (int) $request['rating'];
}
$updated = wp_update_comment( wp_slash( (array) $prepared_args ) );
if ( false === $updated ) {
return new WP_Error( 'woocommerce_rest_comment_failed_edit', __( 'Updating review failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
if ( isset( $request['status'] ) ) {
$this->handle_status_param( $request['status'], $id );
}
}
// Re-read the comment because the same request can move the review to a different product.
$updated_review = get_comment( $id );
$current_product_id = $updated_review instanceof WP_Comment ? (int) $updated_review->comment_post_ID : 0;
/*
* Core chooses its count target before wp_update_comment_data can redirect the persisted review.
* Refresh each affected product that Core did not count.
*/
if ( $updated_review instanceof WP_Comment ) {
foreach ( array_unique( array( $original_product_id, $current_product_id ) ) as $product_id_to_count ) {
if ( $product_id_to_count !== $core_counted_product_id ) {
wp_update_comment_count( $product_id_to_count );
}
}
}
$review = $updated_review;
if ( isset( $request['verified'] ) && ! empty( $request['verified'] ) ) {
update_comment_meta( $id, 'verified', $request['verified'] );
$review = get_comment( $id );
}
/** This action is documented in includes/api/class-wc-rest-product-reviews-controller.php */
do_action( 'woocommerce_rest_insert_product_review', $review, $request, false );
$fields_update = $this->update_additional_fields_for_object( $review, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $review, $request );
return rest_ensure_response( $response );
}