Completed
Push — update/exclude-list ( 104ee0 )
by
unknown
08:34
created

Domain_Mapping::method_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Domain Mapping 3rd Party
4
 *
5
 * @package Jetpack.
6
 */
7
8
namespace Automattic\Jetpack\Third_Party;
9
10
use Automattic\Jetpack\Constants;
11
12
/**
13
 * Class Jetpack_3rd_Party_Domain_Mapping
14
 *
15
 * This class contains methods that are used to provide compatibility between Jetpack sync and domain mapping plugins.
16
 */
17
class Domain_Mapping {
18
19
	/**
20
	 * Singleton holder.
21
	 *
22
	 * @var Domain_Mapping
23
	 **/
24
	private static $instance = null;
25
26
	/**
27
	 * An array of methods that are used to hook the Jetpack sync filters for home_url and site_url to a mapping plugin.
28
	 *
29
	 * @var array
30
	 */
31
	public static $test_methods = array(
32
		'hook_wordpress_mu_domain_mapping',
33
		'hook_wpmu_dev_domain_mapping',
34
	);
35
36
	/**
37
	 * Singleton constructor.
38
	 *
39
	 * @return Domain_Mapping|null
40
	 */
41
	public static function init() {
42
		if ( is_null( self::$instance ) ) {
43
			self::$instance = new Domain_Mapping();
44
		}
45
46
		return self::$instance;
47
	}
48
49
	/**
50
	 * Jetpack_3rd_Party_Domain_Mapping constructor.
51
	 */
52
	private function __construct() {
53
		add_action( 'plugins_loaded', array( $this, 'attempt_to_hook_domain_mapping_plugins' ) );
54
	}
55
56
	/**
57
	 * This function is called on the plugins_loaded action and will loop through the $test_methods
58
	 * to try and hook a domain mapping plugin to the Jetpack sync filters for the home_url and site_url callables.
59
	 */
60
	public function attempt_to_hook_domain_mapping_plugins() {
61
		if ( ! Constants::is_defined( 'SUNRISE' ) ) {
62
			return;
63
		}
64
65
		$hooked = false;
66
		$count  = count( self::$test_methods );
67
		for ( $i = 0; $i < $count && ! $hooked; $i++ ) {
68
			$hooked = call_user_func( array( $this, self::$test_methods[ $i ] ) );
69
		}
70
	}
71
72
	/**
73
	 * This method will test for a constant and function that are known to be used with Donncha's WordPress MU
74
	 * Domain Mapping plugin. If conditions are met, we hook the domain_mapping_siteurl() function to Jetpack sync
75
	 * filters for home_url and site_url callables.
76
	 *
77
	 * @return bool
78
	 */
79
	public function hook_wordpress_mu_domain_mapping() {
80
		if ( ! Constants::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) {
81
			return false;
82
		}
83
84
		add_filter( 'jetpack_sync_home_url', 'domain_mapping_siteurl' );
85
		add_filter( 'jetpack_sync_site_url', 'domain_mapping_siteurl' );
86
87
		return true;
88
	}
89
90
	/**
91
	 * This method will test for a class and method known to be used in WPMU Dev's domain mapping plugin. If the
92
	 * method exists, then we'll hook the swap_to_mapped_url() to our Jetpack sync filters for home_url and site_url.
93
	 *
94
	 * @return bool
95
	 */
96
	public function hook_wpmu_dev_domain_mapping() {
97
		if ( ! $this->class_exists( 'domain_map' ) || ! $this->method_exists( 'domain_map', 'utils' ) ) {
98
			return false;
99
		}
100
101
		$utils = $this->get_domain_mapping_utils_instance();
102
		add_filter( 'jetpack_sync_home_url', array( $utils, 'swap_to_mapped_url' ) );
103
		add_filter( 'jetpack_sync_site_url', array( $utils, 'swap_to_mapped_url' ) );
104
105
		return true;
106
	}
107
108
	/*
109
	 * Utility Methods
110
	 *
111
	 * These methods are very minimal, and in most cases, simply pass on arguments. Why create them you ask?
112
	 * So that we can test.
113
	 */
114
115
	/**
116
	 * Checks if a method exists.
117
	 *
118
	 * @param string $class Class name.
119
	 * @param string $method Method name.
120
	 *
121
	 * @return bool Returns function_exists() without modification.
122
	 */
123
	public function method_exists( $class, $method ) {
124
		return method_exists( $class, $method );
125
	}
126
127
	/**
128
	 * Checks if a class exists.
129
	 *
130
	 * @param string $class Class name.
131
	 *
132
	 * @return bool Returns class_exists() without modification.
133
	 */
134
	public function class_exists( $class ) {
135
		return class_exists( $class );
136
	}
137
138
	/**
139
	 * Checks if a function exists.
140
	 *
141
	 * @param string $function Function name.
142
	 *
143
	 * @return bool Returns function_exists() without modification.
144
	 */
145
	public function function_exists( $function ) {
146
		return function_exists( $function );
147
	}
148
149
	/**
150
	 * Returns the Domain_Map::utils() instance.
151
	 *
152
	 * @see https://github.com/wpmudev/domain-mapping/blob/master/classes/Domainmap/Utils.php
153
	 * @return Domainmap_Utils
154
	 */
155
	public function get_domain_mapping_utils_instance() {
156
		return domain_map::utils();
157
	}
158
}
159
160
Domain_Mapping::init();
161