Aucune description

class-wc-background-updater.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Background Updater
  4. *
  5. * @version 2.6.0
  6. * @deprecated 3.6.0 Replaced with queue.
  7. * @package WooCommerce\Classes
  8. */
  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_Updater Class.
  15. */
  16. class WC_Background_Updater 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_updater';
  24. parent::__construct();
  25. }
  26. /**
  27. * Dispatch updater.
  28. *
  29. * Updater will still run via cron job if this fails for any reason.
  30. */
  31. public function dispatch() {
  32. $dispatched = parent::dispatch();
  33. $logger = wc_get_logger();
  34. if ( is_wp_error( $dispatched ) ) {
  35. $logger->error(
  36. sprintf( 'Unable to dispatch WooCommerce updater: %s', $dispatched->get_error_message() ),
  37. array( 'source' => 'wc_db_updates' )
  38. );
  39. }
  40. }
  41. /**
  42. * Handle cron healthcheck
  43. *
  44. * Restart the background process if not already running
  45. * and data exists in the queue.
  46. */
  47. public function handle_cron_healthcheck() {
  48. if ( $this->is_process_running() ) {
  49. // Background process already running.
  50. return;
  51. }
  52. if ( $this->is_queue_empty() ) {
  53. // No data to process.
  54. $this->clear_scheduled_event();
  55. return;
  56. }
  57. $this->handle();
  58. }
  59. /**
  60. * Schedule fallback event.
  61. */
  62. protected function schedule_event() {
  63. if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
  64. wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
  65. }
  66. }
  67. /**
  68. * Is the updater running?
  69. *
  70. * @return boolean
  71. */
  72. public function is_updating() {
  73. return false === $this->is_queue_empty();
  74. }
  75. /**
  76. * Task
  77. *
  78. * Override this method to perform any actions required on each
  79. * queue item. Return the modified item for further processing
  80. * in the next pass through. Or, return false to remove the
  81. * item from the queue.
  82. *
  83. * @param string $callback Update callback function.
  84. * @return string|bool
  85. */
  86. protected function task( $callback ) {
  87. wc_maybe_define_constant( 'WC_UPDATING', true );
  88. $logger = wc_get_logger();
  89. include_once dirname( __FILE__ ) . '/wc-update-functions.php';
  90. $result = false;
  91. if ( is_callable( $callback ) ) {
  92. $logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
  93. $result = (bool) call_user_func( $callback, $this );
  94. if ( $result ) {
  95. $logger->info( sprintf( '%s callback needs to run again', $callback ), array( 'source' => 'wc_db_updates' ) );
  96. } else {
  97. $logger->info( sprintf( 'Finished running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
  98. }
  99. } else {
  100. $logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
  101. }
  102. return $result ? $callback : false;
  103. }
  104. /**
  105. * Complete
  106. *
  107. * Override if applicable, but ensure that the below actions are
  108. * performed, or, call parent::complete().
  109. */
  110. protected function complete() {
  111. $logger = wc_get_logger();
  112. $logger->info( 'Data update complete', array( 'source' => 'wc_db_updates' ) );
  113. WC_Install::update_db_version();
  114. parent::complete();
  115. }
  116. /**
  117. * See if the batch limit has been exceeded.
  118. *
  119. * @return bool
  120. */
  121. public function is_memory_exceeded() {
  122. return $this->memory_exceeded();
  123. }
  124. }