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