Sin descripción

ActionScheduler_DataController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. use Action_Scheduler\Migration\Controller;
  3. /**
  4. * Class ActionScheduler_DataController
  5. *
  6. * The main plugin/initialization class for the data stores.
  7. *
  8. * Responsible for hooking everything up with WordPress.
  9. *
  10. * @package Action_Scheduler
  11. *
  12. * @since 3.0.0
  13. */
  14. class ActionScheduler_DataController {
  15. /** Action data store class name. */
  16. const DATASTORE_CLASS = 'ActionScheduler_DBStore';
  17. /** Logger data store class name. */
  18. const LOGGER_CLASS = 'ActionScheduler_DBLogger';
  19. /** Migration status option name. */
  20. const STATUS_FLAG = 'action_scheduler_migration_status';
  21. /** Migration status option value. */
  22. const STATUS_COMPLETE = 'complete';
  23. /** Migration minimum required PHP version. */
  24. const MIN_PHP_VERSION = '5.5';
  25. /** @var ActionScheduler_DataController */
  26. private static $instance;
  27. /** @var int */
  28. private static $sleep_time = 0;
  29. /** @var int */
  30. private static $free_ticks = 50;
  31. /**
  32. * Get a flag indicating whether the migration environment dependencies are met.
  33. *
  34. * @return bool
  35. */
  36. public static function dependencies_met() {
  37. $php_support = version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '>=' );
  38. return $php_support && apply_filters( 'action_scheduler_migration_dependencies_met', true );
  39. }
  40. /**
  41. * Get a flag indicating whether the migration is complete.
  42. *
  43. * @return bool Whether the flag has been set marking the migration as complete
  44. */
  45. public static function is_migration_complete() {
  46. return get_option( self::STATUS_FLAG ) === self::STATUS_COMPLETE;
  47. }
  48. /**
  49. * Mark the migration as complete.
  50. */
  51. public static function mark_migration_complete() {
  52. update_option( self::STATUS_FLAG, self::STATUS_COMPLETE );
  53. }
  54. /**
  55. * Unmark migration when a plugin is de-activated. Will not work in case of silent activation, for example in an update.
  56. * We do this to mitigate the bug of lost actions which happens if there was an AS 2.x to AS 3.x migration in the past, but that plugin is now
  57. * deactivated and the site was running on AS 2.x again.
  58. */
  59. public static function mark_migration_incomplete() {
  60. delete_option( self::STATUS_FLAG );
  61. }
  62. /**
  63. * Set the action store class name.
  64. *
  65. * @param string $class Classname of the store class.
  66. *
  67. * @return string
  68. */
  69. public static function set_store_class( $class ) {
  70. return self::DATASTORE_CLASS;
  71. }
  72. /**
  73. * Set the action logger class name.
  74. *
  75. * @param string $class Classname of the logger class.
  76. *
  77. * @return string
  78. */
  79. public static function set_logger_class( $class ) {
  80. return self::LOGGER_CLASS;
  81. }
  82. /**
  83. * Set the sleep time in seconds.
  84. *
  85. * @param integer $sleep_time The number of seconds to pause before resuming operation.
  86. */
  87. public static function set_sleep_time( $sleep_time ) {
  88. self::$sleep_time = (int) $sleep_time;
  89. }
  90. /**
  91. * Set the tick count required for freeing memory.
  92. *
  93. * @param integer $free_ticks The number of ticks to free memory on.
  94. */
  95. public static function set_free_ticks( $free_ticks ) {
  96. self::$free_ticks = (int) $free_ticks;
  97. }
  98. /**
  99. * Free memory if conditions are met.
  100. *
  101. * @param int $ticks Current tick count.
  102. */
  103. public static function maybe_free_memory( $ticks ) {
  104. if ( self::$free_ticks && 0 === $ticks % self::$free_ticks ) {
  105. self::free_memory();
  106. }
  107. }
  108. /**
  109. * Reduce memory footprint by clearing the database query and object caches.
  110. */
  111. public static function free_memory() {
  112. if ( 0 < self::$sleep_time ) {
  113. /* translators: %d: amount of time */
  114. \WP_CLI::warning( sprintf( _n( 'Stopped the insanity for %d second', 'Stopped the insanity for %d seconds', self::$sleep_time, 'woocommerce' ), self::$sleep_time ) );
  115. sleep( self::$sleep_time );
  116. }
  117. \WP_CLI::warning( __( 'Attempting to reduce used memory...', 'woocommerce' ) );
  118. /**
  119. * @var $wpdb \wpdb
  120. * @var $wp_object_cache \WP_Object_Cache
  121. */
  122. global $wpdb, $wp_object_cache;
  123. $wpdb->queries = array();
  124. if ( ! is_a( $wp_object_cache, 'WP_Object_Cache' ) ) {
  125. return;
  126. }
  127. $wp_object_cache->group_ops = array();
  128. $wp_object_cache->stats = array();
  129. $wp_object_cache->memcache_debug = array();
  130. $wp_object_cache->cache = array();
  131. if ( is_callable( array( $wp_object_cache, '__remoteset' ) ) ) {
  132. call_user_func( array( $wp_object_cache, '__remoteset' ) ); // important
  133. }
  134. }
  135. /**
  136. * Connect to table datastores if migration is complete.
  137. * Otherwise, proceed with the migration if the dependencies have been met.
  138. */
  139. public static function init() {
  140. if ( self::is_migration_complete() ) {
  141. add_filter( 'action_scheduler_store_class', array( 'ActionScheduler_DataController', 'set_store_class' ), 100 );
  142. add_filter( 'action_scheduler_logger_class', array( 'ActionScheduler_DataController', 'set_logger_class' ), 100 );
  143. add_action( 'deactivate_plugin', array( 'ActionScheduler_DataController', 'mark_migration_incomplete' ) );
  144. } elseif ( self::dependencies_met() ) {
  145. Controller::init();
  146. }
  147. add_action( 'action_scheduler/progress_tick', array( 'ActionScheduler_DataController', 'maybe_free_memory' ) );
  148. }
  149. /**
  150. * Singleton factory.
  151. */
  152. public static function instance() {
  153. if ( ! isset( self::$instance ) ) {
  154. self::$instance = new static();
  155. }
  156. return self::$instance;
  157. }
  158. }