| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Cli; |
| 4 |
|
| 5 |
use StoreEngine\Classes\AccountMover; |
| 6 |
use WP_CLI; |
| 7 |
use WP_CLI_Command; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class Account extends WP_CLI_Command { |
| 14 |
|
| 15 |
/** |
| 16 |
* Move a customer's StoreEngine data (orders, subscriptions, licenses, etc.) |
| 17 |
* from one WordPress user to another. Neither account is deleted — only the |
| 18 |
* ownership of the records changes. The transfer is additive. |
| 19 |
* |
| 20 |
* ## OPTIONS |
| 21 |
* |
| 22 |
* --from=<from> |
| 23 |
* : Source user ID (data is moved FROM here). |
| 24 |
* |
| 25 |
* --to=<to> |
| 26 |
* : Target user ID (data is moved TO here). |
| 27 |
* |
| 28 |
* [--dry-run] |
| 29 |
* : Show what would move without changing anything. |
| 30 |
* |
| 31 |
* [--yes] |
| 32 |
* : Skip the confirmation prompt. |
| 33 |
* |
| 34 |
* ## EXAMPLES |
| 35 |
* |
| 36 |
* wp storeengine account move --from=12 --to=34 --dry-run |
| 37 |
* wp storeengine account move --from=12 --to=34 --yes |
| 38 |
* |
| 39 |
* @subcommand move |
| 40 |
*/ |
| 41 |
public function move( $args, $assoc_args ) { |
| 42 |
$from = isset( $assoc_args['from'] ) ? absint( $assoc_args['from'] ) : 0; |
| 43 |
$to = isset( $assoc_args['to'] ) ? absint( $assoc_args['to'] ) : 0; |
| 44 |
$dry_run = isset( $assoc_args['dry-run'] ); |
| 45 |
|
| 46 |
if ( ! $from || ! $to ) { |
| 47 |
WP_CLI::error( 'Please provide both --from=<id> and --to=<id>.' ); |
| 48 |
} |
| 49 |
|
| 50 |
$mover = new AccountMover( $from, $to ); |
| 51 |
$preview = $mover->preview(); |
| 52 |
|
| 53 |
if ( is_wp_error( $preview ) ) { |
| 54 |
WP_CLI::error( $preview->get_error_message() ); |
| 55 |
} |
| 56 |
|
| 57 |
$items = []; |
| 58 |
foreach ( $preview as $table => $count ) { |
| 59 |
$items[] = [ |
| 60 |
'Entity' => str_replace( 'storeengine_', '', $table ), |
| 61 |
'Rows' => $count, |
| 62 |
]; |
| 63 |
} |
| 64 |
WP_CLI\Utils\format_items( 'table', $items, [ 'Entity', 'Rows' ] ); |
| 65 |
|
| 66 |
$total = array_sum( $preview ); |
| 67 |
|
| 68 |
if ( $dry_run ) { |
| 69 |
WP_CLI::success( sprintf( 'Dry run: %d record(s) would move from user #%d to user #%d.', $total, $from, $to ) ); |
| 70 |
|
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if ( 0 === $total ) { |
| 75 |
WP_CLI::warning( 'Nothing to move — the source user owns no StoreEngine records.' ); |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
WP_CLI::confirm( sprintf( 'Move %d record(s) from user #%d to user #%d?', $total, $from, $to ), $assoc_args ); |
| 81 |
|
| 82 |
$result = $mover->move(); |
| 83 |
|
| 84 |
if ( is_wp_error( $result ) ) { |
| 85 |
WP_CLI::error( $result->get_error_message() ); |
| 86 |
} |
| 87 |
|
| 88 |
WP_CLI::success( sprintf( 'Moved %d record(s) from user #%d to user #%d.', array_sum( $result['moved'] ), $from, $to ) ); |
| 89 |
} |
| 90 |
} |
| 91 |
|