Completed
Push — master ( 95b035...eea8c5 )
by Marin
02:16
created

wp-hydra.php (79 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
	public function __construct() {
31
		// modify domain where necessary
32
		add_filter('option_blogname', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
33
		add_filter('option_siteurl', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
34
		add_filter('option_home', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
35
		add_filter('stylesheet_uri', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
36
		add_filter('stylesheet_directory_uri', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
37
		add_filter('template_directory_uri', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
38
		add_filter('plugins_url', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
39
40
		// replace occurences in content
41
		add_filter('the_content', array($this, 'setup_content'));
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
42
43
		// replace occurences in widget text
44
		add_filter('widget_text', array($this, 'setup_content'));
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
45
46
		// replace occurences in wp_upload_dir();
47
		add_filter('upload_dir', array($this, 'setup_upload_dir'));
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
48
49
		// allow developers to support multiple domains in fields that contain only a site URL
50
		add_filter('wp_hydra_domain', array($this, 'setup_domain'));
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
51
52
		// allow developers to support URLs with multiple domains in their content
53
		add_filter('wp_hydra_content', array($this, 'setup_content'));
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
54
	}
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
	public function setup_domain($url) {
65
		// parse current URL
66
		$original_domain_parts = parse_url($url);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
67
68
		// if unable to retrieve the host, skip
69
		if (empty($original_domain_parts['host'])) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
70
			return $url;
71
		}
72
73
		// get original and current domain
74
		$original_domain = $original_domain_parts['host'];
75
		$current_domain = $_SERVER['HTTP_HOST'];
76
77
		// if original and current domain match, skip
78
		if ($original_domain == $current_domain) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
79
			return $url;
80
		}
81
82
		// prepare original domain and current domain with the current protocol
83
		$protocols = array('http://', 'https://');
0 ignored issues
show
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
84
		$current_protocol = (is_ssl() ? 'https' : 'http') . '://';
85
		foreach ($protocols as $protocol) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
86
			$original_base = $protocol . $original_domain;
87
			$new_base = $current_protocol . $current_domain;
88
89
			// replace original domain with current domain
90
			$url = str_replace($original_base, $new_base, $url);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
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);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
107
		$original_home = home_url('/');
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
108
		add_filter('option_home', array($this, 'setup_domain'), 1);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space after opening parenthesis of array is bad style
Loading history...
No space before closing parenthesis of array is bad style
Loading history...
109
110
		// get current home URL
111
		$current_home = home_url('/');
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
112
113
		// replace occurences of original URL with current home URL
114
		$content = str_replace($original_home, $current_home, $content);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
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) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
136
			$upload_dir[$key] = apply_filters('wp_hydra_domain', $upload_dir[$key]);
0 ignored issues
show
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
137
		}
138
139
		return $upload_dir;
140
	}
141
142
}
143
144
// initialize WP Hydra - Polycephaly FTW!
145
global $wp_hydra;
146
$wp_hydra = new WP_Hydra();