domain-mapping.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Class Jetpack_3rd_Party_Domain_Mapping
  4. *
  5. * This class contains methods that are used to provide compatibility between Jetpack sync and domain mapping plugins.
  6. */
  7. class Jetpack_3rd_Party_Domain_Mapping {
  8. /**
  9. * @var Jetpack_3rd_Party_Domain_Mapping
  10. **/
  11. private static $instance = null;
  12. /**
  13. * An array of methods that are used to hook the Jetpack sync filters for home_url and site_url to a mapping plugin.
  14. *
  15. * @var array
  16. */
  17. static $test_methods = array(
  18. 'hook_wordpress_mu_domain_mapping',
  19. 'hook_wpmu_dev_domain_mapping'
  20. );
  21. static function init() {
  22. if ( is_null( self::$instance ) ) {
  23. self::$instance = new Jetpack_3rd_Party_Domain_Mapping;
  24. }
  25. return self::$instance;
  26. }
  27. private function __construct() {
  28. add_action( 'plugins_loaded', array( $this, 'attempt_to_hook_domain_mapping_plugins' ) );
  29. }
  30. /**
  31. * This function is called on the plugins_loaded action and will loop through the $test_methods
  32. * to try and hook a domain mapping plugin to the Jetpack sync filters for the home_url and site_url callables.
  33. */
  34. function attempt_to_hook_domain_mapping_plugins() {
  35. if ( ! Jetpack_Constants::is_defined( 'SUNRISE' ) ) {
  36. return;
  37. }
  38. $hooked = false;
  39. $count = count( self::$test_methods );
  40. for ( $i = 0; $i < $count && ! $hooked; $i++ ) {
  41. $hooked = call_user_func( array( $this, self::$test_methods[ $i ] ) );
  42. }
  43. }
  44. /**
  45. * This method will test for a constant and function that are known to be used with Donncha's WordPress MU
  46. * Domain Mapping plugin. If conditions are met, we hook the domain_mapping_siteurl() function to Jetpack sync
  47. * filters for home_url and site_url callables.
  48. *
  49. * @return bool
  50. */
  51. function hook_wordpress_mu_domain_mapping() {
  52. if ( ! Jetpack_Constants::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) {
  53. return false;
  54. }
  55. add_filter( 'jetpack_sync_home_url', 'domain_mapping_siteurl' );
  56. add_filter( 'jetpack_sync_site_url', 'domain_mapping_siteurl' );
  57. return true;
  58. }
  59. /**
  60. * This method will test for a class and method known to be used in WPMU Dev's domain mapping plugin. If the
  61. * method exists, then we'll hook the swap_to_mapped_url() to our Jetpack sync filters for home_url and site_url.
  62. *
  63. * @return bool
  64. */
  65. function hook_wpmu_dev_domain_mapping() {
  66. if ( ! $this->class_exists( 'domain_map' ) || ! $this->method_exists( 'domain_map', 'utils' ) ) {
  67. return false;
  68. }
  69. $utils = $this->get_domain_mapping_utils_instance();
  70. add_filter( 'jetpack_sync_home_url', array( $utils, 'swap_to_mapped_url' ) );
  71. add_filter( 'jetpack_sync_site_url', array( $utils, 'swap_to_mapped_url' ) );
  72. return true;
  73. }
  74. /*
  75. * Utility Methods
  76. *
  77. * These methods are very minimal, and in most cases, simply pass on arguments. Why create them you ask?
  78. * So that we can test.
  79. */
  80. public function method_exists( $class, $method ) {
  81. return method_exists( $class, $method );
  82. }
  83. public function class_exists( $class ) {
  84. return class_exists( $class );
  85. }
  86. public function function_exists( $function ) {
  87. return function_exists( $function );
  88. }
  89. public function get_domain_mapping_utils_instance() {
  90. return domain_map::utils();
  91. }
  92. }
  93. Jetpack_3rd_Party_Domain_Mapping::init();