Changeset 1509894
- Timestamp:
- 10/07/2016 08:55:45 AM (9 years ago)
- Location:
- find-and-replace-all/trunk
- Files:
-
- 3 edited
-
frform.php (modified) (2 diffs)
-
functions.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
find-and-replace-all/trunk/frform.php
r1150781 r1509894 2 2 $findstr = isset($_POST['findstr']) ? esc_attr($_POST['findstr']) : ''; 3 3 $replacestr = isset($_POST['replacestr']) ? esc_attr($_POST['replacestr']) : ''; 4 if ($_SERVER['REQUEST_METHOD'] == 'POST' and $findstr!='') { 4 if ($_SERVER['REQUEST_METHOD'] == 'POST' and $findstr != '') { 5 6 $url = backup_database(); 7 8 echo "<br /><strong>Database backup completed <a href=\"$url\">Click Here to Download</a><br /></strong>"; 9 5 10 global $wpdb; 6 11 $tables = $wpdb->get_results('SHOW TABLES FROM ' . DB_NAME, ARRAY_N); … … 8 13 foreach ($tables as $tablearr) { 9 14 $table = $tablearr[0]; 10 $columns = $wpdb->get_results("SHOW COLUMNS FROM $table"); 15 16 $primary_column = $wpdb->get_var("SHOW COLUMNS FROM $table WHERE `key`='PRI'"); 17 if ($primary_column == '') { 18 continue; 19 } 20 $columns = $wpdb->get_results("SHOW COLUMNS FROM $table WHERE `key`!='PRI'"); 21 11 22 foreach ($columns as $column) { 12 if (isset($column->Key) and $column->Key != 'PRI') {23 if (isset($column->Key)) { 13 24 $field = $column->Field; 14 $csql = "UPDATE $table SET `$field` = REPLACE(`$field`, '$findstr', '$replacestr');"; 15 $wpdb->get_results($csql); 25 //$csql = "UPDATE $table SET `$field` = REPLACE(`$field`, '$findstr', '$replacestr');"; 26 $csql = "SELECT `$primary_column`, `$field` FROM $table"; 27 $cresults = $wpdb->get_results($csql); 28 foreach ($cresults as $cresult) { 29 $primary_id = (int) $cresult->$primary_column; 30 31 if (is_serialized_string($cresult->$field)) { 32 $unserialized = @unserialize($cresult->$field); 33 if ($unserialized) { 34 $unserialized = esc_sql(replace_recursive($unserialized, $findstr, $replacestr)); 35 $updatesql = "UPDATE $table SET `$field` = '$unserialized' WHERE `$primary_column`='$primary_id'"; 36 $wpdb->get_results($updatesql); 37 } 38 } else { 39 $replacedstr = esc_sql(str_replace($findstr, $replacestr, $cresult->$field)); 40 $updatesql = "UPDATE $table SET `$field` = '$replacedstr' WHERE `$primary_column`='$primary_id'"; 41 $wpdb->get_results($updatesql); 42 } 43 } 44 45 echo "<br />Replacing in table {$table}.{$field}"; 16 46 } 17 47 } 18 echo "<br /> Replacingin table $table";48 echo "<br />All fields replaced in table $table"; 19 49 } 20 50 echo "<br />All done. Okay!!"; 51 52 echo "<br /><strong>Database backup: <a href=\"$url\">Click Here to Download</a><br /></strong>"; 21 53 } 54 22 55 ?> 23 56 <script> -
find-and-replace-all/trunk/functions.php
r1150781 r1509894 4 4 Plugin Name: Find and Replace All 5 5 Description: A wordpress plugin to find and replace from all the tables and all the fields 6 Version: 1. 06 Version: 1.1 7 7 Author: Taraprasad Swain 8 8 Author URI: http://www.taraprasad.com 9 9 10 Copyright 201 5by Taraprasad.com (email : swain.tara@gmail.com)10 Copyright 2016 by Taraprasad.com (email : swain.tara@gmail.com) 11 11 12 12 This program is free software; you can redistribute it and/or modify … … 37 37 include('frform.php'); 38 38 } 39 40 if (!function_exists('replace_recursive')) { 41 42 function replace_recursive(Array $array, $key, $value) { 43 array_walk_recursive($array, function(&$v, $k) use ($key, $value) { 44 $k == $key && $v = $value; 45 }); 46 return $array; 47 } 48 49 } 50 51 if (!function_exists('backup_database')) { 52 53 function backup_database($tables = '*') { 54 55 $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 56 57 mysql_select_db(DB_NAME, $link); 58 59 if ($tables == '*') { 60 $tables = array(); 61 $result = mysql_query('SHOW TABLES'); 62 while ($row = mysql_fetch_row($result)) { 63 $tables[] = $row[0]; 64 } 65 } else { 66 $tables = is_array($tables) ? $tables : explode(',', $tables); 67 } 68 69 foreach ($tables as $table) { 70 $result = mysql_query('SELECT * FROM ' . $table); 71 $num_fields = mysql_num_fields($result); 72 73 $return .= "DROP TABLE IF EXISTS `{$table}`;"; 74 75 $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table)); 76 $return.= "\n\n" . $row2 [1] . ";\n\n"; 77 78 for ($i = 0; $i < $num_fields; $i++) { 79 while ($row = mysql_fetch_row($result)) { 80 $return.= 'INSERT INTO ' . $table . ' VALUES('; 81 82 for ($j = 0; $j < $num_fields; $j++) { 83 $row[$j] = addslashes($row[$j]); 84 $row[$j] = ereg_replace("\n", "\\n", $row[$j]); 85 if (isset($row[$j])) { 86 $return.= '"' . $row[$j] . '"'; 87 } else { 88 $return.= '""'; 89 } 90 if ($j < ($num_fields - 1)) { 91 $return.= ','; 92 } 93 } 94 $return.= ");\n"; 95 } 96 } 97 $return.="\n\n\n"; 98 } 99 100 $filename = '/backups/db-backup-' . date('Y-m-d-His') . '.sql'; 101 102 $file_path = WP_CONTENT_DIR . $filename; 103 104 if (!file_exists(WP_CONTENT_DIR . '/backups')) { 105 mkdir(WP_CONTENT_DIR . '/backups', 0755, true); 106 file_put_contents(WP_CONTENT_DIR . '/backups/index.html', ''); 107 } 108 109 file_put_contents($file_path, $return); 110 111 return WP_CONTENT_URL . $filename; 112 } 113 114 } -
find-and-replace-all/trunk/readme.txt
r1488267 r1509894 5 5 Requires at least: 3.0.1 6 6 Tested up to: 4.6 7 Stable tag: 1. 07 Stable tag: 1.1 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 40 40 == Changelog == 41 41 42 = V1.1 - 07.10.2016 = 43 *Release Date - 7th October, 2016* 44 45 * Plugin supports serialized string replace 46 * Backups before string replace 47 42 48 = V1.0 - 29.04.2015 = 43 49 * Plugin created
Note: See TracChangeset
for help on using the changeset viewer.