Geen omschrijving

class-wc-background-emailer.php 4.6KB

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