@@ -10,6 +10,132 @@ function __construct( $plugin_file_path ) {
1010if ( !$ this ->meets_version_requirements ('1.4b1 ' ) )return ;
1111}
1212
13+ function log_list ($ list ) {
14+ foreach ($ listas $ item ) {
15+ WP_CLI ::log ("- $ item " );
16+ }
17+ }
18+
19+ function kebab_case_to_snake_case ($ value ) {
20+ return preg_replace_callback ('/-(.)/u ' ,function ($ el ) {
21+ return '_ ' .$ el [1 ];
22+ },$ value );
23+ }
24+
25+ function values_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+ function keys_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+ function cli_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+ function cli_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+ return true ;
137+ }
138+
13139function cli_migration ($ profile ) {
14140global $ wpsdb ;
15141$ wpsdb_settings =get_option ('wpsdb_settings ' );