WC_REST_Webhooks_Controller

REST API Webhooks controller class.

Defined (1)

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

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