Нет описания

Controller.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Action_Scheduler\Migration;
  3. use Action_Scheduler\WP_CLI\Migration_Command;
  4. use Action_Scheduler\WP_CLI\ProgressBar;
  5. /**
  6. * Class Controller
  7. *
  8. * The main plugin/initialization class for migration to custom tables.
  9. *
  10. * @package Action_Scheduler\Migration
  11. *
  12. * @since 3.0.0
  13. *
  14. * @codeCoverageIgnore
  15. */
  16. class Controller {
  17. private static $instance;
  18. /** @var Action_Scheduler\Migration\Scheduler */
  19. private $migration_scheduler;
  20. /** @var string */
  21. private $store_classname;
  22. /** @var string */
  23. private $logger_classname;
  24. /** @var bool */
  25. private $migrate_custom_store;
  26. /**
  27. * Controller constructor.
  28. *
  29. * @param Scheduler $migration_scheduler Migration scheduler object.
  30. */
  31. protected function __construct( Scheduler $migration_scheduler ) {
  32. $this->migration_scheduler = $migration_scheduler;
  33. $this->store_classname = '';
  34. }
  35. /**
  36. * Set the action store class name.
  37. *
  38. * @param string $class Classname of the store class.
  39. *
  40. * @return string
  41. */
  42. public function get_store_class( $class ) {
  43. if ( \ActionScheduler_DataController::is_migration_complete() ) {
  44. return \ActionScheduler_DataController::DATASTORE_CLASS;
  45. } elseif ( \ActionScheduler_Store::DEFAULT_CLASS !== $class ) {
  46. $this->store_classname = $class;
  47. return $class;
  48. } else {
  49. return 'ActionScheduler_HybridStore';
  50. }
  51. }
  52. /**
  53. * Set the action logger class name.
  54. *
  55. * @param string $class Classname of the logger class.
  56. *
  57. * @return string
  58. */
  59. public function get_logger_class( $class ) {
  60. \ActionScheduler_Store::instance();
  61. if ( $this->has_custom_datastore() ) {
  62. $this->logger_classname = $class;
  63. return $class;
  64. } else {
  65. return \ActionScheduler_DataController::LOGGER_CLASS;
  66. }
  67. }
  68. /**
  69. * Get flag indicating whether a custom datastore is in use.
  70. *
  71. * @return bool
  72. */
  73. public function has_custom_datastore() {
  74. return (bool) $this->store_classname;
  75. }
  76. /**
  77. * Set up the background migration process
  78. *
  79. * @return void
  80. */
  81. public function schedule_migration() {
  82. if ( \ActionScheduler_DataController::is_migration_complete() || $this->migration_scheduler->is_migration_scheduled() ) {
  83. return;
  84. }
  85. $this->migration_scheduler->schedule_migration();
  86. }
  87. /**
  88. * Get the default migration config object
  89. *
  90. * @return ActionScheduler\Migration\Config
  91. */
  92. public function get_migration_config_object() {
  93. static $config = null;
  94. if ( ! $config ) {
  95. $source_store = $this->store_classname ? new $this->store_classname() : new \ActionScheduler_wpPostStore();
  96. $source_logger = $this->logger_classname ? new $this->logger_classname() : new \ActionScheduler_wpCommentLogger();
  97. $config = new Config();
  98. $config->set_source_store( $source_store );
  99. $config->set_source_logger( $source_logger );
  100. $config->set_destination_store( new \ActionScheduler_DBStoreMigrator() );
  101. $config->set_destination_logger( new \ActionScheduler_DBLogger() );
  102. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  103. $config->set_progress_bar( new ProgressBar( '', 0 ) );
  104. }
  105. }
  106. return apply_filters( 'action_scheduler/migration_config', $config );
  107. }
  108. /**
  109. * Hook dashboard migration notice.
  110. */
  111. public function hook_admin_notices() {
  112. if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
  113. return;
  114. }
  115. add_action( 'admin_notices', array( $this, 'display_migration_notice' ), 10, 0 );
  116. }
  117. /**
  118. * Show a dashboard notice that migration is in progress.
  119. */
  120. public function display_migration_notice() {
  121. printf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html__( 'Action Scheduler migration in progress. The list of scheduled actions may be incomplete.', 'woocommerce' ) );
  122. }
  123. /**
  124. * Add store classes. Hook migration.
  125. */
  126. private function hook() {
  127. add_filter( 'action_scheduler_store_class', array( $this, 'get_store_class' ), 100, 1 );
  128. add_filter( 'action_scheduler_logger_class', array( $this, 'get_logger_class' ), 100, 1 );
  129. add_action( 'init', array( $this, 'maybe_hook_migration' ) );
  130. add_action( 'wp_loaded', array( $this, 'schedule_migration' ) );
  131. // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen
  132. add_action( 'load-tools_page_action-scheduler', array( $this, 'hook_admin_notices' ), 10, 0 );
  133. add_action( 'load-woocommerce_page_wc-status', array( $this, 'hook_admin_notices' ), 10, 0 );
  134. }
  135. /**
  136. * Possibly hook the migration scheduler action.
  137. *
  138. * @author Jeremy Pry
  139. */
  140. public function maybe_hook_migration() {
  141. if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
  142. return;
  143. }
  144. $this->migration_scheduler->hook();
  145. }
  146. /**
  147. * Allow datastores to enable migration to AS tables.
  148. */
  149. public function allow_migration() {
  150. if ( ! \ActionScheduler_DataController::dependencies_met() ) {
  151. return false;
  152. }
  153. if ( null === $this->migrate_custom_store ) {
  154. $this->migrate_custom_store = apply_filters( 'action_scheduler_migrate_data_store', false );
  155. }
  156. return ( ! $this->has_custom_datastore() ) || $this->migrate_custom_store;
  157. }
  158. /**
  159. * Proceed with the migration if the dependencies have been met.
  160. */
  161. public static function init() {
  162. if ( \ActionScheduler_DataController::dependencies_met() ) {
  163. self::instance()->hook();
  164. }
  165. }
  166. /**
  167. * Singleton factory.
  168. */
  169. public static function instance() {
  170. if ( ! isset( self::$instance ) ) {
  171. self::$instance = new static( new Scheduler() );
  172. }
  173. return self::$instance;
  174. }
  175. }