function snapshot_ajax_restore_convert_db_content($table_data) { global $wpdb; //echo "table_data<pre>"; print_r($table_data); echo "</pre>"; if (!defined('SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE')) { define('SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE', 500); } if (empty($table_data)) { return; } if (!isset($table_data['table_name'])) { return; } $table_name = $table_data['table_name']; if (isset($this->_session->data['MANIFEST']['TABLES'][$table_name])) { $table_set = $this->_session->data['MANIFEST']['TABLES'][$table_name]; } else { return; } if (!isset($this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'])) { return; } if ($this->_session->data['MANIFEST']['WP_HOME'] == $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'] && $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_BLOG_ID'] == $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['WP_BLOG_ID']) { return; } $blog_prefix = $wpdb->get_blog_prefix($_POST['snapshot-blog-id']); $_old_siteurl = str_replace('http://', '://', $this->_session->data['MANIFEST']['WP_HOME']); $_old_siteurl = str_replace('https://', '://', $_old_siteurl); $_new_siteurl = str_replace('http://', '://', $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_SITEURL']); $_new_siteurl = str_replace('https://', '://', $_new_siteurl); //echo "_old_siteurl[". $_old_siteurl ."]<br />"; //echo "_new_siteurl[". $_new_siteurl ."]<br />"; //echo "MANIFEST<pre>"; print_r($this->_session->data['MANIFEST']); echo "</pre>"; //die(); $replacement_strs = array(); // First we add the fill image path '://www.somesite.com/wp-content/uploads/sites/2/2012/10/image.gif $old_str = trailingslashit($_old_siteurl) . $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR']; $new_str = trailingslashit($_new_siteurl) . $this->_session->data['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR']; //$old_str = $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR']; //$new_str = $this->_session->data['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR']; $replacement_strs[$old_str] = $new_str; // If here we may have URLs in posts which are http://www.somesite.com/files/2012/10/image.gif instead of // http://www.somesite.com/wp-content/uploads/sites/2/2012/10/image.gif if (!defined('BLOGUPLOADDIR') && isset($this->_session->data['MANIFEST']['WP_UPLOADBLOGSDIR']) && !empty($this->_session->data['MANIFEST']['WP_UPLOADBLOGSDIR'])) { $old_str = trailingslashit($_old_siteurl) . 'files'; $new_str = trailingslashit($_new_siteurl) . $this->_session->data['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR']; $replacement_strs[$old_str] = $new_str; } // Now add our base old/new domains as a final check. // $replacement_strs[$_old_siteurl] = $_new_siteurl; //echo "replacement_strs<pre>"; print_r($replacement_strs); echo "</pre>"; //error_log(__FUNCTION__ .": replacement_strs<pre>". print_r($replacement_strs, true) ."</pre>"); $this->snapshot_logger->log_message('restore: table: ' . $table_data['table_name'] . ' converting URLs from [' . $_old_siteurl . '] -> [' . $_new_siteurl . ']'); switch ($table_set['table_name_base']) { case 'options': // Options table $limit_start = 0; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; while (true) { $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` LIMIT %d,%d", $limit_start, $limit_end); //echo "sql_str=[". $sql_str ."]<br />"; //error_log(__FUNCTION__ .": sql[". $sql_str ."]"); $db_rows = $wpdb->get_results($sql_str); if (!empty($db_rows)) { foreach ($db_rows as $row) { //echo "row<pre>"; print_r($row); echo "</pre>"; $new_value = $row->option_value; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->option_value) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET option_value=%s WHERE option_id=%d", $new_value, $row->option_id); //error_log(__FUNCTION__ .": sql[". $sql_str ."]"); $wpdb->query($sql_str); } } $limit_start = $limit_end; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; } else { break; } } // Options - user_roles $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE option_name=%s LIMIT 1", $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'] . "user_roles"); //echo "sql_str=[". $sql_str ."]<br />"; //error_log(__FUNCTION__ .": sql[". $sql_str ."]"); $db_row = $wpdb->get_row($sql_str); //echo "db_row<pre>"; print_r($db_row); echo "</pre>"; //error_log(__FUNCTION__ .": db_row<pre>". print_r($db_row, true). "</pre>"); if (!empty($db_row)) { $new_value = $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_DB_PREFIX'] . "user_roles"; $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET option_name=%s WHERE option_id=%d", $new_value, $db_row->option_id); //echo "sql_str=[". $sql_str ."]<br />"; //error_log(__FUNCTION__ .": sql[". $sql_str ."]"); $wpdb->query($sql_str); } // Options - upload_path $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE option_name=%s LIMIT 1", 'upload_path'); //echo "sql_str=[". $sql_str ."]<br />"; $db_row = $wpdb->get_row($sql_str); //echo "db_row<pre>"; print_r($db_row); echo "</pre>"; if (!empty($db_row)) { $new_value = Snapshot_Helper_Utility::replace_value($db_row->option_value, $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR'], $this->_session->data['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR']); if ($new_value != $db_row->option_value) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET option_value=%s WHERE option_id=%d", $new_value, $db_row->option_id); $wpdb->query($sql_str); } } // Options - upload_url_path $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE option_name=%s LIMIT 1", 'upload_url_path'); //echo "sql_str=[". $sql_str ."]<br />"; $db_row = $wpdb->get_row($sql_str); //echo "db_row<pre>"; print_r($db_row); echo "</pre>"; if (!empty($db_row)) { $new_value = Snapshot_Helper_Utility::replace_value($db_row->option_value, $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR'], $this->_session->data['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR']); if ($new_value != $db_row->option_value) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET option_value=%s WHERE option_id=%d", $new_value, $db_row->option_id); $wpdb->query($sql_str); } } // Options - siteurl $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE option_name=%s LIMIT 1", 'siteurl'); //echo "sql_str=[". $sql_str ."]<br />"; $db_row = $wpdb->get_row($sql_str); //echo "db_row<pre>"; print_r($db_row); echo "</pre>"; if (!empty($db_row)) { $new_value = Snapshot_Helper_Utility::replace_value($db_row->option_value, $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['WP_SITEURL'], $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_SITEURL']); if ($new_value != $db_row->option_value) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET option_value=%s WHERE option_id=%d", $new_value, $db_row->option_id); $wpdb->query($sql_str); } } // Options - home $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE option_name=%s LIMIT 1", 'home'); //echo "sql_str=[". $sql_str ."]<br />"; $db_row = $wpdb->get_row($sql_str); //echo "db_row<pre>"; print_r($db_row); echo "</pre>"; if (!empty($db_row)) { if (defined('SUBDOMAIN_INSTALL')) { $home_url = untrailingslashit($this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_SITEURL']); } else { $blog_id = !empty($this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_BLOG_ID']) ? $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_BLOG_ID'] : false; if ($blog_id && is_multisite()) { switch_to_blog($blog_id); } $home_url = home_url(); if ($blog_id && is_multisite()) { restore_current_blog(); } } $new_value = Snapshot_Helper_Utility::replace_value($db_row->option_value, $this->_session->data['MANIFEST']['WP_HOME'], $home_url); if ($new_value != $db_row->option_value) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET option_value=%s WHERE option_id=%d", $new_value, $db_row->option_id); $wpdb->query($sql_str); } } break; case 'posts': // Posts table $limit_start = 0; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; while (true) { $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` LIMIT %d,%d", $limit_start, $limit_end); //echo "sql_str=[". $sql_str ."]<br />"; $db_rows = $wpdb->get_results($sql_str); if (!empty($db_rows)) { //echo "dp_rows<pre>"; print_r($db_rows); echo "</pre>"; foreach ($db_rows as $row) { // Update post_title if (!empty($row->post_title)) { $new_value = $row->post_title; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->post_title) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET post_title=%s WHERE ID=%d", $new_value, $row->ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } // Update post_content if (!empty($row->post_content)) { $new_value = $row->post_content; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->post_content) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET post_content=%s WHERE ID=%d", $new_value, $row->ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } // Update post_content_filtered if (!empty($row->post_content_filtered)) { $new_value = $row->post_content_filtered; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->post_content_filtered) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET post_content_filtered=%s WHERE ID=%d", $new_value, $row->ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } // Update post_excerpt if (!empty($row->post_excerpt)) { $new_value = $row->post_excerpt; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->post_excerpt) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET post_excerpt=%s WHERE ID=%d", $new_value, $row->ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } // Update guid if (!empty($row->guid)) { $new_value = $row->guid; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->guid) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET guid=%s WHERE ID=%d", $new_value, $row->ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } // Update pinged if (!empty($row->pinged)) { $new_value = $row->pinged; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->guid) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET pinged=%s WHERE ID=%d", $new_value, $row->ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } } $limit_start = $limit_end; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; } else { break; } } break; case 'postmeta': // Posts Meta table $limit_start = 0; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; while (true) { $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` LIMIT %d,%d", $limit_start, $limit_end); //echo "sql_str=[". $sql_str ."]<br />"; $db_rows = $wpdb->get_results($sql_str); if (!empty($db_rows)) { //echo "dp_rows<pre>"; print_r($db_rows); echo "</pre>"; foreach ($db_rows as $row) { $new_value = $row->meta_value; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->meta_value) { //echo "postmeta [". $row->meta_name ."] [". $row->meta_value ."] [". $new_value ."]<br />"; $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET meta_value=%s WHERE meta_id=%d", $new_value, $row->meta_id); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } $limit_start = $limit_end; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; } else { break; } } break; case 'comments': // Comments table $limit_start = 0; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; while (true) { $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` LIMIT %d,%d", $limit_start, $limit_end); //echo "sql_str=[". $sql_str ."]<br />"; $db_rows = $wpdb->get_results($sql_str); //echo "dp_rows<pre>"; print_r($db_rows); echo "</pre>"; if (!empty($db_rows)) { foreach ($db_rows as $row) { // Update comment_content if (!empty($row->comment_content)) { $new_value = $row->comment_content; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->comment_content) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET comment_content=%s WHERE comment_ID=%d", $new_value, $row->comment_ID); $wpdb->query($sql_str); } } // Update comment_author_url if (!empty($row->comment_author_url)) { $new_value = $row->comment_author_url; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->comment_author_url) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET comment_author_url=%s WHERE comment_ID=%d", $new_value, $row->comment_ID); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } } $limit_start = $limit_end; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; } else { break; } } break; case 'commentmeta': // Comment Meta table $limit_start = 0; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; while (true) { $sql_str = $wpdb->prepare("SELECT * FROM `" . $table_set['table_name_restore'] . "` LIMIT %d,%d", $limit_start, $limit_end); //echo "sql_str=[". $sql_str ."]<br />"; $db_rows = $wpdb->get_results($sql_str); if (!empty($db_rows)) { //echo "dp_rows<pre>"; print_r($db_rows); echo "</pre>"; foreach ($db_rows as $row) { $new_value = $row->meta_value; foreach ($replacement_strs as $_old_str => $_new_str) { $new_value = Snapshot_Helper_Utility::replace_value($new_value, $_old_str, $_new_str); } if ($new_value != $row->meta_value) { $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET meta_value=%s WHERE meta_id=%d", $new_value, $row->meta_id); //echo "sql_str=[". $sql_str ."]<br />"; $wpdb->query($sql_str); } } $limit_start = $limit_end; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; } else { break; } } break; case 'usermeta': $limit_start = 0; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; while (true) { $sql_str = sprintf("SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE meta_key like '" . $this->_session->data['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'] . "%s' LIMIT %d,%d", '%', $limit_start, $limit_end); //echo "sql_str=[". $sql_str ."]<br />"; //error_log(__FUNCTION__ .": sql[". $sql_str ."]"); //$this->snapshot_logger->log_message('restore: table: '. $table_data['table_name'] .' sql_str ['. $sql_str .']'); $db_rows = $wpdb->get_results($sql_str); if (!empty($db_rows)) { //echo "dp_rows<pre>"; print_r($db_rows); echo "</pre>"; //die(); foreach ($db_rows as $row) { $new_value = str_replace($this->_session->data['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'], $this->_session->data['MANIFEST']['RESTORE']['DEST']['WP_DB_PREFIX'], $row->meta_key); $sql_str = $wpdb->prepare("UPDATE `" . $table_set['table_name_restore'] . "` SET meta_key=%s WHERE umeta_id=%d", $new_value, $row->umeta_id); //$this->snapshot_logger->log_message('restore: table: '. $table_data['table_name'] .' sql_str ['. $sql_str .']'); //echo "sql_str=[". $sql_str ."]<br />"; //error_log(__FUNCTION__ .": sql[". $sql_str ."]"); $wpdb->query($sql_str); } $limit_start = $limit_end; $limit_end = $limit_start + SNAPSHOT_RESTORE_MIGRATION_LIMIT_SIZE; } else { break; } } break; case 'users': if (!is_multisite()) { // For non-Multisite we want to drop the extra columns from the users table. But we don't // know if the archive was from a regular or Miltisite. $sql_str = "SELECT * FROM `" . $table_set['table_name_restore'] . "` WHERE 1=1 LIMIT 1"; $db_rows = $wpdb->get_row($sql_str); if ($db_rows) { $alter_tables = array(); if (isset($db_rows->spam)) { $alter_tables[] = "DROP `spam`"; } if (isset($db_rows->deleted)) { $alter_tables[] = "DROP `deleted`"; } if (count($alter_tables)) { $sql_str_alter = "ALTER TABLE `" . $table_set['table_name_restore'] . "` " . implode(',', $alter_tables); $wpdb->query($sql_str_alter); } } } break; default: break; } }