Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7930c5

Browse files
authored
Merge pull request#1 from outlandishideas/add-support-for-profile
Add Support For Profile
2 parentse25e4ca +2feac6e commitc7930c5

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#WP Sync DB CLI
2+
23
An addon for[WP Sync DB](https://github.com/slang800/wp-sync-db) that allows you to execute migrations using a function call or via WP-CLI

‎class/command.php‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,108 @@ public function migrate( $args, $assoc_args ) {
3535
return;
3636
}
3737

38+
/**
39+
* Create a profile
40+
*
41+
* ## OPTIONS
42+
*
43+
* <profile>
44+
* : Name of the new profile to create
45+
*
46+
* [--remote-wordpress=<value>]
47+
* : Remote WordPress location to migrate database from.
48+
*
49+
* This is the first part the whole token you are told to copy from
50+
* the /wp-admin/ backend of Migrate DB under Settings, before the newline.
51+
*
52+
* [--token=<value>]
53+
* : Token from WordPress location to migrate database from.
54+
*
55+
* This is the second part the whole token you are told to copy from
56+
* the /wp-admin/ backend of Migrate DB under Settings, after the newline.
57+
*
58+
* [--migrate-tables=<CSV of tables to migrate>]
59+
* : Comma separated list of tables to migrate from the remote WordPress to locally.
60+
*
61+
* [--exclude-post-types=<CSV of tables to migrate>]
62+
* : Comma separated list of post types to exclude from migrate from the remote WordPress to locally.
63+
*
64+
* [--<WP-Migrate-Profile-Option-As-Kebab-Case>=<true|false>]
65+
* : Set any Migrate DB option in this profile.
66+
*
67+
* ## EXAMPLES
68+
*
69+
* # Setup a profile with the defaults called Staging.
70+
* $ wp wpsdb create-profile Staging --remote-wordpress https://wordpress.example \
71+
* --token CUOu2t5kaVienGLUxAGhN4bvWh1FXqJA
72+
*
73+
* # Setup a profile with the defaults called Staging that only migrates the wp_posts table.
74+
* $ wp wpsdb create-profile Staging --remote-wordpress https://wordpress.example \
75+
* --token CUOu2t5kaVienGLUxAGhN4bvWh1FXqJA \
76+
* --migrate-tables=wp_posts
77+
*
78+
* # Setup a profile with the defaults called Staging that excludes WordPress page post types.
79+
* $ wp wpsdb create-profile Staging --remote-wordpress https://wordpress.example \
80+
* --token CUOu2t5kaVienGLUxAGhN4bvWh1FXqJA \
81+
* --exclude-post-types=page
82+
*
83+
* # Setup a profile with the defaults called Staging that creates backups when migrations are run.
84+
* $ wp wpsdb create-profile Staging --remote-wordpress https://wordpress.example \
85+
* --token CUOu2t5kaVienGLUxAGhN4bvWh1FXqJA \
86+
* --create_backup=true
87+
*
88+
* @synopsis <profile>
89+
*
90+
* @since 1.0
91+
* TODO: Incorporate the above into a proper help message.
92+
*/
93+
/**
94+
* @subcommand create-profile
95+
*/
96+
publicfunctioncreate_profile($args,$assoc_args ) {
97+
$name =$args[0];
98+
99+
if ( !isset($assoc_args['remote-wordpress'] ) || !isset($assoc_args['token'] ) ) {
100+
$error_lines =array();
101+
$error_lines[] =__('Connection information to remote WordPress installation for migration is required.' );
102+
$error_lines[] =__('Set with --remote-wordpress and --token flag.' );
103+
WP_CLI::error_multi_line($error_lines );
104+
return;
105+
}
106+
107+
$result =wpdsb_create_profile($name,$assoc_args );
108+
109+
if (true ===$result ) {
110+
WP_CLI::success(sprintf(__('Profile\'%1$s\' created.','wp-sync-db-cli' ),$name ) );
111+
return;
112+
}
113+
114+
WP_CLI::error($result->get_error_message() );
115+
return;
116+
}
117+
118+
/**
119+
* Echo out the DB token. For use in automation.
120+
*
121+
* ## EXAMPLES
122+
*
123+
* wp wpsdb connection-info
124+
*
125+
* @since 1.0
126+
* @subcommand connection-info
127+
*/
128+
publicfunctionconnection_info($args,$assoc_args ) {
129+
$result =wpsdb_cli_connection_info();
130+
131+
if ($result ) {
132+
print($result );
133+
return;
134+
}
135+
136+
WP_CLI::warning($result->get_error_message() );
137+
return;
138+
}
139+
38140
}
39141

40142
WP_CLI::add_command('wpsdb','WPSDBCLI' );

‎class/wpsdb-cli.php‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,132 @@ function __construct( $plugin_file_path ) {
1010
if( !$this->meets_version_requirements('1.4b1' ) )return;
1111
}
1212

13+
functionlog_list($list ) {
14+
foreach ($listas$item ) {
15+
WP_CLI::log("-$item" );
16+
}
17+
}
18+
19+
functionkebab_case_to_snake_case($value ) {
20+
returnpreg_replace_callback('/-(.)/u',function($el) {
21+
return'_' .$el[1];
22+
},$value);
23+
}
24+
25+
functionvalues_as_booleans_to_one_and_zero_strings($array ) {
26+
$result = [];
27+
28+
foreach ($arrayas$key =>$value) {
29+
if ($value ==='true' ||$value ==='false' ) {
30+
$result[$key] = (string)(int)($value ==='true');
31+
continue;
32+
}
33+
34+
$result[$key] =$value;
35+
}
36+
37+
return$result;
38+
}
39+
40+
functionkeys_from_kebab_case_to_snake_case($array ) {
41+
$result = [];
42+
43+
foreach ($arrayas$key =>$value) {
44+
$result[$this->kebab_case_to_snake_case($key ) ] =$value;
45+
}
46+
47+
return$result;
48+
}
49+
50+
functioncli_connection_info( ) {
51+
$wpsdb_settings =get_option('wpsdb_settings' );
52+
53+
if ($wpsdb_settings['key'] ) {
54+
return$wpsdb_settings['key'];
55+
}else {
56+
return$this->cli_error(__('Key could not be found in Migrate DB - this is not expected.','wp-sync-db-cli' ) );
57+
}
58+
}
59+
60+
functioncli_create_profile($name,$assoc_args ) {
61+
$wpsdb_settings =get_option('wpsdb_settings' );
62+
63+
$profile_exists =false;
64+
65+
foreach ($wpsdb_settings['profiles']as$profile ) {
66+
if ($profile['name'] ===$name ) {
67+
$profile_exists =true;
68+
break;
69+
}
70+
}
71+
72+
if ($profile_exists ) {
73+
return$this->cli_error(sprintf(__('Profile with the name\'%1$s\' already exists.','wp-sync-db-cli' ),$name ) );
74+
}
75+
76+
WP_CLI::log(sprintf(__('Creating database migration profile with name\'%1$s\'.','wp-sync-db-cli' ),$name ) );
77+
78+
$new_profile =array();
79+
80+
$new_profile["name"] =$name;
81+
$new_profile["create_new_profile"] =$name;
82+
83+
$new_profile["action"] ="pull";
84+
85+
$new_profile['connection_info'] =implode("\n",array($assoc_args['remote-wordpress'],$assoc_args['token'] ) );
86+
unset($assoc_args['remote-wordpress'] );
87+
unset($assoc_args['token'] );
88+
89+
$new_profile["table_migrate_option"] ='migrate_only_with_prefix';
90+
91+
// This replacement is typically done by calling the other WordPress site all
92+
// retrieving its variables.
93+
//
94+
// For the moment this is a TODO
95+
$new_profile["replace_old"] =array();
96+
$new_profile["replace_new"] =array();
97+
98+
$new_profile["save_computer"] ="1";
99+
$new_profile["gzip_file"] ="1";
100+
$new_profile["replace_guids"] ="1";
101+
$new_profile["exclude_spam"] ="1";
102+
$new_profile["keep_active_plugins"] ="1";
103+
$new_profile["create_backup"] ="0";
104+
$new_profile["backup_option"] ="backup_selected";
105+
106+
$new_profile["exclude_post_types"] ="0";
107+
$new_profile["exclude_transients"] ="1";
108+
$new_profile["media_files"] ="1";
109+
110+
$new_profile["save_migration_profile"] ="1";
111+
$new_profile["save_migration_profile_option"] ="new";
112+
113+
$values_for_wordpress =$this->keys_from_kebab_case_to_snake_case($assoc_args );
114+
$values_for_wordpress =$this->values_as_booleans_to_one_and_zero_strings($values_for_wordpress );
115+
$new_profile =array_merge($new_profile,$values_for_wordpress );
116+
117+
if (isset($assoc_args['exclude-post-types'] ) ) {
118+
$new_profile['exclude_post_types'] ='1';
119+
$new_profile['select_post_types'] =explode(',',$assoc_args['exclude-post-types'] );
120+
121+
WP_CLI::log(__('Excluding WordPress post types from migration profile:','wp-sync-db-cli' ) );
122+
$this->log_list($new_profile['select_post_types'] );
123+
}
124+
125+
if (isset($assoc_args['migrate-tables'] ) ) {
126+
$new_profile["table_migrate_option"] ='migrate_select';
127+
128+
$new_profile["select_tables"] =explode(',',$assoc_args['migrate-tables'] );
129+
WP_CLI::log(__('The following tables are selected for migration:','wp-sync-db-cli' ) );
130+
$this->log_list($new_profile["select_tables"] );
131+
}
132+
133+
$wpsdb_settings['profiles'][] =$new_profile;
134+
update_option('wpsdb_settings',$wpsdb_settings );
135+
136+
returntrue;
137+
}
138+
13139
functioncli_migration($profile ) {
14140
global$wpsdb;
15141
$wpsdb_settings =get_option('wpsdb_settings' );

‎wp-sync-db-cli.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ function wpsdb_migrate( $profile ) {
3636
}
3737
return$wpsdb_cli->cli_migration($profile );
3838
}
39+
40+
functionwpsdb_cli_connection_info() {
41+
global$wpsdb_cli;
42+
if(empty($wpsdb_cli ) ) {
43+
returnnewWP_Error('wpsdb_cli_error',__('WP Sync DB CLI class not available','wp-sync-db-cli' ) );
44+
}
45+
return$wpsdb_cli->cli_connection_info();
46+
}
47+
48+
functionwpdsb_create_profile($name,$assoc_args ) {
49+
global$wpsdb_cli;
50+
if(empty($wpsdb_cli ) ) {
51+
returnnewWP_Error('wpsdb_cli_error',__('WP Sync DB CLI class not available','wp-sync-db-cli' ) );
52+
}
53+
return$wpsdb_cli->cli_create_profile($name,$assoc_args );
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp