Skip to content

Commit

Permalink
Fix product review endpoint based on PR feedback: Code cleanup, prepa…
Browse files Browse the repository at this point in the history
…re_item_for_database like WP-API's comments does, better error handling on create, required arg simplification, trash support for reviews.
  • Loading branch information
justinshreve committed Aug 2, 2016
1 parent 7529b0b commit 5d521b0
Showing 1 changed file with 110 additions and 71 deletions.
181 changes: 110 additions & 71 deletions includes/api/class-wc-rest-product-reviews-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function register_routes() {
'args' => array(
'force' => array(
'default' => false,
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
),
),
),
Expand Down Expand Up @@ -121,7 +121,7 @@ public function get_items_permissions_check( $request ) {
}

/**
* Check if a given request has access to read a product review.
* Check if a given request has access to read a product review.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
Expand Down Expand Up @@ -198,13 +198,13 @@ public function batch_items_permissions_check( $request ) {
* @return array
*/
public function get_items( $request ) {
$product = get_post( (int) $request['product_id'] );
$product_id = (int) $request['product_id'];

if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

$reviews = get_approved_comments( $product->ID );
$reviews = get_approved_comments( $product_id );
$data = array();
foreach ( $reviews as $review_data ) {
$review = $this->prepare_item_for_response( $review_data, $request );
Expand All @@ -222,16 +222,16 @@ public function get_items( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$product = get_post( (int) $request['product_id'] );
$id = (int) $request['id'];
$product_id = (int) $request['product_id'];

if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

$review = get_comment( $id );

if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== intval( $product->ID ) ) {
if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

Expand All @@ -249,52 +249,48 @@ public function get_item( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
$product = get_post( (int) $request['product_id'] );
$product_id = (int) $request['product_id'];

if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

if ( empty( $request['review'] ) ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_review', __( 'Product review content is required.', 'woocommerce' ), array( 'status' => 400 ) );
}
$prepared_review = $this->prepare_item_for_database( $request );

if ( empty( $request['name'] ) ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_name', __( 'Product review author name is required.', 'woocommerce' ), array( 'status' => 400 ) );
}
/**
* 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 );

if ( empty( $request['email'] ) ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_email', __( 'Product review author email is required.', 'woocommerce' ), array( 'status' => 400 ) );
$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.' ), array( 'status' => 500 ) );
}

$data = array(
'comment_post_ID' => $product->ID,
'comment_author' => $request['name'],
'comment_author_email' => $request['email'],
'comment_content' => $request['review'],
'comment_approved' => 1,
'comment_type' => 'review',
);
$product_review_id = wp_insert_comment( $data );
update_comment_meta( $product_review_id, 'rating', ( ! empty( $request['rating'] ) ? $request['rating'] : '0' ) );

$comment = get_comment( $product_review_id );
$this->update_additional_fields_for_object( $comment, $request );
$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 $comment Inserted object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
* @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", $comment, $request, true );
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );

$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $comment, $request );
$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 );
$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;
Expand All @@ -307,38 +303,32 @@ public function create_item( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$id = (int) $request['id'];
$product = get_post( (int) $request['product_id'] );
$product_review_id = (int) $request['id'];
$product_id = (int) $request['product_id'];

if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

$review = get_comment( $id );
$review = get_comment( $product_review_id );

if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== intval( $product->ID ) ) {
if ( empty( $product_review_id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

// Update fields
$commentdata = array( 'comment_ID' => $id );
if ( ! empty( $request['name'] ) ) {
$commentdata['comment_author'] = $request['name' ];
}
if ( ! empty( $request['email'] ) ) {
$commentdata['comment_author_email'] = $request['email' ];
}
if ( ! empty( $request['review'] ) ) {
$commentdata['comment_content'] = $request['review' ];
$prepared_review = $this->prepare_item_for_database( $request );

$updated = wp_update_comment( $prepared_review );
if ( 0 === $updated ) {
return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.' ), array( 'status' => 500 ) );
}

wp_update_comment( $commentdata );
if ( ! empty( $request['rating'] ) ) {
update_comment_meta( $id, 'rating', $request['rating'] );
update_comment_meta( $product_review_id, 'rating', $request['rating'] );
}

$comment = get_comment( $id );
$this->update_additional_fields_for_object( $comment, $request );
$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.
Expand All @@ -347,10 +337,10 @@ public function update_item( $request ) {
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_product_review", $comment, $request, true );
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );

$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $comment, $request );
$response = $this->prepare_item_for_response( $product_review, $request );

return rest_ensure_response( $response );
}
Expand All @@ -362,23 +352,40 @@ public function update_item( $request ) {
* @return WP_Error|boolean
*/
public function delete_item( $request ) {
$id = is_array( $request['id'] ) ? $request['id']['id'] : $request['id'];
$product_review_id = is_array( $request['id'] ) ? $request['id']['id'] : $request['id'];
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;

if ( ! $force ) {
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Product reviews do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
}

$comment = get_comment( $id );

if ( empty( $id ) || empty( $comment->comment_ID ) || empty( $comment->comment_post_ID ) ) {
$product_review = get_comment( $product_review_id );
if ( empty( $product_review_id ) || empty( $product_review->comment_ID ) || empty( $product_review->comment_post_ID ) ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

/**
* Filter whether a product review is trashable.
*
* Return false to disable trash support for the product review.
*
* @param boolean $supports_trash Whether the object supports trashing.
* @param WP_Post $product_review The object being considered for trashing support.
*/
$supports_trash = apply_filters( 'rest_product_review_trashable', ( EMPTY_TRASH_DAYS > 0 ), $product_review );

$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $comment, $request );
$response = $this->prepare_item_for_response( $product_review, $request );

if ( $force ) {
$result = wp_delete_comment( $product_review_id, true );
} else {
if ( ! $supports_trash ) {
return new WP_Error( 'rest_trash_not_supported', __( 'The product review does not support trashing.' ), array( 'status' => 501 ) );
}

if ( 'trash' === $product_review->comment_approved ) {
return new WP_Error( 'rest_already_trashed', __( 'The comment has already been trashed.' ), array( 'status' => 410 ) );
}

$result = wp_delete_comment( $id, true );
$result = wp_trash_comment( $product_review->comment_ID );
}

if ( ! $result ) {
return new WP_Error( 'rest_cannot_delete', __( 'The product review cannot be deleted.' ), array( 'status' => 500 ) );
Expand All @@ -387,11 +394,11 @@ public function delete_item( $request ) {
/**
* Fires after a product review is deleted via the REST API.
*
* @param object $comment The deleted item.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
* @param object $product_review The deleted item.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'rest_delete_product_review', $comment, $response, $request );
do_action( 'rest_delete_product_review', $product_review, $response, $request );

return $response;
}
Expand Down Expand Up @@ -462,6 +469,38 @@ public function prepare_item_for_response( $review, $request ) {
return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request );
}

/**
* Prepare a single product review to be inserted into the database.
*
* @param WP_REST_Request $request Request object.
* @return array|WP_Error $prepared_review
*/
protected function prepare_item_for_database( $request ) {
$prepared_review = array( 'comment_approved' => 1, 'comment_type' => 'review' );

if ( isset( $request['id'] ) ) {
$prepared_review['comment_ID'] = (int) $request['id'];
}

if ( isset( $request['review'] ) ) {
$prepared_review['comment_content'] = $request['review'];
}

if ( isset( $request['product_id'] ) ) {
$prepared_review['comment_post_ID'] = (int) $request['product_id'];
}

if ( isset( $request['name'] ) ) {
$prepared_review['comment_author'] = $request['name'];
}

if ( isset( $request['email'] ) ) {
$prepared_review['comment_author_email'] = $request['email'];
}

return apply_filters( 'rest_preprocess_product_review', $prepared_review, $request );
}

/**
* Prepare links for the request.
*
Expand Down

0 comments on commit 5d521b0

Please sign in to comment.