Skip to content

Commit 77633fe

Browse files
author
Joseph D'Souza
committed
Update the command signature to use optional parameters. Update the manual_profile defaults to only need connection info and action in order to work.
1 parent b3dc3ac commit 77633fe

File tree

1 file changed

+68
-18
lines changed

1 file changed

+68
-18
lines changed

class/command.php

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,103 @@
33
/**
44
* Migrate your DB using WP Sync DB.
55
*/
6-
class WPSDBCLI extends WP_CLI_Command {
6+
class WPSDBCLI extends WP_CLI_Command
7+
{
78

89
/**
9-
* Run a migration.
10+
* Run a migration. Either profile id or connection-info + action are required.
1011
*
1112
* ## OPTIONS
1213
*
13-
* <profile>
14+
* [--profile=<int>]
1415
* : 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+
* ---
1528
*
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+
*
1638
* ## EXAMPLES
1739
*
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
1942
*
20-
* @synopsis <profile>
43+
* @synopsis [--profile=<int>] [--connection-info=<string>] [--action=<string>] [--create-backup=<bit>]
2144
*
2245
* @since 1.0
2346
*/
24-
public function migrate( $args, $assoc_args ) {
25-
$profile = $args[0];
47+
public function migrate($args, $assoc_args)
48+
{
49+
$profile = null;
2650
$manual_profile = [];
2751

2852
// Target manually (maybe no database available yet)
53+
54+
if ($assoc_args['profile']) {
55+
$profile = $assoc_args['profile'];
56+
}
2957
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
3064
$manual_profile = array(
31-
'connection_info' => $assoc_args['connection-info'],
65+
'connection_info' => $connection_info,
3266
'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",
3669
'select_backup' => null,
37-
'table_migrate_option' => null,
3870
'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
3984
);
4085
}
4186

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);
4394

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'));
4697
return;
4798
}
4899

49-
WP_CLI::warning( $result->get_error_message() );
100+
WP_CLI::warning($result->get_error_message());
50101
return;
51102
}
52-
53103
}
54104

55-
WP_CLI::add_command( 'wpsdb', 'WPSDBCLI' );
105+
WP_CLI::add_command('wpsdb', 'WPSDBCLI');

0 commit comments

Comments
 (0)