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