Completed
Push — sync/dbtlr/r159298-wpcom-15003... ( bc0eb2...c27aa0 )
by
unknown
12:20
created

Jetpack_3rd_Party_Domain_Mapping::class_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class Jetpack_3rd_Party_Domain_Mapping
5
 *
6
 * This class contains methods that are used to provide compatibility between Jetpack sync and domain mapping plugins.
7
 */
8
class Jetpack_3rd_Party_Domain_Mapping {
9
10
	/**
11
	 * @var Jetpack_3rd_Party_Domain_Mapping
12
	 **/
13
	private static $instance = null;
14
15
	/**
16
	 * An array of methods that are used to hook the Jetpack sync filters for home_url and site_url to a mapping plugin.
17
	 *
18
	 * @var array
19
	 */
20
	static $test_methods = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $test_methods.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
21
		'hook_wordpress_mu_domain_mapping',
22
		'hook_wpmu_dev_domain_mapping'
23
	);
24
25
	static function init() {
26
		if ( is_null( self::$instance ) ) {
27
			self::$instance = new Jetpack_3rd_Party_Domain_Mapping;
28
		}
29
30
		return self::$instance;
31
	}
32
33
	private function __construct() {
34
		add_action( 'plugins_loaded', array( $this, 'attempt_to_hook_domain_mapping_plugins' ) );
35
	}
36
37
	/**
38
	 * This function is called on the plugins_loaded action and will loop through the $test_methods
39
	 * to try and hook a domain mapping plugin to the Jetpack sync filters for the home_url and site_url callables.
40
	 */
41
	function attempt_to_hook_domain_mapping_plugins() {
42
		if ( ! Jetpack_Constants::is_defined( 'SUNRISE' ) ) {
43
			return;
44
		}
45
46
		$hooked = false;
47
		$count = count( self::$test_methods );
48
		for ( $i = 0; $i < $count && ! $hooked; $i++ ) {
49
			$hooked = call_user_func( array( $this, self::$test_methods[ $i ] ) );
50
		}
51
	}
52
53
	/**
54
	 * This method will test for a constant and function that are known to be used with Donncha's WordPress MU
55
	 * Domain Mapping plugin. If conditions are met, we hook the domain_mapping_siteurl() function to Jetpack sync
56
	 * filters for home_url and site_url callables.
57
	 *
58
	 * @return bool
59
	 */
60
	function hook_wordpress_mu_domain_mapping() {
61
		if ( ! Jetpack_Constants::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) {
62
			return false;
63
		}
64
65
		add_filter( 'jetpack_sync_home_url', 'domain_mapping_siteurl' );
66
		add_filter( 'jetpack_sync_site_url', 'domain_mapping_siteurl' );
67
68
		return true;
69
	}
70
71
	/**
72
	 * This method will test for a class and method known to be used in WPMU Dev's domain mapping plugin. If the
73
	 * method exists, then we'll hook the swap_to_mapped_url() to our Jetpack sync filters for home_url and site_url.
74
	 *
75
	 * @return bool
76
	 */
77
	function hook_wpmu_dev_domain_mapping() {
78
		if ( ! $this->class_exists( 'domain_map' ) || ! $this->method_exists( 'domain_map', 'utils' ) ) {
79
			return false;
80
		}
81
82
		$utils = $this->get_domain_mapping_utils_instance();
83
		add_filter( 'jetpack_sync_home_url', array( $utils, 'swap_to_mapped_url' ) );
84
		add_filter( 'jetpack_sync_site_url', array( $utils, 'swap_to_mapped_url' ) );
85
86
		return true;
87
	}
88
89
	/*
90
	 * Utility Methods
91
	 *
92
	 * These methods are very minimal, and in most cases, simply pass on arguments. Why create them you ask?
93
	 * So that we can test.
94
	 */
95
96
	public function method_exists( $class, $method ) {
97
		return method_exists( $class, $method );
98
	}
99
100
	public function class_exists( $class ) {
101
		return class_exists( $class );
102
	}
103
104
	public function function_exists( $function ) {
105
		return function_exists( $function );
106
	}
107
108
	public function get_domain_mapping_utils_instance() {
109
		return domain_map::utils();
110
	}
111
}
112
113
Jetpack_3rd_Party_Domain_Mapping::init();
114