WC_Background_Emailer

WC_Background_Emailer Class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-background-emailer.php  
  1. class WC_Background_Emailer extends WP_Background_Process { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $action = 'wc_emailer'; 
  7.  
  8. /** 
  9. * Initiate new background process 
  10. */ 
  11. public function __construct() { 
  12. parent::__construct(); 
  13. add_action( 'shutdown', array( $this, 'dispatch_queue' ) ); 
  14.  
  15. /** 
  16. * Schedule fallback event. 
  17. */ 
  18. protected function schedule_event() { 
  19. if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { 
  20. wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); 
  21.  
  22. /** 
  23. * Task 
  24. * Override this method to perform any actions required on each 
  25. * queue item. Return the modified item for further processing 
  26. * in the next pass through. Or, return false to remove the 
  27. * item from the queue. 
  28. * @param string $callback Update callback function 
  29. * @return mixed 
  30. */ 
  31. protected function task( $callback ) { 
  32. if ( isset( $callback['filter'], $callback['args'] ) ) { 
  33. try { 
  34. WC_Emails::send_queued_transactional_email( $callback['filter'], $callback['args'] ); 
  35. } catch ( Exception $e ) { 
  36. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  37. trigger_error( 'Transactional email triggered fatal error for callback ' . $callback['filter'], E_USER_WARNING ); 
  38. return false; 
  39.  
  40. /** 
  41. * Save and run queue. 
  42. */ 
  43. public function dispatch_queue() { 
  44. if ( ! empty( $this->data ) ) { 
  45. $this->save()->dispatch(); 
  46.  
  47. /** 
  48. * Get post args 
  49. * @return array 
  50. */ 
  51. protected function get_post_args() { 
  52. if ( property_exists( $this, 'post_args' ) ) { 
  53. return $this->post_args; 
  54.  
  55. // Pass cookies through with the request so nonces function. 
  56. $cookies = array(); 
  57.  
  58. foreach ( $_COOKIE as $name => $value ) { 
  59. if ( 'PHPSESSID' === $name ) { 
  60. continue; 
  61. $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) ); 
  62.  
  63. return array( 
  64. 'timeout' => 0.01,  
  65. 'blocking' => false,  
  66. 'body' => $this->data,  
  67. 'cookies' => $cookies,  
  68. 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),  
  69. ); 
  70.  
  71. /** 
  72. * Handle 
  73. * Pass each queue item to the task handler, while remaining 
  74. * within server memory and time limit constraints. 
  75. */ 
  76. protected function handle() { 
  77. $this->lock_process(); 
  78.  
  79. do { 
  80. $batch = $this->get_batch(); 
  81.  
  82. if ( empty( $batch->data ) ) { 
  83. break; 
  84.  
  85. foreach ( $batch->data as $key => $value ) { 
  86. $task = $this->task( $value ); 
  87.  
  88. if ( false !== $task ) { 
  89. $batch->data[ $key ] = $task; 
  90. } else { 
  91. unset( $batch->data[ $key ] ); 
  92.  
  93. // Update batch before sending more to prevent duplicate email possibility. 
  94. $this->update( $batch->key, $batch->data ); 
  95.  
  96. if ( $this->time_exceeded() || $this->memory_exceeded() ) { 
  97. // Batch limits reached. 
  98. break; 
  99. if ( empty( $batch->data ) ) { 
  100. $this->delete( $batch->key ); 
  101. } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); 
  102.  
  103. $this->unlock_process(); 
  104.  
  105. // Start next batch or complete process. 
  106. if ( ! $this->is_queue_empty() ) { 
  107. $this->dispatch(); 
  108. } else { 
  109. $this->complete();