Background Emailer instead of just CRON
This commit is contained in:
parent
baaaf493d3
commit
288026ca9f
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
/**
|
||||
* Background Emailer
|
||||
*
|
||||
* Uses https://github.com/A5hleyRich/wp-background-processing to handle emails
|
||||
* in the background.
|
||||
*
|
||||
* @class WC_Background_Emailer
|
||||
* @version 3.0.0
|
||||
* @package WooCommerce/Classes
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WP_Async_Request', false ) ) {
|
||||
include_once( dirname( __FILE__ ) . '/libraries/wp-async-request.php' );
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WP_Background_Process', false ) ) {
|
||||
include_once( dirname( __FILE__ ) . '/libraries/wp-background-process.php' );
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Background_Emailer Class.
|
||||
*/
|
||||
class WC_Background_Emailer extends WP_Background_Process {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $action = 'wc_emailer';
|
||||
|
||||
/**
|
||||
* Initiate new background process
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
add_action( 'shutdown', array( $this, 'dispatch_queue' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule fallback event.
|
||||
*/
|
||||
protected function schedule_event() {
|
||||
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
|
||||
wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Task
|
||||
*
|
||||
* Override this method to perform any actions required on each
|
||||
* queue item. Return the modified item for further processing
|
||||
* in the next pass through. Or, return false to remove the
|
||||
* item from the queue.
|
||||
*
|
||||
* @param string $callback Update callback function
|
||||
* @return mixed
|
||||
*/
|
||||
protected function task( $callback ) {
|
||||
if ( isset( $callback['filter'], $callback['args'] ) ) {
|
||||
WC_Emails::send_queued_transactional_email( $callback['filter'], $callback['args'] );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save and run queue.
|
||||
*/
|
||||
public function dispatch_queue() {
|
||||
if ( ! empty( $this->data ) ) {
|
||||
$this->save()->dispatch();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,6 +23,11 @@ class WC_Emails {
|
|||
/** @var WC_Emails The single instance of the class */
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Background emailer class.
|
||||
*/
|
||||
protected static $background_emailer;
|
||||
|
||||
/**
|
||||
* Main WC_Emails Instance.
|
||||
*
|
||||
|
@ -83,21 +88,26 @@ class WC_Emails {
|
|||
'woocommerce_created_customer',
|
||||
) );
|
||||
|
||||
foreach ( $email_actions as $action ) {
|
||||
add_action( $action, array( __CLASS__, 'queue_transactional_email' ), 10, 10 );
|
||||
if ( apply_filters( 'woocommerce_defer_transactional_emails', true ) ) {
|
||||
self::$background_emailer = new WC_Background_Emailer();
|
||||
|
||||
foreach ( $email_actions as $action ) {
|
||||
add_action( $action, array( __CLASS__, 'queue_transactional_email' ), 10, 10 );
|
||||
}
|
||||
} else {
|
||||
foreach ( $email_actions as $action ) {
|
||||
add_action( $action, array( __CLASS__, 'send_transactional_email' ), 10, 10 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue transactional email via cron so it's not sent in current request.
|
||||
* Queue transactional email so it's not sent in current request.
|
||||
*/
|
||||
public static function queue_transactional_email() {
|
||||
$filter = current_filter();
|
||||
$args = func_get_args();
|
||||
|
||||
wp_schedule_single_event( time() + 5, 'woocommerce_send_queued_transactional_email', array(
|
||||
'filter' => $filter,
|
||||
'args' => $args,
|
||||
self::$background_emailer->push_to_queue( array(
|
||||
'filter' => current_filter(),
|
||||
'args' => func_get_args(),
|
||||
) );
|
||||
}
|
||||
|
||||
|
|
|
@ -181,7 +181,6 @@ final class WooCommerce {
|
|||
add_action( 'init', array( $this, 'init' ), 0 );
|
||||
add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
|
||||
add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
|
||||
add_action( 'woocommerce_send_queued_transactional_email', array( 'WC_Emails', 'send_queued_transactional_email' ), 10, 2 );
|
||||
add_action( 'init', array( $this, 'wpdb_table_fix' ), 0 );
|
||||
add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
|
||||
}
|
||||
|
@ -319,6 +318,7 @@ final class WooCommerce {
|
|||
include_once( WC_ABSPATH . 'includes/class-wc-https.php' ); // https Helper
|
||||
include_once( WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php' );
|
||||
include_once( WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php' );
|
||||
include_once( WC_ABSPATH . 'includes/class-wc-background-emailer.php' );
|
||||
|
||||
/**
|
||||
* Data stores - used to store and retrieve CRUD object data from the database.
|
||||
|
|
Loading…
Reference in New Issue