Add WC_Queue and WC()->queue() wrapper for accessing it
To take care of instantiating a canonical job queue for use across all of WooCommerce, and by 3rd party code. Also use this new API for enqueuing webhooks instead of raw Action Scheduler APIs.
This commit is contained in:
parent
0611ff7665
commit
d9158a8c0e
|
@ -335,6 +335,7 @@ final class WooCommerce {
|
|||
include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
|
||||
include_once WC_ABSPATH . 'includes/class-wc-logger.php';
|
||||
include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
|
||||
include_once WC_ABSPATH . 'includes/queue/class-wc-queue.php';
|
||||
|
||||
/**
|
||||
* Data stores - used to store and retrieve CRUD object data from the database.
|
||||
|
@ -658,6 +659,15 @@ final class WooCommerce {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get queue instance.
|
||||
*
|
||||
* @return WC_Queue_Interface
|
||||
*/
|
||||
public function queue() {
|
||||
return WC_Queue::instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Checkout Class.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
/**
|
||||
* WC Queue
|
||||
*
|
||||
* @version 3.5.0
|
||||
* @package WooCommerce/Interface
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WC Queue
|
||||
*
|
||||
* Singleton for managing the WC queue instance.
|
||||
*
|
||||
* @version 3.5.0
|
||||
*/
|
||||
class WC_Queue {
|
||||
|
||||
/**
|
||||
* The single instance of the queue.
|
||||
*
|
||||
* @var WC_Queue_Interface|null
|
||||
*/
|
||||
protected static $instance = null;
|
||||
|
||||
/**
|
||||
* The default queue class to initialize
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $default_cass = 'WC_Action_Queue';
|
||||
|
||||
/**
|
||||
* @return WC_Queue_Interface
|
||||
*/
|
||||
final public static function instance() {
|
||||
|
||||
if ( is_null( self::$instance ) ) {
|
||||
$class = self::get_class();
|
||||
self::$instance = new $class();
|
||||
self::$instance = self::validate_instance( self::$instance );
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get class to instantiate
|
||||
*
|
||||
* And make sure 3rd party code has the chance to attach a custom queue class.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function get_class() {
|
||||
if ( ! did_action( 'plugins_loaded' ) ) {
|
||||
wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before plugins_loaded.', 'woocommerce' ), '3.5.0' );
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_queue_class', self::$default_cass );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enforce a WC_Queue_Interface is
|
||||
*
|
||||
* @return WC_Queue_Interface
|
||||
*/
|
||||
protected static function validate_instance( $instance ) {
|
||||
if ( false === ( $instance instanceof WC_Queue_Interface ) ) {
|
||||
$default_class = self::$default_cass;
|
||||
wc_doing_it_wrong( __FUNCTION__, sprintf( __( 'The class attached to the "woocommerce_queue_class" does not implement the WC_Queue_Interface interface. The default %s class will be used instead.', 'woocommerce' ), $default_class ), '3.5.0' );
|
||||
$instance = new $default_class();
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ function wc_webhook_process_delivery( $webhook, $arg ) {
|
|||
// user who triggered it.
|
||||
if ( apply_filters( 'woocommerce_webhook_deliver_async', true, $webhook, $arg ) ) {
|
||||
// Deliver in background.
|
||||
wc_schedule_single_action( time(), 'woocommerce_deliver_webhook_async', array( 'webhook_id' => $webhook->get_id(), 'arg' => $arg ), 'woocommerce-webhooks' );
|
||||
WC()->queue()->add( 'woocommerce_deliver_webhook_async', array( 'webhook_id' => $webhook->get_id(), 'arg' => $arg ), 'woocommerce-webhooks' );
|
||||
} else {
|
||||
// Deliver immediately.
|
||||
$webhook->deliver( $arg );
|
||||
|
|
Loading…
Reference in New Issue