Нет описания

class-wc-rest-webhooks-v2-controller.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * REST API Webhooks controller
  4. *
  5. * Handles requests to the /webhooks endpoint.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Webhooks controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Webhooks_V1_Controller
  16. */
  17. class WC_REST_Webhooks_V2_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_keys( wc_get_webhook_statuses() ),
  101. 'context' => array( 'view', 'edit' ),
  102. ),
  103. 'topic' => array(
  104. 'description' => __( 'Webhook topic.', 'woocommerce' ),
  105. 'type' => 'string',
  106. 'context' => array( 'view', 'edit' ),
  107. ),
  108. 'resource' => array(
  109. 'description' => __( 'Webhook resource.', 'woocommerce' ),
  110. 'type' => 'string',
  111. 'context' => array( 'view', 'edit' ),
  112. 'readonly' => true,
  113. ),
  114. 'event' => array(
  115. 'description' => __( 'Webhook event.', 'woocommerce' ),
  116. 'type' => 'string',
  117. 'context' => array( 'view', 'edit' ),
  118. 'readonly' => true,
  119. ),
  120. 'hooks' => array(
  121. 'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ),
  122. 'type' => 'array',
  123. 'context' => array( 'view', 'edit' ),
  124. 'readonly' => true,
  125. 'items' => array(
  126. 'type' => 'string',
  127. ),
  128. ),
  129. 'delivery_url' => array(
  130. 'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ),
  131. 'type' => 'string',
  132. 'format' => 'uri',
  133. 'context' => array( 'view', 'edit' ),
  134. 'readonly' => true,
  135. ),
  136. 'secret' => array(
  137. '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' ),
  138. 'type' => 'string',
  139. 'context' => array( 'edit' ),
  140. ),
  141. 'date_created' => array(
  142. 'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ),
  143. 'type' => 'date-time',
  144. 'context' => array( 'view', 'edit' ),
  145. 'readonly' => true,
  146. ),
  147. 'date_created_gmt' => array(
  148. 'description' => __( 'The date the webhook was created, as GMT.', 'woocommerce' ),
  149. 'type' => 'date-time',
  150. 'context' => array( 'view', 'edit' ),
  151. 'readonly' => true,
  152. ),
  153. 'date_modified' => array(
  154. 'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ),
  155. 'type' => 'date-time',
  156. 'context' => array( 'view', 'edit' ),
  157. 'readonly' => true,
  158. ),
  159. 'date_modified_gmt' => array(
  160. 'description' => __( 'The date the webhook was last modified, as GMT.', 'woocommerce' ),
  161. 'type' => 'date-time',
  162. 'context' => array( 'view', 'edit' ),
  163. 'readonly' => true,
  164. ),
  165. ),
  166. );
  167. return $this->add_additional_fields_schema( $schema );
  168. }
  169. }