More efficient expand

This commit is contained in:
Mike Jolley 2017-02-28 21:16:03 +00:00
parent fb576b015b
commit 4f333d0e82
1 changed files with 17 additions and 23 deletions

View File

@ -92,26 +92,22 @@ class WC_Emails {
* Queue transactional email via cron so it's not sent in current request. * Queue transactional email via cron so it's not sent in current request.
*/ */
public static function queue_transactional_email() { public static function queue_transactional_email() {
$args = func_get_args();
$filter = current_filter(); $filter = current_filter();
$event_args = array( $args = func_get_args();
'filter' => $filter,
'args' => $args,
);
// Remove objects and store IDs. // Remove objects and store IDs.
if ( 0 === strpos( $filter, 'woocommerce_order_status_' ) ) { if ( 0 === strpos( $filter, 'woocommerce_order_status_' ) ) {
$event_args['object_id'] = $args[0]; $args[1] = $args[1]->get_id();
$event_args['args'][1] = false;
} elseif ( 'woocommerce_low_stock' === $filter || 'woocommerce_no_stock' === $filter ) { } elseif ( 'woocommerce_low_stock' === $filter || 'woocommerce_no_stock' === $filter ) {
$event_args['object_id'] = $args[0]->get_id(); $args[0] = $args[0]->get_id();
$event_args['args'][0] = false;
} elseif ( 'woocommerce_product_on_backorder' === $filter ) { } elseif ( 'woocommerce_product_on_backorder' === $filter ) {
$event_args['object_id'] = $args[0]['product']->get_id(); $args[0]['product'] = $args[0]['product']->get_id();
$event_args['args'][0]['product'] = false;
} }
wp_schedule_single_event( time() + 10, 'woocommerce_send_queued_transactional_email', $event_args ); wp_schedule_single_event( time() + 5, 'woocommerce_send_queued_transactional_email', array(
'filter' => $filter,
'args' => $args,
) );
} }
/** /**
@ -122,22 +118,20 @@ class WC_Emails {
* @param string $filter Filter name. * @param string $filter Filter name.
* @param array $event_args Email args (default: []). * @param array $event_args Email args (default: []).
*/ */
public static function send_queued_transactional_email( $filter = '', $event_args = array() ) { public static function send_queued_transactional_email( $filter = '', $args = array() ) {
if ( apply_filters( 'woocommerce_allow_send_queued_transactional_email', true, $filter, $event_args ) ) { if ( apply_filters( 'woocommerce_allow_send_queued_transactional_email', true, $filter, $args ) ) {
self::instance(); // Init self so emails exist. self::instance(); // Init self so emails exist.
// Expand objects from IDs. // Expand objects from IDs.
if ( isset( $event_args['object_id'] ) ) { if ( 0 === strpos( $filter, 'woocommerce_order_status_' ) ) {
if ( 0 === strpos( $filter, 'woocommerce_order_status_' ) ) { $args[1] = wc_get_order( $args[1] );
$event_args['args'][1] = wc_get_order( absint( $event_args['object_id'] ) ); } elseif ( 'woocommerce_low_stock' === $filter || 'woocommerce_no_stock' === $filter ) {
} elseif ( 'woocommerce_low_stock' === $filter || 'woocommerce_no_stock' === $filter ) { $args[0] = wc_get_product( $args[0] );
$event_args['args'][0] = wc_get_product( absint( $event_args['object_id'] ) ); } elseif ( 'woocommerce_product_on_backorder' === $filter ) {
} elseif ( 'woocommerce_product_on_backorder' === $filter ) { $args[0]['product'] = wc_get_product( $args[0]['product'] );
$event_args['args'][0]['product'] = wc_get_product( absint( $event_args['object_id'] ) );
}
} }
do_action_ref_array( $filter . '_notification', $event_args ); do_action_ref_array( $filter . '_notification', $args );
} }
} }