WC_REST_Product_Reviews_V1_Controller::create_item()
Create a 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->create_item( $request );
- $request(WP_REST_Request) (обязательный)
- Full details about the request.
Код WC_REST_Product_Reviews_V1_Controller::create_item() WC REST Product Reviews V1 Controller::create item WC 9.2.3
public function create_item( $request ) { $product_id = (int) $request['product_id']; if ( 'product' !== get_post_type( $product_id ) ) { return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); } $prepared_review = $this->prepare_item_for_database( $request ); /** * Filter a product review (comment) before it is inserted via the REST API. * * Allows modification of the comment right before it is inserted via `wp_insert_comment`. * * @param array $prepared_review The prepared comment data for `wp_insert_comment`. * @param WP_REST_Request $request Request used to insert the comment. */ $prepared_review = apply_filters( 'rest_pre_insert_product_review', $prepared_review, $request ); $product_review_id = wp_insert_comment( $prepared_review ); if ( ! $product_review_id ) { return new WP_Error( 'rest_product_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) ); } update_comment_meta( $product_review_id, 'rating', ( ! empty( $request['rating'] ) ? $request['rating'] : '0' ) ); $product_review = get_comment( $product_review_id ); $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 $product_review 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 ); $response = rest_ensure_response( $response ); $response->set_status( 201 ); $base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base ); $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $product_review_id ) ) ); return $response; }