File "wc-webhook-functions.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/woocommerce/includes/wc-webhook-functions.php
File size: 3.33 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('ABSPATH') || exit;
function wc_webhook_execute_queue()
{
    global $wc_queued_webhooks;
    if (empty($wc_queued_webhooks)) {
        return;
    }
    foreach ($wc_queued_webhooks as $data) {
        if (apply_filters('woocommerce_webhook_deliver_async', true, $data['webhook'], $data['arg'])) {
            $queue_args = array('webhook_id' => $data['webhook']->get_id(), 'arg' => $data['arg']);
            $next_scheduled_date = WC()->queue()->get_next('woocommerce_deliver_webhook_async', $queue_args, 'woocommerce-webhooks');
            if (is_null($next_scheduled_date) || $next_scheduled_date->getTimestamp() >= 600 + gmdate('U')) {
                WC()->queue()->add('woocommerce_deliver_webhook_async', $queue_args, 'woocommerce-webhooks');
            }
        } else {
            $data['webhook']->deliver($data['arg']);
        }
    }
}
add_action('shutdown', 'wc_webhook_execute_queue');
function wc_webhook_process_delivery($webhook, $arg)
{
    global $wc_queued_webhooks;
    if (!isset($wc_queued_webhooks)) {
        $wc_queued_webhooks = array();
    }
    $wc_queued_webhooks[] = array('webhook' => $webhook, 'arg' => $arg);
}
add_action('woocommerce_webhook_process_delivery', 'wc_webhook_process_delivery', 10, 2);
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);
function wc_is_webhook_valid_topic($topic)
{
    $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;
    }
    if (0 === strpos($topic, 'action.woocommerce_') || 0 === strpos($topic, 'action.wc_')) {
        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;
}
function wc_is_webhook_valid_status($status)
{
    return in_array($status, array_keys(wc_get_webhook_statuses()), true);
}
function wc_get_webhook_statuses()
{
    return apply_filters('woocommerce_webhook_statuses', array('active' => __('Active', 'woocommerce'), 'paused' => __('Paused', 'woocommerce'), 'disabled' => __('Disabled', 'woocommerce')));
}
function wc_load_webhooks($status = '', $limit = null)
{
    $data_store = WC_Data_Store::load('webhook');
    $webhooks = $data_store->get_webhooks_ids($status);
    $loaded = 0;
    foreach ($webhooks as $webhook_id) {
        $webhook = new WC_Webhook($webhook_id);
        $webhook->enqueue();
        $loaded++;
        if (!is_null($limit) && $loaded >= $limit) {
            break;
        }
    }
    return 0 < $loaded;
}
function wc_get_webhook($id)
{
    $webhook = new WC_Webhook($id);
    return 0 !== $webhook->get_id() ? $webhook : null;
}
function wc_get_webhook_rest_api_versions()
{
    return array('wp_api_v1', 'wp_api_v2', 'wp_api_v3');
}