class-wc-background-updater.php 3.3 KB

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