2014-12-16 20:54:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce Webhook functions
|
2014-12-16 20:54:48 +00:00
|
|
|
*
|
2018-03-08 21:31:01 +00:00
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 3.3.0
|
2014-12-16 20:54:48 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-08 21:31:01 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2017-08-17 15:06:45 +00:00
|
|
|
|
2017-12-01 16:33:30 +00:00
|
|
|
/**
|
|
|
|
* Process webhook delivery.
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
* @param WC_Webhook $webhook Webhook instance.
|
|
|
|
* @param array $arg Delivery arguments.
|
|
|
|
*/
|
|
|
|
function wc_webhook_process_delivery( $webhook, $arg ) {
|
|
|
|
// Webhooks are processed in the background by default
|
|
|
|
// so as to avoid delays or failures in delivery from affecting the
|
|
|
|
// user who triggered it.
|
|
|
|
if ( apply_filters( 'woocommerce_webhook_deliver_async', true, $webhook, $arg ) ) {
|
|
|
|
// Deliver in background.
|
2018-10-29 22:31:47 +00:00
|
|
|
WC()->queue()->add( 'woocommerce_deliver_webhook_async', array(
|
|
|
|
'webhook_id' => $webhook->get_id(),
|
|
|
|
'arg' => $arg,
|
|
|
|
), 'woocommerce-webhooks' );
|
2017-12-01 16:33:30 +00:00
|
|
|
} else {
|
|
|
|
// Deliver immediately.
|
|
|
|
$webhook->deliver( $arg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_action( 'woocommerce_webhook_process_delivery', 'wc_webhook_process_delivery', 10, 2 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper function to execute the `woocommerce_deliver_webhook_async` cron.
|
|
|
|
* hook, see WC_Webhook::process().
|
|
|
|
*
|
|
|
|
* @since 2.2.0
|
|
|
|
* @param int $webhook_id Webhook ID to deliver.
|
2018-06-05 12:30:16 +00:00
|
|
|
* @throws Exception If webhook cannot be read/found and $data parameter of WC_Webhook class constructor is set.
|
2017-12-01 16:33:30 +00:00
|
|
|
* @param mixed $arg Hook argument.
|
|
|
|
*/
|
|
|
|
function wc_deliver_webhook_async( $webhook_id, $arg ) {
|
|
|
|
$webhook = new WC_Webhook( $webhook_id );
|
|
|
|
$webhook->deliver( $arg );
|
|
|
|
}
|
|
|
|
add_action( 'woocommerce_deliver_webhook_async', 'wc_deliver_webhook_async', 10, 2 );
|
|
|
|
|
2017-08-17 15:06:45 +00:00
|
|
|
/**
|
|
|
|
* Check if the given topic is a valid webhook topic, a topic is valid if:
|
|
|
|
*
|
|
|
|
* + starts with `action.woocommerce_` or `action.wc_`.
|
|
|
|
* + it has a valid resource & event.
|
|
|
|
*
|
2017-08-18 16:41:51 +00:00
|
|
|
* @since 2.2.0
|
2017-08-17 15:06:45 +00:00
|
|
|
* @param string $topic Webhook topic.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_is_webhook_valid_topic( $topic ) {
|
2018-10-29 17:29:55 +00:00
|
|
|
$invalid_topics = array(
|
|
|
|
'action.woocommerce_login_credentials',
|
|
|
|
'action.woocommerce_product_csv_importer_check_import_file_path',
|
|
|
|
'action.woocommerce_webhook_should_deliver',
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( in_array( $topic, $invalid_topics, true ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-17 15:06:45 +00:00
|
|
|
|
|
|
|
// Custom topics are prefixed with woocommerce_ or wc_ are valid.
|
2018-10-29 17:32:14 +00:00
|
|
|
if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) {
|
2017-08-17 15:06:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = explode( '.', $topic );
|
|
|
|
|
|
|
|
if ( ! isset( $data[0] ) || ! isset( $data[1] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) );
|
|
|
|
$valid_events = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted', 'restored' ) );
|
|
|
|
|
|
|
|
if ( in_array( $data[0], $valid_resources, true ) && in_array( $data[1], $valid_events, true ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-12-16 20:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get Webhook statuses.
|
2014-12-16 20:54:48 +00:00
|
|
|
*
|
|
|
|
* @since 2.3.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_webhook_statuses() {
|
|
|
|
return apply_filters( 'woocommerce_webhook_statuses', array(
|
|
|
|
'active' => __( 'Active', 'woocommerce' ),
|
|
|
|
'paused' => __( 'Paused', 'woocommerce' ),
|
|
|
|
'disabled' => __( 'Disabled', 'woocommerce' ),
|
|
|
|
) );
|
|
|
|
}
|
2017-08-17 13:51:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load webhooks.
|
|
|
|
*
|
2017-11-07 18:10:12 +00:00
|
|
|
* @since 3.3.0
|
2018-06-05 12:30:16 +00:00
|
|
|
* @throws Exception If webhook cannot be read/found and $data parameter of WC_Webhook class constructor is set.
|
2017-08-17 15:32:30 +00:00
|
|
|
* @return bool
|
2017-08-17 13:51:20 +00:00
|
|
|
*/
|
|
|
|
function wc_load_webhooks() {
|
|
|
|
$data_store = WC_Data_Store::load( 'webhook' );
|
|
|
|
$webhooks = $data_store->get_webhooks_ids();
|
2017-08-17 15:29:34 +00:00
|
|
|
$loaded = false;
|
2017-08-17 13:51:20 +00:00
|
|
|
|
|
|
|
foreach ( $webhooks as $webhook_id ) {
|
|
|
|
$webhook = new WC_Webhook( $webhook_id );
|
|
|
|
$webhook->enqueue();
|
2017-08-17 15:29:34 +00:00
|
|
|
$loaded = true;
|
2017-08-17 13:51:20 +00:00
|
|
|
}
|
2017-08-17 15:29:34 +00:00
|
|
|
|
|
|
|
return $loaded;
|
2017-08-17 13:51:20 +00:00
|
|
|
}
|
2017-11-07 18:10:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get webhook.
|
|
|
|
*
|
2018-01-31 10:40:06 +00:00
|
|
|
* @param int|WC_Webhook $id Webhook ID or object.
|
2018-06-05 12:30:16 +00:00
|
|
|
* @throws Exception If webhook cannot be read/found and $data parameter of WC_Webhook class constructor is set.
|
2017-11-07 18:10:12 +00:00
|
|
|
* @return WC_Webhook|null
|
|
|
|
*/
|
|
|
|
function wc_get_webhook( $id ) {
|
2018-01-31 10:40:06 +00:00
|
|
|
$webhook = new WC_Webhook( $id );
|
2017-11-07 18:10:12 +00:00
|
|
|
|
|
|
|
return 0 !== $webhook->get_id() ? $webhook : null;
|
|
|
|
}
|
2018-10-29 22:31:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get webhoook REST API versions.
|
|
|
|
*
|
|
|
|
* @since 3.5.1
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_webhook_rest_api_versions() {
|
|
|
|
return array(
|
|
|
|
'wp_api_v1',
|
|
|
|
'wp_api_v2',
|
|
|
|
'wp_api_v3',
|
|
|
|
);
|
|
|
|
}
|