Nav apraksta

class-domain-mapping.php 4.1KB

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