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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <?php
  2. /**
  3. * REST API Webhooks controller
  4. *
  5. * Handles requests to the /webhooks endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 3.0.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * REST API Webhooks controller class.
  15. *
  16. * @package WooCommerce/API
  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. // Get the webhooks.
  227. $data_store = WC_Data_Store::load( 'webhook' );
  228. $results = $data_store->search_webhooks( $prepared_args );
  229. $webhooks = array();
  230. foreach ( $results as $webhook_id ) {
  231. $data = $this->prepare_item_for_response( $webhook_id, $request );
  232. $webhooks[] = $this->prepare_response_for_collection( $data );
  233. }
  234. $response = rest_ensure_response( $webhooks );
  235. // Store pagination values for headers then unset for count query.
  236. $per_page = (int) $prepared_args['limit'];
  237. $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
  238. // Calculate totals.
  239. $prepared_args['limit'] = -1;
  240. $prepared_args['offset'] = 0;
  241. $total_webhooks = count( $data_store->search_webhooks( $prepared_args ) );
  242. $response->header( 'X-WP-Total', (int) $total_webhooks );
  243. $max_pages = ceil( $total_webhooks / $per_page );
  244. $response->header( 'X-WP-TotalPages', (int) $max_pages );
  245. $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
  246. if ( $page > 1 ) {
  247. $prev_page = $page - 1;
  248. if ( $prev_page > $max_pages ) {
  249. $prev_page = $max_pages;
  250. }
  251. $prev_link = add_query_arg( 'page', $prev_page, $base );
  252. $response->link_header( 'prev', $prev_link );
  253. }
  254. if ( $max_pages > $page ) {
  255. $next_page = $page + 1;
  256. $next_link = add_query_arg( 'page', $next_page, $base );
  257. $response->link_header( 'next', $next_link );
  258. }
  259. return $response;
  260. }
  261. /**
  262. * Get a single item.
  263. *
  264. * @param WP_REST_Request $request Full details about the request.
  265. * @return WP_Error|WP_REST_Response
  266. */
  267. public function get_item( $request ) {
  268. $id = (int) $request['id'];
  269. if ( empty( $id ) ) {
  270. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
  271. }
  272. $data = $this->prepare_item_for_response( $id, $request );
  273. $response = rest_ensure_response( $data );
  274. return $response;
  275. }
  276. /**
  277. * Create a single webhook.
  278. *
  279. * @param WP_REST_Request $request Full details about the request.
  280. * @return WP_Error|WP_REST_Response
  281. */
  282. public function create_item( $request ) {
  283. if ( ! empty( $request['id'] ) ) {
  284. /* translators: %s: post type */
  285. return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
  286. }
  287. // Validate topic.
  288. if ( empty( $request['topic'] ) || ! wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
  289. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic is required and must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
  290. }
  291. // Validate delivery URL.
  292. if ( empty( $request['delivery_url'] ) || ! wc_is_valid_url( $request['delivery_url'] ) ) {
  293. 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 ) );
  294. }
  295. $post = $this->prepare_item_for_database( $request );
  296. if ( is_wp_error( $post ) ) {
  297. return $post;
  298. }
  299. $webhook = new WC_Webhook();
  300. $webhook->set_name( $post->post_title );
  301. $webhook->set_user_id( $post->post_author );
  302. $webhook->set_status( 'publish' === $post->post_status ? 'active' : 'disabled' );
  303. $webhook->set_topic( $request['topic'] );
  304. $webhook->set_delivery_url( $request['delivery_url'] );
  305. $webhook->set_secret( ! empty( $request['secret'] ) ? $request['secret'] : wp_generate_password( 50, true, true ) );
  306. $webhook->set_api_version( $this->get_default_api_version() );
  307. $webhook->save();
  308. $this->update_additional_fields_for_object( $webhook, $request );
  309. /**
  310. * Fires after a single item is created or updated via the REST API.
  311. *
  312. * @param WC_Webhook $webhook Webhook data.
  313. * @param WP_REST_Request $request Request object.
  314. * @param bool $creating True when creating item, false when updating.
  315. */
  316. do_action( "woocommerce_rest_insert_webhook_object", $webhook, $request, true );
  317. $request->set_param( 'context', 'edit' );
  318. $response = $this->prepare_item_for_response( $webhook->get_id(), $request );
  319. $response = rest_ensure_response( $response );
  320. $response->set_status( 201 );
  321. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $webhook->get_id() ) ) );
  322. // Send ping.
  323. $webhook->deliver_ping();
  324. return $response;
  325. }
  326. /**
  327. * Update a single webhook.
  328. *
  329. * @param WP_REST_Request $request Full details about the request.
  330. * @return WP_Error|WP_REST_Response
  331. */
  332. public function update_item( $request ) {
  333. $id = (int) $request['id'];
  334. $webhook = wc_get_webhook( $id );
  335. if ( empty( $webhook ) || is_null( $webhook ) ) {
  336. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
  337. }
  338. // Update topic.
  339. if ( ! empty( $request['topic'] ) ) {
  340. if ( wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
  341. $webhook->set_topic( $request['topic'] );
  342. } else {
  343. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
  344. }
  345. }
  346. // Update delivery URL.
  347. if ( ! empty( $request['delivery_url'] ) ) {
  348. if ( wc_is_valid_url( $request['delivery_url'] ) ) {
  349. $webhook->set_delivery_url( $request['delivery_url'] );
  350. } else {
  351. 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 ) );
  352. }
  353. }
  354. // Update secret.
  355. if ( ! empty( $request['secret'] ) ) {
  356. $webhook->set_secret( $request['secret'] );
  357. }
  358. // Update status.
  359. if ( ! empty( $request['status'] ) ) {
  360. $webhook->set_status( $request['status'] );
  361. }
  362. $post = $this->prepare_item_for_database( $request );
  363. if ( is_wp_error( $post ) ) {
  364. return $post;
  365. }
  366. if ( isset( $post->post_title ) ) {
  367. $webhook->set_name( $post->post_title );
  368. }
  369. $webhook->save();
  370. $this->update_additional_fields_for_object( $webhook, $request );
  371. /**
  372. * Fires after a single item is created or updated via the REST API.
  373. *
  374. * @param WC_Webhook $webhook Webhook data.
  375. * @param WP_REST_Request $request Request object.
  376. * @param bool $creating True when creating item, false when updating.
  377. */
  378. do_action( "woocommerce_rest_insert_webhook_object", $webhook, $request, false );
  379. $request->set_param( 'context', 'edit' );
  380. $response = $this->prepare_item_for_response( $webhook->get_id(), $request );
  381. return rest_ensure_response( $response );
  382. }
  383. /**
  384. * Delete a single webhook.
  385. *
  386. * @param WP_REST_Request $request Full details about the request.
  387. * @return WP_REST_Response|WP_Error
  388. */
  389. public function delete_item( $request ) {
  390. $id = (int) $request['id'];
  391. $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
  392. // We don't support trashing for this type, error out.
  393. if ( ! $force ) {
  394. return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Webhooks do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
  395. }
  396. $webhook = wc_get_webhook( $id );
  397. if ( empty( $webhook ) || is_null( $webhook ) ) {
  398. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
  399. }
  400. $request->set_param( 'context', 'edit' );
  401. $response = $this->prepare_item_for_response( $webhook, $request );
  402. $result = $webhook->delete( true );
  403. if ( ! $result ) {
  404. /* translators: %s: post type */
  405. return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
  406. }
  407. /**
  408. * Fires after a single item is deleted or trashed via the REST API.
  409. *
  410. * @param WC_Webhook $webhook The deleted or trashed item.
  411. * @param WP_REST_Response $response The response data.
  412. * @param WP_REST_Request $request The request sent to the API.
  413. */
  414. do_action( "woocommerce_rest_delete_webhook_object", $webhook, $response, $request );
  415. return $response;
  416. }
  417. /**
  418. * Prepare a single webhook for create or update.
  419. *
  420. * @param WP_REST_Request $request Request object.
  421. * @return WP_Error|stdClass $data Post object.
  422. */
  423. protected function prepare_item_for_database( $request ) {
  424. $data = new stdClass;
  425. // Post ID.
  426. if ( isset( $request['id'] ) ) {
  427. $data->ID = absint( $request['id'] );
  428. }
  429. // Validate required POST fields.
  430. if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
  431. $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
  432. // Post author.
  433. $data->post_author = get_current_user_id();
  434. // Post password.
  435. $password = strlen( uniqid( 'webhook_' ) );
  436. $data->post_password = $password > 20 ? substr( $password, 0, 20 ) : $password;
  437. // Post status.
  438. $data->post_status = 'publish';
  439. } else {
  440. // Allow edit post title.
  441. if ( ! empty( $request['name'] ) ) {
  442. $data->post_title = $request['name'];
  443. }
  444. }
  445. // Comment status.
  446. $data->comment_status = 'closed';
  447. // Ping status.
  448. $data->ping_status = 'closed';
  449. /**
  450. * Filter the query_vars used in `get_items` for the constructed query.
  451. *
  452. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
  453. * prepared for insertion.
  454. *
  455. * @param stdClass $data An object representing a single item prepared
  456. * for inserting or updating the database.
  457. * @param WP_REST_Request $request Request object.
  458. */
  459. return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
  460. }
  461. /**
  462. * Prepare a single webhook output for response.
  463. *
  464. * @param int $id Webhook ID or object.
  465. * @param WP_REST_Request $request Request object.
  466. * @return WP_REST_Response $response Response data.
  467. */
  468. public function prepare_item_for_response( $id, $request ) {
  469. $webhook = wc_get_webhook( $id );
  470. if ( empty( $webhook ) || is_null( $webhook ) ) {
  471. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
  472. }
  473. $data = array(
  474. 'id' => $webhook->get_id(),
  475. 'name' => $webhook->get_name(),
  476. 'status' => $webhook->get_status(),
  477. 'topic' => $webhook->get_topic(),
  478. 'resource' => $webhook->get_resource(),
  479. 'event' => $webhook->get_event(),
  480. 'hooks' => $webhook->get_hooks(),
  481. 'delivery_url' => $webhook->get_delivery_url(),
  482. 'date_created' => wc_rest_prepare_date_response( $webhook->get_date_created() ),
  483. 'date_modified' => wc_rest_prepare_date_response( $webhook->get_date_modified() ),
  484. );
  485. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  486. $data = $this->add_additional_fields_to_object( $data, $request );
  487. $data = $this->filter_response_by_context( $data, $context );
  488. // Wrap the data in a response object.
  489. $response = rest_ensure_response( $data );
  490. $response->add_links( $this->prepare_links( $webhook->get_id() ) );
  491. /**
  492. * Filter webhook object returned from the REST API.
  493. *
  494. * @param WP_REST_Response $response The response object.
  495. * @param WC_Webhook $webhook Webhook object used to create response.
  496. * @param WP_REST_Request $request Request object.
  497. */
  498. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $webhook, $request );
  499. }
  500. /**
  501. * Prepare links for the request.
  502. *
  503. * @param int $id Webhook ID.
  504. * @return array
  505. */
  506. protected function prepare_links( $id ) {
  507. $links = array(
  508. 'self' => array(
  509. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ),
  510. ),
  511. 'collection' => array(
  512. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
  513. ),
  514. );
  515. return $links;
  516. }
  517. /**
  518. * Get the Webhook's schema, conforming to JSON Schema.
  519. *
  520. * @return array
  521. */
  522. public function get_item_schema() {
  523. $schema = array(
  524. '$schema' => 'http://json-schema.org/draft-04/schema#',
  525. 'title' => 'webhook',
  526. 'type' => 'object',
  527. 'properties' => array(
  528. 'id' => array(
  529. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  530. 'type' => 'integer',
  531. 'context' => array( 'view', 'edit' ),
  532. 'readonly' => true,
  533. ),
  534. 'name' => array(
  535. 'description' => __( 'A friendly name for the webhook.', 'woocommerce' ),
  536. 'type' => 'string',
  537. 'context' => array( 'view', 'edit' ),
  538. ),
  539. 'status' => array(
  540. 'description' => __( 'Webhook status.', 'woocommerce' ),
  541. 'type' => 'string',
  542. 'default' => 'active',
  543. 'enum' => array( 'active', 'paused', 'disabled' ),
  544. 'context' => array( 'view', 'edit' ),
  545. 'arg_options' => array(
  546. 'sanitize_callback' => 'wc_is_webhook_valid_topic',
  547. ),
  548. ),
  549. 'topic' => array(
  550. 'description' => __( 'Webhook topic.', 'woocommerce' ),
  551. 'type' => 'string',
  552. 'context' => array( 'view', 'edit' ),
  553. ),
  554. 'resource' => array(
  555. 'description' => __( 'Webhook resource.', 'woocommerce' ),
  556. 'type' => 'string',
  557. 'context' => array( 'view', 'edit' ),
  558. 'readonly' => true,
  559. ),
  560. 'event' => array(
  561. 'description' => __( 'Webhook event.', 'woocommerce' ),
  562. 'type' => 'string',
  563. 'context' => array( 'view', 'edit' ),
  564. 'readonly' => true,
  565. ),
  566. 'hooks' => array(
  567. 'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ),
  568. 'type' => 'array',
  569. 'context' => array( 'view', 'edit' ),
  570. 'readonly' => true,
  571. 'items' => array(
  572. 'type' => 'string',
  573. ),
  574. ),
  575. 'delivery_url' => array(
  576. 'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ),
  577. 'type' => 'string',
  578. 'format' => 'uri',
  579. 'context' => array( 'view', 'edit' ),
  580. 'readonly' => true,
  581. ),
  582. 'secret' => array(
  583. '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' ),
  584. 'type' => 'string',
  585. 'context' => array( 'edit' ),
  586. ),
  587. 'date_created' => array(
  588. 'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ),
  589. 'type' => 'date-time',
  590. 'context' => array( 'view', 'edit' ),
  591. 'readonly' => true,
  592. ),
  593. 'date_modified' => array(
  594. 'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ),
  595. 'type' => 'date-time',
  596. 'context' => array( 'view', 'edit' ),
  597. 'readonly' => true,
  598. ),
  599. ),
  600. );
  601. return $this->add_additional_fields_schema( $schema );
  602. }
  603. /**
  604. * Get the query params for collections of attachments.
  605. *
  606. * @return array
  607. */
  608. public function get_collection_params() {
  609. $params = parent::get_collection_params();
  610. $params['context']['default'] = 'view';
  611. $params['after'] = array(
  612. 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
  613. 'type' => 'string',
  614. 'format' => 'date-time',
  615. 'validate_callback' => 'rest_validate_request_arg',
  616. );
  617. $params['before'] = array(
  618. 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
  619. 'type' => 'string',
  620. 'format' => 'date-time',
  621. 'validate_callback' => 'rest_validate_request_arg',
  622. );
  623. $params['exclude'] = array(
  624. 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
  625. 'type' => 'array',
  626. 'items' => array(
  627. 'type' => 'integer',
  628. ),
  629. 'default' => array(),
  630. 'sanitize_callback' => 'wp_parse_id_list',
  631. );
  632. $params['include'] = array(
  633. 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ),
  634. 'type' => 'array',
  635. 'items' => array(
  636. 'type' => 'integer',
  637. ),
  638. 'default' => array(),
  639. 'sanitize_callback' => 'wp_parse_id_list',
  640. );
  641. $params['offset'] = array(
  642. 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
  643. 'type' => 'integer',
  644. 'sanitize_callback' => 'absint',
  645. 'validate_callback' => 'rest_validate_request_arg',
  646. );
  647. $params['order'] = array(
  648. 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
  649. 'type' => 'string',
  650. 'default' => 'desc',
  651. 'enum' => array( 'asc', 'desc' ),
  652. 'validate_callback' => 'rest_validate_request_arg',
  653. );
  654. $params['orderby'] = array(
  655. 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
  656. 'type' => 'string',
  657. 'default' => 'date',
  658. 'enum' => array(
  659. 'date',
  660. 'id',
  661. 'title',
  662. ),
  663. 'validate_callback' => 'rest_validate_request_arg',
  664. );
  665. $params['status'] = array(
  666. 'default' => 'all',
  667. 'description' => __( 'Limit result set to webhooks assigned a specific status.', 'woocommerce' ),
  668. 'type' => 'string',
  669. 'enum' => array( 'all', 'active', 'paused', 'disabled' ),
  670. 'sanitize_callback' => 'sanitize_key',
  671. 'validate_callback' => 'rest_validate_request_arg',
  672. );
  673. return $params;
  674. }
  675. }