class-wc-rest-webhook-deliveries-controller.php 9.5 KB

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