Completed
Push — master ( 915e44...153a5f )
by Marin
02:08
created

WP_Hydra   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 144
ccs 49
cts 49
cp 1
rs 10

6 Methods

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