PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.1
2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / lib / action-scheduler / classes / WP_CLI / Migration_Command.php
sureforms / inc / lib / action-scheduler / classes / WP_CLI Last commit date
ActionScheduler_WPCLI_Clean_Command.php 2 years ago ActionScheduler_WPCLI_QueueRunner.php 2 years ago ActionScheduler_WPCLI_Scheduler_command.php 2 years ago Migration_Command.php 2 years ago ProgressBar.php 2 years ago
Migration_Command.php
191 lines
1 <?php
2
3
4 namespace Action_Scheduler\WP_CLI;
5
6 use Action_Scheduler\Migration\Config;
7 use Action_Scheduler\Migration\Runner;
8 use Action_Scheduler\Migration\Scheduler;
9 use Action_Scheduler\Migration\Controller;
10 use WP_CLI;
11 use WP_CLI_Command;
12
13 /**
14 * Class Migration_Command
15 *
16 * @package Action_Scheduler\WP_CLI
17 *
18 * @since 3.0.0
19 *
20 * @codeCoverageIgnore
21 */
22 class Migration_Command extends WP_CLI_Command {
23
24 /** @var int */
25 private $total_processed = 0;
26
27 /**
28 * Register the command with WP-CLI
29 */
30 public function register() {
31 if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
32 return;
33 }
34
35 WP_CLI::add_command(
36 'action-scheduler migrate',
37 [ $this, 'migrate' ],
38 [
39 'shortdesc' => 'Migrates actions to the DB tables store',
40 'synopsis' => [
41 [
42 'type' => 'assoc',
43 'name' => 'batch-size',
44 'optional' => true,
45 'default' => 100,
46 'description' => 'The number of actions to process in each batch',
47 ],
48 [
49 'type' => 'assoc',
50 'name' => 'free-memory-on',
51 'optional' => true,
52 'default' => 50,
53 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory',
54 ],
55 [
56 'type' => 'assoc',
57 'name' => 'pause',
58 'optional' => true,
59 'default' => 0,
60 'description' => 'The number of seconds to pause when freeing memory',
61 ],
62 [
63 'type' => 'flag',
64 'name' => 'dry-run',
65 'optional' => true,
66 'description' => 'Reports on the actions that would have been migrated, but does not change any data',
67 ],
68 ],
69 ]
70 );
71 }
72
73 /**
74 * Process the data migration.
75 *
76 * @param array $positional_args Required for WP CLI. Not used in migration.
77 * @param array $assoc_args Optional arguments.
78 *
79 * @return void
80 */
81 public function migrate( $positional_args, $assoc_args ) {
82 $this->init_logging();
83
84 $config = $this->get_migration_config( $assoc_args );
85 $runner = new Runner( $config );
86 $runner->init_destination();
87
88 $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100;
89 $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50;
90 $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0;
91 \ActionScheduler_DataController::set_free_ticks( $free_on );
92 \ActionScheduler_DataController::set_sleep_time( $sleep );
93
94 do {
95 $actions_processed = $runner->run( $batch_size );
96 $this->total_processed += $actions_processed;
97 } while ( $actions_processed > 0 );
98
99 if ( ! $config->get_dry_run() ) {
100 // let the scheduler know that there's nothing left to do
101 $scheduler = new Scheduler();
102 $scheduler->mark_complete();
103 }
104
105 WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) );
106 }
107
108 /**
109 * Build the config object used to create the Runner
110 *
111 * @param array $args Optional arguments.
112 *
113 * @return ActionScheduler\Migration\Config
114 */
115 private function get_migration_config( $args ) {
116 $args = wp_parse_args(
117 $args,
118 [
119 'dry-run' => false,
120 ]
121 );
122
123 $config = Controller::instance()->get_migration_config_object();
124 $config->set_dry_run( ! empty( $args['dry-run'] ) );
125
126 return $config;
127 }
128
129 /**
130 * Hook command line logging into migration actions.
131 */
132 private function init_logging() {
133 add_action(
134 'action_scheduler/migrate_action_dry_run',
135 function ( $action_id ) {
136 WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) );
137 },
138 10,
139 1
140 );
141 add_action(
142 'action_scheduler/no_action_to_migrate',
143 function ( $action_id ) {
144 WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) );
145 },
146 10,
147 1
148 );
149 add_action(
150 'action_scheduler/migrate_action_failed',
151 function ( $action_id ) {
152 WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) );
153 },
154 10,
155 1
156 );
157 add_action(
158 'action_scheduler/migrate_action_incomplete',
159 function ( $source_id, $destination_id ) {
160 WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) );
161 },
162 10,
163 2
164 );
165 add_action(
166 'action_scheduler/migrated_action',
167 function ( $source_id, $destination_id ) {
168 WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) );
169 },
170 10,
171 2
172 );
173 add_action(
174 'action_scheduler/migration_batch_starting',
175 function ( $batch ) {
176 WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) );
177 },
178 10,
179 1
180 );
181 add_action(
182 'action_scheduler/migration_batch_complete',
183 function ( $batch ) {
184 WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) );
185 },
186 10,
187 1
188 );
189 }
190 }
191