Bez popisu

wc-webhook-functions.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * WooCommerce Webhook functions
  4. *
  5. * @package WooCommerce\Functions
  6. * @version 3.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Process the web hooks at the end of the request.
  11. *
  12. * @since 4.4.0
  13. */
  14. function wc_webhook_execute_queue() {
  15. global $wc_queued_webhooks;
  16. if ( empty( $wc_queued_webhooks ) ) {
  17. return;
  18. }
  19. foreach ( $wc_queued_webhooks as $data ) {
  20. // Webhooks are processed in the background by default
  21. // so as to avoid delays or failures in delivery from affecting the
  22. // user who triggered it.
  23. if ( apply_filters( 'woocommerce_webhook_deliver_async', true, $data['webhook'], $data['arg'] ) ) {
  24. $queue_args = array(
  25. 'webhook_id' => $data['webhook']->get_id(),
  26. 'arg' => $data['arg'],
  27. );
  28. $next_scheduled_date = WC()->queue()->get_next( 'woocommerce_deliver_webhook_async', $queue_args, 'woocommerce-webhooks' );
  29. // Make webhooks unique - only schedule one webhook every 10 minutes to maintain backward compatibility with WP Cron behaviour seen in WC < 3.5.0.
  30. if ( is_null( $next_scheduled_date ) || $next_scheduled_date->getTimestamp() >= ( 600 + gmdate( 'U' ) ) ) {
  31. WC()->queue()->add( 'woocommerce_deliver_webhook_async', $queue_args, 'woocommerce-webhooks' );
  32. }
  33. } else {
  34. // Deliver immediately.
  35. $data['webhook']->deliver( $data['arg'] );
  36. }
  37. }
  38. }
  39. add_action( 'shutdown', 'wc_webhook_execute_queue' );
  40. /**
  41. * Process webhook delivery.
  42. *
  43. * @since 3.3.0
  44. * @param WC_Webhook $webhook Webhook instance.
  45. * @param array $arg Delivery arguments.
  46. */
  47. function wc_webhook_process_delivery( $webhook, $arg ) {
  48. // We need to queue the webhook so that it can be ran after the request has finished processing.
  49. global $wc_queued_webhooks;
  50. if ( ! isset( $wc_queued_webhooks ) ) {
  51. $wc_queued_webhooks = array();
  52. }
  53. $wc_queued_webhooks[] = array(
  54. 'webhook' => $webhook,
  55. 'arg' => $arg,
  56. );
  57. }
  58. add_action( 'woocommerce_webhook_process_delivery', 'wc_webhook_process_delivery', 10, 2 );
  59. /**
  60. * Wrapper function to execute the `woocommerce_deliver_webhook_async` cron.
  61. * hook, see WC_Webhook::process().
  62. *
  63. * @since 2.2.0
  64. * @param int $webhook_id Webhook ID to deliver.
  65. * @throws Exception If webhook cannot be read/found and $data parameter of WC_Webhook class constructor is set.
  66. * @param mixed $arg Hook argument.
  67. */
  68. function wc_deliver_webhook_async( $webhook_id, $arg ) {
  69. $webhook = new WC_Webhook( $webhook_id );
  70. $webhook->deliver( $arg );
  71. }
  72. add_action( 'woocommerce_deliver_webhook_async', 'wc_deliver_webhook_async', 10, 2 );
  73. /**
  74. * Check if the given topic is a valid webhook topic, a topic is valid if:
  75. *
  76. * + starts with `action.woocommerce_` or `action.wc_`.
  77. * + it has a valid resource & event.
  78. *
  79. * @since 2.2.0
  80. * @param string $topic Webhook topic.
  81. * @return bool
  82. */
  83. function wc_is_webhook_valid_topic( $topic ) {
  84. $invalid_topics = array(
  85. 'action.woocommerce_login_credentials',
  86. 'action.woocommerce_product_csv_importer_check_import_file_path',
  87. 'action.woocommerce_webhook_should_deliver',
  88. );
  89. if ( in_array( $topic, $invalid_topics, true ) ) {
  90. return false;
  91. }
  92. // Custom topics are prefixed with woocommerce_ or wc_ are valid.
  93. if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) {
  94. return true;
  95. }
  96. $data = explode( '.', $topic );
  97. if ( ! isset( $data[0] ) || ! isset( $data[1] ) ) {
  98. return false;
  99. }
  100. $valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) );
  101. $valid_events = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted', 'restored' ) );
  102. if ( in_array( $data[0], $valid_resources, true ) && in_array( $data[1], $valid_events, true ) ) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Check if given status is a valid webhook status.
  109. *
  110. * @since 3.5.3
  111. * @param string $status Status to check.
  112. * @return bool
  113. */
  114. function wc_is_webhook_valid_status( $status ) {
  115. return in_array( $status, array_keys( wc_get_webhook_statuses() ), true );
  116. }
  117. /**
  118. * Get Webhook statuses.
  119. *
  120. * @since 2.3.0
  121. * @return array
  122. */
  123. function wc_get_webhook_statuses() {
  124. return apply_filters(
  125. 'woocommerce_webhook_statuses',
  126. array(
  127. 'active' => __( 'Active', 'woocommerce' ),
  128. 'paused' => __( 'Paused', 'woocommerce' ),
  129. 'disabled' => __( 'Disabled', 'woocommerce' ),
  130. )
  131. );
  132. }
  133. /**
  134. * Load webhooks.
  135. *
  136. * @since 3.3.0
  137. * @throws Exception If webhook cannot be read/found and $data parameter of WC_Webhook class constructor is set.
  138. * @param string $status Optional - status to filter results by. Must be a key in return value of @see wc_get_webhook_statuses(). @since 3.5.0.
  139. * @param null|int $limit Limit number of webhooks loaded. @since 3.6.0.
  140. * @return bool
  141. */
  142. function wc_load_webhooks( $status = '', $limit = null ) {
  143. $data_store = WC_Data_Store::load( 'webhook' );
  144. $webhooks = $data_store->get_webhooks_ids( $status );
  145. $loaded = 0;
  146. foreach ( $webhooks as $webhook_id ) {
  147. $webhook = new WC_Webhook( $webhook_id );
  148. $webhook->enqueue();
  149. $loaded ++;
  150. if ( ! is_null( $limit ) && $loaded >= $limit ) {
  151. break;
  152. }
  153. }
  154. return 0 < $loaded;
  155. }
  156. /**
  157. * Get webhook.
  158. *
  159. * @param int|WC_Webhook $id Webhook ID or object.
  160. * @throws Exception If webhook cannot be read/found and $data parameter of WC_Webhook class constructor is set.
  161. * @return WC_Webhook|null
  162. */
  163. function wc_get_webhook( $id ) {
  164. $webhook = new WC_Webhook( $id );
  165. return 0 !== $webhook->get_id() ? $webhook : null;
  166. }
  167. /**
  168. * Get webhoook REST API versions.
  169. *
  170. * @since 3.5.1
  171. * @return array
  172. */
  173. function wc_get_webhook_rest_api_versions() {
  174. return array(
  175. 'wp_api_v1',
  176. 'wp_api_v2',
  177. 'wp_api_v3',
  178. );
  179. }