| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Action_Scheduler\Migration; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Scheduler |
| 8 |
* |
| 9 |
* @package Action_Scheduler\WP_CLI |
| 10 |
* |
| 11 |
* @since 3.0.0 |
| 12 |
* |
| 13 |
* @codeCoverageIgnore |
| 14 |
*/ |
| 15 |
class Scheduler { |
| 16 |
/** Migration action hook. */ |
| 17 |
const HOOK = 'action_scheduler/migration_hook'; |
| 18 |
|
| 19 |
/** Migration action group. */ |
| 20 |
const GROUP = 'action-scheduler-migration'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Set up the callback for the scheduled job. |
| 24 |
*/ |
| 25 |
public function hook() { |
| 26 |
add_action( self::HOOK, array( $this, 'run_migration' ), 10, 0 ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Remove the callback for the scheduled job. |
| 31 |
*/ |
| 32 |
public function unhook() { |
| 33 |
remove_action( self::HOOK, array( $this, 'run_migration' ), 10 ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* The migration callback. |
| 38 |
*/ |
| 39 |
public function run_migration() { |
| 40 |
$migration_runner = $this->get_migration_runner(); |
| 41 |
$count = $migration_runner->run( $this->get_batch_size() ); |
| 42 |
|
| 43 |
if ( 0 === $count ) { |
| 44 |
$this->mark_complete(); |
| 45 |
} else { |
| 46 |
$this->schedule_migration( time() + $this->get_schedule_interval() ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Mark the migration complete. |
| 52 |
*/ |
| 53 |
public function mark_complete() { |
| 54 |
$this->unschedule_migration(); |
| 55 |
|
| 56 |
\ActionScheduler_DataController::mark_migration_complete(); |
| 57 |
do_action( 'action_scheduler/migration_complete' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get a flag indicating whether the migration is scheduled. |
| 62 |
* |
| 63 |
* @return bool Whether there is a pending action in the store to handle the migration |
| 64 |
*/ |
| 65 |
public function is_migration_scheduled() { |
| 66 |
$next = as_next_scheduled_action( self::HOOK ); |
| 67 |
|
| 68 |
return ! empty( $next ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Schedule the migration. |
| 73 |
* |
| 74 |
* @param int $when Optional timestamp to run the next migration batch. Defaults to now. |
| 75 |
* |
| 76 |
* @return string The action ID |
| 77 |
*/ |
| 78 |
public function schedule_migration( $when = 0 ) { |
| 79 |
$next = as_next_scheduled_action( self::HOOK ); |
| 80 |
|
| 81 |
if ( ! empty( $next ) ) { |
| 82 |
return $next; |
| 83 |
} |
| 84 |
|
| 85 |
if ( empty( $when ) ) { |
| 86 |
$when = time() + MINUTE_IN_SECONDS; |
| 87 |
} |
| 88 |
|
| 89 |
return as_schedule_single_action( $when, self::HOOK, array(), self::GROUP ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Remove the scheduled migration action. |
| 94 |
*/ |
| 95 |
public function unschedule_migration() { |
| 96 |
as_unschedule_action( self::HOOK, null, self::GROUP ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get migration batch schedule interval. |
| 101 |
* |
| 102 |
* @return int Seconds between migration runs. Defaults to 0 seconds to allow chaining migration via Async Runners. |
| 103 |
*/ |
| 104 |
private function get_schedule_interval() { |
| 105 |
return (int) apply_filters( 'action_scheduler/migration_interval', 0 ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get migration batch size. |
| 110 |
* |
| 111 |
* @return int Number of actions to migrate in each batch. Defaults to 250. |
| 112 |
*/ |
| 113 |
private function get_batch_size() { |
| 114 |
return (int) apply_filters( 'action_scheduler/migration_batch_size', 250 ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get migration runner object. |
| 119 |
* |
| 120 |
* @return Runner |
| 121 |
*/ |
| 122 |
private function get_migration_runner() { |
| 123 |
$config = Controller::instance()->get_migration_config_object(); |
| 124 |
|
| 125 |
return new Runner( $config ); |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|