PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / packages / action-scheduler / classes / migration / Scheduler.php

Scheduler.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at packages/action-scheduler/classes/migration/Scheduler.php

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