WC_Background_Updater

WC_Background_Updater Class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-background-updater.php  
  1. class WC_Background_Updater extends WP_Background_Process { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $action = 'wc_updater'; 
  7.  
  8. /** 
  9. * Dispatch updater. 
  10. * Updater will still run via cron job if this fails for any reason. 
  11. */ 
  12. public function dispatch() { 
  13. $dispatched = parent::dispatch(); 
  14. $logger = wc_get_logger(); 
  15.  
  16. if ( is_wp_error( $dispatched ) ) { 
  17. $logger->error( 
  18. sprintf( 'Unable to dispatch WooCommerce updater: %s', $dispatched->get_error_message() ),  
  19. array( 'source' => 'wc_db_updates' ) 
  20. ); 
  21.  
  22. /** 
  23. * Handle cron healthcheck 
  24. * Restart the background process if not already running 
  25. * and data exists in the queue. 
  26. */ 
  27. public function handle_cron_healthcheck() { 
  28. if ( $this->is_process_running() ) { 
  29. // Background process already running. 
  30. return; 
  31.  
  32. if ( $this->is_queue_empty() ) { 
  33. // No data to process. 
  34. $this->clear_scheduled_event(); 
  35. return; 
  36.  
  37. $this->handle(); 
  38.  
  39. /** 
  40. * Schedule fallback event. 
  41. */ 
  42. protected function schedule_event() { 
  43. if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { 
  44. wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); 
  45.  
  46. /** 
  47. * Is the updater running? 
  48. * @return boolean 
  49. */ 
  50. public function is_updating() { 
  51. return false === $this->is_queue_empty(); 
  52.  
  53. /** 
  54. * Task 
  55. * Override this method to perform any actions required on each 
  56. * queue item. Return the modified item for further processing 
  57. * in the next pass through. Or, return false to remove the 
  58. * item from the queue. 
  59. * @param string $callback Update callback function 
  60. * @return mixed 
  61. */ 
  62. protected function task( $callback ) { 
  63. if ( ! defined( 'WC_UPDATING' ) ) { 
  64. define( 'WC_UPDATING', true ); 
  65.  
  66. $logger = wc_get_logger(); 
  67.  
  68. include_once( dirname( __FILE__ ) . '/wc-update-functions.php' ); 
  69.  
  70. if ( is_callable( $callback ) ) { 
  71. $logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); 
  72. call_user_func( $callback ); 
  73. $logger->info( sprintf( 'Finished %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); 
  74. } else { 
  75. $logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); 
  76.  
  77. return false; 
  78.  
  79. /** 
  80. * Complete 
  81. * Override if applicable, but ensure that the below actions are 
  82. * performed, or, call parent::complete(). 
  83. */ 
  84. protected function complete() { 
  85. $logger = wc_get_logger(); 
  86. $logger->info( 'Data update complete', array( 'source' => 'wc_db_updates' ) ); 
  87. WC_Install::update_db_version(); 
  88. parent::complete();