Completed
Push — master ( 3c1344...b2171c )
by Marin
01:55
created

WP_Hydra   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 61.7%
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 133
ccs 29
cts 47
cp 0.617
rs 10

5 Methods

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