WC_REST_Product_Reviews_Controller::create_item() │ public │ WC 1.0
Create a single review.
Метод класса: WC_REST_Product_Reviews_Controller{}
Возвращает
WP_Error|WP_REST_Response
.
Использование
$WC_REST_Product_Reviews_Controller = new WC_REST_Product_Reviews_Controller(); $WC_REST_Product_Reviews_Controller->create_item( $request );
- $request(WP_REST_Request) (обязательный)
- Full details about the request.
Код WC_REST_Product_Reviews_Controller::create_item() WC REST Product Reviews Controller::create item WC 9.2.3
public function create_item( $request ) { if ( ! empty( $request['id'] ) ) { return new WP_Error( 'woocommerce_rest_review_exists', __( 'Cannot create existing product review.', 'woocommerce' ), array( 'status' => 400 ) ); } $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 ); if ( is_wp_error( $prepared_review ) ) { return $prepared_review; } $prepared_review['comment_type'] = 'review'; /* * Do not allow a comment to be created with missing or empty comment_content. See wp_handle_comment_submission(). */ if ( empty( $prepared_review['comment_content'] ) ) { return new WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) ); } // Setting remaining values before wp_insert_comment so we can use wp_allow_comment(). if ( ! isset( $prepared_review['comment_date_gmt'] ) ) { $prepared_review['comment_date_gmt'] = current_time( 'mysql', true ); } if ( ! empty( $_SERVER['REMOTE_ADDR'] ) && rest_is_ip_address( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) ) { // WPCS: input var ok, sanitization ok. $prepared_review['comment_author_IP'] = wc_clean( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); // WPCS: input var ok. } else { $prepared_review['comment_author_IP'] = '127.0.0.1'; } if ( ! empty( $request['author_user_agent'] ) ) { $prepared_review['comment_agent'] = $request['author_user_agent']; } elseif ( $request->get_header( 'user_agent' ) ) { $prepared_review['comment_agent'] = $request->get_header( 'user_agent' ); } else { $prepared_review['comment_agent'] = ''; } $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_review ); 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 ) ); } $prepared_review['comment_parent'] = 0; $prepared_review['comment_author_url'] = ''; $prepared_review['comment_approved'] = wp_allow_comment( $prepared_review, true ); if ( is_wp_error( $prepared_review['comment_approved'] ) ) { $error_code = $prepared_review['comment_approved']->get_error_code(); $error_message = $prepared_review['comment_approved']->get_error_message(); if ( 'comment_duplicate' === $error_code ) { return new WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 409 ) ); } if ( 'comment_flood' === $error_code ) { return new WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 400 ) ); } return $prepared_review['comment_approved']; } /** * Filters a review before it is inserted via the REST API. * * Allows modification of the review right before it is inserted via wp_insert_comment(). * Returning a WP_Error value from the filter will shortcircuit insertion and allow * skipping further processing. * * @since 3.5.0 * @param array|WP_Error $prepared_review The prepared review data for wp_insert_comment(). * @param WP_REST_Request $request Request used to insert the review. */ $prepared_review = apply_filters( 'woocommerce_rest_pre_insert_product_review', $prepared_review, $request ); if ( is_wp_error( $prepared_review ) ) { return $prepared_review; } $review_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_review ) ) ); if ( ! $review_id ) { return new WP_Error( 'woocommerce_rest_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) ); } if ( isset( $request['status'] ) ) { $this->handle_status_param( $request['status'], $review_id ); } update_comment_meta( $review_id, 'rating', ! empty( $request['rating'] ) ? $request['rating'] : '0' ); $review = get_comment( $review_id ); /** * Fires after a comment is created or updated via the REST API. * * @param WP_Comment $review Inserted or updated comment object. * @param WP_REST_Request $request Request object. * @param bool $creating True when creating a comment, false when updating. */ do_action( 'woocommerce_rest_insert_product_review', $review, $request, true ); $fields_update = $this->update_additional_fields_for_object( $review, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view'; $request->set_param( 'context', $context ); $response = $this->prepare_item_for_response( $review, $request ); $response = rest_ensure_response( $response ); $response->set_status( 201 ); $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $review_id ) ) ); return $response; }