private function restored_table($table, $import_table_prefix, $old_table_prefix) { global $wpdb, $updraftplus; if ($table == $import_table_prefix . UpdraftPlus_Options::options_table()) { $this->restore_configuration_bundle($table); } if (preg_match('/^([\\d+]_)?options$/', substr($table, strlen($import_table_prefix)), $matches)) { if ($this->is_multisite && !empty($matches[1]) || !$this->is_multisite && $table == $import_table_prefix . 'options') { $mprefix = empty($matches[1]) ? '' : $matches[1]; $new_table_name = $import_table_prefix . $mprefix . "options"; // WordPress has an option name predicated upon the table prefix. Yuk. if ($import_table_prefix != $old_table_prefix) { $updraftplus->log("Table prefix has changed: changing options table field(s) accordingly (" . $mprefix . "options)"); echo sprintf(__('Table prefix has changed: changing %s table field(s) accordingly:', 'updraftplus'), 'option') . ' '; if (false === $wpdb->query("UPDATE {$new_table_name} SET option_name='{$import_table_prefix}" . $mprefix . "user_roles' WHERE option_name='{$old_table_prefix}" . $mprefix . "user_roles' LIMIT 1")) { echo __('Error', 'updraftplus'); $updraftplus->log("Error when changing options table fields: " . $wpdb->last_error); } else { $updraftplus->log("Options table fields changed OK"); echo __('OK', 'updraftplus'); } echo '<br>'; } // Now deal with the situation where the imported database sets a new over-ride upload_path that is absolute - which may not be wanted $new_upload_path = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM {$import_table_prefix}" . $mprefix . "options WHERE option_name = %s LIMIT 1", 'upload_path')); $new_upload_path = is_object($new_upload_path) ? $new_upload_path->option_value : ''; // The danger situation is absolute and points somewhere that is now perhaps not accessible at all if (!empty($new_upload_path) && $new_upload_path != $this->prior_upload_path && strpos($new_upload_path, '/') === 0 || preg_match('#^[A-Za-z]:[/\\\\]#', $new_upload_path)) { // $this->old_siteurl != untrailingslashit(site_url()) is not a perfect proxy for "is a migration" (other possibilities exist), but since the upload_path option should not exist since WP 3.5 anyway, the chances of other possibilities are vanishingly small if (!file_exists($new_upload_path) || $this->old_siteurl != $this->our_siteurl) { if (!file_exists($new_upload_path)) { $updraftplus->log_e("Uploads path (%s) does not exist - resetting (%s)", $new_upload_path, $this->prior_upload_path); } else { $updraftplus->log_e("Uploads path (%s) has changed during a migration - resetting (to: %s)", $new_upload_path, $this->prior_upload_path); } if (false === $wpdb->query("UPDATE {$import_table_prefix}" . $mprefix . "options SET option_value='" . esc_sql($this->prior_upload_path) . "' WHERE option_name='upload_path' LIMIT 1")) { echo __('Error', 'updraftplus'); $updraftplus->log("Error when changing upload path: " . $wpdb->last_error); $updraftplus->log("Failed"); } #update_option('upload_path', $this->prior_upload_path); } } # TODO:Do on all WPMU tables if ($table == $import_table_prefix . 'options') { # Bad plugin that hard-codes path references - https://wordpress.org/plugins/custom-content-type-manager/ $cctm_data = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM {$new_table_name} WHERE option_name = %s LIMIT 1", 'cctm_data')); if (!empty($cctm_data->option_value)) { $cctm_data = maybe_unserialize($cctm_data->option_value); if (is_array($cctm_data) && !empty($cctm_data['cache']) && is_array($cctm_data['cache'])) { $cctm_data['cache'] = array(); $updraftplus->log_e("Custom content type manager plugin data detected: clearing option cache"); update_option('cctm_data', $cctm_data); } } # Another - http://www.elegantthemes.com/gallery/elegant-builder/ $elegant_data = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM {$new_table_name} WHERE option_name = %s LIMIT 1", 'et_images_temp_folder')); if (!empty($elegant_data->option_value)) { $dbase = basename($elegant_data->option_value); $wp_upload_dir = wp_upload_dir(); $edir = $wp_upload_dir['basedir']; if (!is_dir($edir . '/' . $dbase)) { @mkdir($edir . '/' . $dbase); } $updraftplus->log_e("Elegant themes theme builder plugin data detected: resetting temporary folder"); update_option('et_images_temp_folder', $edir . '/' . $dbase); } } # The gantry menu plugin sometimes uses too-long transient names, causing the timeout option to be missing; and hence the transient becomes permanent. # WP 3.4 onwards has $wpdb->delete(). But we support 3.2 onwards. $wpdb->query("DELETE FROM {$new_table_name} WHERE option_name LIKE '_transient_gantry-menu%' OR option_name LIKE '_transient_timeout_gantry-menu%'"); # Jetpack: see: https://wordpress.org/support/topic/issues-with-dev-site if ($this->old_siteurl != $this->our_siteurl) { $wpdb->query("DELETE FROM {$new_table_name} WHERE option_name = 'jetpack_options'"); } } } elseif ($import_table_prefix != $old_table_prefix && preg_match('/^([\\d+]_)?usermeta$/', substr($table, strlen($import_table_prefix)), $matches)) { # This table is not a per-site table, but per-install $updraftplus->log("Table prefix has changed: changing usermeta table field(s) accordingly"); echo sprintf(__('Table prefix has changed: changing %s table field(s) accordingly:', 'updraftplus'), 'usermeta') . ' '; $errors_occurred = false; if (false === strpos($old_table_prefix, '_')) { // Old, slow way: do it row-by-row // By Jul 2015, doing this on the updraftplus.com database took 20 minutes on a slow test machine $old_prefix_length = strlen($old_table_prefix); $um_sql = "SELECT umeta_id, meta_key \n\t\t\t\t\tFROM {$import_table_prefix}usermeta \n\t\t\t\t\tWHERE meta_key \n\t\t\t\t\tLIKE '" . str_replace('_', '\\_', $old_table_prefix) . "%'"; $meta_keys = $wpdb->get_results($um_sql); foreach ($meta_keys as $meta_key) { //Create new meta key $new_meta_key = $import_table_prefix . substr($meta_key->meta_key, $old_prefix_length); $query = "UPDATE " . $import_table_prefix . "usermeta \n\t\t\t\t\t\tSET meta_key='" . $new_meta_key . "' \n\t\t\t\t\t\tWHERE umeta_id=" . $meta_key->umeta_id; if (false === $wpdb->query($query)) { $errors_occurred = true; } } } else { // New, fast way: do it in a single query $sql = "UPDATE {$import_table_prefix}usermeta SET meta_key = REPLACE(meta_key, '{$old_table_prefix}', '{$import_table_prefix}') WHERE meta_key LIKE '" . str_replace('_', '\\_', $old_table_prefix) . "%';"; if (false === $wpdb->query($sql)) { $errors_occurred = true; } } if ($errors_occurred) { $updraftplus->log("Error when changing usermeta table fields"); echo __('Error', 'updraftplus'); } else { $updraftplus->log("Usermeta table fields changed OK"); echo __('OK', 'updraftplus'); } echo "<br>"; } do_action('updraftplus_restored_db_table', $table, $import_table_prefix); // Re-generate permalinks. Do this last - i.e. make sure everything else is fixed up first. if ($table == $import_table_prefix . 'options') { $this->flush_rewrite_rules(); } }