Нет описания

class-wc-rest-webhooks-v1-controller.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. /**
  3. * REST API Webhooks controller
  4. *
  5. * Handles requests to the /webhooks endpoint.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 3.0.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * REST API Webhooks controller class.
  15. *
  16. * @package WooCommerce\RestApi
  17. * @extends WC_REST_Controller
  18. */
  19. class WC_REST_Webhooks_V1_Controller extends WC_REST_Controller {
  20. /**
  21. * Endpoint namespace.
  22. *
  23. * @var string
  24. */
  25. protected $namespace = 'wc/v1';
  26. /**
  27. * Route base.
  28. *
  29. * @var string
  30. */
  31. protected $rest_base = 'webhooks';
  32. /**
  33. * Post type.
  34. *
  35. * @var string
  36. */
  37. protected $post_type = 'shop_webhook';
  38. /**
  39. * Register the routes for webhooks.
  40. */
  41. public function register_routes() {
  42. register_rest_route( $this->namespace, '/' . $this->rest_base, array(
  43. array(
  44. 'methods' => WP_REST_Server::READABLE,
  45. 'callback' => array( $this, 'get_items' ),
  46. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  47. 'args' => $this->get_collection_params(),
  48. ),
  49. array(
  50. 'methods' => WP_REST_Server::CREATABLE,
  51. 'callback' => array( $this, 'create_item' ),
  52. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  53. 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
  54. 'topic' => array(
  55. 'required' => true,
  56. 'type' => 'string',
  57. 'description' => __( 'Webhook topic.', 'woocommerce' ),
  58. ),
  59. 'delivery_url' => array(
  60. 'required' => true,
  61. 'type' => 'string',
  62. 'description' => __( 'Webhook delivery URL.', 'woocommerce' ),
  63. ),
  64. ) ),
  65. ),
  66. 'schema' => array( $this, 'get_public_item_schema' ),
  67. ) );
  68. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
  69. 'args' => array(
  70. 'id' => array(
  71. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  72. 'type' => 'integer',
  73. ),
  74. ),
  75. array(
  76. 'methods' => WP_REST_Server::READABLE,
  77. 'callback' => array( $this, 'get_item' ),
  78. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  79. 'args' => array(
  80. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  81. ),
  82. ),
  83. array(
  84. 'methods' => WP_REST_Server::EDITABLE,
  85. 'callback' => array( $this, 'update_item' ),
  86. 'permission_callback' => array( $this, 'update_item_permissions_check' ),
  87. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  88. ),
  89. array(
  90. 'methods' => WP_REST_Server::DELETABLE,
  91. 'callback' => array( $this, 'delete_item' ),
  92. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  93. 'args' => array(
  94. 'force' => array(
  95. 'default' => false,
  96. 'type' => 'boolean',
  97. 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
  98. ),
  99. ),
  100. ),
  101. 'schema' => array( $this, 'get_public_item_schema' ),
  102. ) );
  103. register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
  104. array(
  105. 'methods' => WP_REST_Server::EDITABLE,
  106. 'callback' => array( $this, 'batch_items' ),
  107. 'permission_callback' => array( $this, 'batch_items_permissions_check' ),
  108. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  109. ),
  110. 'schema' => array( $this, 'get_public_batch_schema' ),
  111. ) );
  112. }
  113. /**
  114. * Check whether a given request has permission to read webhooks.
  115. *
  116. * @param WP_REST_Request $request Full details about the request.
  117. * @return WP_Error|boolean
  118. */
  119. public function get_items_permissions_check( $request ) {
  120. if ( ! wc_rest_check_manager_permissions( 'webhooks', 'read' ) ) {
  121. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  122. }
  123. return true;
  124. }
  125. /**
  126. * Check if a given request has access create webhooks.
  127. *
  128. * @param WP_REST_Request $request Full details about the request.
  129. *
  130. * @return bool|WP_Error
  131. */
  132. public function create_item_permissions_check( $request ) {
  133. if ( ! wc_rest_check_manager_permissions( 'webhooks', 'create' ) ) {
  134. return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  135. }
  136. return true;
  137. }
  138. /**
  139. * Check if a given request has access to read a webhook.
  140. *
  141. * @param WP_REST_Request $request Full details about the request.
  142. * @return WP_Error|boolean
  143. */
  144. public function get_item_permissions_check( $request ) {
  145. if ( ! wc_rest_check_manager_permissions( 'webhooks', 'read' ) ) {
  146. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  147. }
  148. return true;
  149. }
  150. /**
  151. * Check if a given request has access update a webhook.
  152. *
  153. * @param WP_REST_Request $request Full details about the request.
  154. *
  155. * @return bool|WP_Error
  156. */
  157. public function update_item_permissions_check( $request ) {
  158. if ( ! wc_rest_check_manager_permissions( 'webhooks', 'edit' ) ) {
  159. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  160. }
  161. return true;
  162. }
  163. /**
  164. * Check if a given request has access delete a webhook.
  165. *
  166. * @param WP_REST_Request $request Full details about the request.
  167. *
  168. * @return bool|WP_Error
  169. */
  170. public function delete_item_permissions_check( $request ) {
  171. if ( ! wc_rest_check_manager_permissions( 'webhooks', 'delete' ) ) {
  172. return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  173. }
  174. return true;
  175. }
  176. /**
  177. * Check if a given request has access batch create, update and delete items.
  178. *
  179. * @param WP_REST_Request $request Full details about the request.
  180. *
  181. * @return bool|WP_Error
  182. */
  183. public function batch_items_permissions_check( $request ) {
  184. if ( ! wc_rest_check_manager_permissions( 'webhooks', 'batch' ) ) {
  185. return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  186. }
  187. return true;
  188. }
  189. /**
  190. * Get the default REST API version.
  191. *
  192. * @since 3.0.0
  193. * @return string
  194. */
  195. protected function get_default_api_version() {
  196. return 'wp_api_v1';
  197. }
  198. /**
  199. * Get all webhooks.
  200. *
  201. * @param WP_REST_Request $request Full details about the request.
  202. * @return WP_Error|WP_REST_Response
  203. */
  204. public function get_items( $request ) {
  205. $args = array();
  206. $args['order'] = $request['order'];
  207. $args['orderby'] = $request['orderby'];
  208. $args['status'] = 'all' === $request['status'] ? '' : $request['status'];
  209. $args['include'] = implode( ',', $request['include'] );
  210. $args['exclude'] = implode( ',', $request['exclude'] );
  211. $args['limit'] = $request['per_page'];
  212. $args['search'] = $request['search'];
  213. $args['before'] = $request['before'];
  214. $args['after'] = $request['after'];
  215. if ( empty( $request['offset'] ) ) {
  216. $args['offset'] = 1 < $request['page'] ? ( $request['page'] - 1 ) * $args['limit'] : 0;
  217. }
  218. /**
  219. * Filter arguments, before passing to WC_Webhook_Data_Store->search_webhooks, when querying webhooks via the REST API.
  220. *
  221. * @param array $args Array of arguments for $wpdb->get_results().
  222. * @param WP_REST_Request $request The current request.
  223. */
  224. $prepared_args = apply_filters( 'woocommerce_rest_webhook_query', $args, $request );
  225. unset( $prepared_args['page'] );
  226. $prepared_args['paginate'] = true;
  227. // Get the webhooks.
  228. $webhooks = array();
  229. $data_store = WC_Data_Store::load( 'webhook' );
  230. $results = $data_store->search_webhooks( $prepared_args );
  231. $webhook_ids = $results->webhooks;
  232. foreach ( $webhook_ids as $webhook_id ) {
  233. $data = $this->prepare_item_for_response( $webhook_id, $request );
  234. $webhooks[] = $this->prepare_response_for_collection( $data );
  235. }
  236. $response = rest_ensure_response( $webhooks );
  237. $per_page = (int) $prepared_args['limit'];
  238. $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
  239. $total_webhooks = $results->total;
  240. $max_pages = $results->max_num_pages;
  241. $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
  242. $response->header( 'X-WP-Total', $total_webhooks );
  243. $response->header( 'X-WP-TotalPages', $max_pages );
  244. if ( $page > 1 ) {
  245. $prev_page = $page - 1;
  246. if ( $prev_page > $max_pages ) {
  247. $prev_page = $max_pages;
  248. }
  249. $prev_link = add_query_arg( 'page', $prev_page, $base );
  250. $response->link_header( 'prev', $prev_link );
  251. }
  252. if ( $max_pages > $page ) {
  253. $next_page = $page + 1;
  254. $next_link = add_query_arg( 'page', $next_page, $base );
  255. $response->link_header( 'next', $next_link );
  256. }
  257. return $response;
  258. }
  259. /**
  260. * Get a single item.
  261. *
  262. * @param WP_REST_Request $request Full details about the request.
  263. * @return WP_Error|WP_REST_Response
  264. */
  265. public function get_item( $request ) {
  266. $id = (int) $request['id'];
  267. if ( empty( $id ) ) {
  268. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
  269. }
  270. $data = $this->prepare_item_for_response( $id, $request );
  271. $response = rest_ensure_response( $data );
  272. return $response;
  273. }
  274. /**
  275. * Create a single webhook.
  276. *
  277. * @param WP_REST_Request $request Full details about the request.
  278. * @return WP_Error|WP_REST_Response
  279. */
  280. public function create_item( $request ) {
  281. if ( ! empty( $request['id'] ) ) {
  282. /* translators: %s: post type */
  283. return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
  284. }
  285. // Validate topic.
  286. if ( empty( $request['topic'] ) || ! wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
  287. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic is required and must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
  288. }
  289. // Validate delivery URL.
  290. if ( empty( $request['delivery_url'] ) || ! wc_is_valid_url( $request['delivery_url'] ) ) {
  291. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
  292. }
  293. $post = $this->prepare_item_for_database( $request );
  294. if ( is_wp_error( $post ) ) {
  295. return $post;
  296. }
  297. $webhook = new WC_Webhook();
  298. $webhook->set_name( $post->post_title );
  299. $webhook->set_user_id( $post->post_author );
  300. $webhook->set_status( 'publish' === $post->post_status ? 'active' : 'disabled' );
  301. $webhook->set_topic( $request['topic'] );
  302. $webhook->set_delivery_url( $request['delivery_url'] );
  303. $webhook->set_secret( ! empty( $request['secret'] ) ? $request['secret'] : wp_generate_password( 50, true, true ) );
  304. $webhook->set_api_version( $this->get_default_api_version() );
  305. $webhook->save();
  306. $this->update_additional_fields_for_object( $webhook, $request );
  307. /**
  308. * Fires after a single item is created or updated via the REST API.
  309. *
  310. * @param WC_Webhook $webhook Webhook data.
  311. * @param WP_REST_Request $request Request object.
  312. * @param bool $creating True when creating item, false when updating.
  313. */
  314. do_action( "woocommerce_rest_insert_webhook_object", $webhook, $request, true );
  315. $request->set_param( 'context', 'edit' );
  316. $response = $this->prepare_item_for_response( $webhook->get_id(), $request );
  317. $response = rest_ensure_response( $response );
  318. $response->set_status( 201 );
  319. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $webhook->get_id() ) ) );
  320. // Send ping.
  321. $webhook->deliver_ping();
  322. return $response;
  323. }
  324. /**
  325. * Update a single webhook.
  326. *
  327. * @param WP_REST_Request $request Full details about the request.
  328. * @return WP_Error|WP_REST_Response
  329. */
  330. public function update_item( $request ) {
  331. $id = (int) $request['id'];
  332. $webhook = wc_get_webhook( $id );
  333. if ( empty( $webhook ) || is_null( $webhook ) ) {
  334. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
  335. }
  336. // Update topic.
  337. if ( ! empty( $request['topic'] ) ) {
  338. if ( wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
  339. $webhook->set_topic( $request['topic'] );
  340. } else {
  341. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
  342. }
  343. }
  344. // Update delivery URL.
  345. if ( ! empty( $request['delivery_url'] ) ) {
  346. if ( wc_is_valid_url( $request['delivery_url'] ) ) {
  347. $webhook->set_delivery_url( $request['delivery_url'] );
  348. } else {
  349. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
  350. }
  351. }
  352. // Update secret.
  353. if ( ! empty( $request['secret'] ) ) {
  354. $webhook->set_secret( $request['secret'] );
  355. }
  356. // Update status.
  357. if ( ! empty( $request['status'] ) ) {
  358. if ( wc_is_webhook_valid_status( strtolower( $request['status'] ) ) ) {
  359. $webhook->set_status( $request['status'] );
  360. } else {
  361. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_status", __( 'Webhook status must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
  362. }
  363. }
  364. $post = $this->prepare_item_for_database( $request );
  365. if ( is_wp_error( $post ) ) {
  366. return $post;
  367. }
  368. if ( isset( $post->post_title ) ) {
  369. $webhook->set_name( $post->post_title );
  370. }
  371. $webhook->save();
  372. $this->update_additional_fields_for_object( $webhook, $request );
  373. /**
  374. * Fires after a single item is created or updated via the REST API.
  375. *
  376. * @param WC_Webhook $webhook Webhook data.
  377. * @param WP_REST_Request $request Request object.
  378. * @param bool $creating True when creating item, false when updating.
  379. */
  380. do_action( "woocommerce_rest_insert_webhook_object", $webhook, $request, false );
  381. $request->set_param( 'context', 'edit' );
  382. $response = $this->prepare_item_for_response( $webhook->get_id(), $request );
  383. return rest_ensure_response( $response );
  384. }
  385. /**
  386. * Delete a single webhook.
  387. *
  388. * @param WP_REST_Request $request Full details about the request.
  389. * @return WP_REST_Response|WP_Error
  390. */
  391. public function delete_item( $request ) {
  392. $id = (int) $request['id'];
  393. $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
  394. // We don't support trashing for this type, error out.
  395. if ( ! $force ) {
  396. return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Webhooks do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
  397. }
  398. $webhook = wc_get_webhook( $id );
  399. if ( empty( $webhook ) || is_null( $webhook ) ) {
  400. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
  401. }
  402. $request->set_param( 'context', 'edit' );
  403. $response = $this->prepare_item_for_response( $webhook, $request );
  404. $result = $webhook->delete( true );
  405. if ( ! $result ) {
  406. /* translators: %s: post type */
  407. return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
  408. }
  409. /**
  410. * Fires after a single item is deleted or trashed via the REST API.
  411. *
  412. * @param WC_Webhook $webhook The deleted or trashed item.
  413. * @param WP_REST_Response $response The response data.
  414. * @param WP_REST_Request $request The request sent to the API.
  415. */
  416. do_action( "woocommerce_rest_delete_webhook_object", $webhook, $response, $request );
  417. return $response;
  418. }
  419. /**
  420. * Prepare a single webhook for create or update.
  421. *
  422. * @param WP_REST_Request $request Request object.
  423. * @return WP_Error|stdClass $data Post object.
  424. */
  425. protected function prepare_item_for_database( $request ) {
  426. $data = new stdClass;
  427. // Post ID.
  428. if ( isset( $request['id'] ) ) {
  429. $data->ID = absint( $request['id'] );
  430. }
  431. // Validate required POST fields.
  432. if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
  433. $data->post_title = ! empty( $request['name'] ) ? $request['name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ); // @codingStandardsIgnoreLine
  434. // Post author.
  435. $data->post_author = get_current_user_id();
  436. // Post password.
  437. $data->post_password = 'webhook_' . wp_generate_password();
  438. // Post status.
  439. $data->post_status = 'publish';
  440. } else {
  441. // Allow edit post title.
  442. if ( ! empty( $request['name'] ) ) {
  443. $data->post_title = $request['name'];
  444. }
  445. }
  446. // Comment status.
  447. $data->comment_status = 'closed';
  448. // Ping status.
  449. $data->ping_status = 'closed';
  450. /**
  451. * Filter the query_vars used in `get_items` for the constructed query.
  452. *
  453. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
  454. * prepared for insertion.
  455. *
  456. * @param stdClass $data An object representing a single item prepared
  457. * for inserting or updating the database.
  458. * @param WP_REST_Request $request Request object.
  459. */
  460. return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
  461. }
  462. /**
  463. * Prepare a single webhook output for response.
  464. *
  465. * @param int $id Webhook ID or object.
  466. * @param WP_REST_Request $request Request object.
  467. * @return WP_REST_Response $response Response data.
  468. */
  469. public function prepare_item_for_response( $id, $request ) {
  470. $webhook = wc_get_webhook( $id );
  471. if ( empty( $webhook ) || is_null( $webhook ) ) {
  472. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
  473. }
  474. $data = array(
  475. 'id' => $webhook->get_id(),
  476. 'name' => $webhook->get_name(),
  477. 'status' => $webhook->get_status(),
  478. 'topic' => $webhook->get_topic(),
  479. 'resource' => $webhook->get_resource(),
  480. 'event' => $webhook->get_event(),
  481. 'hooks' => $webhook->get_hooks(),
  482. 'delivery_url' => $webhook->get_delivery_url(),
  483. 'date_created' => wc_rest_prepare_date_response( $webhook->get_date_created() ),
  484. 'date_modified' => wc_rest_prepare_date_response( $webhook->get_date_modified() ),
  485. );
  486. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  487. $data = $this->add_additional_fields_to_object( $data, $request );
  488. $data = $this->filter_response_by_context( $data, $context );
  489. // Wrap the data in a response object.
  490. $response = rest_ensure_response( $data );
  491. $response->add_links( $this->prepare_links( $webhook->get_id() ) );
  492. /**
  493. * Filter webhook object returned from the REST API.
  494. *
  495. * @param WP_REST_Response $response The response object.
  496. * @param WC_Webhook $webhook Webhook object used to create response.
  497. * @param WP_REST_Request $request Request object.
  498. */
  499. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $webhook, $request );
  500. }
  501. /**
  502. * Prepare links for the request.
  503. *
  504. * @param int $id Webhook ID.
  505. * @return array
  506. */
  507. protected function prepare_links( $id ) {
  508. $links = array(
  509. 'self' => array(
  510. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ),
  511. ),
  512. 'collection' => array(
  513. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
  514. ),
  515. );
  516. return $links;
  517. }
  518. /**
  519. * Get the Webhook's schema, conforming to JSON Schema.
  520. *
  521. * @return array
  522. */
  523. public function get_item_schema() {
  524. $schema = array(
  525. '$schema' => 'http://json-schema.org/draft-04/schema#',
  526. 'title' => 'webhook',
  527. 'type' => 'object',
  528. 'properties' => array(
  529. 'id' => array(
  530. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  531. 'type' => 'integer',
  532. 'context' => array( 'view', 'edit' ),
  533. 'readonly' => true,
  534. ),
  535. 'name' => array(
  536. 'description' => __( 'A friendly name for the webhook.', 'woocommerce' ),
  537. 'type' => 'string',
  538. 'context' => array( 'view', 'edit' ),
  539. ),
  540. 'status' => array(
  541. 'description' => __( 'Webhook status.', 'woocommerce' ),
  542. 'type' => 'string',
  543. 'default' => 'active',
  544. 'enum' => array_keys( wc_get_webhook_statuses() ),
  545. 'context' => array( 'view', 'edit' ),
  546. ),
  547. 'topic' => array(
  548. 'description' => __( 'Webhook topic.', 'woocommerce' ),
  549. 'type' => 'string',
  550. 'context' => array( 'view', 'edit' ),
  551. ),
  552. 'resource' => array(
  553. 'description' => __( 'Webhook resource.', 'woocommerce' ),
  554. 'type' => 'string',
  555. 'context' => array( 'view', 'edit' ),
  556. 'readonly' => true,
  557. ),
  558. 'event' => array(
  559. 'description' => __( 'Webhook event.', 'woocommerce' ),
  560. 'type' => 'string',
  561. 'context' => array( 'view', 'edit' ),
  562. 'readonly' => true,
  563. ),
  564. 'hooks' => array(
  565. 'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ),
  566. 'type' => 'array',
  567. 'context' => array( 'view', 'edit' ),
  568. 'readonly' => true,
  569. 'items' => array(
  570. 'type' => 'string',
  571. ),
  572. ),
  573. 'delivery_url' => array(
  574. 'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ),
  575. 'type' => 'string',
  576. 'format' => 'uri',
  577. 'context' => array( 'view', 'edit' ),
  578. 'readonly' => true,
  579. ),
  580. 'secret' => array(
  581. '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' ),
  582. 'type' => 'string',
  583. 'context' => array( 'edit' ),
  584. ),
  585. 'date_created' => array(
  586. 'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ),
  587. 'type' => 'date-time',
  588. 'context' => array( 'view', 'edit' ),
  589. 'readonly' => true,
  590. ),
  591. 'date_modified' => array(
  592. 'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ),
  593. 'type' => 'date-time',
  594. 'context' => array( 'view', 'edit' ),
  595. 'readonly' => true,
  596. ),
  597. ),
  598. );
  599. return $this->add_additional_fields_schema( $schema );
  600. }
  601. /**
  602. * Get the query params for collections of attachments.
  603. *
  604. * @return array
  605. */
  606. public function get_collection_params() {
  607. $params = parent::get_collection_params();
  608. $params['context']['default'] = 'view';
  609. $params['after'] = array(
  610. 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
  611. 'type' => 'string',
  612. 'format' => 'date-time',
  613. 'validate_callback' => 'rest_validate_request_arg',
  614. );
  615. $params['before'] = array(
  616. 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
  617. 'type' => 'string',
  618. 'format' => 'date-time',
  619. 'validate_callback' => 'rest_validate_request_arg',
  620. );
  621. $params['exclude'] = array(
  622. 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
  623. 'type' => 'array',
  624. 'items' => array(
  625. 'type' => 'integer',
  626. ),
  627. 'default' => array(),
  628. 'sanitize_callback' => 'wp_parse_id_list',
  629. );
  630. $params['include'] = array(
  631. 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ),
  632. 'type' => 'array',
  633. 'items' => array(
  634. 'type' => 'integer',
  635. ),
  636. 'default' => array(),
  637. 'sanitize_callback' => 'wp_parse_id_list',
  638. );
  639. $params['offset'] = array(
  640. 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
  641. 'type' => 'integer',
  642. 'sanitize_callback' => 'absint',
  643. 'validate_callback' => 'rest_validate_request_arg',
  644. );
  645. $params['order'] = array(
  646. 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
  647. 'type' => 'string',
  648. 'default' => 'desc',
  649. 'enum' => array( 'asc', 'desc' ),
  650. 'validate_callback' => 'rest_validate_request_arg',
  651. );
  652. $params['orderby'] = array(
  653. 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
  654. 'type' => 'string',
  655. 'default' => 'date',
  656. 'enum' => array(
  657. 'date',
  658. 'id',
  659. 'title',
  660. ),
  661. 'validate_callback' => 'rest_validate_request_arg',
  662. );
  663. $params['status'] = array(
  664. 'default' => 'all',
  665. 'description' => __( 'Limit result set to webhooks assigned a specific status.', 'woocommerce' ),
  666. 'type' => 'string',
  667. 'enum' => array( 'all', 'active', 'paused', 'disabled' ),
  668. 'sanitize_callback' => 'sanitize_key',
  669. 'validate_callback' => 'rest_validate_request_arg',
  670. );
  671. return $params;
  672. }
  673. }