wc-webhook-functions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 webhook delivery.
  11. *
  12. * @since 3.3.0
  13. * @param WC_Webhook $webhook Webhook instance.
  14. * @param array $arg Delivery arguments.
  15. */
  16. function wc_webhook_process_delivery( $webhook, $arg ) {
  17. // Webhooks are processed in the background by default
  18. // so as to avoid delays or failures in delivery from affecting the
  19. // user who triggered it.
  20. if ( apply_filters( 'woocommerce_webhook_deliver_async', true, $webhook, $arg ) ) {
  21. // Deliver in background.
  22. wp_schedule_single_event( time(), 'woocommerce_deliver_webhook_async', array( $webhook->get_id(), $arg ) );
  23. } else {
  24. // Deliver immediately.
  25. $webhook->deliver( $arg );
  26. }
  27. }
  28. add_action( 'woocommerce_webhook_process_delivery', 'wc_webhook_process_delivery', 10, 2 );
  29. /**
  30. * Wrapper function to execute the `woocommerce_deliver_webhook_async` cron.
  31. * hook, see WC_Webhook::process().
  32. *
  33. * @since 2.2.0
  34. * @param int $webhook_id Webhook ID to deliver.
  35. * @param mixed $arg Hook argument.
  36. */
  37. function wc_deliver_webhook_async( $webhook_id, $arg ) {
  38. $webhook = new WC_Webhook( $webhook_id );
  39. $webhook->deliver( $arg );
  40. }
  41. add_action( 'woocommerce_deliver_webhook_async', 'wc_deliver_webhook_async', 10, 2 );
  42. /**
  43. * Check if the given topic is a valid webhook topic, a topic is valid if:
  44. *
  45. * + starts with `action.woocommerce_` or `action.wc_`.
  46. * + it has a valid resource & event.
  47. *
  48. * @since 2.2.0
  49. * @param string $topic Webhook topic.
  50. * @return bool
  51. */
  52. function wc_is_webhook_valid_topic( $topic ) {
  53. // Custom topics are prefixed with woocommerce_ or wc_ are valid.
  54. if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) {
  55. return true;
  56. }
  57. $data = explode( '.', $topic );
  58. if ( ! isset( $data[0] ) || ! isset( $data[1] ) ) {
  59. return false;
  60. }
  61. $valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) );
  62. $valid_events = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted', 'restored' ) );
  63. if ( in_array( $data[0], $valid_resources, true ) && in_array( $data[1], $valid_events, true ) ) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. /**
  69. * Get Webhook statuses.
  70. *
  71. * @since 2.3.0
  72. * @return array
  73. */
  74. function wc_get_webhook_statuses() {
  75. return apply_filters( 'woocommerce_webhook_statuses', array(
  76. 'active' => __( 'Active', 'woocommerce' ),
  77. 'paused' => __( 'Paused', 'woocommerce' ),
  78. 'disabled' => __( 'Disabled', 'woocommerce' ),
  79. ) );
  80. }
  81. /**
  82. * Load webhooks.
  83. *
  84. * @since 3.3.0
  85. * @return bool
  86. */
  87. function wc_load_webhooks() {
  88. $data_store = WC_Data_Store::load( 'webhook' );
  89. $webhooks = $data_store->get_webhooks_ids();
  90. $loaded = false;
  91. foreach ( $webhooks as $webhook_id ) {
  92. $webhook = new WC_Webhook( $webhook_id );
  93. $webhook->enqueue();
  94. $loaded = true;
  95. }
  96. return $loaded;
  97. }
  98. /**
  99. * Get webhook.
  100. *
  101. * @param int|WC_Webhook $id Webhook ID or object.
  102. * @return WC_Webhook|null
  103. */
  104. function wc_get_webhook( $id ) {
  105. $webhook = new WC_Webhook( $id );
  106. return 0 !== $webhook->get_id() ? $webhook : null;
  107. }