class-wc-rest-webhooks-controller.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * REST API Webhooks controller
  4. *
  5. * Handles requests to the /webhooks endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Webhooks controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Webhooks_V1_Controller
  16. */
  17. class WC_REST_Webhooks_Controller extends WC_REST_Webhooks_V1_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Prepare a single webhook output for response.
  26. *
  27. * @param int $id Webhook ID.
  28. * @param WP_REST_Request $request Request object.
  29. * @return WP_REST_Response $response
  30. */
  31. public function prepare_item_for_response( $id, $request ) {
  32. $webhook = wc_get_webhook( $id );
  33. if ( empty( $webhook ) || is_null( $webhook ) ) {
  34. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
  35. }
  36. $data = array(
  37. 'id' => $webhook->get_id(),
  38. 'name' => $webhook->get_name(),
  39. 'status' => $webhook->get_status(),
  40. 'topic' => $webhook->get_topic(),
  41. 'resource' => $webhook->get_resource(),
  42. 'event' => $webhook->get_event(),
  43. 'hooks' => $webhook->get_hooks(),
  44. 'delivery_url' => $webhook->get_delivery_url(),
  45. 'date_created' => wc_rest_prepare_date_response( $webhook->get_date_created(), false ),
  46. 'date_created_gmt' => wc_rest_prepare_date_response( $webhook->get_date_created() ),
  47. 'date_modified' => wc_rest_prepare_date_response( $webhook->get_date_modified(), false ),
  48. 'date_modified_gmt' => wc_rest_prepare_date_response( $webhook->get_date_modified() ),
  49. );
  50. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  51. $data = $this->add_additional_fields_to_object( $data, $request );
  52. $data = $this->filter_response_by_context( $data, $context );
  53. // Wrap the data in a response object.
  54. $response = rest_ensure_response( $data );
  55. $response->add_links( $this->prepare_links( $webhook->get_id(), $request ) );
  56. /**
  57. * Filter webhook object returned from the REST API.
  58. *
  59. * @param WP_REST_Response $response The response object.
  60. * @param WC_Webhook $webhook Webhook object used to create response.
  61. * @param WP_REST_Request $request Request object.
  62. */
  63. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $webhook, $request );
  64. }
  65. /**
  66. * Get the default REST API version.
  67. *
  68. * @since 3.0.0
  69. * @return string
  70. */
  71. protected function get_default_api_version() {
  72. return 'wp_api_v2';
  73. }
  74. /**
  75. * Get the Webhook's schema, conforming to JSON Schema.
  76. *
  77. * @return array
  78. */
  79. public function get_item_schema() {
  80. $schema = array(
  81. '$schema' => 'http://json-schema.org/draft-04/schema#',
  82. 'title' => 'webhook',
  83. 'type' => 'object',
  84. 'properties' => array(
  85. 'id' => array(
  86. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  87. 'type' => 'integer',
  88. 'context' => array( 'view', 'edit' ),
  89. 'readonly' => true,
  90. ),
  91. 'name' => array(
  92. 'description' => __( 'A friendly name for the webhook.', 'woocommerce' ),
  93. 'type' => 'string',
  94. 'context' => array( 'view', 'edit' ),
  95. ),
  96. 'status' => array(
  97. 'description' => __( 'Webhook status.', 'woocommerce' ),
  98. 'type' => 'string',
  99. 'default' => 'active',
  100. 'enum' => array( 'active', 'paused', 'disabled' ),
  101. 'context' => array( 'view', 'edit' ),
  102. 'arg_options' => array(
  103. 'sanitize_callback' => 'wc_is_webhook_valid_topic',
  104. ),
  105. ),
  106. 'topic' => array(
  107. 'description' => __( 'Webhook topic.', 'woocommerce' ),
  108. 'type' => 'string',
  109. 'context' => array( 'view', 'edit' ),
  110. ),
  111. 'resource' => array(
  112. 'description' => __( 'Webhook resource.', 'woocommerce' ),
  113. 'type' => 'string',
  114. 'context' => array( 'view', 'edit' ),
  115. 'readonly' => true,
  116. ),
  117. 'event' => array(
  118. 'description' => __( 'Webhook event.', 'woocommerce' ),
  119. 'type' => 'string',
  120. 'context' => array( 'view', 'edit' ),
  121. 'readonly' => true,
  122. ),
  123. 'hooks' => array(
  124. 'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ),
  125. 'type' => 'array',
  126. 'context' => array( 'view', 'edit' ),
  127. 'readonly' => true,
  128. 'items' => array(
  129. 'type' => 'string',
  130. ),
  131. ),
  132. 'delivery_url' => array(
  133. 'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ),
  134. 'type' => 'string',
  135. 'format' => 'uri',
  136. 'context' => array( 'view', 'edit' ),
  137. 'readonly' => true,
  138. ),
  139. 'secret' => array(
  140. 'description' => __( "Secret key used to generate a hash of the delivered webhook and provided in the request headers. This will default to a MD5 hash from the current user's ID|username if not provided.", 'woocommerce' ),
  141. 'type' => 'string',
  142. 'context' => array( 'edit' ),
  143. ),
  144. 'date_created' => array(
  145. 'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ),
  146. 'type' => 'date-time',
  147. 'context' => array( 'view', 'edit' ),
  148. 'readonly' => true,
  149. ),
  150. 'date_created_gmt' => array(
  151. 'description' => __( 'The date the webhook was created, as GMT.', 'woocommerce' ),
  152. 'type' => 'date-time',
  153. 'context' => array( 'view', 'edit' ),
  154. 'readonly' => true,
  155. ),
  156. 'date_modified' => array(
  157. 'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ),
  158. 'type' => 'date-time',
  159. 'context' => array( 'view', 'edit' ),
  160. 'readonly' => true,
  161. ),
  162. 'date_modified_gmt' => array(
  163. 'description' => __( 'The date the webhook was last modified, as GMT.', 'woocommerce' ),
  164. 'type' => 'date-time',
  165. 'context' => array( 'view', 'edit' ),
  166. 'readonly' => true,
  167. ),
  168. ),
  169. );
  170. return $this->add_additional_fields_schema( $schema );
  171. }
  172. }