[API] Refactored the coupons endpoint to use the WC_API_Exception class #6138
This commit is contained in:
parent
d880ee390b
commit
a96815ced9
|
@ -100,6 +100,8 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
public function get_coupon( $id, $fields = null ) {
|
||||
global $wpdb;
|
||||
|
||||
try {
|
||||
|
||||
$id = $this->validate_request( $id, 'shop_coupon', 'read' );
|
||||
|
||||
if ( is_wp_error( $id ) ) {
|
||||
|
@ -110,13 +112,11 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
$code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id ) );
|
||||
|
||||
if ( is_null( $code ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
$coupon = new WC_Coupon( $code );
|
||||
|
||||
$coupon_post = get_post( $coupon->id );
|
||||
|
||||
$coupon_data = array(
|
||||
'id' => $coupon->id,
|
||||
'code' => $coupon->code,
|
||||
|
@ -143,6 +143,9 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
);
|
||||
|
||||
return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -154,13 +157,17 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
*/
|
||||
public function get_coupons_count( $filter = array() ) {
|
||||
|
||||
try {
|
||||
$query = $this->query_coupons( $filter );
|
||||
|
||||
if ( ! current_user_can( 'read_private_shop_coupons' ) ) {
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), 401 );
|
||||
}
|
||||
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -174,13 +181,17 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
public function get_coupon_by_code( $code, $fields = null ) {
|
||||
global $wpdb;
|
||||
|
||||
try {
|
||||
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $code ) );
|
||||
|
||||
if ( is_null( $id ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
return $this->get_coupon( $id, $fields );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -193,18 +204,19 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
public function create_coupon( $data ) {
|
||||
global $wpdb;
|
||||
|
||||
try {
|
||||
$data = isset( $data['coupon'] ) ? $data['coupon'] : array();
|
||||
|
||||
// Check user permission
|
||||
if ( ! current_user_can( 'publish_shop_coupons' ) ) {
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_create_coupon', __( 'You do not have permission to create coupons', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_coupon', __( 'You do not have permission to create coupons', 'woocommerce' ), 401 );
|
||||
}
|
||||
|
||||
$data = apply_filters( 'woocommerce_api_create_coupon_data', $data, $this );
|
||||
|
||||
// Check if coupon code is specified
|
||||
if ( ! isset( $data['code'] ) ) {
|
||||
return new WP_Error( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), 400 );
|
||||
}
|
||||
|
||||
$coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] );
|
||||
|
@ -219,7 +231,7 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
", $coupon_code ) );
|
||||
|
||||
if ( $coupon_found ) {
|
||||
return new WP_Error( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
$defaults = array(
|
||||
|
@ -246,7 +258,7 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
|
||||
// Validate coupon types
|
||||
if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ), 400 );
|
||||
}
|
||||
|
||||
$new_coupon = array(
|
||||
|
@ -261,10 +273,10 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
$id = wp_insert_post( $new_coupon, $wp_error = false );
|
||||
|
||||
if ( is_wp_error( $id ) ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_create_coupon', $id->get_error_message(), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_create_coupon', $id->get_error_message(), 400 );
|
||||
}
|
||||
|
||||
// set coupon meta
|
||||
// Set coupon meta
|
||||
update_post_meta( $id, 'discount_type', $coupon_data['type'] );
|
||||
update_post_meta( $id, 'coupon_amount', wc_format_decimal( $coupon_data['amount'] ) );
|
||||
update_post_meta( $id, 'individual_use', ( true === $coupon_data['individual_use'] ) ? 'yes' : 'no' );
|
||||
|
@ -288,6 +300,9 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
$this->server->send_status( 201 );
|
||||
|
||||
return $this->get_coupon( $id );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -300,6 +315,7 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
*/
|
||||
public function edit_coupon( $id, $data ) {
|
||||
|
||||
try {
|
||||
$data = isset( $data['coupon'] ) ? $data['coupon'] : array();
|
||||
|
||||
$id = $this->validate_request( $id, 'shop_coupon', 'edit' );
|
||||
|
@ -326,19 +342,19 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
", $coupon_code, $id ) );
|
||||
|
||||
if ( $coupon_found ) {
|
||||
return new WP_Error( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
$id = wp_update_post( array( 'ID' => intval( $id ), 'post_title' => $coupon_code, 'post_excerpt' => isset( $data['description'] ) ? $data['description'] : '' ) );
|
||||
if ( 0 === $id ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $data['type'] ) ) {
|
||||
// Validate coupon types
|
||||
if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ), 400 );
|
||||
}
|
||||
update_post_meta( $id, 'discount_type', $data['type'] );
|
||||
}
|
||||
|
@ -410,6 +426,9 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
do_action( 'woocommerce_api_edit_coupon', $id, $data );
|
||||
|
||||
return $this->get_coupon( $id );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue