@see LICENSE /** setup ************************************************************************************************************* $GLOBALS['TM_RENAME_SETUP'] = array( 'system' => __DIR__, //Path to wp-load.php 'old' => 'http://my-old-url.de',//Attention: without post-slash 'new' => 'http://my-new-url.de',//Attention: without post-slash ); //*/ if(!file_exists('rename_config.php')) { echo 'You have to add a "rename_config.php".'; exit(); } include_once(__DIR__.'/rename_config.php'); /** html output *******************************************************************************************************/ if(empty($_GET['action'])) : ?> Rename

Rename

-->

open current domain
'Wordpress system can\'t be included - make sure the system-path in your "rename_config.php" leads to the wp-load.php file.'); } echo json_encode($result); exit(); /** controller ********************************************************************************************************/ /** * Typical report output structure * array['title'] * array['query'] * array['result'][]['status'] * array['result'][]['content'] * array['summary']['count'] * array['summary']['table'] * array['summary']['col'] * array['summary']['text'] */ class TmRename { /** * will update the domain of a multisite blog if multisite is active and blog_id and blog_domain is set */ public static function renameMuBlogDomain() { global $wpdb; $report = []; $report['title'] = 'Rename Multisite blog domain'; $report['result'][] = [ 'status' => 'success', 'content' => 'Multisite not active', 'debug' => $GLOBALS['TM_RENAME_SETUP'] ]; if(empty($GLOBALS['TM_RENAME_SETUP']['blog_id'])) return $report; if(empty($GLOBALS['TM_RENAME_SETUP']['blog_domain'])) return $report; try { $job_table = 'blogs'; $query = "SHOW TABLES LIKE '".$wpdb->base_prefix.$job_table."'"; if($wpdb->query($query)===0) return $report; $query = "UPDATE ".$wpdb->base_prefix.$job_table." SET domain='".$GLOBALS['TM_RENAME_SETUP']['blog_domain']."' WHERE blog_id=".$GLOBALS['TM_RENAME_SETUP']['blog_id']; $report['query'] = $query; $result = $wpdb->query($query); if(is_wp_error($result)) throw new Exception('Error updating blog domain'); $report['result'] = []; $report['result'][] = [ 'status' => 'success', 'content' => 'blog_id: '.$GLOBALS['TM_RENAME_SETUP']['blog_id'].' - domain: '.$GLOBALS['TM_RENAME_SETUP']['blog_domain'] ]; $report['summary'] = [ 'count' => 1, 'table' => $wpdb->base_prefix.$job_table, 'col' => 'domain', 'text' => 'Multisite blog domain updated' ]; return $report; } catch(Exception $e) { $report['result'][] = [ 'status' => 'failed', 'content' => 'ERROR: '.$e->getMessage() ]; return $report; } } /** * [renameMastersliderSlides description] * @return [type] [description] */ public static function renameMastersliderSlides() { global $wpdb,$table_prefix; $job_table = 'masterslider_sliders'; $report = []; $report['title'] = 'Rename Masterslider'; try { if($wpdb->query("SHOW TABLES LIKE '".$table_prefix.$job_table."'")===0) throw new Exception('NO TABLE'); $query = "SELECT ID,title,params FROM ".$table_prefix.$job_table; $report['query'] = $query; $mastersliderParams = $wpdb->get_results($query); foreach ($mastersliderParams as $key => $dbLine) { $newJsonString = str_replace($GLOBALS['TM_RENAME_SETUP']['old'], $GLOBALS['TM_RENAME_SETUP']['new'], base64_decode($dbLine->params) ); $newValue = base64_encode($newJsonString); if($dbLine->params !== $newValue) { $updateQuery = $wpdb->prepare( 'UPDATE '.$table_prefix.$job_table.' SET params = %s WHERE ID = %d', $newValue, $dbLine->ID ); $result = $wpdb->query($updateQuery); if($result) { $report['result'][] = array( 'status' => 'success', 'content' => $dbLine->ID.' - '.$dbLine->title ); } else { $report['result'][] = array( 'status' => 'failure', 'content' => $dbLine->ID.' - '.$dbLine->title ); } } } } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR: '.$e->getMessage() ); } return $report; } public static function renameOptions() { global $wpdb,$table_prefix; $report = []; $report['title'] = 'rename options'; $query = "SELECT option_id, option_name, option_value FROM ".$table_prefix."options WHERE option_value LIKE '%".$GLOBALS['TM_RENAME_SETUP']['old']."%' "; $report['query'] = $query; $options = $wpdb->get_results($query); foreach($options as $line) { $currentOption = get_option($line->option_name); self::recursiveArrayReplace($GLOBALS['TM_RENAME_SETUP']['old'],$GLOBALS['TM_RENAME_SETUP']['new'],$currentOption); $result = update_option($line->option_name, $currentOption); if($result) { $report['result'][] = array( 'status' => 'success', 'content' => $line->option_name ); } else { $report['result'][] = array( 'status' => 'failure', 'content' => $line->option_name ); } } return $report; } public static function renamePostmeta() { global $wpdb,$table_prefix; $report = []; $report['title'] = 'rename postmeta'; $query = "SELECT meta_id, post_id, meta_key,meta_value FROM ".$table_prefix."postmeta WHERE meta_value LIKE '%".$GLOBALS['TM_RENAME_SETUP']['old']."%' "; $report['query'] = $query; $dbResult = $wpdb->get_results($query); foreach($dbResult as $dbLine) { $data = maybe_unserialize($dbLine->meta_value); self::recursiveArrayReplace( $GLOBALS['TM_RENAME_SETUP']['old'], $GLOBALS['TM_RENAME_SETUP']['new'], $data); $newValue = maybe_serialize($data); $updateQuery = $wpdb->prepare( 'UPDATE '.$table_prefix.'postmeta SET meta_value = %s WHERE meta_id = %d', $newValue, $dbLine->meta_id); $result = $wpdb->query($updateQuery); if($result) { $report['result'][] = array( 'status' => 'success', 'content' => $dbLine->meta_id.' - '.$dbLine->meta_key ); } else { $report['result'][] = array( 'status' => 'failure', 'content' => $dbLine->meta_id.' - '.$dbLine->meta_key ); } } return $report; } public static function renameAdditionalTableColumns(&$result) { $walker = array( array( 'description' => 'Rename posts guid', 'table' => 'posts', 'col' => 'guid', 'type' => '' ), array( 'description' => 'Rename post content', 'table' => 'posts', 'col' => 'post_content', 'type' => '' ), array( 'description' => 'Rename post title', 'table' => 'posts', 'col' => 'post_title', 'type' => '' ), array( 'description' => 'Rename links - link_url', 'table' => 'links', 'col' => 'link_url', 'type' => '' ), array( 'description' => 'ICL - Multilanguage', 'table' => 'icl_translation_status', 'col' => 'translation_package', 'type' => 'mixed_serialized',//may be serialized data - may be not 'uniqueID' => 'rid' ), array( 'description' => 'Rename Edge-Theme frontend builder pages', 'table' => 'frontend_builder_pages', 'col' => 'items', 'type' => '' ), array( 'description' => 'Rename Rev-Slider slides', 'table' => 'revslider_slides', 'col' => 'params', 'type' => 'json_revslider' ), array( 'description' => 'Rename Rev-Slider slides', 'table' => 'revslider_slides', 'col' => 'layers', 'type' => 'json_revslider' ), array( 'description' => 'Rename Rev-Slider sliders', 'table' => 'revslider_sliders', 'col' => 'params', 'type' => 'json' ), array( 'description' => 'MailPoet body', 'table' => 'wysija_email', 'col' => 'body', 'type' => '' ), array( 'description' => 'MailPoet params', 'table' => 'wysija_email', 'col' => 'params', 'type' => 'base64_serialized', 'uniqueID' => 'email_id' ), array( 'description' => 'MailPoet wj_data', 'table' => 'wysija_email', 'col' => 'wj_data', 'type' => 'base64_serialized', 'uniqueID' => 'email_id' ), array( 'description' => 'Elementor postmeta data', 'table' => 'postmeta', 'key' => '_elementor_data',//the meta_key 'type' => 'json_postmeta' ),//*/ ); foreach($walker as $job) { switch($job['type']) { case 'json': $old = str_replace("/", "\/", $GLOBALS['TM_RENAME_SETUP']['old']); $new = str_replace("/", "\/", $GLOBALS['TM_RENAME_SETUP']['new']); $result[] = self::doStandardUpdateRename($old,$new,$job); break; case 'json_revslider': $old = str_replace("/", "\\\\\\/", $GLOBALS['TM_RENAME_SETUP']['old']); $new = str_replace("/", "\\\\\\/", $GLOBALS['TM_RENAME_SETUP']['new']); $result[] = self::doStandardUpdateRename($old,$new,$job); break; case 'json_postmeta': $old = str_replace("/", "\\\\\\/", $GLOBALS['TM_RENAME_SETUP']['old']); $new = str_replace("/", "\\\\\\/", $GLOBALS['TM_RENAME_SETUP']['new']); $result[] = self::doJsonPostmeta($old,$new,$job); break; case 'mixed_serialized': $result[] = self::doUpdateMixedSerialized($GLOBALS['TM_RENAME_SETUP']['old'], $GLOBALS['TM_RENAME_SETUP']['new'], $job); break; case 'base64_serialized': $result[] = self::doUpdateBase64Serialized($GLOBALS['TM_RENAME_SETUP']['old'], $GLOBALS['TM_RENAME_SETUP']['new'], $job); break; default: $result[] = self::doStandardUpdateRename($GLOBALS['TM_RENAME_SETUP']['old'], $GLOBALS['TM_RENAME_SETUP']['new'], $job); break; } } } public static function clearTransients() { global $wpdb; $report = []; $report['title'] = 'clear transient cache'; $sql = "DELETE FROM $wpdb->options WHERE option_name LIKE '%_transient_%'"; $clean = $wpdb->query( $sql ); $report['result'][] = array( 'status' => 'success', 'content' => $clean ); return $report; } public static function doElementorRename() { $report = []; $report['title'] = 'Elementor rename'; try { if(!class_exists('\Elementor\Utils')) { throw new Exception("Error Elementor not present", 1); } $result = \Elementor\Utils::replace_urls( $GLOBALS['TM_RENAME_SETUP']['old'], $GLOBALS['TM_RENAME_SETUP']['new'] ); $report['result'][] = array( 'status' => 'success', 'content' => 'Elementor Utils used successfull: '.$result ); } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR Elementor not found' ); } return $report; } public static function clearElementorCache() { $report = []; $report['title'] = 'clear Elementor cache'; try { if(!class_exists('Elementor\Plugin')) { throw new Exception("Error Elementor not present", 1); } $result = \Elementor\Plugin::$instance->files_manager->clear_cache(); $report['result'][] = array( 'status' => 'success', 'content' => 'Elementor cache clear' ); } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR Elementor not found' ); } return $report; } public static function clearAvadaCache() { $report = []; $report['title'] = 'clear Avada cache'; try { if(!function_exists('avada_reset_all_caches')) { throw new Exception("Error Avada not present", 1); } else { avada_reset_all_caches(); $report['result'][] = array( 'status' => 'success', 'content' => 'Avada cache cleared' ); } if(!class_exists('Fusion_Cache')) { throw new Exception("Error Fusion_Cache not present", 1); } else { $fusion_cache = new Fusion_Cache(); $fusion_cache->reset_all_caches(); $report['result'][] = array( 'status' => 'success', 'content' => 'Fusion cache cleared' ); } } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR Avada not found' ); } return $report; } public static function getTest(&$result) { $report = []; $report['title'] = 'Test'; $report['query'] = 'select * from test'; $report['result'][] = array( 'status' => 'success', 'content' => 'great, it works' ); $report['result'][] = array( 'status' => 'failure', 'content' => 'thats terrible' ); $result = array(); $result[] = $report; $result[] = array( 'title' => 'Test 2', 'summary'=> array( 'count' => 577, 'table' => 'posts', 'col' => 'guid' ) ); } /******************************************************************************************************************/ private static function doJsonPostmeta($old,$new,$job) { global $wpdb,$table_prefix; $job['table'] = 'postmeta'; $report = []; $report['title'] = $job['description']; try { if($wpdb->query("SHOW TABLES LIKE '".$table_prefix.$job['table']."'")===0) throw new Exception('NO TABLE'); $query = 'UPDATE '.$table_prefix.$job['table'].' SET meta_value = replace(meta_value, "'.$old.'", "'.$new.'") where meta_key="'.$job['key'].'";'; $result = $wpdb->query($query); if(is_wp_error($result)) throw new Exception('DB ERROR'); $report['query'] = $query; $report['summary'] = array( 'count' => $result, 'table' => $table_prefix.$job['table'], 'col' => $job['key'] ); } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR: '.$e->getMessage() ); } return $report; } private static function doStandardUpdateRename($old,$new,$job) { global $wpdb,$table_prefix; $report = []; $report['title'] = $job['description']; try { if($wpdb->query("SHOW TABLES LIKE '".$table_prefix.$job['table']."'")===0) throw new Exception('NO TABLE'); $query = "UPDATE ".$table_prefix.$job['table']." SET ".$job['col']." = replace(".$job['col'].", '".$old."','".$new."');"; $result = $wpdb->query($query); if(is_wp_error($result)) throw new Exception('DB ERROR'); $report['query'] = $query; $report['summary'] = array( 'count' => $result, 'table' => $table_prefix.$job['table'], 'col' => $job['col'] ); } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR: '.$e->getMessage() ); } return $report; } /** * Table-fields with base64 encoded serialized php objects * @param [type] $old [description] * @param [type] $new [description] * @param [type] $job [description] * @return [type] [description] */ private static function doUpdateBase64Serialized($old,$new,$job) { global $wpdb,$table_prefix; $report = []; $report['title'] = $job['description']; try { if(empty($job['uniqueID'])) throw new Exception('uniqueID fehlt'); if($wpdb->query("SHOW TABLES LIKE '".$table_prefix.$job['table']."'")===0) throw new Exception('NO TABLE'); $query = $wpdb->prepare('SELECT '.$job['uniqueID'].','.$job['col'].' FROM '.$table_prefix.$job['table'].' WHERE FROM_BASE64('.$job['col'].') LIKE %s', '%'.$old.'%'); $select = $wpdb->get_results($query, ARRAY_A); $report['query'] = $query; foreach($select as $row) { $data = $row[$job['col']]; $unserialized = unserialize(base64_decode($data), ['allowed_classes' => true]); $data = $unserialized; self::recursiveArrayReplace($old, $new, $data); $data = base64_encode(serialize($data)); $updateQuery = $wpdb->prepare( 'UPDATE '.$table_prefix.$job['table'].' SET '.$job['col'].' = %s WHERE '.$job['uniqueID'].' = %d', $data, $row[$job['uniqueID']]); $_result = $wpdb->query($updateQuery); $report['result'][] = array( 'status' => 'success', 'content' => $job['col'].' - '. $job['uniqueID'].' -- '.$row[$job['uniqueID']] ); } } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR: '.$e->getMessage() ); } return $report; } private static function doUpdateMixedSerialized($old,$new,$job) { global $wpdb,$table_prefix; $report = []; $report['title'] = $job['description']; try { if(empty($job['uniqueID'])) throw new Exception('uniqueID fehlt'); if($wpdb->query("SHOW TABLES LIKE '".$table_prefix.$job['table']."'")===0) throw new Exception('NO TABLE'); $query = $wpdb->prepare('SELECT '.$job['uniqueID'].','.$job['col'].' FROM '.$table_prefix.$job['table'].' WHERE '.$job['col'].' LIKE %s', '%'.$old.'%'); $select = $wpdb->get_results($query, ARRAY_A); $report['query'] = $query; foreach($select as $row) { $data = $row[$job['col']]; $unserialized = @unserialize($data, ['allowed_classes' => true]); if($unserialized === FALSE) {//data was not serialized self::recursiveArrayReplace($old, $new, $data); } else {//data where serialized $data = $unserialized; self::recursiveArrayReplace($old, $new, $data); $data = serialize($data); } $updateQuery = $wpdb->prepare( 'UPDATE '.$table_prefix.$job['table'].' SET '.$job['col'].' = %s WHERE '.$job['uniqueID'].' = %d', $data, $row[$job['uniqueID']]); $_result = $wpdb->query($updateQuery); $report['result'][] = array( 'status' => 'success', 'content' => $job['col'].' - '. $job['uniqueID'].' -- '.$row[$job['uniqueID']] ); } } catch(Exception $e) { $report['result'][] = array( 'status' => 'failed', 'content' => 'ERROR: '.$e->getMessage() ); } return $report; } /** * Helper function for finding an replacing * @param string $find the string to find * @param string $replace the replacement string * @param mixed $data array, object or string * @return mixed array, object or string */ private static function recursiveArrayReplace($find, $replace, &$data) { if (is_array($data) || is_object($data)) { self::fixObject($data); foreach ($data as $key => $value) { if (is_array($value) || is_object($value)) { if(is_object($data)) { self::recursiveArrayReplace($find, $replace, $data->$key); } else { self::recursiveArrayReplace($find, $replace, $data[$key]); } } else { try { if(is_object($data)) { $data->$key = str_replace($find, $replace, $value); } else { $data[$key] = str_replace($find, $replace, $value); } } catch (\Throwable $th) { continue; } } } } else { $data = str_replace($find, $replace, $data); } } private static function fixObject(&$object) { if (!is_object ($object) && gettype ($object) == 'object') return ($object = unserialize(serialize ($object))); return $object; } public static function echoStyle() { ?>