-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathwpmdb.php
4091 lines (3415 loc) · 149 KB
/
wpmdb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class WPMDB extends WPMDB_Base {
protected $fp;
protected $form_defaults;
protected $accepted_fields;
protected $default_profile;
protected $maximum_chunk_size;
protected $current_chunk = '';
protected $connection_details;
protected $max_insert_string_len;
protected $row_tracker;
protected $rows_per_segment = 100;
protected $create_alter_table_query;
protected $session_salt;
protected $primary_keys;
protected $unhook_templates = array();
protected $plugin_tabs;
protected $lock_url_find_replace_row = false;
protected $subdomain_replaces_on;
protected $domain_replace;
protected $checkbox_options;
protected $find_replace_pairs = array();
protected $query_buffer = '';
protected $query_template = '';
protected $query_size = 0;
protected $first_select = true;
public $wpdb;
public $target_db_version = '';
public $compatibility_plugin_manager;
function __construct( $plugin_file_path ) {
parent::__construct( $plugin_file_path );
$this->plugin_version = $GLOBALS['wpmdb_meta'][ $this->core_slug ]['version'];
$this->max_insert_string_len = 50000; // 50000 is the default as defined by PhpMyAdmin
// For Firefox extend "Cache-Control" header to include 'no-store' so that refresh after migration doesn't override JS set values.
add_filter( 'nocache_headers', array( $this, 'nocache_headers' ) );
add_filter( 'plugin_action_links_' . $this->plugin_basename, array( $this, 'plugin_action_links' ) );
add_filter( 'network_admin_plugin_action_links_' . $this->plugin_basename, array( $this, 'plugin_action_links' ) );
// internal AJAX handlers
add_action( 'wp_ajax_wpmdb_delete_migration_profile', array( $this, 'ajax_delete_migration_profile' ) );
add_action( 'wp_ajax_wpmdb_save_profile', array( $this, 'ajax_save_profile' ) );
add_action( 'wp_ajax_wpmdb_save_setting', array( $this, 'ajax_save_setting' ) );
add_action( 'wp_ajax_wpmdb_initiate_migration', array( $this, 'ajax_initiate_migration' ) );
add_action( 'wp_ajax_wpmdb_migrate_table', array( $this, 'ajax_migrate_table' ) );
add_action( 'wp_ajax_wpmdb_clear_log', array( $this, 'ajax_clear_log' ) );
add_action( 'wp_ajax_wpmdb_get_log', array( $this, 'ajax_get_log' ) );
add_action( 'wp_ajax_wpmdb_whitelist_plugins', array( $this, 'ajax_whitelist_plugins' ) );
add_action( 'wp_ajax_wpmdb_update_max_request_size', array( $this, 'ajax_update_max_request_size' ) );
add_action( 'wp_ajax_wpmdb_update_delay_between_requests', array( $this, 'ajax_update_delay_between_requests' ) );
add_action( 'wp_ajax_wpmdb_cancel_migration', array( $this, 'ajax_cancel_migration' ) );
add_action( 'wp_ajax_wpmdb_finalize_migration', array( $this, 'ajax_finalize_migration' ) );
add_action( 'wp_ajax_wpmdb_flush', array( $this, 'ajax_flush' ) );
add_action( 'wp_ajax_nopriv_wpmdb_flush', array( $this, 'ajax_nopriv_flush', ) );
$this->accepted_fields = array(
'action',
'save_computer',
'gzip_file',
'connection_info',
'replace_old',
'replace_new',
'table_migrate_option',
'select_tables',
'replace_guids',
'exclude_spam',
'save_migration_profile',
'save_migration_profile_option',
'create_new_profile',
'create_backup',
'remove_backup',
'keep_active_plugins',
'select_post_types',
'backup_option',
'select_backup',
'exclude_transients',
'exclude_post_types',
'exclude_post_revisions',
'compatibility_older_mysql',
'export_dest',
'import_find_replace',
);
$this->default_profile = array(
'action' => 'savefile',
'save_computer' => '1',
'gzip_file' => '1',
'table_migrate_option' => 'migrate_only_with_prefix',
'replace_guids' => '1',
'default_profile' => true,
'name' => '',
'select_tables' => array(),
'select_post_types' => array(),
'backup_option' => 'backup_only_with_prefix',
'exclude_transients' => '1',
'compatibility_older_mysql' => '0',
'import_find_replace' => '1',
);
$this->checkbox_options = array(
'save_computer' => '0',
'gzip_file' => '0',
'replace_guids' => '0',
'exclude_spam' => '0',
'keep_active_plugins' => '0',
'create_backup' => '0',
'exclude_post_types' => '0',
'exclude_transients' => '0',
'compatibility_older_mysql' => '0',
'import_find_replace' => '0',
);
$this->plugin_tabs = array(
'<a href="#" class="nav-tab nav-tab-active js-action-link migrate" data-div-name="migrate-tab">' . esc_html( _x( 'Migrate', 'Configure a migration or export', 'wp-migrate-db' ) ) . '</a>',
'<a href="#" class="nav-tab js-action-link settings" data-div-name="settings-tab">' . esc_html( _x( 'Settings', 'Plugin configuration and preferences', 'wp-migrate-db' ) ) . '</a>',
'<a href="#" class="nav-tab js-action-link addons" data-div-name="addons-tab">' . esc_html( _x( 'Addons', 'Plugin extensions', 'wp-migrate-db' ) ) . '</a>',
'<a href="#" class="nav-tab js-action-link help" data-div-name="help-tab">' . esc_html( _x( 'Help', 'Get help or contact support', 'wp-migrate-db' ) ) . '</a>',
);
// display a notice when either WP Migrate DB or WP Migrate DB Pro is automatically deactivated
add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) );
// check if WP Engine is filtering the buffer and prevent it
add_action( 'plugins_loaded', array( $this, 'maybe_disable_wp_engine_filtering' ) );
// this is how many DB rows are processed at a time, allow devs to change this value
$this->rows_per_segment = apply_filters( 'wpmdb_rows_per_segment', $this->rows_per_segment );
if ( is_multisite() ) {
add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
add_action( 'admin_menu', array( $this, 'network_tools_admin_menu' ) );
/*
* The URL find & replace is locked down (delete & reorder disabled) on multisite installations as we require the URL
* of the remote site for export migrations. This URL is parsed into its various components and
* used to change values in the 'domain' & 'path' columns in the wp_blogs and wp_site tables.
*/
$this->lock_url_find_replace_row = true;
} else {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
if ( class_exists( 'WPMDB_Compatibility_Plugin_Manager' ) ) {
// Initialize the WPMDB_Compatibility_Plugin_Manager class to handle the `Compatibility Mode' MU Plugin
$this->compatibility_plugin_manager = new WPMDB_Compatibility_Plugin_Manager( $this );
}
add_action( 'wp_ajax_wpmdb_process_notice_link', array( $this, 'ajax_process_notice_link' ) );
}
/**
* Extend Cache-Control header to include "no-store" so that Firefox doesn't override input selection after refresh.
*
* @param array $headers
*
* @return array
*/
public function nocache_headers( $headers ) {
if ( is_array( $headers ) &&
key_exists( 'Cache-Control', $headers ) &&
false === strpos( $headers['Cache-Control'], 'no-store' )
) {
$headers['Cache-Control'] .= ', no-store';
}
return $headers;
}
/**
* Handler for ajax request to process a link click in a notice, e.g. license deactivated ... re-check.
*
* @return bool|null
*/
function ajax_process_notice_link() {
$this->check_ajax_referer( 'process-notice-link' );
$key_rules = array(
'action' => 'key',
'nonce' => 'key',
'notice' => 'key',
'type' => 'key',
'reminder' => 'int',
);
$_POST = WPMDB_Sanitize::sanitize_data( $_POST, $key_rules, __METHOD__ );
if ( false === $_POST ) {
exit;
}
global $current_user;
$key = 'wpmdb_' . $_POST['type'] . '_' . $_POST['notice'];
$value = true;
if ( 'reminder' == $_POST['type'] && isset( $_POST['reminder'] ) ) {
$value = strtotime( 'now' ) + ( is_numeric( $_POST['reminder'] ) ? $_POST['reminder'] : 604800 );
}
update_user_meta( $current_user->ID, $key, $value );
$result = $this->end_ajax();
return $result;
}
/**
* Returns a fragment of SQL for creating the table where the alter statements are held during the migration.
*
* @return string
*/
function get_create_alter_table_query() {
if ( ! is_null( $this->create_alter_table_query ) ) {
return $this->create_alter_table_query;
}
$legacy_alter_table_name = $this->get_legacy_alter_table_name();
$this->create_alter_table_query = sprintf( "DROP TABLE IF EXISTS `%s`;\n", $legacy_alter_table_name );
$alter_table_name = $this->get_alter_table_name();
$this->create_alter_table_query .= sprintf( "DROP TABLE IF EXISTS `%s`;\n", $alter_table_name );
$this->create_alter_table_query .= sprintf( "CREATE TABLE `%s` ( `query` LONGTEXT NOT NULL );\n", $alter_table_name );
$this->create_alter_table_query = apply_filters( 'wpmdb_create_alter_table_query', $this->create_alter_table_query );
return $this->create_alter_table_query;
}
/**
* Handler for updating the plugins that are not to be loaded during a request (Compatibility Mode).
*/
function ajax_whitelist_plugins() {
$this->check_ajax_referer( 'whitelist_plugins' );
$key_rules = array(
'action' => 'key',
'whitelist_plugins' => 'array',
);
$this->set_post_data( $key_rules );
$this->settings['whitelist_plugins'] = (array) $this->state_data['whitelist_plugins'];
update_site_option( 'wpmdb_settings', $this->settings );
exit;
}
/**
* Updates the Maximum Request Size setting.
*
* @return void
*/
function ajax_update_max_request_size() {
$this->check_ajax_referer( 'update-max-request-size' );
$key_rules = array(
'action' => 'key',
'max_request_size' => 'positive_int',
'nonce' => 'key',
);
$this->set_post_data( $key_rules );
$this->settings['max_request'] = (int) $this->state_data['max_request_size'] * 1024;
$result = update_site_option( 'wpmdb_settings', $this->settings );
$this->end_ajax( $result );
}
/**
* Updates the Delay Between Requests setting.
*
* @return void
*/
function ajax_update_delay_between_requests() {
$this->check_ajax_referer( 'update-delay-between-requests' );
$key_rules = array(
'action' => 'key',
'delay_between_requests' => 'positive_int',
'nonce' => 'key',
);
$this->set_post_data( $key_rules );
$this->settings['delay_between_requests'] = (int) $this->state_data['delay_between_requests'];
$result = update_site_option( 'wpmdb_settings', $this->settings );
$this->end_ajax( $result );
}
static function is_json( $string, $strict = false ) {
$json = @json_decode( $string, true );
if ( $strict == true && ! is_array( $json ) ) {
return false;
}
return ! ( $json == null || $json == false );
}
function get_sql_dump_info( $migration_type, $info_type ) {
if ( empty( $this->session_salt ) ) {
$this->session_salt = strtolower( wp_generate_password( 5, false, false ) );
}
$datetime = date( 'YmdHis' );
$ds = ( $info_type == 'path' ? DIRECTORY_SEPARATOR : '/' );
$dump_info = sprintf( '%s%s%s-%s-%s-%s.sql', $this->get_upload_info( $info_type ), $ds, sanitize_title_with_dashes( DB_NAME ), $migration_type, $datetime, $this->session_salt );
return ( $info_type == 'path' ? $this->slash_one_direction( $dump_info ) : $dump_info );
}
/**
* Returns validated and sanitized form data.
*
* @param array|string $data
*
* @return array|string
*/
function parse_migration_form_data( $data ) {
$form_data = parent::parse_migration_form_data( $data );
$this->accepted_fields = apply_filters( 'wpmdb_accepted_profile_fields', $this->accepted_fields );
$form_data = array_intersect_key( $form_data, array_flip( $this->accepted_fields ) );
unset( $form_data['replace_old'][0] );
unset( $form_data['replace_new'][0] );
if ( ! isset( $form_data['replace_old'] ) ) {
$form_data['replace_old'] = array();
}
if ( ! isset( $form_data['replace_new'] ) ) {
$form_data['replace_new'] = array();
}
if ( isset( $form_data['exclude_post_revisions'] ) ) {
$form_data['exclude_post_types'] = '1';
$form_data['select_post_types'][] = 'revision';
$form_data['select_post_types'] = array_unique( $form_data['select_post_types'] );
unset( $form_data['exclude_post_revisions'] );
}
$this->form_data = $form_data;
return $this->form_data;
}
/**
* Adds settings link to plugin page
*
* @param array $links
*
* @return array $links
*/
function plugin_action_links( $links ) {
$link = sprintf( '<a href="%s">%s</a>', network_admin_url( $this->plugin_base ) . '#settings', _x( 'Settings', 'Plugin configuration and preferences', 'wp-migrate-db' ) );
array_unshift( $links, $link );
return $links;
}
function ajax_clear_log() {
$this->check_ajax_referer( 'clear-log' );
delete_site_option( 'wpmdb_error_log' );
$result = $this->end_ajax();
return $result;
}
function ajax_get_log() {
$this->check_ajax_referer( 'get-log' );
ob_start();
$this->output_diagnostic_info();
$this->output_log_file();
$return = ob_get_clean();
$result = $this->end_ajax( $return );
return $result;
}
function output_log_file() {
$this->load_error_log();
if ( isset( $this->error_log ) ) {
echo $this->error_log;
}
}
/**
* Outputs diagnostic info for debugging.
*
* Outputs useful diagnostic info text at the Diagnostic Info & Error Log
* section under the Help tab so the information can be viewed or
* downloaded and shared for debugging.
*
* If you would like to add additional diagnostic information use the
* `wpmdb_diagnostic_info` action hook (see {@link https://developer.wordpress.org/reference/functions/add_action/}).
*
* <code>
* add_action( 'wpmdb_diagnostic_info', 'my_diagnostic_info' ) {
* echo "Additional Diagnostic Info: \r\n";
* echo "...\r\n";
* }
* </code>
*
* @return void
*/
function output_diagnostic_info() {
global $wpdb;
$table_prefix = $wpdb->base_prefix;
echo 'site_url(): ';
echo esc_html( site_url() );
echo "\r\n";
echo 'home_url(): ';
echo esc_html( home_url() );
echo "\r\n";
echo "\r\n";
echo 'Database Name: ';
echo esc_html( $wpdb->dbname );
echo "\r\n";
echo 'Table Prefix: ';
echo esc_html( $table_prefix );
echo "\r\n";
echo "\r\n";
echo 'WordPress: ' . get_bloginfo( 'version' );
if ( is_multisite() ) {
$multisite_type = defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ? 'Sub-domain' : 'Sub-directory';
echo ' Multisite (' . $multisite_type . ')';
echo "\r\n";
if ( defined( 'DOMAIN_CURRENT_SITE' ) ) {
echo 'Domain Current Site: ';
echo DOMAIN_CURRENT_SITE;
echo "\r\n";
}
if ( defined( 'PATH_CURRENT_SITE' ) ) {
echo 'Path Current Site: ';
echo PATH_CURRENT_SITE;
echo "\r\n";
}
if ( defined( 'SITE_ID_CURRENT_SITE' ) ) {
echo 'Site ID Current Site: ';
echo SITE_ID_CURRENT_SITE;
echo "\r\n";
}
if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
echo 'Blog ID Current Site: ';
echo BLOG_ID_CURRENT_SITE;
echo "\r\n";
}
} else {
echo "\r\n";
}
echo "\r\n";
foreach ( array_reverse( $GLOBALS['wpmdb_meta'] ) as $wpmdb_plugin => $wpmdb_plugin_info ) {
if ( strlen( $wpmdb_plugin ) > strlen( 'wp-migrate-db-pro' ) ) {
$wpmdb_plugin = str_replace( 'wp-migrate-db-pro-', '', $wpmdb_plugin );
}
$wpmdb_plugin = ucwords( str_replace( array( 'wp', 'db', 'cli', '-' ), array( 'WP', 'DB', 'CLI',' '), $wpmdb_plugin ) );
echo $wpmdb_plugin . ": " . $wpmdb_plugin_info['version'] . "\r\n";
}
echo "\r\n";
echo 'Web Server: ';
echo esc_html( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '' );
echo "\r\n";
echo 'PHP: ';
if ( function_exists( 'phpversion' ) ) {
echo esc_html( phpversion() );
}
echo "\r\n";
echo 'WP Memory Limit: ';
echo esc_html( WP_MEMORY_LIMIT );
echo "\r\n";
echo 'PHP Time Limit: ';
if ( function_exists( 'ini_get' ) ) {
echo esc_html( ini_get( 'max_execution_time' ) );
}
echo "\r\n";
echo 'Blocked External HTTP Requests: ';
if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) {
echo 'None';
} else {
$accessible_hosts = ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) ? WP_ACCESSIBLE_HOSTS : '';
if ( empty( $accessible_hosts ) ) {
echo 'ALL';
} else {
echo 'Partially (Accessible Hosts: ' . esc_html( $accessible_hosts ) . ')';
}
}
echo "\r\n";
echo 'fsockopen: ';
if ( function_exists( 'fsockopen' ) ) {
echo 'Enabled';
} else {
echo 'Disabled';
}
echo "\r\n";
echo 'OpenSSL: ';
if ( $this->open_ssl_enabled() ) {
echo esc_html( OPENSSL_VERSION_TEXT );
} else {
echo 'Disabled';
}
echo "\r\n";
echo 'cURL: ';
if ( function_exists( 'curl_init' ) ) {
echo 'Enabled';
} else {
echo 'Disabled';
}
echo "\r\n";
echo 'Enable SSL verification setting: ';
if ( 1 == $this->settings['verify_ssl'] ) {
echo 'Yes';
} else {
echo 'No';
}
echo "\r\n";
echo "\r\n";
echo 'MySQL: ';
echo esc_html( empty( $wpdb->use_mysqli ) ? mysql_get_server_info() : mysqli_get_server_info( $wpdb->dbh ) );
echo "\r\n";
echo 'ext/mysqli: ';
echo empty( $wpdb->use_mysqli ) ? 'no' : 'yes';
echo "\r\n";
echo 'WP Locale: ';
echo esc_html( get_locale() );
echo "\r\n";
echo 'DB Charset: ';
echo esc_html( DB_CHARSET );
echo "\r\n";
echo 'WPMDB_STRIP_INVALID_TEXT: ';
echo esc_html( ( defined( 'WPMDB_STRIP_INVALID_TEXT' ) && WPMDB_STRIP_INVALID_TEXT ) ? 'Yes' : 'No' );
echo "\r\n";
echo "\r\n";
echo 'Debug Mode: ';
echo esc_html( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No' );
echo "\r\n";
echo 'Debug Log: ';
echo esc_html( ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) ? 'Yes' : 'No' );
echo "\r\n";
echo 'Debug Display: ';
echo esc_html( ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ) ? 'Yes' : 'No' );
echo "\r\n";
echo 'Script Debug: ';
echo esc_html( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? 'Yes' : 'No' );
echo "\r\n";
echo 'PHP Error Log: ';
if ( function_exists( 'ini_get' ) ) {
echo esc_html( ini_get( 'error_log' ) );
}
echo "\r\n";
echo "\r\n";
echo 'WP Max Upload Size: ';
echo esc_html( size_format( wp_max_upload_size() ) );
echo "\r\n";
echo 'PHP Post Max Size: ';
echo esc_html( size_format( $this->get_post_max_size() ) );
echo "\r\n";
if ( function_exists( 'ini_get' ) && $suhosin_limit = ini_get( 'suhosin.post.max_value_length' ) ) {
echo 'Suhosin Post Max Value Length: ';
echo esc_html( is_numeric( $suhosin_limit ) ? size_format( $suhosin_limit ) : $suhosin_limit );
echo "\r\n";
}
if ( function_exists( 'ini_get' ) && $suhosin_limit = ini_get( 'suhosin.request.max_value_length' ) ) {
echo 'Suhosin Request Max Value Length: ';
echo esc_html( is_numeric( $suhosin_limit ) ? size_format( $suhosin_limit ) : $suhosin_limit );
echo "\r\n";
}
echo "\r\n";
echo 'WPMDB Bottleneck: ';
echo esc_html( size_format( $this->get_bottleneck() ) );
echo "\r\n";
echo 'Compatibility Mode: ';
if ( isset( $GLOBALS['wpmdb_compatibility']['active'] ) ) {
echo 'Yes';
} else {
echo 'No';
}
echo "\r\n";
echo 'Delay Between Requests: ';
$delay_between_requests = $this->settings['delay_between_requests'];
$delay_between_requests = $delay_between_requests > 0 ? $delay_between_requests / 1000 : $delay_between_requests;
echo esc_html( $delay_between_requests ) . ' s';
echo "\r\n\r\n";
echo 'WP_HOME: ';
echo esc_html( ( defined( 'WP_HOME' ) && WP_HOME ) ? WP_HOME : 'Not defined' );
echo "\r\n";
echo 'WP_SITEURL: ';
echo esc_html( ( defined( 'WP_SITEURL' ) && WP_SITEURL ) ? esc_html( WP_SITEURL ) : 'Not defined' );
echo "\r\n";
echo 'WP_CONTENT_URL: ';
echo esc_html( ( defined( 'WP_CONTENT_URL' ) && WP_CONTENT_URL ) ? WP_CONTENT_URL : 'Not defined' );
echo "\r\n";
echo 'WP_CONTENT_DIR: ';
echo esc_html( ( defined( 'WP_CONTENT_DIR' ) && WP_CONTENT_DIR ) ? WP_CONTENT_DIR : 'Not defined' );
echo "\r\n";
echo 'WP_PLUGIN_DIR: ';
echo esc_html( ( defined( 'WP_PLUGIN_DIR' ) ) ? WP_PLUGIN_DIR : 'Not defined' );
echo "\r\n";
echo 'WP_PLUGIN_URL: ';
echo esc_html( ( defined( 'WP_PLUGIN_URL' ) ) ? WP_PLUGIN_URL : 'Not defined' );
echo "\r\n";
if ( is_multisite() ) {
echo 'UPLOADS Constant: ';
echo esc_html( ( defined( 'UPLOADS' ) && UPLOADS ) ? UPLOADS : 'Not defined' );
echo "\r\n";
echo 'UPLOADBLOGSDIR Constant: ';
echo esc_html( ( defined( 'UPLOADBLOGSDIR' ) && UPLOADS ) ? UPLOADBLOGSDIR : 'Not defined' );
echo "\r\n";
}
echo "\r\n";
do_action( 'wpmdb_diagnostic_info' );
if ( has_action( 'wpmdb_diagnostic_info' ) ) {
echo "\r\n";
}
$theme_info = wp_get_theme();
echo "Active Theme Name: " . esc_html( $theme_info->Name ) . "\r\n";
echo "Active Theme Folder: " . esc_html( basename( $theme_info->get_stylesheet_directory() ) ) . "\r\n";
if ( $theme_info->get( 'Template' ) ) {
echo "Parent Theme Folder: " . esc_html( $theme_info->get( 'Template' ) ) . "\r\n";
}
if ( ! file_exists( $theme_info->get_stylesheet_directory() ) ) {
echo "WARNING: Active Theme Folder Not Found\r\n";
}
echo "\r\n";
echo "Active Plugins:\r\n";
if ( isset( $GLOBALS['wpmdb_compatibility']['active'] ) ) {
$whitelist = array_flip( (array) $this->settings['whitelist_plugins'] );
} else {
$whitelist = array();
}
$active_plugins = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ) {
$network_active_plugins = wp_get_active_network_plugins();
$active_plugins = array_map( array( $this, 'remove_wp_plugin_dir' ), $network_active_plugins );
}
foreach ( $active_plugins as $plugin ) {
$prefix = ( isset( $whitelist[ $plugin ] ) ) ? '*' : '';
$this->print_plugin_details( WP_PLUGIN_DIR . '/' . $plugin, $prefix );
}
$mu_plugins = wp_get_mu_plugins();
if ( $mu_plugins ) {
echo "\r\n";
echo "Must-use Plugins:\r\n";
foreach ( $mu_plugins as $mu_plugin ) {
$this->print_plugin_details( $mu_plugin );
}
echo "\r\n";
}
}
function print_plugin_details( $plugin_path, $prefix = '' ) {
$plugin_data = get_plugin_data( $plugin_path );
if ( empty( $plugin_data['Name'] ) ) {
return;
}
printf( "%s%s (v%s) by %s\r\n", $prefix, $plugin_data['Name'], $plugin_data['Version'], $plugin_data['AuthorName'] );
}
function remove_wp_plugin_dir( $name ) {
$plugin = str_replace( WP_PLUGIN_DIR, '', $name );
return substr( $plugin, 1 );
}
function get_alter_queries() {
global $wpdb;
$alter_table_name = $this->get_alter_table_name();
$alter_queries = array();
$sql = '';
if ( $alter_table_name === $wpdb->get_var( "SHOW TABLES LIKE '$alter_table_name'" ) ) {
$alter_queries = $wpdb->get_results( "SELECT * FROM `{$alter_table_name}`", ARRAY_A );
$alter_queries = apply_filters( 'wpmdb_get_alter_queries', $alter_queries );
}
if ( ! empty( $alter_queries ) ) {
foreach ( $alter_queries as $alter_query ) {
$sql .= $alter_query['query'] . "\n";
}
}
return $sql;
}
function process_chunk( $chunk ) {
// prepare db
global $wpdb;
$this->set_time_limit();
$queries = array_filter( explode( ";\n", $chunk ) );
array_unshift( $queries, "SET sql_mode='NO_AUTO_VALUE_ON_ZERO';" );
ob_start();
$wpdb->show_errors();
if ( empty( $wpdb->charset ) ) {
$charset = ( defined( 'DB_CHARSET' ) ? DB_CHARSET : 'utf8' );
$wpdb->charset = $charset;
$wpdb->set_charset( $wpdb->dbh, $wpdb->charset );
}
foreach ( $queries as $query ) {
if ( false === $wpdb->query( $query ) ) {
$return = ob_get_clean();
$return = array( 'wpmdb_error' => 1, 'body' => $return );
$invalid_text = $this->maybe_strip_invalid_text_and_retry( $query );
if ( false !== $invalid_text ) {
$return = $invalid_text;
}
if ( true !== $return ) {
$result = $this->end_ajax( json_encode( $return ) );
return $result;
}
}
}
return true;
}
/**
* Check if query failed due to invalid text and retry stripped query if WPMDB_STRIP_INVALID is defined as true
*
* @param string $query
* @param string $context
*
* @return array|bool|WP_Error
*/
function maybe_strip_invalid_text_and_retry( $query, $context = 'default' ) {
global $wpdb;
$return = true;
// For insert/update queries, check if it's due to invalid text
if ( ! $wpdb->last_error && ( strstr( $query, 'INSERT' ) || strstr( $query, 'UPDATE' ) ) ) {
// Only instantiate WPMDB_WPDB if needed
if ( ! $this->wpdb ) {
$this->wpdb = WPMDB_Utils::make_wpmdb_wpdb_instance();
}
if ( $this->wpdb->query_has_invalid_text( $query ) ) {
if ( ! ( defined( 'WPMDB_STRIP_INVALID_TEXT' ) && WPMDB_STRIP_INVALID_TEXT ) ) {
$table = $this->wpdb->get_table_from_query( $query );
$table = str_replace( $this->temp_prefix, '', $table );
if ( 'import' === $context ) {
$message = sprintf( __( 'The imported table `%1s` contains characters which are invalid in the target schema.<br><br>If this is a WP Migrate DB Pro export file, ensure that the `Compatible with older versions of MySQL` setting under `Advanced Options` is unchecked and try exporting again.<br><br> See <a href="%2s">our documentation</a> for more information.', 'wp-migrate-db' ), $table, 'https://deliciousbrains.com/wp-migrate-db-pro/doc/invalid-text/#imports' );
$return = new WP_Error( 'import_sql_execution_failed', $message );
} else {
$message = sprintf( __( 'The table `%1s` contains characters which are invalid in the target database. See <a href="%2s">our documentation</a> for more information.', 'wp-migrate-db' ), $table, 'https://deliciousbrains.com/wp-migrate-db-pro/doc/invalid-text/' );
$return = array(
'wpmdb_error' => 1,
'body' => $message,
);
}
$this->log_error( $message );
error_log( $message . ":\n" . $query );
} else {
if ( false === $wpdb->query( $this->wpdb->last_stripped_query ) ) {
$error = ob_get_clean();
$return = new WP_Error( 'strip_invalid_text_query_failed', 'Failed to import the stripped SQL query: ' . $error );
} else {
$return = true;
}
}
}
}
return $return;
}
/**
* Called for each database table to be migrated.
*
* @return string
*/
function ajax_migrate_table() {
$this->check_ajax_referer( 'migrate-table' );
$key_rules = array(
'action' => 'key',
'migration_state_id' => 'key',
'table' => 'string',
'stage' => 'key',
'current_row' => 'numeric',
'last_table' => 'string',
'primary_keys' => 'string',
'gzip' => 'int',
'nonce' => 'key',
'bottleneck' => 'positive_int',
'prefix' => 'string',
'path_current_site' => 'string',
'domain_current_site' => 'text',
'import_info' => 'array',
);
$this->set_post_data( $key_rules );
global $wpdb;
$this->form_data = $this->parse_migration_form_data( $this->state_data['form_data'] );
if ( 'import' === $this->state_data['intent'] && ! $this->table_exists( $this->state_data['table'] ) ) {
return $this->end_ajax( json_encode( array( 'current_row' => -1 ) ) );
}
// checks if we're performing a backup, if so, continue with the backup and exit immediately after
if ( $this->state_data['stage'] == 'backup' && $this->state_data['intent'] != 'savefile' ) {
// if performing a push we need to backup the REMOTE machine's DB
if ( $this->state_data['intent'] == 'push' ) {
$data = $this->filter_post_elements(
$this->state_data,
array(
'action',
'remote_state_id',
'url',
'table',
'form_data',
'stage',
'bottleneck',
'prefix',
'current_row',
'last_table',
'gzip',
'primary_keys',
'path_current_site',
'domain_current_site',
)
);
$data['action'] = 'wpmdb_backup_remote_table';
$data['intent'] = 'pull';
$data['sig'] = $this->create_signature( $data, $this->state_data['key'] );
$data['primary_keys'] = addslashes( $data['primary_keys'] );
$ajax_url = $this->ajax_url();
$response = $this->remote_post( $ajax_url, $data, __FUNCTION__ );
ob_start();
$this->display_errors();
$maybe_errors = ob_get_clean();
if ( false === empty( $maybe_errors ) ) {
$maybe_errors = array( 'wpmdb_error' => 1, 'body' => $maybe_errors );
$return = json_encode( $maybe_errors );
} else {
$return = $response;
}
} else {
$return = $this->handle_table_backup();
}
$result = $this->end_ajax( $return );
return $result;
}
// Pull and push need to be handled differently for obvious reasons,
// and trigger different code depending on the migration intent (push or pull).
if ( in_array( $this->state_data['intent'], array( 'push', 'savefile', 'find_replace', 'import' ) ) ) {
$this->maximum_chunk_size = $this->get_bottleneck();
if ( isset( $this->state_data['bottleneck'] ) ) {
$this->maximum_chunk_size = (int) $this->state_data['bottleneck'];
}
if ( 'savefile' === $this->state_data['intent'] ) {
$sql_dump_file_name = $this->get_upload_info( 'path' ) . DIRECTORY_SEPARATOR;
$sql_dump_file_name .= $this->format_dump_name( $this->state_data['dump_filename'] );
$this->fp = $this->open( $sql_dump_file_name );
}
if ( ! empty( $this->state_data['db_version'] ) ) {
$this->target_db_version = $this->state_data['db_version'];
if ( 'push' == $this->state_data['intent'] ) {
// $this->target_db_version has been set to remote database's version.
add_filter( 'wpmdb_create_table_query', array( $this, 'mysql_compat_filter' ), 10, 5 );
} elseif ( 'savefile' == $this->state_data['intent'] && ! empty( $this->form_data['compatibility_older_mysql'] ) ) {
// compatibility_older_mysql is currently a checkbox meaning pre-5.5 compatibility (we play safe and target 5.1),
// this may change in the future to be a dropdown or radiobox returning the version to be compatible with.
$this->target_db_version = '5.1';
add_filter( 'wpmdb_create_table_query', array( $this, 'mysql_compat_filter' ), 10, 5 );
}
}
if ( ! empty( $this->state_data['find_replace_pairs'] ) ) {
$this->find_replace_pairs = $this->state_data['find_replace_pairs'];
}
ob_start();
$result = $this->process_table( $this->state_data['table'] );
if ( $this->state_data['intent'] == 'savefile' && isset( $this->fp ) ) {
$this->close( $this->fp );
}
$this->display_errors();
$maybe_errors = trim( ob_get_clean() );
if ( false === empty( $maybe_errors ) ) {
$return = array( 'wpmdb_error' => 1, 'body' => $maybe_errors );
$result = $this->end_ajax( json_encode( $return ) );
return $result;
}
return $result;
} else {
$data = $this->filter_post_elements(
$this->state_data,
array(
'remote_state_id',
'intent',
'url',
'table',
'form_data',
'stage',
'bottleneck',
'current_row',
'last_table',
'gzip',
'primary_keys',
'site_url',
'find_replace_pairs',
)
);
$data['action'] = 'wpmdb_process_pull_request';
$data['pull_limit'] = $this->get_sensible_pull_limit();
$data['db_version'] = $wpdb->db_version();
if ( is_multisite() ) {
$data['path_current_site'] = $this->get_path_current_site();
$data['domain_current_site'] = $this->get_domain_current_site();
}
$data['prefix'] = $wpdb->base_prefix;
if ( isset( $data['find_replace_pairs'] ) ) {
$data['find_replace_pairs'] = serialize( $data['find_replace_pairs'] );
}