|
3 | 3 | /**
|
4 | 4 | * Migrate your DB using WP Sync DB.
|
5 | 5 | */
|
6 |
| -class WPSDBCLI extends WP_CLI_Command { |
| 6 | +class WPSDBCLI extends WP_CLI_Command |
| 7 | +{ |
7 | 8 |
|
8 | 9 | /**
|
9 |
| - * Run a migration. |
| 10 | + * Run a migration. Either profile id or connection-info + action are required. |
10 | 11 | *
|
11 | 12 | * ## OPTIONS
|
12 | 13 | *
|
13 |
| - * <profile> |
| 14 | + * [--profile=<int>] |
14 | 15 | * : ID of the profile to use for the migration.
|
| 16 | + * |
| 17 | + * [--connection-info=<string>] |
| 18 | + * : Manual connection info for when a profile by the ID is not found. The above ID will be used to save a copy |
| 19 | + * |
| 20 | + * [--action=<string>] |
| 21 | + * : The type of action to perform against the target connection |
| 22 | + * --- |
| 23 | + * default: pull |
| 24 | + * options: |
| 25 | + * - pull |
| 26 | + * - push |
| 27 | + * --- |
15 | 28 | *
|
| 29 | + * [--create-backup=<bit>] |
| 30 | + * : Whether to take a backup before running the action. |
| 31 | + * --- |
| 32 | + * default: 0 |
| 33 | + * options: |
| 34 | + * - 0 |
| 35 | + * - 1 |
| 36 | + * --- |
| 37 | + * |
16 | 38 | * ## EXAMPLES
|
17 | 39 | *
|
18 |
| - * wp wpsdb migrate 1 |
| 40 | + * wp wpsdb migrate --profile=1 |
| 41 | + * wp wpsdb migrate --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1 |
19 | 42 | *
|
20 |
| - * @synopsis <profile> |
| 43 | + * @synopsis [--profile=<int>] [--connection-info=<string>] [--action=<string>] [--create-backup=<bit>] |
21 | 44 | *
|
22 | 45 | * @since 1.0
|
23 | 46 | */
|
24 |
| - public function migrate( $args, $assoc_args ) { |
25 |
| - $profile = $args[0]; |
| 47 | + public function migrate($args, $assoc_args) |
| 48 | + { |
| 49 | + $profile = null; |
26 | 50 | $manual_profile = [];
|
27 | 51 |
|
28 | 52 | // Target manually (maybe no database available yet)
|
| 53 | + |
| 54 | + if ($assoc_args['profile']) { |
| 55 | + $profile = $assoc_args['profile']; |
| 56 | + } |
29 | 57 | if ($assoc_args['connection-info'] && $assoc_args['action']) {
|
| 58 | + // Preprocess some variables |
| 59 | + $connection_info = stripcslashes($assoc_args['connection-info']); |
| 60 | + $connection_info_segments = explode("\n", $connection_info); |
| 61 | + $friendly_name = preg_replace("(^https?://)", "", $connection_info_segments[0]); |
| 62 | + |
| 63 | + // Create a default profile, that will save afterwards |
30 | 64 | $manual_profile = array(
|
31 |
| - 'connection_info' => $assoc_args['connection-info'], |
| 65 | + 'connection_info' => $connection_info, |
32 | 66 | 'action' => $assoc_args['action'],
|
33 |
| - 'create_backup' => $assoc_args['create_backup'], |
34 |
| - 'backup_option' => null, |
35 |
| - 'prefixed_tables' => null, |
| 67 | + 'create_backup' => $assoc_args['create-backup'], |
| 68 | + 'backup_option' => "backup_only_with_prefix", |
36 | 69 | 'select_backup' => null,
|
37 |
| - 'table_migrate_option' => null, |
38 | 70 | 'select_tables' => null,
|
| 71 | + 'table_migrate_option' => "migrate_only_with_prefix", |
| 72 | + 'exclude_transients' => 1, |
| 73 | + 'media_files' => 1, |
| 74 | + 'remove_local_media' => 1, |
| 75 | + 'save_migration_profile_option' => 0, |
| 76 | + 'create_new_profile' => $friendly_name, |
| 77 | + 'name' => $friendly_name, |
| 78 | + 'save_computer' => 0, |
| 79 | + 'gzip_file' => 1, |
| 80 | + 'replace_guids' => 1, |
| 81 | + 'exclude_spam' => 0, |
| 82 | + 'keep_active_plugins' => 1, |
| 83 | + 'exclude_post_types' => 0 |
39 | 84 | );
|
40 | 85 | }
|
41 | 86 |
|
42 |
| - $result = wpsdb_migrate( $profile, $manual_profile ); |
| 87 | + if ($profile == null && empty($manual_profile)) { |
| 88 | + WP_CLI::warning(__('Either profile id or connection-info + action are required.', 'wp-sync-db-cli')); |
| 89 | + WP_CLI::log('Usage: wpsdb migrate [--profile=<int>] [--connection-info=<string>] [--action=<string>] [--create-backup=<bit>]'); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $result = wpsdb_migrate($profile, $manual_profile); |
43 | 94 |
|
44 |
| - if ( true === $result ) { |
45 |
| - WP_CLI::success( __( 'Migration successful.', 'wp-sync-db-cli' ) ); |
| 95 | + if (true === $result) { |
| 96 | + WP_CLI::success(__('Migration successful.', 'wp-sync-db-cli')); |
46 | 97 | return;
|
47 | 98 | }
|
48 | 99 |
|
49 |
| - WP_CLI::warning( $result->get_error_message() ); |
| 100 | + WP_CLI::warning($result->get_error_message()); |
50 | 101 | return;
|
51 | 102 | }
|
52 |
| - |
53 | 103 | }
|
54 | 104 |
|
55 |
| -WP_CLI::add_command( 'wpsdb', 'WPSDBCLI' ); |
| 105 | +WP_CLI::add_command('wpsdb', 'WPSDBCLI'); |
0 commit comments