[API] Refactored the webhooks endpoint to use the WC_API_Exception class #6138
This commit is contained in:
parent
e14a5b0624
commit
d71e135c27
|
@ -4,10 +4,10 @@
|
|||
*
|
||||
* Handles requests to the /webhooks endpoint
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.2
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.2
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
|
@ -80,7 +80,7 @@ class WC_API_Webhooks extends WC_API_Resource {
|
|||
|
||||
$webhooks = array();
|
||||
|
||||
foreach( $query->posts as $webhook_id ) {
|
||||
foreach ( $query->posts as $webhook_id ) {
|
||||
|
||||
if ( ! $this->is_readable( $webhook_id ) ) {
|
||||
continue;
|
||||
|
@ -138,18 +138,21 @@ class WC_API_Webhooks extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_webhooks_count( $status = null, $filter = array() ) {
|
||||
try {
|
||||
if ( ! current_user_can( 'read_private_shop_webhooks' ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_webhooks_count', __( 'You do not have permission to read the webhooks count', 'woocommerce' ), 401 );
|
||||
}
|
||||
|
||||
if ( ! empty( $status ) ) {
|
||||
$filter['status'] = $status;
|
||||
if ( ! empty( $status ) ) {
|
||||
$filter['status'] = $status;
|
||||
}
|
||||
|
||||
$query = $this->query_webhooks( $filter );
|
||||
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
$query = $this->query_webhooks( $filter );
|
||||
|
||||
if ( ! current_user_can( 'read_private_shop_webhooks' ) ) {
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_webhooks_count', __( 'You do not have permission to read the webhooks count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
}
|
||||
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -333,13 +336,13 @@ class WC_API_Webhooks extends WC_API_Resource {
|
|||
*/
|
||||
private function query_webhooks( $args ) {
|
||||
|
||||
// set base query arguments
|
||||
// Set base query arguments
|
||||
$query_args = array(
|
||||
'fields' => 'ids',
|
||||
'post_type' => 'shop_webhook',
|
||||
);
|
||||
|
||||
// add status argument
|
||||
// Add status argument
|
||||
if ( ! empty( $args['status'] ) ) {
|
||||
|
||||
switch ( $args['status'] ) {
|
||||
|
@ -373,25 +376,23 @@ class WC_API_Webhooks extends WC_API_Resource {
|
|||
*/
|
||||
public function get_webhook_deliveries( $webhook_id, $fields = null ) {
|
||||
|
||||
// ensure ID is valid webhook ID
|
||||
// Ensure ID is valid webhook ID
|
||||
$webhook_id = $this->validate_request( $webhook_id, 'shop_webhook', 'read' );
|
||||
|
||||
if ( is_wp_error( $webhook_id ) ) {
|
||||
return $webhook_id;
|
||||
}
|
||||
|
||||
$webhook = new WC_Webhook( $webhook_id );
|
||||
|
||||
$logs = $webhook->get_delivery_logs();
|
||||
|
||||
$webhook = new WC_Webhook( $webhook_id );
|
||||
$logs = $webhook->get_delivery_logs();
|
||||
$delivery_logs = array();
|
||||
|
||||
foreach ( $logs as $log ) {
|
||||
|
||||
// add timestamp
|
||||
// Add timestamp
|
||||
$log['created_at'] = $this->server->format_datetime( $log['comment']->comment_date_gmt );
|
||||
|
||||
// remove comment object
|
||||
// Remove comment object
|
||||
unset( $log['comment'] );
|
||||
|
||||
$delivery_logs[] = $log;
|
||||
|
@ -410,37 +411,40 @@ class WC_API_Webhooks extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_webhook_delivery( $webhook_id, $id, $fields = null ) {
|
||||
try {
|
||||
// Validate webhook ID
|
||||
$webhook_id = $this->validate_request( $webhook_id, 'shop_webhook', 'read' );
|
||||
|
||||
// validate webhook ID
|
||||
$webhook_id = $this->validate_request( $webhook_id, 'shop_webhook', 'read' );
|
||||
if ( is_wp_error( $webhook_id ) ) {
|
||||
return $webhook_id;
|
||||
}
|
||||
|
||||
if ( is_wp_error( $webhook_id ) ) {
|
||||
return $webhook_id;
|
||||
$id = absint( $id );
|
||||
|
||||
if ( empty( $id ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_webhook_delivery_id', __( 'Invalid webhook delivery ID', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
$webhook = new WC_Webhook( $webhook_id );
|
||||
|
||||
$log = $webhook->get_delivery_log( $id );
|
||||
|
||||
if ( ! $log ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_webhook_delivery_id', __( 'Invalid webhook delivery', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
$delivery_log = $log;
|
||||
|
||||
// Add timestamp
|
||||
$delivery_log['created_at'] = $this->server->format_datetime( $log['comment']->comment_date_gmt );
|
||||
|
||||
// Remove comment object
|
||||
unset( $delivery_log['comment'] );
|
||||
|
||||
return array( 'webhook_delivery' => apply_filters( 'woocommerce_api_webhook_delivery_response', $delivery_log, $id, $fields, $log, $webhook_id, $this ) );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
$id = absint( $id );
|
||||
|
||||
if ( empty( $id ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_webhook_delivery_id', __( 'Invalid webhook delivery ID', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$webhook = new WC_Webhook( $webhook_id );
|
||||
|
||||
$log = $webhook->get_delivery_log( $id );
|
||||
|
||||
if ( ! $log ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_webhook_delivery_id', __( 'Invalid webhook delivery', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$delivery_log = $log;
|
||||
|
||||
// add timestamp
|
||||
$delivery_log['created_at'] = $this->server->format_datetime( $log['comment']->comment_date_gmt );
|
||||
|
||||
// remove comment object
|
||||
unset( $delivery_log['comment'] );
|
||||
|
||||
return array( 'webhook_delivery' => apply_filters( 'woocommerce_api_webhook_delivery_response', $delivery_log, $id, $fields, $log, $webhook_id, $this ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue