1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plugin Name: WP Hydra |
4
|
|
|
* Plugin URI: https://wordpress.org/plugins/wp-hydra/ |
5
|
|
|
* Description: Allows one WordPress installation to be resolved and browsed at multiple domains. |
6
|
|
|
* Version: 1.0.4 |
7
|
|
|
* Author: tyxla |
8
|
|
|
* Author URI: http://marinatanasov.com/ |
9
|
|
|
* Plugin URI: https://github.com/tyxla/WP-Hydra |
10
|
|
|
* License: GPL2 |
11
|
|
|
* Requires at least: 3.0.1 |
12
|
|
|
* Tested up to: 4.4.2 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Main WP_Hydra class. |
17
|
|
|
* |
18
|
|
|
* Replaces the original domain with the current domain, where necessary. |
19
|
|
|
* All of the magic is hooked upon WP_Hydra object initialization. |
20
|
|
|
*/ |
21
|
|
|
class WP_Hydra { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* Hooks all of the domain replacement functionality. |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
*/ |
30
|
1 |
|
public function __construct() { |
31
|
|
|
// modify domain where necessary |
32
|
1 |
|
add_filter( 'option_blogname', array( $this, 'setup_domain' ), 1 ); |
33
|
1 |
|
add_filter( 'option_siteurl', array( $this, 'setup_domain' ), 1 ); |
34
|
1 |
|
add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
35
|
1 |
|
add_filter( 'stylesheet_uri', array( $this, 'setup_domain' ), 1 ); |
36
|
1 |
|
add_filter( 'stylesheet_directory_uri', array( $this, 'setup_domain' ), 1 ); |
37
|
1 |
|
add_filter( 'template_directory_uri', array( $this, 'setup_domain' ), 1 ); |
38
|
1 |
|
add_filter( 'plugins_url', array( $this, 'setup_domain' ), 1 ); |
39
|
|
|
|
40
|
|
|
// replace various occurences |
41
|
1 |
|
add_filter( 'the_content', array( $this, 'setup_content' ) ); // content |
42
|
1 |
|
add_filter( 'widget_text', array( $this, 'setup_content' ) ); // widget text |
43
|
1 |
|
add_filter( 'upload_dir', array( $this, 'setup_upload_dir' ) ); // wp_upload_dir(); |
44
|
|
|
|
45
|
|
|
// allow developers to support multiple domains in fields that contain only a site URL |
46
|
1 |
|
add_filter( 'wp_hydra_domain', array( $this, 'setup_domain' ) ); |
47
|
|
|
|
48
|
|
|
// allow developers to support URLs with multiple domains in their content |
49
|
1 |
|
add_filter( 'wp_hydra_content', array( $this, 'setup_content' ) ); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Replaces original domain with current domain in simple fields. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* |
57
|
|
|
* @param string $url The current URL. |
58
|
|
|
* @return string $url The URL with the (maybe) replaced domain. |
59
|
|
|
*/ |
60
|
|
|
public function setup_domain( $url ) { |
61
|
|
|
// parse current URL |
62
|
|
|
$original_domain_parts = parse_url( $url ); |
63
|
|
|
|
64
|
|
|
// if unable to retrieve the host, skip |
65
|
|
|
if ( empty( $original_domain_parts['host'] ) ) { |
66
|
|
|
return $url; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// get original and current domain |
70
|
|
|
$original_domain = $original_domain_parts['host']; |
71
|
|
|
$current_domain = $_SERVER['HTTP_HOST']; |
|
|
|
|
72
|
|
|
|
73
|
|
|
// if original and current domain match, skip |
74
|
|
|
if ( $original_domain == $current_domain ) { |
75
|
|
|
return $url; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->replace_domain( $url, $original_domain, $current_domain ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Replace the old domain with a new domain in a specific URL. |
83
|
|
|
* |
84
|
|
|
* @access protected |
85
|
|
|
* |
86
|
|
|
* @param string $url The current URL. |
87
|
|
|
* @param string $old_domain The old domain. |
88
|
|
|
* @param string $new_domain The new domain. |
89
|
|
|
* @return string $url The new URL. |
90
|
|
|
*/ |
91
|
|
|
protected function replace_domain( $url, $old_domain, $new_domain ) { |
92
|
|
|
// prepare original domain and current domain with the current protocol |
93
|
|
|
$protocols = array( 'http://', 'https://' ); |
94
|
|
|
$current_protocol = ( is_ssl() ? 'https' : 'http' ) . '://'; |
95
|
|
|
|
96
|
|
|
foreach ( $protocols as $protocol ) { |
97
|
|
|
$original_base = $protocol . $old_domain; |
98
|
|
|
$new_base = $current_protocol . $new_domain; |
99
|
|
|
|
100
|
|
|
// replace original domain with current domain |
101
|
|
|
$url = str_replace( $original_base, $new_base, $url ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $url; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Replaces original domain with current domain in content. |
109
|
|
|
* |
110
|
|
|
* @access public |
111
|
|
|
* |
112
|
|
|
* @param string $content The current content with the original domain. |
113
|
|
|
* @return string $content The content with the new domain. |
114
|
|
|
*/ |
115
|
|
|
public function setup_content( $content ) { |
116
|
|
|
// get original home URL |
117
|
|
|
remove_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
118
|
|
|
$original_home = home_url( '/' ); |
119
|
|
|
add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
120
|
|
|
|
121
|
|
|
// get current home URL |
122
|
|
|
$current_home = home_url( '/' ); |
123
|
|
|
|
124
|
|
|
// replace occurences of original URL with current home URL |
125
|
|
|
$content = str_replace( $original_home, $current_home, $content ); |
126
|
|
|
|
127
|
|
|
return $content; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Replaces original domain with current domain in wp_upload_dir(). |
132
|
|
|
* |
133
|
|
|
* @access public |
134
|
|
|
* |
135
|
|
|
* @param array $upload_dir The current upload dir settings with the original domain. |
136
|
|
|
* @return array $upload_dir The upload dir settings with the new domain. |
137
|
|
|
*/ |
138
|
|
|
public function setup_upload_dir( $upload_dir ) { |
139
|
|
|
// keys of array element that we'll be updating |
140
|
|
|
$keys_to_update = array( |
141
|
|
|
'url', |
142
|
|
|
'baseurl', |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
// fix all targeted array elements |
146
|
|
|
foreach ( $keys_to_update as $key ) { |
147
|
|
|
$upload_dir[ $key ] = apply_filters( 'wp_hydra_domain', $upload_dir[ $key ] ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $upload_dir; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// initialize WP Hydra - Polycephaly FTW! |
156
|
|
|
global $wp_hydra; |
157
|
|
|
$wp_hydra = new WP_Hydra(); |