WC_REST_Product_Reviews_V1_Controller::update_item
Update a single product review.
Метод класса: WC_REST_Product_Reviews_V1_Controller{}
Хуки из метода
Возвращает
WP_Error|WP_REST_Response.
Использование
$WC_REST_Product_Reviews_V1_Controller = new WC_REST_Product_Reviews_V1_Controller(); $WC_REST_Product_Reviews_V1_Controller->update_item( $request );
- $request(WP_REST_Request) (обязательный)
- Full details about the request.
Код WC_REST_Product_Reviews_V1_Controller::update_item() WC REST Product Reviews V1 Controller::update item WC 11.2.1
public function update_item( $request ) {
$product_review_id = (int) $request['id'];
$product_id = (int) $request['product_id'];
$review = $this->get_review( $product_review_id, $product_id );
if ( is_wp_error( $review ) ) {
return $review;
}
$original_product_id = (int) $review->comment_post_ID;
$prepared_review = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_review ) ) {
return $prepared_review;
}
if ( ! is_array( $prepared_review ) ) {
return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
/*
* Core stores update-side comment meta before it updates the comment count. Passing the
* rating here lets WooCommerce's count callback see the new value in the same product save.
*/
if ( ! empty( $request['rating'] ) ) {
if ( array_key_exists( 'comment_meta', $prepared_review ) && ! is_array( $prepared_review['comment_meta'] ) ) {
return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
$prepared_review['comment_meta']['rating'] = (int) $request['rating'];
}
$core_counted_product_id = (int) ( $prepared_review['comment_post_ID'] ?? $original_product_id );
/*
* Zero means no comment-table row changed; false is the actual update failure. A rating-only
* request legitimately returns zero while still storing comment_meta and refreshing counts.
*/
$updated = wp_update_comment( $prepared_review );
if ( false === $updated ) {
return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
$product_review = get_comment( $product_review_id );
$current_product_id = $product_review instanceof WP_Comment ? (int) $product_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 ( $product_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 );
}
}
}
$this->update_additional_fields_for_object( $product_review, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
* @param WP_Comment $comment Inserted object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $product_review, $request );
return rest_ensure_response( $response );
}