class-wc-background-emailer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Background Emailer
  4. *
  5. * @version 3.0.1
  6. * @package WooCommerce/Classes
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. if ( ! class_exists( 'WC_Background_Process', false ) ) {
  10. include_once dirname( __FILE__ ) . '/abstracts/class-wc-background-process.php';
  11. }
  12. /**
  13. * WC_Background_Emailer Class.
  14. */
  15. class WC_Background_Emailer extends WC_Background_Process {
  16. /**
  17. * Initiate new background process.
  18. */
  19. public function __construct() {
  20. // Uses unique prefix per blog so each blog has separate queue.
  21. $this->prefix = 'wp_' . get_current_blog_id();
  22. $this->action = 'wc_emailer';
  23. // Dispatch queue after shutdown.
  24. add_action( 'shutdown', array( $this, 'dispatch_queue' ), 100 );
  25. parent::__construct();
  26. }
  27. /**
  28. * Schedule fallback event.
  29. */
  30. protected function schedule_event() {
  31. if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
  32. wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
  33. }
  34. }
  35. /**
  36. * Task
  37. *
  38. * Override this method to perform any actions required on each
  39. * queue item. Return the modified item for further processing
  40. * in the next pass through. Or, return false to remove the
  41. * item from the queue.
  42. *
  43. * @param array $callback Update callback function.
  44. * @return mixed
  45. */
  46. protected function task( $callback ) {
  47. if ( isset( $callback['filter'], $callback['args'] ) ) {
  48. try {
  49. WC_Emails::send_queued_transactional_email( $callback['filter'], $callback['args'] );
  50. } catch ( Exception $e ) {
  51. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  52. trigger_error( 'Transactional email triggered fatal error for callback ' . esc_html( $callback['filter'] ), E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. /**
  59. * Finishes replying to the client, but keeps the process running for further (async) code execution.
  60. *
  61. * @see https://core.trac.wordpress.org/ticket/41358 .
  62. */
  63. protected function close_http_connection() {
  64. // Only 1 PHP process can access a session object at a time, close this so the next request isn't kept waiting.
  65. // @codingStandardsIgnoreStart
  66. if ( session_id() ) {
  67. session_write_close();
  68. }
  69. // @codingStandardsIgnoreEnd
  70. wc_set_time_limit( 0 );
  71. // fastcgi_finish_request is the cleanest way to send the response and keep the script running, but not every server has it.
  72. if ( is_callable( 'fastcgi_finish_request' ) ) {
  73. fastcgi_finish_request();
  74. } else {
  75. // Fallback: send headers and flush buffers.
  76. if ( ! headers_sent() ) {
  77. header( 'Connection: close' );
  78. }
  79. @ob_end_flush(); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  80. flush();
  81. }
  82. }
  83. /**
  84. * Save and run queue.
  85. */
  86. public function dispatch_queue() {
  87. if ( ! empty( $this->data ) ) {
  88. $this->close_http_connection();
  89. $this->save()->dispatch();
  90. }
  91. }
  92. /**
  93. * Get post args
  94. *
  95. * @return array
  96. */
  97. protected function get_post_args() {
  98. if ( property_exists( $this, 'post_args' ) ) {
  99. return $this->post_args;
  100. }
  101. // Pass cookies through with the request so nonces function.
  102. $cookies = array();
  103. foreach ( $_COOKIE as $name => $value ) { // WPCS: input var ok.
  104. if ( 'PHPSESSID' === $name ) {
  105. continue;
  106. }
  107. $cookies[] = new WP_Http_Cookie( array(
  108. 'name' => $name,
  109. 'value' => $value,
  110. ) );
  111. }
  112. return array(
  113. 'timeout' => 0.01,
  114. 'blocking' => false,
  115. 'body' => $this->data,
  116. 'cookies' => $cookies,
  117. 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
  118. );
  119. }
  120. /**
  121. * Handle
  122. *
  123. * Pass each queue item to the task handler, while remaining
  124. * within server memory and time limit constraints.
  125. */
  126. protected function handle() {
  127. $this->lock_process();
  128. do {
  129. $batch = $this->get_batch();
  130. if ( empty( $batch->data ) ) {
  131. break;
  132. }
  133. foreach ( $batch->data as $key => $value ) {
  134. $task = $this->task( $value );
  135. if ( false !== $task ) {
  136. $batch->data[ $key ] = $task;
  137. } else {
  138. unset( $batch->data[ $key ] );
  139. }
  140. // Update batch before sending more to prevent duplicate email possibility.
  141. $this->update( $batch->key, $batch->data );
  142. if ( $this->time_exceeded() || $this->memory_exceeded() ) {
  143. // Batch limits reached.
  144. break;
  145. }
  146. }
  147. if ( empty( $batch->data ) ) {
  148. $this->delete( $batch->key );
  149. }
  150. } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() );
  151. $this->unlock_process();
  152. // Start next batch or complete process.
  153. if ( ! $this->is_queue_empty() ) {
  154. $this->dispatch();
  155. } else {
  156. $this->complete();
  157. }
  158. }
  159. }