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