Delete responses

This commit is contained in:
Mike Jolley 2019-06-06 15:31:04 +01:00
parent e865ca1b71
commit ccf3a939ed
8 changed files with 93 additions and 56 deletions

View File

@ -321,14 +321,21 @@ class OrderNotes extends AbstractController {
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $note, $request );
$result = wc_delete_order_note( $note->comment_ID );
$previous = $this->prepare_item_for_response( $note, $request );
$result = wc_delete_order_note( $note->comment_ID );
if ( ! $result ) {
return new \WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), 'order_note' ), array( 'status' => 500 ) );
}
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
/**
* Fires after a order note is deleted or trashed via the REST API.
*

View File

@ -374,14 +374,21 @@ class ProductAttributes extends AbstractController {
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $attribute, $request );
$deleted = wc_delete_attribute( $attribute->attribute_id );
$previous = $this->prepare_item_for_response( $attribute, $request );
$deleted = wc_delete_attribute( $attribute->attribute_id );
if ( false === $deleted ) {
return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
}
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
/**
* Fires after a single attribute is deleted via the REST API.
*

View File

@ -676,12 +676,21 @@ class ProductVariations extends Products {
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_object_for_response( $object, $request );
// If we're forcing, then delete permanently.
if ( $force ) {
$previous = $this->prepare_object_for_response( $object, $request );
$object->delete( true );
$result = 0 === $object->get_id();
$result = 0 === $object->get_id();
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
} else {
// If we don't support trashing for this type, error out.
if ( ! $supports_trash ) {
@ -707,6 +716,8 @@ class ProductVariations extends Products {
$object->delete();
$result = 'trash' === $object->get_status();
}
$response = $this->prepare_object_for_response( $object, $request );
}
if ( ! $result ) {
@ -718,11 +729,6 @@ class ProductVariations extends Products {
);
}
// Delete parent product transients.
if ( 0 !== $object->get_parent_id() ) {
wc_delete_product_transients( $object->get_parent_id() );
}
/**
* Fires after a single object is deleted or trashed via the REST API.
*

View File

@ -1492,30 +1492,21 @@ class Products extends AbstractObjectsController {
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_object_for_response( $object, $request );
// If we're forcing, then delete permanently.
if ( $force ) {
if ( $object->is_type( 'variable' ) ) {
foreach ( $object->get_children() as $child_id ) {
$child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->delete( true );
}
}
} else {
// For other product types, if the product has children, remove the relationship.
foreach ( $object->get_children() as $child_id ) {
$child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->set_parent_id( 0 );
$child->save();
}
}
}
$previous = $this->prepare_object_for_response( $object, $request );
$object->delete( true );
$result = 0 === $object->get_id();
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
} else {
// If we don't support trashing for this type, error out.
if ( ! $supports_trash ) {
@ -1545,6 +1536,8 @@ class Products extends AbstractObjectsController {
$object->delete();
$result = 'trash' === $object->get_status();
}
$response = $this->prepare_object_for_response( $object, $request );
}
if ( ! $result ) {
@ -1558,11 +1551,6 @@ class Products extends AbstractObjectsController {
);
}
// Delete parent product transients.
if ( 0 !== $object->get_parent_id() ) {
wc_delete_product_transients( $object->get_parent_id() );
}
/**
* Fires after a single object is deleted or trashed via the REST API.
*

View File

@ -206,6 +206,11 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
$instance_id = (int) $request['instance_id'];
$force = $request['force'];
// We don't support trashing for this type, error out.
if ( ! $force ) {
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Shipping methods do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
}
$methods = $zone->get_shipping_methods();
$method = false;
@ -226,23 +231,26 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
}
$request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $method, $request );
$previous = $this->prepare_item_for_response( $method, $request );
// Actually delete.
if ( $force ) {
$zone->delete_shipping_method( $instance_id );
} else {
return new \WP_Error( 'rest_trash_not_supported', __( 'Shipping methods do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
}
$zone->delete_shipping_method( $instance_id );
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous,
)
);
/**
* Fires after a product review is deleted via the REST API.
* Fires after a method is deleted via the REST API.
*
* @param object $method
* @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', $method, $response, $request );
do_action( 'woocommerce_rest_delete_shipping_zone_method', $method, $response, $request );
return $response;
}

View File

@ -211,14 +211,21 @@ class ShippingZones extends AbstractShippingZonesController {
$force = $request['force'];
$response = $this->get_item( $request );
if ( $force ) {
$zone->delete();
} else {
return new \WP_Error( 'rest_trash_not_supported', __( 'Shipping zones do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
// We don't support trashing for this type, error out.
if ( ! $force ) {
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Shipping zones do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
}
$previous = $this->get_item( $request );
$zone->delete();
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
return $response;
}

View File

@ -242,6 +242,9 @@ class TaxClasses extends AbstractController {
return new \WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) );
}
$request->set_param( 'context', 'edit' );
$previous = $this->prepare_item_for_response( $tax_class, $request );
update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) );
// Delete tax rate locations locations from the selected class.
@ -262,8 +265,13 @@ class TaxClasses extends AbstractController {
// Delete tax rates in the selected class.
$wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $tax_class['slug'] ), array( '%s' ) );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $tax_class, $request );
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
/**
* Fires after a tax class is deleted via the REST API.

View File

@ -463,13 +463,19 @@ class Webhooks extends AbstractController {
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $webhook, $request );
$previous = $this->prepare_item_for_response( $webhook, $request );
$result = $webhook->delete( true );
if ( ! $result ) {
/* translators: %s: post type */
return new \WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
}
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
/**
* Fires after a single item is deleted or trashed via the REST API.
@ -478,7 +484,7 @@ class Webhooks extends AbstractController {
* @param WP_REST_Response $response The response data.
* @param \WP_REST_Request $request The request sent to the API.
*/
do_action( "woocommerce_rest_delete_webhook_object", $webhook, $response, $request );
do_action( 'woocommerce_rest_delete_webhook_object', $webhook, $response, $request );
return $response;
}