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 / WP_CLI / Migration_Command.php

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

151 lines 4.8 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\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 /**
25 * @var int
26 */
27 private $total_processed = 0;
28
29 /**
30 * Register the command with WP-CLI
31 */
32 public function register() {
33 if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
34 return;
35 }
36
37 WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [
38 'shortdesc' => 'Migrates actions to the DB tables store',
39 'synopsis' => [
40 [
41 'type' => 'assoc',
42 'name' => 'batch-size',
43 'optional' => true,
44 'default' => 100,
45 'description' => 'The number of actions to process in each batch',
46 ],
47 [
48 'type' => 'assoc',
49 'name' => 'free-memory-on',
50 'optional' => true,
51 'default' => 50,
52 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory',
53 ],
54 [
55 'type' => 'assoc',
56 'name' => 'pause',
57 'optional' => true,
58 'default' => 0,
59 'description' => 'The number of seconds to pause when freeing memory',
60 ],
61 [
62 'type' => 'flag',
63 'name' => 'dry-run',
64 'optional' => true,
65 'description' => 'Reports on the actions that would have been migrated, but does not change any data',
66 ],
67 ],
68 ] );
69 }
70
71 /**
72 * Process the data migration.
73 *
74 * @param array $positional_args Required for WP CLI. Not used in migration.
75 * @param array $assoc_args Optional arguments.
76 *
77 * @return void
78 */
79 public function migrate( $positional_args, $assoc_args ) {
80 $this->init_logging();
81
82 $config = $this->get_migration_config( $assoc_args );
83 $runner = new Runner( $config );
84 $runner->init_destination();
85
86 $batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100;
87 $free_on = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50;
88 $sleep = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0;
89 \ActionScheduler_DataController::set_free_ticks( $free_on );
90 \ActionScheduler_DataController::set_sleep_time( $sleep );
91
92 do {
93 $actions_processed = $runner->run( $batch_size );
94 $this->total_processed += $actions_processed;
95 } while ( $actions_processed > 0 );
96
97 if ( ! $config->get_dry_run() ) {
98 // let the scheduler know that there's nothing left to do
99 $scheduler = new Scheduler();
100 $scheduler->mark_complete();
101 }
102
103 WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) );
104 }
105
106 /**
107 * Build the config object used to create the Runner
108 *
109 * @param array $args Optional arguments.
110 *
111 * @return ActionScheduler\Migration\Config
112 */
113 private function get_migration_config( $args ) {
114 $args = wp_parse_args( $args, [
115 'dry-run' => false,
116 ] );
117
118 $config = Controller::instance()->get_migration_config_object();
119 $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) );
120
121 return $config;
122 }
123
124 /**
125 * Hook command line logging into migration actions.
126 */
127 private function init_logging() {
128 add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) {
129 WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) );
130 }, 10, 1 );
131 add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) {
132 WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) );
133 }, 10, 1 );
134 add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) {
135 WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) );
136 }, 10, 1 );
137 add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) {
138 WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) );
139 }, 10, 2 );
140 add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) {
141 WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) );
142 }, 10, 2 );
143 add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) {
144 WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) );
145 }, 10, 1 );
146 add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) {
147 WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) );
148 }, 10, 1 );
149 }
150 }
151