class-wc-background-process.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Abstract WP_Background_Process class.
  4. *
  5. * Uses https://github.com/A5hleyRich/wp-background-processing to handle DB
  6. * updates in the background.
  7. *
  8. * @package WooCommerce/Classes
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. if ( ! class_exists( 'WP_Async_Request', false ) ) {
  12. include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-async-request.php';
  13. }
  14. if ( ! class_exists( 'WP_Background_Process', false ) ) {
  15. include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-background-process.php';
  16. }
  17. /**
  18. * WC_Background_Process class.
  19. */
  20. abstract class WC_Background_Process extends WP_Background_Process {
  21. /**
  22. * Is queue empty.
  23. *
  24. * @return bool
  25. */
  26. protected function is_queue_empty() {
  27. global $wpdb;
  28. $table = $wpdb->options;
  29. $column = 'option_name';
  30. if ( is_multisite() ) {
  31. $table = $wpdb->sitemeta;
  32. $column = 'meta_key';
  33. }
  34. $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
  35. $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
  36. return ! ( $count > 0 );
  37. }
  38. /**
  39. * Get batch.
  40. *
  41. * @return stdClass Return the first batch from the queue.
  42. */
  43. protected function get_batch() {
  44. global $wpdb;
  45. $table = $wpdb->options;
  46. $column = 'option_name';
  47. $key_column = 'option_id';
  48. $value_column = 'option_value';
  49. if ( is_multisite() ) {
  50. $table = $wpdb->sitemeta;
  51. $column = 'meta_key';
  52. $key_column = 'meta_id';
  53. $value_column = 'meta_value';
  54. }
  55. $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
  56. $query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", $key ) ); // @codingStandardsIgnoreLine.
  57. $batch = new stdClass();
  58. $batch->key = $query->$column;
  59. $batch->data = array_filter( (array) maybe_unserialize( $query->$value_column ) );
  60. return $batch;
  61. }
  62. /**
  63. * See if the batch limit has been exceeded.
  64. *
  65. * @return bool
  66. */
  67. protected function batch_limit_exceeded() {
  68. return $this->time_exceeded() || $this->memory_exceeded();
  69. }
  70. /**
  71. * Handle.
  72. *
  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. do {
  79. $batch = $this->get_batch();
  80. foreach ( $batch->data as $key => $value ) {
  81. $task = $this->task( $value );
  82. if ( false !== $task ) {
  83. $batch->data[ $key ] = $task;
  84. } else {
  85. unset( $batch->data[ $key ] );
  86. }
  87. if ( $this->batch_limit_exceeded() ) {
  88. // Batch limits reached.
  89. break;
  90. }
  91. }
  92. // Update or delete current batch.
  93. if ( ! empty( $batch->data ) ) {
  94. $this->update( $batch->key, $batch->data );
  95. } else {
  96. $this->delete( $batch->key );
  97. }
  98. } while ( ! $this->batch_limit_exceeded() && ! $this->is_queue_empty() );
  99. $this->unlock_process();
  100. // Start next batch or complete process.
  101. if ( ! $this->is_queue_empty() ) {
  102. $this->dispatch();
  103. } else {
  104. $this->complete();
  105. }
  106. }
  107. /**
  108. * Get memory limit.
  109. *
  110. * @return int
  111. */
  112. protected function get_memory_limit() {
  113. if ( function_exists( 'ini_get' ) ) {
  114. $memory_limit = ini_get( 'memory_limit' );
  115. } else {
  116. // Sensible default.
  117. $memory_limit = '128M';
  118. }
  119. if ( ! $memory_limit || -1 === intval( $memory_limit ) ) {
  120. // Unlimited, set to 32GB.
  121. $memory_limit = '32000M';
  122. }
  123. return intval( $memory_limit ) * 1024 * 1024;
  124. }
  125. /**
  126. * Schedule cron healthcheck.
  127. *
  128. * @param array $schedules Schedules.
  129. * @return array
  130. */
  131. public function schedule_cron_healthcheck( $schedules ) {
  132. $interval = apply_filters( $this->identifier . '_cron_interval', 5 );
  133. if ( property_exists( $this, 'cron_interval' ) ) {
  134. $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
  135. }
  136. // Adds every 5 minutes to the existing schedules.
  137. $schedules[ $this->identifier . '_cron_interval' ] = array(
  138. 'interval' => MINUTE_IN_SECONDS * $interval,
  139. /* translators: %d: interval */
  140. 'display' => sprintf( __( 'Every %d minutes', 'woocommerce' ), $interval ),
  141. );
  142. return $schedules;
  143. }
  144. /**
  145. * Delete all batches.
  146. *
  147. * @return WC_Background_Process
  148. */
  149. public function delete_all_batches() {
  150. global $wpdb;
  151. $table = $wpdb->options;
  152. $column = 'option_name';
  153. if ( is_multisite() ) {
  154. $table = $wpdb->sitemeta;
  155. $column = 'meta_key';
  156. }
  157. $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
  158. $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
  159. return $this;
  160. }
  161. /**
  162. * Kill process.
  163. *
  164. * Stop processing queue items, clear cronjob and delete all batches.
  165. */
  166. public function kill_process() {
  167. if ( ! $this->is_queue_empty() ) {
  168. $this->delete_all_batches();
  169. wp_clear_scheduled_hook( $this->cron_hook_identifier );
  170. }
  171. }
  172. }