/wp-content/plugins/wp-sync-db-master/class/wpsdb.php
PHP | 2521 lines | 2102 code | 312 blank | 107 comment | 407 complexity | 83433841bedc6559499bb5ec4eab577c MD5 | raw file
Possible License(s): GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- class WPSDB extends WPSDB_Base {
- protected $fp;
- protected $absolute_root_file_path;
- protected $form_defaults;
- protected $accepted_fields;
- protected $default_profile;
- protected $maximum_chunk_size;
- protected $current_chunk = '';
- protected $connection_details;
- protected $remote_url;
- protected $remote_key;
- protected $form_data;
- protected $max_insert_string_len;
- protected $row_tracker;
- protected $rows_per_segment = 100;
- protected $create_alter_table_query;
- protected $alter_table_name;
- protected $session_salt;
- protected $primary_keys;
- function __construct( $plugin_file_path ) {
- parent::__construct( $plugin_file_path );
- $this->plugin_slug = 'wp-sync-db';
- $this->plugin_version = $GLOBALS['wpsdb_meta']['wp-sync-db']['version'];
- $this->max_insert_string_len = 50000; // 50000 is the default as defined by phphmyadmin
- $default_settings = array(
- 'key' => $this->generate_key(),
- 'allow_pull' => false,
- 'allow_push' => false,
- 'profiles' => array(),
- 'verify_ssl' => false,
- 'blacklist_plugins' => array(),
- );
- if( empty( $this->settings['max_request'] ) ) {
- $this->settings['max_request'] = min( 1024 * 1024, $this->get_bottleneck( 'max' ) );
- update_option( 'wpsdb_settings', $this->settings );
- }
- // if no settings exist then this is a fresh install, set up some default settings
- if ( empty( $this->settings ) ) {
- $this->settings = $default_settings;
- update_option( 'wpsdb_settings', $this->settings );
- }
- // When we add a new setting, an existing customer's db won't have this
- // new setting, so we need to add it. Otherwise, they'll see
- // array index errors in debug mode
- else {
- $update_settings = false;
- foreach ( $default_settings as $key => $value ) {
- if ( !isset( $this->settings[$key] ) ) {
- $this->settings[$key] = $value;
- $update_settings = true;
- }
- }
- if ( $update_settings ) {
- update_option( 'wpsdb_settings', $this->settings );
- }
- }
- 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_wpsdb_verify_connection_to_remote_site', array( $this, 'ajax_verify_connection_to_remote_site' ) );
- add_action( 'wp_ajax_wpsdb_reset_api_key', array( $this, 'ajax_reset_api_key' ) );
- add_action( 'wp_ajax_wpsdb_delete_migration_profile', array( $this, 'ajax_delete_migration_profile' ) );
- add_action( 'wp_ajax_wpsdb_save_setting', array( $this, 'ajax_save_setting' ) );
- add_action( 'wp_ajax_wpsdb_save_profile', array( $this, 'ajax_save_profile' ) );
- add_action( 'wp_ajax_wpsdb_initiate_migration', array( $this, 'ajax_initiate_migration' ) );
- add_action( 'wp_ajax_wpsdb_migrate_table', array( $this, 'ajax_migrate_table' ) );
- add_action( 'wp_ajax_wpsdb_finalize_migration', array( $this, 'ajax_finalize_migration' ) );
- add_action( 'wp_ajax_wpsdb_clear_log', array( $this, 'ajax_clear_log' ) );
- add_action( 'wp_ajax_wpsdb_get_log', array( $this, 'ajax_get_log' ) );
- add_action( 'wp_ajax_wpsdb_fire_migration_complete', array( $this, 'fire_migration_complete' ) );
- add_action( 'wp_ajax_wpsdb_update_max_request_size', array( $this, 'ajax_update_max_request_size' ) );
- add_action( 'wp_ajax_wpsdb_plugin_compatibility', array( $this, 'ajax_plugin_compatibility' ) );
- add_action( 'wp_ajax_wpsdb_blacklist_plugins', array( $this, 'ajax_blacklist_plugins' ) );
- add_action( 'wp_ajax_wpsdb_cancel_migration', array( $this, 'ajax_cancel_migration' ) );
- // external AJAX handlers
- add_action( 'wp_ajax_nopriv_wpsdb_verify_connection_to_remote_site', array( $this, 'respond_to_verify_connection_to_remote_site' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_remote_initiate_migration', array( $this, 'respond_to_remote_initiate_migration' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_process_chunk', array( $this, 'ajax_process_chunk' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_process_pull_request', array( $this, 'respond_to_process_pull_request' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_fire_migration_complete', array( $this, 'fire_migration_complete' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_backup_remote_table', array( $this, 'respond_to_backup_remote_table' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_remote_finalize_migration', array( $this, 'respond_to_remote_finalize_migration' ) );
- add_action( 'wp_ajax_nopriv_wpsdb_process_push_migration_cancellation', array( $this, 'respond_to_process_push_migration_cancellation' ) );
- // Clear update transients when the user clicks the "Check Again" button from the update screen
- add_action( 'current_screen', array( $this, 'check_again_clear_transients' ) );
- $absolute_path = rtrim( ABSPATH, '\\/' );
- $site_url = rtrim( site_url( '', 'http' ), '\\/' );
- $home_url = rtrim( home_url( '', 'http' ), '\\/' );
- if ( $site_url != $home_url ) {
- $difference = str_replace( $home_url, '', $site_url );
- if( strpos( $absolute_path, $difference ) !== false ) {
- $absolute_path = rtrim( substr( $absolute_path, 0, -strlen( $difference ) ), '\\/' );
- }
- }
- $this->absolute_root_file_path = $absolute_path;
- $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'
- );
- $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',
- );
- $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'
- );
- if ( is_multisite() ) {
- add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
- }
- else {
- add_action( 'admin_menu', array( $this, 'admin_menu' ) );
- }
- add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
- // this is how many DB rows are processed at a time, allow devs to change this value
- $this->rows_per_segment = apply_filters( 'wpsdb_rows_per_segment', $this->rows_per_segment );
- if ( is_multisite() ) {
- add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
- $this->plugin_base = 'settings.php?page=wp-sync-db';
- }
- else {
- add_action( 'admin_menu', array( $this, 'admin_menu' ) );
- $this->plugin_base = 'tools.php?page=wp-sync-db';
- }
- }
- function ajax_blacklist_plugins() {
- $this->settings['blacklist_plugins'] = $_POST['blacklist_plugins'];
- update_option( 'wpsdb_settings', $this->settings );
- exit;
- }
- function ajax_plugin_compatibility() {
- $mu_dir = ( defined( 'WPMU_PLUGIN_DIR' ) && defined( 'WPMU_PLUGIN_URL' ) ) ? WPMU_PLUGIN_DIR : trailingslashit( WP_CONTENT_DIR ) . 'mu-plugins';
- $source = trailingslashit( $this->plugin_dir_path ) . 'compatibility/wp-sync-db-compatibility.php';
- $dest = trailingslashit( $mu_dir ) . 'wp-sync-db-compatibility.php';
- if ( '1' === trim( $_POST['install'] ) ) { // install MU plugin
- if ( !wp_mkdir_p( $mu_dir ) ) {
- _e( sprintf( 'The following directory could not be created: %s', $mu_dir ), 'wp-sync-db' );
- exit;
- }
- if ( !copy( $source, $dest ) ) {
- _e( sprintf( 'Could not copy the compatibility plugin from %1$s to %2$s', $source, $destination ), 'wp-sync-db' );
- exit;
- }
- } else { // uninstall MU plugin
- if ( file_exists( $dest ) && !unlink( $dest ) ) {
- _e( sprintf( 'Could not remove the compatibility plugin from %s', $dest ), 'wp-sync-db' );
- exit;
- }
- }
- exit;
- }
- function check_again_clear_transients( $current_screen ) {
- if( ! isset( $current_screen->id ) || strpos( $current_screen->id, 'update-core' ) === false || ! isset( $_GET['force-check'] ) ) return;
- delete_site_transient( 'wpsdb_upgrade_data' );
- delete_site_transient( 'update_plugins' );
- }
- function get_alter_table_name() {
- if ( ! is_null( $this->alter_table_name ) ) {
- return $this->alter_table_name;
- }
- global $wpdb;
- $this->alter_table_name = apply_filters( 'wpsdb_alter_table_name', $wpdb->prefix . 'wpsdb_alter_statements' );
- return $this->alter_table_name;
- }
- function get_create_alter_table_query() {
- if ( ! is_null( $this->create_alter_table_query ) ) {
- return $this->create_alter_table_query;
- }
- $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( 'wpsdb_create_alter_table_query', $this->create_alter_table_query );
- return $this->create_alter_table_query;
- }
- function get_short_uploads_dir() {
- $short_path = str_replace( $this->absolute_root_file_path, '', $this->get_upload_info( 'path' ) );
- return trailingslashit( substr( str_replace( '\\', '/', $short_path ), 1 ) );
- }
- function get_upload_info( $type = 'path' ) {
- // Let developers define their own path to for export files
- // Note: We require a very specific data set here, it should be similiar to the following
- // array(
- // 'path' => '/path/to/custom/uploads/directory', <- note missing end trailing slash
- // 'url' => 'http://yourwebsite.com/custom/uploads/directory' <- note missing end trailing slash
- // );
- $upload_info = apply_filters( 'wpsdb_upload_info', array() );
- if ( !empty( $upload_info ) ) {
- return $upload_info[$type];
- }
- $upload_dir = wp_upload_dir();
- $upload_info['path'] = $upload_dir['basedir'];
- $upload_info['url'] = $upload_dir['baseurl'];
- $upload_dir_name = apply_filters( 'wpsdb_upload_dir_name', 'wp-sync-db' );
- if( ! file_exists( $upload_dir['basedir'] . DS . $upload_dir_name ) ) {
- $url = wp_nonce_url( $this->plugin_base, 'wp-sync-db-nonce' );
- if( false === @mkdir( $upload_dir['basedir'] . DS . $upload_dir_name, 0755 ) ) {
- return $upload_info[$type];
- }
- $filename = $upload_dir['basedir'] . DS . $upload_dir_name . DS . 'index.php';
- if( false === @file_put_contents( $filename, "<?php\r\n// Silence is golden\r\n?>" ) ) {
- return $upload_info[$type];
- }
- }
- $upload_info['path'] .= DS . $upload_dir_name;
- $upload_info['url'] .= '/' . $upload_dir_name;
- return $upload_info[$type];
- }
- function ajax_update_max_request_size() {
- $this->check_ajax_referer( 'update-max-request-size' );
- $this->settings['max_request'] = (int) $_POST['max_request_size'] * 1024;
- update_option( 'wpsdb_settings', $this->settings );
- $result = $this->end_ajax();
- return $result;
- }
- 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' ? DS : '/' );
- return 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 );
- }
- function parse_migration_form_data( $data ) {
- parse_str( $data, $form_data );
- $this->accepted_fields = apply_filters( 'wpsdb_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] );
- return $form_data;
- }
- function plugin_action_links( $links ) {
- $link = sprintf( '<a href="%s">%s</a>', network_admin_url( $this->plugin_base ), __( 'Settings', 'wp-sync-db' ) );
- array_unshift( $links, $link );
- return $links;
- }
- function ajax_clear_log() {
- $this->check_ajax_referer( 'clear-log' );
- delete_option( 'wpsdb_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() {
- $log = get_option( 'wpsdb_error_log' );
- if( $log ) {
- echo $log;
- }
- }
- function output_diagnostic_info() {
- global $table_prefix;
- global $wpdb;
- echo 'site_url(): ';
- echo site_url();
- echo "\r\n";
- echo 'home_url(): ';
- echo home_url();
- echo "\r\n";
- echo 'Table Prefix: ';
- echo $table_prefix;
- echo "\r\n";
- echo 'WordPress: ';
- if ( is_multisite() ) echo 'WPMU'; else echo 'WP'; echo bloginfo('version');
- echo "\r\n";
- echo 'Web Server: ';
- echo $_SERVER['SERVER_SOFTWARE'];
- echo "\r\n";
- echo 'PHP: ';
- if ( function_exists( 'phpversion' ) ) echo esc_html( phpversion() );
- 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";
- _e( 'ext/mysqli', 'wp-app-store' ); echo ': ';
- echo empty( $wpdb->use_mysqli ) ? 'no' : 'yes';
- echo "\r\n";
- _e( 'WP Memory Limit', 'wp-app-store' ); echo ': ';
- echo WP_MEMORY_LIMIT;
- echo "\r\n";
- echo 'WPSDB Bottleneck: ';
- echo size_format( $this->get_bottleneck() );
- echo "\r\n";
- if ( function_exists( 'ini_get' ) && $suhosin_limit = ini_get( 'suhosin.post.max_value_length' ) ) {
- echo 'Suhosin Post Max Value Length: ';
- echo 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 is_numeric( $suhosin_limit ) ? size_format( $suhosin_limit ) : $suhosin_limit;
- echo "\r\n";
- }
- echo 'Debug Mode: ';
- if ( defined('WP_DEBUG') && WP_DEBUG ) { echo 'Yes'; } else { echo 'No'; }
- echo "\r\n";
- echo 'WP Max Upload Size: ';
- echo size_format( wp_max_upload_size() );
- echo "\r\n";
- echo 'PHP Post Max Size: ';
- echo size_format( $this->get_post_max_size() );
- echo "\r\n";
- echo 'PHP Time Limit: ';
- if ( function_exists( 'ini_get' ) ) echo ini_get('max_execution_time');
- echo "\r\n";
- echo 'PHP Error Log: ';
- if ( function_exists( 'ini_get' ) ) echo ini_get('error_log');
- 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 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 "\r\n";
- echo "Active Plugins:\r\n";
- $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 ) {
- $plugin_data = @get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
- if ( empty( $plugin_data['Name'] ) ) continue;
- printf( "%s (v%s) by %s\r\n", $plugin_data['Name'], $plugin_data['Version'], $plugin_data['AuthorName'] );
- }
- echo "\r\n";
- }
- function remove_wp_plugin_dir( $name ) {
- $plugin = str_replace( WP_PLUGIN_DIR, '', $name );
- return substr( $plugin, 1 );
- }
- function fire_migration_complete() {
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'url' ) );
- if ( ! $this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- $error_msg = $this->invalid_content_verification_error . ' (#138)';
- $this->log_error( $error_msg, $filtered_post );
- $result = $this->end_ajax( $error_msg );
- return $result;
- }
- do_action( 'wpsdb_migration_complete', 'pull', $_POST['url'] );
- $result = $this->end_ajax();
- return $result;
- }
- function get_alter_queries() {
- global $wpdb;
- $alter_table_name = $this->get_alter_table_name();
- $sql = '';
- $alter_queries = $wpdb->get_results( "SELECT * FROM `{$alter_table_name}`", ARRAY_A );
- if( ! empty( $alter_queries ) ) {
- foreach( $alter_queries as $alter_query ) {
- $sql .= $alter_query['query'];
- }
- }
- return $sql;
- }
- // After table migration, delete old tables and rename new tables removing the temporarily prefix
- function ajax_finalize_migration() {
- $this->check_ajax_referer( 'finalize-migration' );
- global $wpdb;
- $return = '';
- if ( $_POST['intent'] == 'pull' ) {
- $return = $this->finalize_migration();
- }
- else {
- do_action( 'wpsdb_migration_complete', 'push', $_POST['url'] );
- $data = $_POST;
- if ( isset( $data['nonce'] ) ) {
- unset( $data['nonce'] );
- }
- $data['action'] = 'wpsdb_remote_finalize_migration';
- $data['intent'] = 'pull';
- $data['prefix'] = $wpdb->prefix;
- $data['type'] = 'push';
- $data['location'] = home_url();
- $data['temp_prefix'] = $this->temp_prefix;
- $data['sig'] = $this->create_signature( $data, $data['key'] );
- $ajax_url = trailingslashit( $_POST['url'] ) . 'wp-admin/admin-ajax.php';
- $response = $this->remote_post( $ajax_url, $data, __FUNCTION__ );
- ob_start();
- echo $response;
- $this->display_errors();
- $return = ob_get_clean();
- }
- $result = $this->end_ajax( $return );
- return $result;
- }
- function respond_to_remote_finalize_migration() {
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'intent', 'url', 'key', 'form_data', 'prefix', 'type', 'location', 'tables', 'temp_prefix' ) );
- if ( ! $this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- $error_msg = $this->invalid_content_verification_error . ' (#123)';
- $this->log_error( $error_msg, $filtered_post );
- $result = $this->end_ajax( $error_msg );
- return $result;
- }
- $return = $this->finalize_migration();
- $result = $this->end_ajax( $return );
- return $result;
- }
- function finalize_migration() {
- global $wpdb;
- $tables = explode( ',', $_POST['tables'] );
- $temp_tables = array();
- foreach( $tables as $table ) {
- $temp_prefix = stripslashes( $_POST['temp_prefix'] );
- $temp_tables[] = $temp_prefix . $table;
- }
- $sql = "SET FOREIGN_KEY_CHECKS=0;\n";
- $preserved_options = array( 'wpsdb_settings', 'wpsdb_error_log' );
- $this->form_data = $this->parse_migration_form_data( $_POST['form_data'] );
- if( isset( $this->form_data['keep_active_plugins'] ) ) {
- $preserved_options[] = 'active_plugins';
- }
- $preserved_options = apply_filters( 'wpsdb_preserved_options', $preserved_options );
- foreach ( $temp_tables as $table ) {
- $sql .= 'DROP TABLE IF EXISTS ' . $this->backquote( substr( $table, strlen( $temp_prefix ) ) ) . ';';
- $sql .= "\n";
- $sql .= 'RENAME TABLE ' . $this->backquote( $table ) . ' TO ' . $this->backquote( substr( $table, strlen( $temp_prefix ) ) ) . ';';
- $sql .= "\n";
- }
- $preserved_options_data = $wpdb->get_results( sprintf( "SELECT * FROM %soptions WHERE `option_name` IN ('%s')", $wpdb->prefix, implode( "','", $preserved_options ) ), ARRAY_A );
- foreach( $preserved_options_data as $option ) {
- $sql .= $wpdb->prepare( "DELETE FROM `{$_POST['prefix']}options` WHERE `option_name` = %s;\n", $option['option_name'] );
- $sql .= $wpdb->prepare( "INSERT INTO `{$_POST['prefix']}options` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s );\n", $option['option_name'], $option['option_value'], $option['autoload'] );
- }
- $alter_table_name = $this->get_alter_table_name();
- $sql .= $this->get_alter_queries();
- $sql .= "DROP TABLE IF EXISTS " . $this->backquote( $alter_table_name ) . ";\n";
- $process_chunk_result = $this->process_chunk( $sql );
- if( true !== $process_chunk_result ) {
- $result = $this->end_ajax( $process_chunk_result );
- return $result;
- }
- $type = ( isset( $_POST['type'] ) ? 'push' : 'pull' );
- $location = ( isset( $_POST['location'] ) ? $_POST['location'] : $_POST['url'] );
- if( ! isset( $_POST['location'] ) ) {
- $data = array();
- $data['action'] = 'wpsdb_fire_migration_complete';
- $data['url'] = home_url();
- $data['sig'] = $this->create_signature( $data, $_POST['key'] );
- $ajax_url = trailingslashit( $_POST['url'] ) . 'wp-admin/admin-ajax.php';
- $response = $this->remote_post( $ajax_url, $data, __FUNCTION__ );
- ob_start();
- echo $response;
- $this->display_errors();
- $maybe_errors = trim( ob_get_clean() );
- if( false === empty( $maybe_errors ) ) {
- $result = $this->end_ajax( $maybe_errors );
- return $result;
- }
- }
- // flush rewrite rules to prevent 404s and other oddities
- flush_rewrite_rules( true ); // true = hard refresh, recreates the .htaccess file
- do_action( 'wpsdb_migration_complete', $type, $location );
- }
- function ajax_process_chunk() {
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'table', 'chunk_gzipped' ) );
- $gzip = ( isset( $_POST['chunk_gzipped'] ) && $_POST['chunk_gzipped'] );
- $tmp_file_name = 'chunk.txt';
- if( $gzip ) {
- $tmp_file_name .= '.gz';
- }
- $tmp_file_path = wp_tempnam( $tmp_file_name );
- if ( !isset( $_FILES['chunk']['tmp_name'] ) || !move_uploaded_file( $_FILES['chunk']['tmp_name'], $tmp_file_path ) ) {
- $result = $this->end_ajax( __( 'Could not upload the SQL to the server. (#135)', 'wp-sync-db' ) );
- return $result;
- }
- if ( false === ( $chunk = file_get_contents( $tmp_file_path ) ) ) {
- $result = $this->end_ajax( __( 'Could not read the SQL file we uploaded to the server. (#136)', 'wp-sync-db' ) );
- return $result;
- }
- @unlink( $tmp_file_path );
- $filtered_post['chunk'] = $chunk;
- if ( !$this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- $error_msg = $this->invalid_content_verification_error . ' (#130)';
- $this->log_error( $error_msg, $filtered_post );
- $result = $this->end_ajax( $error_msg );
- return $result;
- }
- if ( $this->settings['allow_push'] != true ) {
- $result = $this->end_ajax( __( 'The connection succeeded but the remote site is configured to reject push connections. You can change this in the "settings" tab on the remote site. (#133)', 'wp-sync-db' ) );
- return $result;
- }
- if( $gzip ) {
- $filtered_post['chunk'] = gzuncompress( $filtered_post['chunk'] );
- }
- $process_chunk_result = $this->process_chunk( $filtered_post['chunk'] );
- $result = $this->end_ajax( $process_chunk_result );
- return $result;
- }
- 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();
- $result = $this->end_ajax( $return );
- return $result;
- }
- }
- return true;
- }
- function ajax_migrate_table() {
- $this->check_ajax_referer( 'migrate-table' );
- global $wpdb;
- $this->form_data = $this->parse_migration_form_data( $_POST['form_data'] );
- $result = '';
- // checks if we're performing a backup, if so, continue with the backup and exit immediately after
- if ( $_POST['stage'] == 'backup' && $_POST['intent'] != 'savefile' ) {
- // if performing a push we need to backup the REMOTE machine's DB
- if ( $_POST['intent'] == 'push' ) {
- $data = $_POST;
- if ( isset( $data['nonce'] ) ) {
- unset( $data['nonce'] );
- }
- $data['action'] = 'wpsdb_backup_remote_table';
- $data['intent'] = 'pull';
- $ajax_url = trailingslashit( $_POST['url'] ) . 'wp-admin/admin-ajax.php';
- $data['primary_keys'] = stripslashes( $data['primary_keys'] );
- $data['sig'] = $this->create_signature( $data, $data['key'] );
- $response = $this->remote_post( $ajax_url, $data, __FUNCTION__ );
- ob_start();
- $this->display_errors();
- $return = ob_get_clean();
- $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, trigger different code depending on the migration intent (push or pull)
- if ( $_POST['intent'] == 'push' || $_POST['intent'] == 'savefile' ) {
- $this->maximum_chunk_size = $this->get_bottleneck();
- if ( isset( $_POST['bottleneck'] ) ) {
- $this->maximum_chunk_size = (int) $_POST['bottleneck'];
- }
- if ( $_POST['intent'] == 'push' ) {
- $this->remote_key = $_POST['key'];
- $this->remote_url = $_POST['url'];
- }
- $sql_dump_file_name = $this->get_upload_info( 'path' ) . DS;
- $sql_dump_file_name .= $this->format_dump_name( $_POST['dump_filename'] );
- if ( $_POST['intent'] == 'savefile' ) {
- $this->fp = $this->open( $sql_dump_file_name );
- }
- $result = $this->export_table( $_POST['table'] );
- if ( $_POST['intent'] == 'savefile' ) {
- $this->close( $this->fp );
- }
- ob_start();
- $this->display_errors();
- $maybe_errors = trim( ob_get_clean() );
- if( false === empty( $maybe_errors ) ) {
- $result = $this->end_ajax( $maybe_errors );
- return $result;
- }
- return $result;
- }
- else {
- $data = $_POST;
- if ( isset( $data['nonce'] ) ) {
- unset( $data['nonce'] );
- }
- $data['action'] = 'wpsdb_process_pull_request';
- $data['pull_limit'] = $this->get_sensible_pull_limit();
- if( is_multisite() ) {
- $data['path_current_site'] = $this->get_path_current_site();
- $data['domain_current_site'] = $this->get_domain_current_site();
- }
- $data['prefix'] = $wpdb->prefix;
- if ( isset( $data['sig'] ) ) {
- unset( $data['sig'] );
- }
- $ajax_url = trailingslashit( $data['url'] ) . 'wp-admin/admin-ajax.php';
- $data['primary_keys'] = stripslashes( $data['primary_keys'] );
- $data['sig'] = $this->create_signature( $data, $data['key'] );
- $response = $this->remote_post( $ajax_url, $data, __FUNCTION__ );
- ob_start();
- $this->display_errors();
- $maybe_errors = trim( ob_get_clean() );
- if( false === empty( $maybe_errors ) ) {
- $result = $this->end_ajax( $maybe_errors );
- return $result;
- }
- if( strpos( $response, ';' ) === false ) {
- $result = $this->end_ajax( $response );
- return $result;
- }
- // returned data is just a big string like this query;query;query;33
- // need to split this up into a chunk and row_tracker
- $row_information = trim( substr( strrchr( $response, "\n" ), 1 ) );
- $row_information = explode( ',', $row_information );
- $chunk = substr( $response, 0, strrpos( $response, ";\n" ) + 1 );
- if ( ! empty( $chunk ) ) {
- $process_chunk_result = $this->process_chunk( $chunk );
- if( true !== $process_chunk_result ) {
- $result = $this->end_ajax( $process_chunk_result );
- return $result;
- }
- }
- $result = $this->end_ajax( json_encode(
- array(
- 'current_row' => $row_information[0],
- 'primary_keys' => $row_information[1]
- )
- ) );
- }
- return $result;
- }
- function respond_to_backup_remote_table() {
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'intent', 'url', 'key', 'table', 'form_data', 'stage', 'bottleneck', 'prefix', 'current_row', 'dump_filename', 'last_table', 'gzip', 'primary_keys', 'path_current_site', 'domain_current_site' ) );
- $filtered_post['primary_keys'] = stripslashes( $filtered_post['primary_keys'] );
- if ( ! $this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- $error_msg = $this->invalid_content_verification_error . ' (#137)';
- $this->log_error( $error_msg, $filtered_post );
- $result = $this->end_ajax( $error_msg );
- return $result;
- }
- $this->form_data = $this->parse_migration_form_data( $_POST['form_data'] );
- $result = $this->handle_table_backup();
- return $result;
- }
- function handle_table_backup() {
- if ( isset( $this->form_data['gzip_file'] ) ) {
- unset( $this->form_data['gzip_file'] );
- }
- $this->maximum_chunk_size = $this->get_bottleneck();
- $sql_dump_file_name = $this->get_upload_info( 'path' ) . DS;
- $sql_dump_file_name .= $this->format_dump_name( $_POST['dump_filename'] );
- $file_created = file_exists( $sql_dump_file_name );
- $this->fp = $this->open( $sql_dump_file_name );
- if ( $file_created == false ) {
- $this->db_backup_header();
- }
- $result = $this->export_table( $_POST['table'] );
- if( isset( $this->fp ) ) {
- $this->close( $this->fp );
- }
- ob_start();
- $this->display_errors();
- $maybe_errors = trim( ob_get_clean() );
- if( false === empty( $maybe_errors ) ) {
- $result = $this->end_ajax( $maybe_errors );
- return $result;
- }
- return $result;
- }
- function respond_to_process_pull_request() {
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'intent', 'url', 'key', 'table', 'form_data', 'stage', 'bottleneck', 'prefix', 'current_row', 'dump_filename', 'pull_limit', 'last_table', 'gzip', 'primary_keys', 'path_current_site', 'domain_current_site' ) );
- // verification will fail unless we strip slashes on primary_keys and form_data
- $filtered_post['primary_keys'] = stripslashes( $filtered_post['primary_keys'] );
- $filtered_post['form_data'] = stripslashes( $filtered_post['form_data'] );
- if( isset( $filtered_post['path_current_site'] ) ) {
- $filtered_post['path_current_site'] = stripslashes( $filtered_post['path_current_site'] );
- }
- if ( ! $this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- $error_msg = $this->invalid_content_verification_error . ' (#124)';
- $this->log_error( $error_msg, $filtered_post );
- $result = $this->end_ajax( $error_msg );
- return $result;
- }
- if ( $this->settings['allow_pull'] != true ) {
- $result = $this->end_ajax( __( 'The connection succeeded but the remote site is configured to reject pull connections. You can change this in the "settings" tab on the remote site. (#132)', 'wp-sync-db' ) );
- return $result;
- }
- $this->maximum_chunk_size = $_POST['pull_limit'];
- $this->export_table( $_POST['table'] );
- ob_start();
- $this->display_errors();
- $return = ob_get_clean();
- $result = $this->end_ajax( $return );
- return $result;
- }
- // Occurs right before the first table is migrated / backed up during the migration process
- // Does a quick check to make sure the verification string is valid and also opens / creates files for writing to (if required)
- function ajax_initiate_migration() {
- $this->check_ajax_referer( 'initiate-migration' );
- $this->form_data = $this->parse_migration_form_data( $_POST['form_data'] );
- if ( $_POST['intent'] == 'savefile' ) {
- $return = array(
- 'code' => 200,
- 'message' => 'OK',
- 'body' => json_encode( array( 'error' => 0 ) ),
- );
- $return['dump_filename'] = basename( $this->get_sql_dump_info( 'migrate', 'path' ) );
- $return['dump_url'] = $this->get_sql_dump_info( 'migrate', 'url' );
- $dump_filename_no_extension = substr( $return['dump_filename'], 0, -4 );
- $create_alter_table_query = $this->get_create_alter_table_query();
- // sets up our table to store 'ALTER' queries
- $process_chunk_result = $this->process_chunk( $create_alter_table_query );
- if( true !== $process_chunk_result ) {
- $result = $this->end_ajax( $process_chunk_result );
- return $result;
- }
- if ( $this->gzip() && isset( $this->form_data['gzip_file'] ) ) {
- $return['dump_filename'] .= '.gz';
- $return['dump_url'] .= '.gz';
- }
- $this->fp = $this->open( $this->get_upload_info( 'path' ) . DS . $return['dump_filename'] );
- $this->db_backup_header();
- $this->close( $this->fp );
- $return['dump_filename'] = $dump_filename_no_extension;
- }
- else { // does one last check that our verification string is valid
- $data = array(
- 'action' => 'wpsdb_remote_initiate_migration',
- 'intent' => $_POST['intent'],
- 'form_data' => $_POST['form_data'],
- );
- $data['sig'] = $this->create_signature( $data, $_POST['key'] );
- $ajax_url = trailingslashit( $_POST['url'] ) . 'wp-admin/admin-ajax.php';
- $response = $this->remote_post( $ajax_url, $data, __FUNCTION__ );
- if ( false === $response ) {
- $return = array( 'wpsdb_error' => 1, 'body' => $this->error );
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- $return = @unserialize( trim( $response ) );
- if ( false === $return ) {
- $error_msg = __( 'Failed attempting to unserialize the response from the remote server. Please contact support.', 'wp-sync-db' );
- $return = array( 'wpsdb_error' => 1, 'body' => $error_msg );
- $this->log_error( $error_msg, $response );
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- if ( isset( $return['error'] ) && $return['error'] == 1 ) {
- $return = array( 'wpsdb_error' => 1, 'body' => $return['message'] );
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- if( $_POST['intent'] == 'pull' ) {
- // sets up our table to store 'ALTER' queries
- $create_alter_table_query = $this->get_create_alter_table_query();
- $process_chunk_result = $this->process_chunk( $create_alter_table_query );
- if( true !== $process_chunk_result ) {
- $result = $this->end_ajax( $process_chunk_result );
- return $result;
- }
- }
- if( ! empty( $this->form_data['create_backup'] ) && $_POST['intent'] == 'pull' ) {
- $return['dump_filename'] = basename( $this->get_sql_dump_info( 'backup', 'path' ) );
- $return['dump_filename'] = substr( $return['dump_filename'], 0, -4 );
- $return['dump_url'] = $this->get_sql_dump_info( 'backup', 'url' );
- }
- }
- $return['dump_filename'] = ( empty( $return['dump_filename'] ) ) ? '' : $return['dump_filename'];
- $return['dump_url'] = ( empty( $return['dump_url'] ) ) ? '' : $return['dump_url'];
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- // End point for the above remote_post call, ensures that the verification string is valid before continuing with the migration
- function respond_to_remote_initiate_migration() {
- $return = array();
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'intent', 'form_data' ) );
- if ( $this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- if ( isset( $this->settings['allow_' . $_POST['intent']] ) && ( true === $this->settings['allow_' . $_POST['intent']] || 1 === $this->settings['allow_' . $_POST['intent']] ) ) {
- $return['error'] = 0;
- }
- else {
- $return['error'] = 1;
- if( $_POST['intent'] == 'pull' ) {
- $intent = __( 'pull', 'wp-sync-db' );
- }
- else {
- $intent = __( 'push', 'wp-sync-db' );
- }
- $return['message'] = sprintf( __( 'The connection succeeded but the remote site is configured to reject %s connections. You can change this in the "settings" tab on the remote site. (#110)', 'wp-sync-db'), $intent );
- }
- }
- else {
- $return['error'] = 1;
- $error_msg = $this->invalid_content_verification_error . ' (#111)';
- $this->log_error( $error_msg, $filtered_post );
- $return['message'] = $error_msg;
- }
- $this->form_data = $this->parse_migration_form_data( $_POST['form_data'] );
- if( ! empty( $this->form_data['create_backup'] ) && $_POST['intent'] == 'push' ) {
- $return['dump_filename'] = basename( $this->get_sql_dump_info( 'backup', 'path' ) );
- $return['dump_filename'] = substr( $return['dump_filename'], 0, -4 );
- $return['dump_url'] = $this->get_sql_dump_info( 'backup', 'url' );
- }
- if( $_POST['intent'] == 'push' ) {
- // sets up our table to store 'ALTER' queries
- $create_alter_table_query = $this->get_create_alter_table_query();
- $process_chunk_result = $this->process_chunk( $create_alter_table_query );
- if( true !== $process_chunk_result ) {
- $result = $this->end_ajax( $process_chunk_result );
- return $result;
- }
- }
- $result = $this->end_ajax( serialize( $return ) );
- return $result;
- }
- function ajax_save_profile() {
- $this->check_ajax_referer( 'save-profile' );
- $profile = $this->parse_migration_form_data( $_POST['profile'] );
- $profile = wp_parse_args( $profile, $this->checkbox_options );
- if ( isset( $profile['save_migration_profile_option'] ) && $profile['save_migration_profile_option'] == 'new' ) {
- $profile['name'] = $profile['create_new_profile'];
- $this->settings['profiles'][] = $profile;
- }
- else {
- $key = $profile['save_migration_profile_option'];
- $name = $this->settings['profiles'][$key]['name'];
- $this->settings['profiles'][$key] = $profile;
- $this->settings['profiles'][$key]['name'] = $name;
- }
- update_option( 'wpsdb_settings', $this->settings );
- end( $this->settings['profiles'] );
- $key = key( $this->settings['profiles'] );
- $result = $this->end_ajax( $key );
- return $result;
- }
- function ajax_save_setting() {
- $this->check_ajax_referer( 'save-setting' );
- $this->settings[$_POST['setting']] = ( $_POST['checked'] == 'false' ? false : true );
- update_option( 'wpsdb_settings', $this->settings );
- $result = $this->end_ajax();
- return $result;
- }
- function ajax_delete_migration_profile() {
- $this->check_ajax_referer( 'delete-migration-profile' );
- $key = absint( $_POST['profile_id'] );
- --$key;
- $return = '';
- if ( isset( $this->settings['profiles'][$key] ) ) {
- unset( $this->settings['profiles'][$key] );
- update_option( 'wpsdb_settings', $this->settings );
- }
- else {
- $return = '-1';
- }
- $result = $this->end_ajax( $return );
- return $result;
- }
- function ajax_reset_api_key() {
- $this->check_ajax_referer( 'reset-api-key' );
- $this->settings['key'] = $this->generate_key();
- update_option( 'wpsdb_settings', $this->settings );
- $result = $this->end_ajax( sprintf( "%s\n%s", site_url( '', 'https' ), $this->settings['key'] ) );
- return $result;
- }
- // AJAX endpoint for when the user pastes into the connection info box (or when they click "connect")
- // Responsible for contacting the remote website and retrieving info and testing the verification string
- function ajax_verify_connection_to_remote_site() {
- $this->check_ajax_referer( 'verify-connection-to-remote-site' );
- $data = array(
- 'action' => 'wpsdb_verify_connection_to_remote_site',
- 'intent' => $_POST['intent']
- );
- $data['sig'] = $this->create_signature( $data, $_POST['key'] );
- $ajax_url = trailingslashit( $_POST['url'] ) . 'wp-admin/admin-ajax.php';
- $timeout = apply_filters( 'wpsdb_prepare_remote_connection_timeout', 10 );
- $response = $this->remote_post( $ajax_url, $data, __FUNCTION__, compact( 'timeout' ), true );
- $url_bits = parse_url( $this->attempting_to_connect_to );
- $return = $response;
- $alt_action = '';
- if ( false === $response ) {
- $return = array( 'wpsdb_error' => 1, 'body' => $this->error );
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- $response = unserialize( trim( $response ) );
- if ( false === $response ) {
- $error_msg = __( 'Failed attempting to unserialize the response from the remote server. Please contact support.', 'wp-sync-db' );
- $return = array( 'wpsdb_error' => 1, 'body' => $error_msg );
- $this->log_error( $error_msg );
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- if ( isset( $response['error'] ) && $response['error'] == 1 ) {
- $return = array( 'wpsdb_error' => 1, 'body' => $response['message'] );
- $this->log_error( $response['message'], $response );
- $result = $this->end_ajax( json_encode( $return ) );
- return $result;
- }
- if ( isset( $_POST['convert_post_type_selection'] ) && '1' == $_POST['convert_post_type_selection'] ) {
- $profile = (int) $_POST['profile'];
- unset( $this->settings['profiles'][$profile]['post_type_migrate_option'] );
- $this->settings['profiles'][$profile]['exclude_post_types'] = '1';
- $this->settings['profiles'][$profile]['select_post_types'] = array_values( array_diff( $response['post_types'], $this->settings['profiles'][$profile]['select_post_types'] ) );
- $response['select_post_types'] = $this->settings['profiles'][$profile]['select_post_types'];
- update_option( 'wpsdb_settings', $this->settings );
- }
- $response['scheme'] = $url_bits['scheme'];
- $return = json_encode( $response );
- $result = $this->end_ajax( $return );
- return $result;
- }
- // End point for the above remote_post call, returns table information, absolute file path, table prefix, etc
- function respond_to_verify_connection_to_remote_site() {
- global $wpdb;
- $return = array();
- $filtered_post = $this->filter_post_elements( $_POST, array( 'action', 'intent' ) );
- if ( !$this->verify_signature( $filtered_post, $this->settings['key'] ) ) {
- $return['error'] = 1;
- $return['message'] = $this->invalid_content_verification_error . ' (#120) <a href="#" class="try-again js-action-link">' . __( 'Try again?', 'wp-sync-db' ) . '</a>';
- $this->log_error( $this->invalid_content_verification_error . ' (#120)', $filtered_post );
- $result = $this->end_ajax( serialize( $return ) );
- return $result;
- }
- if ( !isset( $this->settings['allow_' . $_POST['intent']] ) || $this->settings['allow_' . $_POST['intent']] != true ) {
- $return['error'] = 1;
- if( $_POST['intent'] == 'pull' ) {
- $intent = __( 'pull', 'wp-sync-db' );
- }
- else {
- $intent = __( 'push', 'wp-sync-db' );
- }
- $return['message'] = sprintf( __( 'The connection succeeded but the remote site is configured to reject %s connections. You can change this in the "settings" tab on the remote site. (#122) <a href="#" class="try-again js-action-link">Try again?</a>', 'wp-sync-db' ), $intent );
- $result = $this->end_ajax( serialize( $return ) );
- return $result;
- }
- $return['tables'] = $this->get_tables();
- $return['prefixed_tables'] = $this->get_tables( 'prefix' );
- $return['table_sizes'] = $this->get_table_sizes();
- $return['table_rows'] = $this->get_table_row_count();
- $return['table_sizes_hr'] = array_map( array( $this, 'format_table_sizes' ), $this->get_table_sizes() );
- $return['path'] = $this->absolute_root_file_path;
- $return['url'] = home_url();
- $return['prefix'] = $wpdb->prefix;
- $return['bottleneck'] = $this->get_bottleneck();
- $return['error'] = 0;
- $return['plugin_version'] = $this->plugin_version;
- $return['domain'] = $this->get_domain_current_site();
- $return['path_current_site'] = $this->get_path_current_site();
- $return['uploads_dir'] = $this->get_short_uploads_dir();
- $return['gzip'] = ( $this->gzip() ? '1' : '0' );
- $return['post_types'] = $this->get_post_types();
- $return['write_permissions'] = ( is_writeable( $this->get_upload_info( 'path' ) ) ? '1' : '0' );
- $return['upload_dir_long'] = $this->get_upload_info( 'path' );
- $return['temp_prefix'] = $this->temp_prefix;
- $return = apply_filters( 'wpsdb_establish_remote_connection_data', $return );
- $result = $this->end_ajax( serialize( $return ) );
- return $result;
- }
- function format_table_sizes( $size ) {
- $size *= 1024;
- return size_format( $size );
- }
- // Generates our secret key
- function generate_key() {
- $keyset = 'abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/';
- $key = '';
- for ( $i = 0; $i < 32; $i++ ) {
- $key .= substr( $keyset, rand( 0, strlen( $keyset ) -1 ), 1 );
- }
- return $key;
- }
- function get_post_types() {
- global $wpdb;
- if( is_multisite() ) {
- $tables = $this->get_tables();
- $sql = "SELECT `post_type` FROM `{$wpdb->prefix}posts` ";
- foreach( $tables as $table ) {
- if( 0 == preg_match( '/' . $wpdb->prefix . '[0-9]+_posts/', $table ) ) continue;
- $blog_id = str_replace( array( $wpdb->prefix, '_posts' ), array( '', '' ), $table );
- $sql .= "UNION SELECT `post_type` FROM `{$wpdb->prefix}" . $blog_id . "_posts` ";
- }
- $sql .= ";";
- $post_types = $wpdb->get_results( $sql, ARRAY_A );
- }
- else {
- $post_types = $wpdb->get_results(
- "SELECT DISTINCT `post_type`
- FROM `{$wpdb->prefix}posts`
- WHERE 1;", ARRAY_A
- );
- }
- $return = array( 'revision' );
- foreach( $post_types as $post_type ) {
- $return[] = $post_type['post_type'];
- }
- return apply_filters( 'wpsdb_post_types', array_unique( $return ) );
- }
- // Retrieves the specified profile, if -1, returns the default profile
- function get_profile( $profile_id ) {
- --$profile_id;
- if ( $profile_id == '-1' || ! isset( $this->settings['profiles'][$profile_id] ) ) {
- return $this->default_profile;
- }
- return $this->settings['profiles'][$profile_id];
- }
- function get_table_row_count() {
- global $wpdb;
- $results = $wpdb->get_results( $wpdb->prepare(
- 'SELECT table_name, TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s', DB_NAME
- ), ARRAY_A
- );
- $return = array();
- foreach( $results as $results ) {
- $return[$results['table_name']] = ( $results['TABLE_ROWS'] == 0 ? 1 : $results['TABLE_ROWS'] );
- }
- return $return;
- }
- function get_table_sizes( $scope = 'regular' ) {
- global $wpdb;
- $prefix = ( $scope == 'temp' ? $this->temp_prefix : $wpdb->prefix );
- $results = $wpdb->get_results( $wpdb->prepare(
- 'SELECT TABLE_NAME AS "table",
- ROUND((data_length + index_length)/1024,0) AS "size"
- FROM information_schema.TABLES
- WHERE information_schema.TABLES.table_schema="%s"
- AND information_schema.TABLES.table_type="%s"', DB_NAME, "BASE TABLE"
- ), ARRAY_A
- );
- $return = array();
- foreach ( $results as $result ) {
- $return[$result['table']] = $result['size'];
- }
- return apply_filters( 'wpsdb_table_sizes', $return, $scope );
- }
- function get_post_max_size() {
- $val = trim( ini_get( 'post_max_size' ) );
- $last = strtolower( $val[ strlen( $val ) - 1 ] );
- switch ( $last ) {
- case 'g':
- $val *= 1024;
- case 'm':
- $val *= 1024;
- case 'k':
- $val *= 1024;
- }
- return $val;
- }
- function get_sensible_pull_limit() {
- return apply_filters( 'wpsdb_sensible_pull_limit', min( 26214400, $this->settings['max_request'] ) );
- }
- function get_bottleneck( $type = 'regular' ) {
- $suhosin_limit = false;
- $suhosin_request_limit = false;
- $suhosin_post_limit = false;
- if ( function_exists( 'ini_get' ) ) {
- $suhosin_request_limit = $this->return_bytes( ini_get( 'suhosin.request.max_value_length' ) );
- $suhosin_post_limit = $this->return_bytes( ini_get( 'suhosin.post.max_value_length' ) );
- }
- if ( $suhosin_request_limit && $suhosin_post_limit ) {
- $suhosin_limit = min( $suhosin_request_limit, $suhosin_post_limit );
- }
- // we have to account for HTTP headers and other bloating, here we minus 1kb for bloat
- $post_max_upper_size = apply_filters( 'wpsdb_post_max_upper_size', 26214400 );
- $calculated_bottleneck = min( ( $this->get_post_max_size() - 1024 ), $post_max_upper_size );
- if ( $suhosin_limit ) {
- $calculated_bottleneck = min( $calculated_bottleneck, $suhosin_limit - 1024 );
- }
- if( $type != 'max' ) {
- $calculated_bottleneck = min( $calculated_bottleneck, $this->settings['max_request'] );
- }
- return apply_filters( 'wpsdb_bottleneck', $calculated_bottleneck );
- }
- function format_dump_name( $dump_name ) {
- $extension = '.sql';
- $dump_name = sanitize_file_name( $dump_name );
- if ( $this->gzip() && isset( $this->form_data['gzip_file'] ) ) {
- $extension .= '.gz';
- }
- return $dump_name . $extension;
- }
- function options_page() {
- ?>
- <div class="wrap wpsdb">
- <div id="icon-tools" class="icon32"><br /></div><h2>Migrate DB</h2>
- <h2 class="nav-tab-wrapper"><a href="#" class="nav-tab nav-tab-active js-action-link migrate" data-div-name="migrate-tab"><?php _e( 'Migrate', 'wp-sync-db' ); ?></a><a href="#" class="nav-tab js-action-link settings" data-div-name="settings-tab"><?php _e( 'Settings', 'wp-sync-db' ); ?></a><a href="#" class="nav-tab js-action-link help" data-div-name="help-tab"><?php _e( 'Help', 'wp-sync-db' ); ?></a></h2>
- <?php do_action( 'wpsdb_notices' ); ?>
- <?php
- $hide_warning = apply_filters( 'wpsdb_hide_outdated_addons_warning', false );
- foreach( $this->addons as $addon_basename => $addon ) {
- if( false == $this->is_addon_outdated( $addon_basename ) || false == is_plugin_active( $addon_basename ) ) continue;
- $update_url = wp_nonce_url( network_admin_url( 'update.php?action=upgrade-plugin&plugin=' . urlencode( $addon_basename ) ), 'upgrade-plugin_' . $addon_basename );
- $addon_slug = current( explode( '/', $addon_basename ) );
- if ( isset( $GLOBALS['wpsdb_meta'][$addon_slug]['version'] ) ) {
- $version = ' (' . $GLOBALS['wpsdb_meta'][$addon_slug]['version'] . ')';
- }
- else {
- $version = '';
- }
- ?>
- <div class="updated warning inline-message">
- <strong>Update Required</strong> —
- <?php printf( __( 'The version of the %1$s addon you have installed%2$s is out-of-date and will not work with this version WP Sync DB. <a href="%3$s">Update Now</a>', 'wp-sync-db' ), $addon['name'], $version, $update_url ); ?>
- </div>
- <?php
- }
- $hide_warning = apply_filters( 'wpsdb_hide_safe_mode_warning', false );
- if ( function_exists( 'ini_get' ) && ini_get( 'safe_mode' ) && !$hide_warning ) { ?>
- <div class="updated warning inline-message">
- <?php
- _e( "<strong>PHP Safe Mode Enabled</strong> — We do not officially sup…
Large files files are truncated, but you can click here to view the full file