abstracts
8 years ago
admin
8 years ago
api
8 years ago
cli
8 years ago
customizer
8 years ago
data-stores
8 years ago
emails
8 years ago
export
8 years ago
gateways
8 years ago
import
8 years ago
interfaces
8 years ago
legacy
8 years ago
libraries
8 years ago
log-handlers
8 years ago
payment-tokens
9 years ago
shipping
8 years ago
shortcodes
8 years ago
theme-support
8 years ago
walkers
8 years ago
widgets
8 years ago
class-wc-ajax.php
8 years ago
class-wc-api.php
8 years ago
class-wc-auth.php
8 years ago
class-wc-autoloader.php
8 years ago
class-wc-background-emailer.php
8 years ago
class-wc-background-updater.php
8 years ago
class-wc-breadcrumb.php
8 years ago
class-wc-cache-helper.php
8 years ago
class-wc-cart-fees.php
8 years ago
class-wc-cart-session.php
8 years ago
class-wc-cart-totals.php
8 years ago
class-wc-cart.php
8 years ago
class-wc-checkout.php
8 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
8 years ago
class-wc-countries.php
8 years ago
class-wc-coupon.php
8 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
8 years ago
class-wc-customer.php
8 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
8 years ago
class-wc-datetime.php
8 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
8 years ago
class-wc-discounts.php
8 years ago
class-wc-download-handler.php
8 years ago
class-wc-emails.php
8 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
8 years ago
class-wc-frontend-scripts.php
8 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
8 years ago
class-wc-https.php
9 years ago
class-wc-install.php
8 years ago
class-wc-integrations.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
8 years ago
class-wc-meta-data.php
8 years ago
class-wc-order-factory.php
8 years ago
class-wc-order-item-coupon.php
8 years ago
class-wc-order-item-fee.php
8 years ago
class-wc-order-item-meta.php
8 years ago
class-wc-order-item-product.php
8 years ago
class-wc-order-item-shipping.php
8 years ago
class-wc-order-item-tax.php
8 years ago
class-wc-order-item.php
8 years ago
class-wc-order-query.php
8 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
8 years ago
class-wc-payment-gateways.php
8 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
8 years ago
class-wc-post-types.php
8 years ago
class-wc-product-attribute.php
8 years ago
class-wc-product-download.php
8 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
8 years ago
class-wc-product-simple.php
8 years ago
class-wc-product-variable.php
8 years ago
class-wc-product-variation.php
8 years ago
class-wc-query.php
8 years ago
class-wc-regenerate-images-request.php
8 years ago
class-wc-regenerate-images.php
8 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
8 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
8 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
8 years ago
class-wc-shortcodes.php
8 years ago
class-wc-structured-data.php
8 years ago
class-wc-tax.php
8 years ago
class-wc-template-loader.php
8 years ago
class-wc-tracker.php
8 years ago
class-wc-validation.php
8 years ago
class-wc-webhook.php
8 years ago
class-woocommerce.php
8 years ago
wc-account-functions.php
8 years ago
wc-attribute-functions.php
8 years ago
wc-cart-functions.php
8 years ago
wc-conditional-functions.php
8 years ago
wc-core-functions.php
8 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
8 years ago
wc-formatting-functions.php
8 years ago
wc-notice-functions.php
8 years ago
wc-order-functions.php
8 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
8 years ago
wc-product-functions.php
8 years ago
wc-rest-functions.php
8 years ago
wc-stock-functions.php
8 years ago
wc-template-functions.php
8 years ago
wc-template-hooks.php
8 years ago
wc-term-functions.php
8 years ago
wc-update-functions.php
8 years ago
wc-user-functions.php
8 years ago
wc-webhook-functions.php
8 years ago
wc-widget-functions.php
8 years ago
class-wc-background-emailer.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Background Emailer |
| 4 | * |
| 5 | * Uses https://github.com/A5hleyRich/wp-background-processing to handle emails |
| 6 | * in the background. |
| 7 | * |
| 8 | * @class WC_Background_Emailer |
| 9 | * @version 3.0.1 |
| 10 | * @package WooCommerce/Classes |
| 11 | * @category Class |
| 12 | * @author WooCommerce |
| 13 | */ |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | if ( ! class_exists( 'WP_Async_Request', false ) ) { |
| 19 | include_once( dirname( __FILE__ ) . '/libraries/wp-async-request.php' ); |
| 20 | } |
| 21 | |
| 22 | if ( ! class_exists( 'WP_Background_Process', false ) ) { |
| 23 | include_once( dirname( __FILE__ ) . '/libraries/wp-background-process.php' ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * WC_Background_Emailer Class. |
| 28 | */ |
| 29 | class WC_Background_Emailer extends WP_Background_Process { |
| 30 | |
| 31 | /** |
| 32 | * Initiate new background process. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | // Uses unique prefix per blog so each blog has separate queue. |
| 36 | $this->prefix = 'wp_' . get_current_blog_id(); |
| 37 | $this->action = 'wc_emailer'; |
| 38 | |
| 39 | // Dispatch queue after shutdown. |
| 40 | add_action( 'shutdown', array( $this, 'dispatch_queue' ), 100 ); |
| 41 | |
| 42 | parent::__construct(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Schedule fallback event. |
| 47 | */ |
| 48 | protected function schedule_event() { |
| 49 | if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
| 50 | wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Task |
| 56 | * |
| 57 | * Override this method to perform any actions required on each |
| 58 | * queue item. Return the modified item for further processing |
| 59 | * in the next pass through. Or, return false to remove the |
| 60 | * item from the queue. |
| 61 | * |
| 62 | * @param array $callback Update callback function |
| 63 | * @return mixed |
| 64 | */ |
| 65 | protected function task( $callback ) { |
| 66 | if ( isset( $callback['filter'], $callback['args'] ) ) { |
| 67 | try { |
| 68 | WC_Emails::send_queued_transactional_email( $callback['filter'], $callback['args'] ); |
| 69 | } catch ( Exception $e ) { |
| 70 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 71 | trigger_error( 'Transactional email triggered fatal error for callback ' . $callback['filter'], E_USER_WARNING ); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Finishes replying to the client, but keeps the process running for further (async) code execution. |
| 80 | * |
| 81 | * @see https://core.trac.wordpress.org/ticket/41358 . |
| 82 | */ |
| 83 | protected function close_http_connection() { |
| 84 | // Only 1 PHP process can access a session object at a time, close this so the next request isn't kept waiting. |
| 85 | // @codingStandardsIgnoreStart |
| 86 | if ( session_id() ) { |
| 87 | session_write_close(); |
| 88 | } |
| 89 | // @codingStandardsIgnoreEnd |
| 90 | |
| 91 | wc_set_time_limit( 0 ); |
| 92 | |
| 93 | // fastcgi_finish_request is the cleanest way to send the response and keep the script running, but not every server has it. |
| 94 | if ( is_callable( 'fastcgi_finish_request' ) ) { |
| 95 | fastcgi_finish_request(); |
| 96 | } else { |
| 97 | // Fallback: send headers and flush buffers. |
| 98 | if ( ! headers_sent() ) { |
| 99 | header( 'Connection: close' ); |
| 100 | } |
| 101 | @ob_end_flush(); |
| 102 | flush(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Save and run queue. |
| 108 | */ |
| 109 | public function dispatch_queue() { |
| 110 | if ( ! empty( $this->data ) ) { |
| 111 | $this->close_http_connection(); |
| 112 | $this->save()->dispatch(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get post args |
| 118 | * |
| 119 | * @return array |
| 120 | */ |
| 121 | protected function get_post_args() { |
| 122 | if ( property_exists( $this, 'post_args' ) ) { |
| 123 | return $this->post_args; |
| 124 | } |
| 125 | |
| 126 | // Pass cookies through with the request so nonces function. |
| 127 | $cookies = array(); |
| 128 | |
| 129 | foreach ( $_COOKIE as $name => $value ) { |
| 130 | if ( 'PHPSESSID' === $name ) { |
| 131 | continue; |
| 132 | } |
| 133 | $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) ); |
| 134 | } |
| 135 | |
| 136 | return array( |
| 137 | 'timeout' => 0.01, |
| 138 | 'blocking' => false, |
| 139 | 'body' => $this->data, |
| 140 | 'cookies' => $cookies, |
| 141 | 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Handle |
| 147 | * |
| 148 | * Pass each queue item to the task handler, while remaining |
| 149 | * within server memory and time limit constraints. |
| 150 | */ |
| 151 | protected function handle() { |
| 152 | $this->lock_process(); |
| 153 | |
| 154 | do { |
| 155 | $batch = $this->get_batch(); |
| 156 | |
| 157 | if ( empty( $batch->data ) ) { |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | foreach ( $batch->data as $key => $value ) { |
| 162 | $task = $this->task( $value ); |
| 163 | |
| 164 | if ( false !== $task ) { |
| 165 | $batch->data[ $key ] = $task; |
| 166 | } else { |
| 167 | unset( $batch->data[ $key ] ); |
| 168 | } |
| 169 | |
| 170 | // Update batch before sending more to prevent duplicate email possibility. |
| 171 | $this->update( $batch->key, $batch->data ); |
| 172 | |
| 173 | if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
| 174 | // Batch limits reached. |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | if ( empty( $batch->data ) ) { |
| 179 | $this->delete( $batch->key ); |
| 180 | } |
| 181 | } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
| 182 | |
| 183 | $this->unlock_process(); |
| 184 | |
| 185 | // Start next batch or complete process. |
| 186 | if ( ! $this->is_queue_empty() ) { |
| 187 | $this->dispatch(); |
| 188 | } else { |
| 189 | $this->complete(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 |