Crated methods to list and get webhook deliveries

This commit is contained in:
Claudio Sanches 2016-03-15 19:38:48 -03:00
parent bd3b5e986e
commit 0ab93eebeb
1 changed files with 151 additions and 0 deletions

View File

@ -63,6 +63,146 @@ class WC_REST_Webhook_Deliveries_Controller extends WP_REST_Controller {
) );
}
/**
* Check whether a given request has permission to read webhook deliveries.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list taxes.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to read a webhook develivery.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get all webhook deliveries.
*
* @param WP_REST_Request $request
* @return array
*/
public function get_items( $request ) {
$id = (int) $request['id'];
$webhook = new WC_Webhook( (int) $request['webhook_id'] );
if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) {
return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook id.', 'woocommerce' ), array( 'status' => 404 ) );
}
$logs = $webhook->get_delivery_logs();
$data = array();
foreach ( $logs as $log ) {
$delivery = $this->prepare_item_for_response( (object) $log, $request );
$delivery = $this->prepare_response_for_collection( $delivery );
$data[] = $delivery;
}
return rest_ensure_response( $data );
}
/**
* Get a single webhook delivery.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$webhook = new WC_Webhook( (int) $request['webhook_id'] );
if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) {
return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook id.', 'woocommerce' ), array( 'status' => 404 ) );
}
$log = $webhook->get_delivery_log( $id );
if ( empty( $id ) || empty( $log ) ) {
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 404 ) );
}
$delivery = $this->prepare_item_for_response( (object) $log, $request );
$response = rest_ensure_response( $delivery );
return $response;
}
/**
* Prepare a single webhook delivery output for response.
*
* @param stdClass $log Delivery log object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $log, $request ) {
$data = (array) $log;
// Add timestamp.
$data['created_at'] = wc_rest_api_prepare_date_response( $log->comment->comment_date_gmt );
// Remove comment object.
unset( $data['comment'] );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $log ) );
/**
* Filter webhook delivery object returned from the REST API.
*
* @param WP_REST_Response $response The response object.
* @param stdClass $log Delivery log object used to create response.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( 'woocommerce_rest_prepare_webhook_delivery', $response, $log, $request );
}
/**
* Prepare links for the request.
*
* @param stdClass $log Delivery log object.
* @return array Links for the given webhook delivery.
*/
protected function prepare_links( $log ) {
$webhook_id = (int) $log->request_headers['X-WC-Webhook-ID'];
$base = str_replace( '(?P<webhook_id>[\d]+)', $webhook_id, $this->rest_base );
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $log->id ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
),
'up' => array(
'href' => rest_url( sprintf( '/wc/v1/webhooks/%d', $webhook_id ) ),
),
);
return $links;
}
/**
* Get the Webhook's schema, conforming to JSON Schema.
*
@ -153,4 +293,15 @@ class WC_REST_Webhook_Deliveries_Controller extends WP_REST_Controller {
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
return array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
);
}
}