WC_REST_Webhook_Deliveries_V1_Controller

REST API Webhook Deliveries controller class.

Defined (1)

The class is defined in the following location(s).

/includes/api/v1/class-wc-rest-webhook-deliveries.php  
  1. class WC_REST_Webhook_Deliveries_V1_Controller extends WC_REST_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v1'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = 'webhooks/(?P<webhook_id>[\d]+)/deliveries'; 
  14.  
  15. /** 
  16. * Register the routes for webhook deliveries. 
  17. */ 
  18. public function register_routes() { 
  19. register_rest_route( $this->namespace, '/' . $this->rest_base, array( 
  20. 'args' => array( 
  21. 'webhook_id' => array( 
  22. 'description' => __( 'Unique identifier for the webhook.', 'woocommerce' ),  
  23. 'type' => 'integer',  
  24. ),  
  25. ),  
  26. array( 
  27. 'methods' => WP_REST_Server::READABLE,  
  28. 'callback' => array( $this, 'get_items' ),  
  29. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  30. 'args' => $this->get_collection_params(),  
  31. ),  
  32. 'schema' => array( $this, 'get_public_item_schema' ),  
  33. ) ); 
  34.  
  35. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 
  36. 'args' => array( 
  37. 'webhook_id' => array( 
  38. 'description' => __( 'Unique identifier for the webhook.', 'woocommerce' ),  
  39. 'type' => 'integer',  
  40. ),  
  41. 'id' => array( 
  42. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  43. 'type' => 'integer',  
  44. ),  
  45. ),  
  46. array( 
  47. 'methods' => WP_REST_Server::READABLE,  
  48. 'callback' => array( $this, 'get_item' ),  
  49. 'permission_callback' => array( $this, 'get_item_permissions_check' ),  
  50. 'args' => array( 
  51. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  52. ),  
  53. ),  
  54. 'schema' => array( $this, 'get_public_item_schema' ),  
  55. ) ); 
  56.  
  57. /** 
  58. * Check whether a given request has permission to read webhook deliveries. 
  59. * @param WP_REST_Request $request Full details about the request. 
  60. * @return WP_Error|boolean 
  61. */ 
  62. public function get_items_permissions_check( $request ) { 
  63. if ( ! wc_rest_check_post_permissions( 'shop_webhook', 'read' ) ) { 
  64. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  65.  
  66. return true; 
  67.  
  68. /** 
  69. * Check if a given request has access to read a webhook develivery. 
  70. * @param WP_REST_Request $request Full details about the request. 
  71. * @return WP_Error|boolean 
  72. */ 
  73. public function get_item_permissions_check( $request ) { 
  74. $post = get_post( (int) $request['webhook_id'] ); 
  75.  
  76. if ( $post && ! wc_rest_check_post_permissions( 'shop_webhook', 'read', $post->ID ) ) { 
  77. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  78.  
  79. return true; 
  80.  
  81. /** 
  82. * Get all webhook deliveries. 
  83. * @param WP_REST_Request $request 
  84. * @return array 
  85. */ 
  86. public function get_items( $request ) { 
  87. $webhook = new WC_Webhook( (int) $request['webhook_id'] ); 
  88.  
  89. if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) { 
  90. return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook ID.', 'woocommerce' ), array( 'status' => 404 ) ); 
  91.  
  92. $logs = $webhook->get_delivery_logs(); 
  93.  
  94. $data = array(); 
  95. foreach ( $logs as $log ) { 
  96. $delivery = $this->prepare_item_for_response( (object) $log, $request ); 
  97. $delivery = $this->prepare_response_for_collection( $delivery ); 
  98. $data[] = $delivery; 
  99.  
  100. return rest_ensure_response( $data ); 
  101.  
  102. /** 
  103. * Get a single webhook delivery. 
  104. * @param WP_REST_Request $request Full details about the request. 
  105. * @return WP_Error|WP_REST_Response 
  106. */ 
  107. public function get_item( $request ) { 
  108. $id = (int) $request['id']; 
  109. $webhook = new WC_Webhook( (int) $request['webhook_id'] ); 
  110.  
  111. if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) { 
  112. return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook ID.', 'woocommerce' ), array( 'status' => 404 ) ); 
  113.  
  114. $log = $webhook->get_delivery_log( $id ); 
  115.  
  116. if ( empty( $id ) || empty( $log ) ) { 
  117. return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) ); 
  118.  
  119. $delivery = $this->prepare_item_for_response( (object) $log, $request ); 
  120. $response = rest_ensure_response( $delivery ); 
  121.  
  122. return $response; 
  123.  
  124. /** 
  125. * Prepare a single webhook delivery output for response. 
  126. * @param stdClass $log Delivery log object. 
  127. * @param WP_REST_Request $request Request object. 
  128. * @return WP_REST_Response $response Response data. 
  129. */ 
  130. public function prepare_item_for_response( $log, $request ) { 
  131. $data = (array) $log; 
  132.  
  133. // Add timestamp. 
  134. $data['date_created'] = wc_rest_prepare_date_response( $log->comment->comment_date_gmt ); 
  135.  
  136. // Remove comment object. 
  137. unset( $data['comment'] ); 
  138.  
  139. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  140. $data = $this->add_additional_fields_to_object( $data, $request ); 
  141. $data = $this->filter_response_by_context( $data, $context ); 
  142.  
  143. // Wrap the data in a response object. 
  144. $response = rest_ensure_response( $data ); 
  145.  
  146. $response->add_links( $this->prepare_links( $log ) ); 
  147.  
  148. /** 
  149. * Filter webhook delivery object returned from the REST API. 
  150. * @param WP_REST_Response $response The response object. 
  151. * @param stdClass $log Delivery log object used to create response. 
  152. * @param WP_REST_Request $request Request object. 
  153. */ 
  154. return apply_filters( 'woocommerce_rest_prepare_webhook_delivery', $response, $log, $request ); 
  155.  
  156. /** 
  157. * Prepare links for the request. 
  158. * @param stdClass $log Delivery log object. 
  159. * @return array Links for the given webhook delivery. 
  160. */ 
  161. protected function prepare_links( $log ) { 
  162. $webhook_id = (int) $log->request_headers['X-WC-Webhook-ID']; 
  163. $base = str_replace( '(?P<webhook_id>[\d]+)', $webhook_id, $this->rest_base ); 
  164. $links = array( 
  165. 'self' => array( 
  166. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $log->id ) ),  
  167. ),  
  168. 'collection' => array( 
  169. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),  
  170. ),  
  171. 'up' => array( 
  172. 'href' => rest_url( sprintf( '/%s/webhooks/%d', $this->namespace, $webhook_id ) ),  
  173. ),  
  174. ); 
  175.  
  176. return $links; 
  177.  
  178. /** 
  179. * Get the Webhook's schema, conforming to JSON Schema. 
  180. * @return array 
  181. */ 
  182. public function get_item_schema() { 
  183. $schema = array( 
  184. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  185. 'title' => 'webhook_delivery',  
  186. 'type' => 'object',  
  187. 'properties' => array( 
  188. 'id' => array( 
  189. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  190. 'type' => 'integer',  
  191. 'context' => array( 'view' ),  
  192. 'readonly' => true,  
  193. ),  
  194. 'duration' => array( 
  195. 'description' => __( 'The delivery duration, in seconds.', 'woocommerce' ),  
  196. 'type' => 'string',  
  197. 'context' => array( 'view' ),  
  198. 'readonly' => true,  
  199. ),  
  200. 'summary' => array( 
  201. 'description' => __( 'A friendly summary of the response including the HTTP response code, message, and body.', 'woocommerce' ),  
  202. 'type' => 'string',  
  203. 'context' => array( 'view' ),  
  204. 'readonly' => true,  
  205. ),  
  206. 'request_url' => array( 
  207. 'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),  
  208. 'type' => 'string',  
  209. 'format' => 'uri',  
  210. 'context' => array( 'view' ),  
  211. 'readonly' => true,  
  212. ),  
  213. 'request_headers' => array( 
  214. 'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),  
  215. 'type' => 'string',  
  216. 'format' => 'uri',  
  217. 'context' => array( 'view' ),  
  218. 'readonly' => true,  
  219. ),  
  220. 'request_headers' => array( 
  221. 'description' => __( 'Request headers.', 'woocommerce' ),  
  222. 'type' => 'array',  
  223. 'context' => array( 'view' ),  
  224. 'readonly' => true,  
  225. 'items' => array( 
  226. 'type' => 'string',  
  227. ),  
  228. ),  
  229. 'request_body' => array( 
  230. 'description' => __( 'Request body.', 'woocommerce' ),  
  231. 'type' => 'string',  
  232. 'context' => array( 'view' ),  
  233. 'readonly' => true,  
  234. ),  
  235. 'response_code' => array( 
  236. 'description' => __( 'The HTTP response code from the receiving server.', 'woocommerce' ),  
  237. 'type' => 'string',  
  238. 'context' => array( 'view' ),  
  239. 'readonly' => true,  
  240. ),  
  241. 'response_message' => array( 
  242. 'description' => __( 'The HTTP response message from the receiving server.', 'woocommerce' ),  
  243. 'type' => 'string',  
  244. 'context' => array( 'view' ),  
  245. 'readonly' => true,  
  246. ),  
  247. 'response_headers' => array( 
  248. 'description' => __( 'Array of the response headers from the receiving server.', 'woocommerce' ),  
  249. 'type' => 'array',  
  250. 'context' => array( 'view' ),  
  251. 'readonly' => true,  
  252. 'items' => array( 
  253. 'type' => 'string',  
  254. ),  
  255. ),  
  256. 'response_body' => array( 
  257. 'description' => __( 'The response body from the receiving server.', 'woocommerce' ),  
  258. 'type' => 'string',  
  259. 'context' => array( 'view' ),  
  260. 'readonly' => true,  
  261. ),  
  262. 'date_created' => array( 
  263. 'description' => __( "The date the webhook delivery was logged, in the site's timezone.", 'woocommerce' ),  
  264. 'type' => 'date-time',  
  265. 'context' => array( 'view', 'edit' ),  
  266. 'readonly' => true,  
  267. ),  
  268. ),  
  269. ); 
  270.  
  271. return $this->add_additional_fields_schema( $schema ); 
  272.  
  273. /** 
  274. * Get the query params for collections. 
  275. * @return array 
  276. */ 
  277. public function get_collection_params() { 
  278. return array( 
  279. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  280. );