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