1
+ <?php
2
+ class WPMDBPro_CLI extends WPMDBPro_Addon {
3
+
4
+ function __construct ( $ plugin_file_path ) {
5
+ parent ::__construct ( $ plugin_file_path );
6
+
7
+ $ this ->plugin_slug = 'wp-migrate-db-pro-cli ' ;
8
+ $ this ->plugin_version = $ GLOBALS ['wpmdb_meta ' ]['wp-migrate-db-pro-cli ' ]['version ' ];
9
+
10
+ if ( ! $ this ->meets_version_requirements ( '1.4b1 ' ) ) return ;
11
+ }
12
+
13
+ function cli_migration ( $ profile ) {
14
+ global $ wpmdbpro ;
15
+ $ wpmdb_settings = get_option ( 'wpmdb_settings ' );
16
+ --$ profile ;
17
+ if ( ! $ this ->meets_version_requirements ( '1.4b1 ' ) ) return $ this ->cli_error ( __ ( 'Please update WP Migrate DB Pro. ' , 'wp-migrate-db-pro-cli ' ) );
18
+ if ( ! isset ( $ profile ) ) return $ this ->cli_error ( __ ( 'Profile ID missing. ' , 'wp-migrate-db-pro-cli ' ) );
19
+ if ( ! isset ( $ wpmdb_settings ['profiles ' ][$ profile ] ) ) return $ this ->cli_error ( __ ( 'Profile ID not found. ' , 'wp-migrate-db-pro-cli ' ) );
20
+
21
+ $ this ->set_time_limit ();
22
+ $ wpmdbpro ->set_cli_migration ();
23
+
24
+ $ profile = apply_filters ( 'wpmdb_cli_profile_before_migration ' , $ wpmdb_settings ['profiles ' ][$ profile ] );
25
+ $ connection_info = explode ( "\n" , $ profile ['connection_info ' ] );
26
+ $ form_data = http_build_query ( $ profile );
27
+
28
+ if ( 'savefile ' == $ profile ['action ' ] ) return $ this ->cli_error ( __ ( 'Exports not supported for CLI migrations. Please instead select push or pull instead. ' , 'wp-migrate-db-pro-cli ' ) );
29
+
30
+ do_action ( 'wpmdb_cli_before_verify_connection_to_remote_site ' , $ profile );
31
+
32
+ // this request returns certain information from the remote machine, e.g. tables, prefix, bottleneck, gzip, etc
33
+ $ _POST ['intent ' ] = $ profile ['action ' ];
34
+ $ _POST ['url ' ] = trim ( $ connection_info [0 ] );
35
+ $ _POST ['key ' ] = trim ( $ connection_info [1 ] );
36
+ $ _POST = apply_filters ( 'wpmdb_cli_verify_connection_to_remote_site_args ' , $ _POST , $ profile );
37
+ $ response = $ wpmdbpro ->ajax_verify_connection_to_remote_site ();
38
+ if ( is_wp_error ( $ verify_connection_response = $ this ->verify_cli_response ( $ response , 'ajax_verify_connection_to_remote_site() ' ) ) ) return $ verify_connection_response ;
39
+
40
+ $ verify_connection_response = apply_filters ( 'wpmdb_cli_verify_connection_response ' , $ verify_connection_response );
41
+ do_action ( 'wpmdb_cli_before_initiate_migration ' , $ profile , $ verify_connection_response );
42
+
43
+ // this request does one last verification check and creates export / backup files (if required)
44
+ $ _POST ['form_data ' ] = $ form_data ;
45
+ $ _POST ['stage ' ] = ( '0 ' == $ profile ['create_backup ' ] ) ? 'migrate ' : 'backup ' ;
46
+ $ _POST = apply_filters ( 'wpmdb_cli_initiate_migration_args ' , $ _POST , $ profile , $ verify_connection_response );
47
+ $ response = $ wpmdbpro ->ajax_initiate_migration ();
48
+ if ( is_wp_error ( $ initiate_migration_response = $ this ->verify_cli_response ( $ response , 'ajax_initiate_migration() ' ) ) ) return $ initiate_migration_response ;
49
+
50
+ $ initiate_migration_response = apply_filters ( 'wpmdb_cli_initiate_migration_response ' , $ initiate_migration_response );
51
+
52
+ // determine which tables to backup (if required)
53
+ $ tables_to_backup = array ();
54
+ if ( 'backup ' == $ _POST ['stage ' ] ) {
55
+ if ( 'push ' == $ profile ['action ' ] ) {
56
+ if ( 'backup_only_with_prefix ' == $ profile ['backup_option ' ] ) {
57
+ $ tables_to_backup = $ verify_connection_response ['prefixed_tables ' ];
58
+ } elseif ( 'backup_selected ' == $ profile ['backup_option ' ] ) {
59
+ $ tables_to_backup = array_intersect ( $ profile ['select_backup ' ], $ verify_connection_response ['tables ' ] );
60
+ } elseif ( 'backup_manual_select ' == $ profile ['backup_option ' ] ) {
61
+ $ tables_to_backup = array_intersect ( $ profile ['select_backup ' ], $ verify_connection_response ['tables ' ] );
62
+ }
63
+ } else {
64
+ if ( 'backup_only_with_prefix ' == $ profile ['backup_option ' ] ) {
65
+ $ tables_to_backup = $ this ->get_tables ( 'prefix ' );
66
+ } elseif ( 'backup_selected ' == $ profile ['backup_option ' ] ) {
67
+ $ tables_to_backup = array_intersect ( $ profile ['select_backup ' ], $ this ->get_tables () );
68
+ } elseif ( 'backup_manual_select ' == $ profile ['backup_option ' ] ) {
69
+ $ tables_to_backup = array_intersect ( $ profile ['select_backup ' ], $ this ->get_tables () );
70
+ }
71
+ }
72
+ }
73
+ $ tables_to_backup = apply_filters ( 'wpmdb_cli_tables_to_backup ' , $ tables_to_backup , $ profile , $ verify_connection_response , $ initiate_migration_response );
74
+
75
+ // determine which tables to migrate
76
+ $ tables_to_migrate = array ();
77
+ if ( 'push ' == $ profile ['action ' ] ) {
78
+ if ( 'migrate_only_with_prefix ' == $ profile ['table_migrate_option ' ] ) {
79
+ $ tables_to_migrate = $ this ->get_tables ( 'prefix ' );
80
+ } elseif ( 'migrate_select ' == $ profile ['table_migrate_option ' ] ) {
81
+ $ tables_to_migrate = array_intersect ( $ profile ['select_tables ' ], $ this ->get_tables () );
82
+ }
83
+ } elseif ( 'pull ' == $ profile ['action ' ] ) {
84
+ if ( 'migrate_only_with_prefix ' == $ profile ['table_migrate_option ' ] ) {
85
+ $ tables_to_migrate = $ verify_connection_response ['prefixed_tables ' ];
86
+ } elseif ( 'migrate_select ' == $ profile ['table_migrate_option ' ] ) {
87
+ $ tables_to_migrate = array_intersect ( $ profile ['select_tables ' ], $ verify_connection_response ['tables ' ] );
88
+ }
89
+ }
90
+ $ tables_to_migrate = apply_filters ( 'wpmdb_cli_tables_to_migrate ' , $ tables_to_migrate , $ profile , $ verify_connection_response , $ initiate_migration_response );
91
+
92
+ $ _POST ['dump_filename ' ] = $ initiate_migration_response ['dump_filename ' ];
93
+ $ _POST ['gzip ' ] = ( '1 ' == $ verify_connection_response ['gzip ' ] ) ? 1 : 0 ;
94
+ $ _POST ['bottleneck ' ] = $ verify_connection_response ['bottleneck ' ];
95
+ $ _POST ['prefix ' ] = $ verify_connection_response ['prefix ' ];
96
+
97
+ $ tables_to_process = ( 'backup ' == $ _POST ['stage ' ] ) ? $ tables_to_backup : $ tables_to_migrate ;
98
+ $ stage_interator = ( 'backup ' == $ _POST ['stage ' ] ) ? 1 : 2 ;
99
+
100
+ do_action ( 'wpmdb_cli_before_migrate_tables ' , $ profile , $ verify_connection_response , $ initiate_migration_response );
101
+
102
+ do {
103
+ foreach ( $ tables_to_process as $ key => $ table ) {
104
+ $ current_row = -1 ;
105
+ $ primary_keys = '' ;
106
+ $ _POST ['table ' ] = $ table ;
107
+ $ _POST ['last_table ' ] = ( $ key == count ( $ tables_to_process ) - 1 ) ? '1 ' : '0 ' ;
108
+
109
+ do {
110
+ // reset the current chunk
111
+ $ wpmdbpro ->empty_current_chunk ();
112
+
113
+ $ _POST ['current_row ' ] = $ current_row ;
114
+ $ _POST ['primary_keys ' ] = $ primary_keys ;
115
+ $ _POST = apply_filters ( 'wpmdb_cli_migrate_table_args ' , $ _POST , $ profile , $ verify_connection_response , $ initiate_migration_response );
116
+ $ response = $ wpmdbpro ->ajax_migrate_table ();
117
+ if ( is_wp_error ( $ migrate_table_response = $ this ->verify_cli_response ( $ response , 'ajax_migrate_table() ' ) ) ) return $ migrate_table_response ;
118
+ $ migrate_table_response = apply_filters ( 'wpmdb_cli_migrate_table_response ' , $ migrate_table_response , $ _POST , $ profile , $ verify_connection_response , $ initiate_migration_response );
119
+
120
+ $ current_row = $ migrate_table_response ['current_row ' ];
121
+ $ primary_keys = $ migrate_table_response ['primary_keys ' ];
122
+
123
+ } while ( -1 != $ current_row );
124
+ }
125
+ ++$ stage_interator ;
126
+ $ _POST ['stage ' ] = 'migrate ' ;
127
+ $ tables_to_process = $ tables_to_migrate ;
128
+
129
+ } while ( $ stage_interator < 3 );
130
+
131
+ do_action ( 'wpmdb_cli_before_finalize_migration ' , $ profile , $ verify_connection_response , $ initiate_migration_response );
132
+
133
+ $ finalize_migration_response = apply_filters ( 'wpmdb_cli_finalize_migration ' , true , $ profile , $ verify_connection_response , $ initiate_migration_response );
134
+ if ( is_wp_error ( $ finalize_migration_response ) ) return $ finalize_migration_response ;
135
+
136
+ $ _POST ['tables ' ] = implode ( ', ' , $ tables_to_process );
137
+ $ _POST ['temp_prefix ' ] = $ verify_connection_response ['temp_prefix ' ];
138
+ $ _POST = apply_filters ( 'wpmdb_cli_finalize_migration_args ' , $ _POST , $ profile , $ verify_connection_response , $ initiate_migration_response );
139
+ // don't send redundant POST variables
140
+ $ _POST = $ this ->filter_post_elements ( $ _POST , array ( 'action ' , 'intent ' , 'url ' , 'key ' , 'form_data ' , 'prefix ' , 'type ' , 'location ' , 'tables ' , 'temp_prefix ' ) );
141
+ $ response = trim ( $ wpmdbpro ->ajax_finalize_migration () );
142
+ if ( ! empty ( $ response ) ) return $ this ->cli_error ( $ response );
143
+
144
+ do_action ( 'wpmdb_cli_after_finalize_migration ' , $ profile , $ verify_connection_response , $ initiate_migration_response );
145
+
146
+ return true ;
147
+ }
148
+
149
+ function verify_cli_response ( $ response , $ function_name ) {
150
+ global $ wpmdbpro ;
151
+ $ response = trim ( $ response );
152
+ if ( false === $ response ) {
153
+ return $ this ->cli_error ( $ this ->error );
154
+ }
155
+ if ( false === $ wpmdbpro ->is_json ( $ response ) ) {
156
+ return $ this ->cli_error ( sprintf ( __ ( '%1$s was expecting a JSON response, instead we received: %2$s ' , 'wp-migrate-db-pro-cli ' ), $ function_name , $ response ) );
157
+ }
158
+ $ response = json_decode ( $ response , true );
159
+ if ( isset ( $ response ['wpmdb_error ' ] ) ) {
160
+ return $ this ->cli_error ( $ response ['body ' ] );
161
+ }
162
+ return $ response ;
163
+ }
164
+
165
+ function cli_error ( $ message ){
166
+ return new WP_Error ( 'wpmdb_cli_error ' , $ message );
167
+ }
168
+
169
+ }
0 commit comments