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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * REST API Webhooks controller
  4. *
  5. * Handles requests to the /webhooks/<webhook_id>/deliveries endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Webhook Deliveries controller class.
  13. *
  14. * @deprecated 3.3.0 Webhooks deliveries logs now uses logging system.
  15. * @package WooCommerce/API
  16. * @extends WC_REST_Webhook_Deliveries_V1_Controller
  17. */
  18. class WC_REST_Webhook_Deliveries_Controller extends WC_REST_Webhook_Deliveries_V1_Controller {
  19. /**
  20. * Endpoint namespace.
  21. *
  22. * @var string
  23. */
  24. protected $namespace = 'wc/v2';
  25. /**
  26. * Prepare a single webhook delivery output for response.
  27. *
  28. * @param stdClass $log Delivery log object.
  29. * @param WP_REST_Request $request Request object.
  30. * @return WP_REST_Response
  31. */
  32. public function prepare_item_for_response( $log, $request ) {
  33. $data = (array) $log;
  34. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  35. $data = $this->add_additional_fields_to_object( $data, $request );
  36. $data = $this->filter_response_by_context( $data, $context );
  37. // Wrap the data in a response object.
  38. $response = rest_ensure_response( $data );
  39. $response->add_links( $this->prepare_links( $log ) );
  40. /**
  41. * Filter webhook delivery object returned from the REST API.
  42. *
  43. * @param WP_REST_Response $response The response object.
  44. * @param stdClass $log Delivery log object used to create response.
  45. * @param WP_REST_Request $request Request object.
  46. */
  47. return apply_filters( 'woocommerce_rest_prepare_webhook_delivery', $response, $log, $request );
  48. }
  49. /**
  50. * Get the Webhook's schema, conforming to JSON Schema.
  51. *
  52. * @return array
  53. */
  54. public function get_item_schema() {
  55. $schema = array(
  56. '$schema' => 'http://json-schema.org/draft-04/schema#',
  57. 'title' => 'webhook_delivery',
  58. 'type' => 'object',
  59. 'properties' => array(
  60. 'id' => array(
  61. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  62. 'type' => 'integer',
  63. 'context' => array( 'view' ),
  64. 'readonly' => true,
  65. ),
  66. 'duration' => array(
  67. 'description' => __( 'The delivery duration, in seconds.', 'woocommerce' ),
  68. 'type' => 'string',
  69. 'context' => array( 'view' ),
  70. 'readonly' => true,
  71. ),
  72. 'summary' => array(
  73. 'description' => __( 'A friendly summary of the response including the HTTP response code, message, and body.', 'woocommerce' ),
  74. 'type' => 'string',
  75. 'context' => array( 'view' ),
  76. 'readonly' => true,
  77. ),
  78. 'request_url' => array(
  79. 'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),
  80. 'type' => 'string',
  81. 'format' => 'uri',
  82. 'context' => array( 'view' ),
  83. 'readonly' => true,
  84. ),
  85. 'request_headers' => array(
  86. 'description' => __( 'Request headers.', 'woocommerce' ),
  87. 'type' => 'array',
  88. 'context' => array( 'view' ),
  89. 'readonly' => true,
  90. 'items' => array(
  91. 'type' => 'string',
  92. ),
  93. ),
  94. 'request_body' => array(
  95. 'description' => __( 'Request body.', 'woocommerce' ),
  96. 'type' => 'string',
  97. 'context' => array( 'view' ),
  98. 'readonly' => true,
  99. ),
  100. 'response_code' => array(
  101. 'description' => __( 'The HTTP response code from the receiving server.', 'woocommerce' ),
  102. 'type' => 'string',
  103. 'context' => array( 'view' ),
  104. 'readonly' => true,
  105. ),
  106. 'response_message' => array(
  107. 'description' => __( 'The HTTP response message from the receiving server.', 'woocommerce' ),
  108. 'type' => 'string',
  109. 'context' => array( 'view' ),
  110. 'readonly' => true,
  111. ),
  112. 'response_headers' => array(
  113. 'description' => __( 'Array of the response headers from the receiving server.', 'woocommerce' ),
  114. 'type' => 'array',
  115. 'context' => array( 'view' ),
  116. 'readonly' => true,
  117. 'items' => array(
  118. 'type' => 'string',
  119. ),
  120. ),
  121. 'response_body' => array(
  122. 'description' => __( 'The response body from the receiving server.', 'woocommerce' ),
  123. 'type' => 'string',
  124. 'context' => array( 'view' ),
  125. 'readonly' => true,
  126. ),
  127. 'date_created' => array(
  128. 'description' => __( "The date the webhook delivery was logged, in the site's timezone.", 'woocommerce' ),
  129. 'type' => 'date-time',
  130. 'context' => array( 'view', 'edit' ),
  131. 'readonly' => true,
  132. ),
  133. 'date_created_gmt' => array(
  134. 'description' => __( 'The date the webhook delivery was logged, as GMT.', 'woocommerce' ),
  135. 'type' => 'date-time',
  136. 'context' => array( 'view', 'edit' ),
  137. 'readonly' => true,
  138. ),
  139. ),
  140. );
  141. return $this->add_additional_fields_schema( $schema );
  142. }
  143. }