These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Main WP_Hydra class. |
||
4 | * |
||
5 | * Replaces the original domain with the current domain, where necessary. |
||
6 | * All of the magic is hooked upon WP_Hydra object initialization. |
||
7 | * |
||
8 | * @package wp-hydra |
||
9 | */ |
||
10 | class WP_Hydra { |
||
11 | |||
12 | /** |
||
13 | * Constructor. |
||
14 | * |
||
15 | * Hooks all of the domain replacement functionality. |
||
16 | * |
||
17 | * @access public |
||
18 | */ |
||
19 | public function __construct() { |
||
20 | // Modify domain where necessary. |
||
21 | add_filter( 'option_blogname', array( $this, 'setup_domain' ), 1 ); |
||
22 | add_filter( 'option_siteurl', array( $this, 'setup_domain' ), 1 ); |
||
23 | add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
24 | add_filter( 'stylesheet_uri', array( $this, 'setup_domain' ), 1 ); |
||
25 | add_filter( 'stylesheet_directory_uri', array( $this, 'setup_domain' ), 1 ); |
||
26 | add_filter( 'template_directory_uri', array( $this, 'setup_domain' ), 1 ); |
||
27 | add_filter( 'plugins_url', array( $this, 'setup_domain' ), 1 ); |
||
28 | |||
29 | // Replace various occurences. |
||
30 | add_filter( 'the_content', array( $this, 'setup_content' ) ); // In oost content. |
||
31 | add_filter( 'widget_text', array( $this, 'setup_content' ) ); // In Widget text. |
||
32 | add_filter( 'upload_dir', array( $this, 'setup_upload_dir' ) ); // In wp_upload_dir(). |
||
33 | |||
34 | // Allow developers to support multiple domains in fields that contain only a site URL. |
||
35 | add_filter( 'wp_hydra_domain', array( $this, 'setup_domain' ) ); |
||
36 | |||
37 | // Allow developers to support URLs with multiple domains in their content. |
||
38 | add_filter( 'wp_hydra_content', array( $this, 'setup_content' ) ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Replaces original domain with current domain in simple fields. |
||
43 | * |
||
44 | * @access public |
||
45 | * |
||
46 | * @param string $url The current URL. |
||
47 | * @return string $url The URL with the (maybe) replaced domain. |
||
48 | */ |
||
49 | public function setup_domain( $url ) { |
||
50 | // Parse current URL. |
||
51 | $original_domain_parts = parse_url( $url ); |
||
52 | |||
53 | // If unable to retrieve the host, skip. |
||
54 | if ( empty( $original_domain_parts['host'] ) || ! isset( $_SERVER['HTTP_HOST'] ) ) { |
||
55 | return $url; |
||
56 | } |
||
57 | |||
58 | // Get original and current domain. |
||
59 | $original_domain = $original_domain_parts['host']; |
||
60 | $current_domain = $_SERVER['HTTP_HOST']; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
61 | |||
62 | // If original and current domain match, skip. |
||
63 | if ( $original_domain == $current_domain ) { |
||
64 | return $url; |
||
65 | } |
||
66 | |||
67 | return $this->replace_domain( $url, $original_domain, $current_domain ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Replace the old domain with a new domain in a specific URL. |
||
72 | * |
||
73 | * @access protected |
||
74 | * |
||
75 | * @param string $url The current URL. |
||
76 | * @param string $old_domain The old domain. |
||
77 | * @param string $new_domain The new domain. |
||
78 | * @return string $url The new URL. |
||
79 | */ |
||
80 | protected function replace_domain( $url, $old_domain, $new_domain ) { |
||
81 | // Prepare original domain and current domain with the current protocol. |
||
82 | $protocols = array( 'http://', 'https://' ); |
||
83 | $current_protocol = ( $this->is_ssl() ? 'https' : 'http' ) . '://'; |
||
84 | |||
85 | foreach ( $protocols as $protocol ) { |
||
86 | $original_base = $protocol . $old_domain; |
||
87 | $new_base = $current_protocol . $new_domain; |
||
88 | |||
89 | // Replace original domain with current domain. |
||
90 | $url = str_replace( $original_base, $new_base, $url ); |
||
91 | } |
||
92 | |||
93 | return $url; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Replaces original domain with current domain in content. |
||
98 | * |
||
99 | * @access public |
||
100 | * |
||
101 | * @param string $content The current content with the original domain. |
||
102 | * @return string $content The content with the new domain. |
||
103 | */ |
||
104 | public function setup_content( $content ) { |
||
105 | // Get original home URL. |
||
106 | remove_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
107 | $original_home = home_url( '/' ); |
||
108 | add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
109 | |||
110 | // Get current home URL. |
||
111 | $current_home = home_url( '/' ); |
||
112 | |||
113 | // Replace occurences of original URL with current home URL. |
||
114 | $content = str_replace( $original_home, $current_home, $content ); |
||
115 | |||
116 | return $content; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Replaces original domain with current domain in wp_upload_dir(). |
||
121 | * |
||
122 | * @access public |
||
123 | * |
||
124 | * @param array $upload_dir The current upload dir settings with the original domain. |
||
125 | * @return array $upload_dir The upload dir settings with the new domain. |
||
126 | */ |
||
127 | public function setup_upload_dir( $upload_dir ) { |
||
128 | // Keys of array element that we'll be updating. |
||
129 | $keys_to_update = array( |
||
130 | 'url', |
||
131 | 'baseurl', |
||
132 | ); |
||
133 | |||
134 | // Fix all targeted array elements. |
||
135 | foreach ( $keys_to_update as $key ) { |
||
136 | $upload_dir[ $key ] = apply_filters( 'wp_hydra_domain', $upload_dir[ $key ] ); |
||
137 | } |
||
138 | |||
139 | return $upload_dir; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Determine if SSL is used. |
||
144 | * |
||
145 | * @access public |
||
146 | * |
||
147 | * @return bool True if SSL, false if not used. |
||
148 | */ |
||
149 | public function is_ssl() { |
||
150 | return is_ssl(); |
||
151 | } |
||
152 | |||
153 | } |
||
154 |