This repository was archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitMigrateWP.php
More file actions
81 lines (75 loc) · 2.35 KB
/
BitMigrateWP.php
File metadata and controls
81 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* Plugin Name: Bit Migrate WP
* Plugin URI: https://github.com/doublebit/BitMigrateWP
* Description: Automatic migration for WordPress websites
* Author: DoubleBit
* Version: 0.9
* Author URI: http://www.doublebit.net/
* License: GPL2
*/
function BMWP_check() {
$old_url = site_url();
$new_url = BMWP_guess_url();
if ($old_url != $new_url) {
BMWP_update_url($old_url, $new_url);
}
}
function BMWP_update_url($old, $new) {
global $wpdb;
$queries = array (
'content' => "UPDATE $wpdb->posts SET post_content = replace(post_content, %s, %s)",
'excerpts' => "UPDATE $wpdb->posts SET post_excerpt = replace(post_excerpt, %s, %s)",
'attachments' => "UPDATE $wpdb->posts SET guid = replace(guid, %s, %s) WHERE post_type = 'attachment'",
'custom' => "UPDATE $wpdb->postmeta SET meta_value = replace(meta_value, %s, %s)",
'guids' => "UPDATE $wpdb->posts SET guid = replace(guid, %s, %s)"
);
foreach ($queries as $query) {
$result = $wpdb->query($wpdb->prepare($query, $old, $new));
}
$all = wp_load_alloptions();
foreach ($all as $name => $value) {
if (stristr($value, $old)) {
$x = get_option($name);
$x = BMWP_replace($old, $new, $x);
update_option($name, $x);
}
}
}
function BMWP_guess_url() {
if (defined('WP_SITEURL') && '' != WP_SITEURL) {
$url = WP_SITEURL;
} else {
$current_slug = '';
global $post;
$current_slug = get_post( $post )->post_name;
$schema = is_ssl() ? 'https://' : 'http://';
$remove_what = array (
'|/wp-admin/.*|i',
'|/wp-content/.*|i',
'|/wp-include/.*|i',
'|/wp-login.php.*|i',
'|\?.*|i'
);
if ($current_slug != '') {
$remove_what[] = '|/' . $current_slug . '.*|i';
}
$url = preg_replace($remove_what, '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
return rtrim($url, '/');
}
function BMWP_replace($find, $replace, $data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = BMWP_replace($find, $replace, $data[$key]);
} else if (is_string($value)) {
$data[$key] = str_ireplace( $find, $replace, $value);
}
}
} else if (is_string($data)) {
$data = str_ireplace($find, $replace, $data);
}
return $data;
}
add_action('wp', 'BMWP_check');