WP_CLI
1 year ago
abstracts
1 year ago
actions
1 year ago
data-stores
1 year ago
migration
1 year ago
schedules
1 year ago
schema
1 year ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
1 year ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
4 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
4 years ago
index.php
3 years ago
ActionScheduler_DataController.php
79 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | use Action_Scheduler\Migration\Controller; |
| 4 | class ActionScheduler_DataController { |
| 5 | const DATASTORE_CLASS = 'ActionScheduler_DBStore'; |
| 6 | const LOGGER_CLASS = 'ActionScheduler_DBLogger'; |
| 7 | const STATUS_FLAG = 'action_scheduler_migration_status'; |
| 8 | const STATUS_COMPLETE = 'complete'; |
| 9 | const MIN_PHP_VERSION = '5.5'; |
| 10 | private static $instance; |
| 11 | private static $sleep_time = 0; |
| 12 | private static $free_ticks = 50; |
| 13 | public static function dependencies_met() { |
| 14 | $php_support = version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '>=' ); |
| 15 | return $php_support && apply_filters( 'action_scheduler_migration_dependencies_met', true ); |
| 16 | } |
| 17 | public static function is_migration_complete() { |
| 18 | return get_option( self::STATUS_FLAG ) === self::STATUS_COMPLETE; |
| 19 | } |
| 20 | public static function mark_migration_complete() { |
| 21 | update_option( self::STATUS_FLAG, self::STATUS_COMPLETE ); |
| 22 | } |
| 23 | public static function mark_migration_incomplete() { |
| 24 | delete_option( self::STATUS_FLAG ); |
| 25 | } |
| 26 | public static function set_store_class( $class ) { |
| 27 | return self::DATASTORE_CLASS; |
| 28 | } |
| 29 | public static function set_logger_class( $class ) { |
| 30 | return self::LOGGER_CLASS; |
| 31 | } |
| 32 | public static function set_sleep_time( $sleep_time ) { |
| 33 | self::$sleep_time = (int) $sleep_time; |
| 34 | } |
| 35 | public static function set_free_ticks( $free_ticks ) { |
| 36 | self::$free_ticks = (int) $free_ticks; |
| 37 | } |
| 38 | public static function maybe_free_memory( $ticks ) { |
| 39 | if ( self::$free_ticks && 0 === $ticks % self::$free_ticks ) { |
| 40 | self::free_memory(); |
| 41 | } |
| 42 | } |
| 43 | public static function free_memory() { |
| 44 | if ( 0 < self::$sleep_time ) { |
| 45 | \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 ) ); |
| 46 | sleep( self::$sleep_time ); |
| 47 | } |
| 48 | \WP_CLI::warning( __( 'Attempting to reduce used memory...', 'action-scheduler' ) ); |
| 49 | global $wpdb, $wp_object_cache; |
| 50 | $wpdb->queries = array(); |
| 51 | if ( ! is_a( $wp_object_cache, 'WP_Object_Cache' ) ) { |
| 52 | return; |
| 53 | } |
| 54 | $wp_object_cache->group_ops = array(); |
| 55 | $wp_object_cache->stats = array(); |
| 56 | $wp_object_cache->memcache_debug = array(); |
| 57 | $wp_object_cache->cache = array(); |
| 58 | if ( is_callable( array( $wp_object_cache, '__remoteset' ) ) ) { |
| 59 | call_user_func( array( $wp_object_cache, '__remoteset' ) ); // important! |
| 60 | } |
| 61 | } |
| 62 | public static function init() { |
| 63 | if ( self::is_migration_complete() ) { |
| 64 | add_filter( 'action_scheduler_store_class', array( 'ActionScheduler_DataController', 'set_store_class' ), 100 ); |
| 65 | add_filter( 'action_scheduler_logger_class', array( 'ActionScheduler_DataController', 'set_logger_class' ), 100 ); |
| 66 | add_action( 'deactivate_plugin', array( 'ActionScheduler_DataController', 'mark_migration_incomplete' ) ); |
| 67 | } elseif ( self::dependencies_met() ) { |
| 68 | Controller::init(); |
| 69 | } |
| 70 | add_action( 'action_scheduler/progress_tick', array( 'ActionScheduler_DataController', 'maybe_free_memory' ) ); |
| 71 | } |
| 72 | public static function instance() { |
| 73 | if ( ! isset( self::$instance ) ) { |
| 74 | self::$instance = new static(); |
| 75 | } |
| 76 | return self::$instance; |
| 77 | } |
| 78 | } |
| 79 |