class.jetpack-modules-overrides.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Provides methods for dealing with module overrides.
  4. *
  5. * @since 5.9.0
  6. */
  7. class Jetpack_Modules_Overrides {
  8. /**
  9. * Used to cache module overrides so that we minimize how many times we appy the
  10. * option_jetpack_active_modules filter.
  11. *
  12. * @var null|array
  13. */
  14. private $overrides = null;
  15. /**
  16. * Clears the $overrides member used for caching.
  17. *
  18. * Since get_overrides() can be passed a falsey value to skip caching, this is probably
  19. * most useful for clearing cache between tests.
  20. *
  21. * @return void
  22. */
  23. public function clear_cache() {
  24. $this->overrides = null;
  25. }
  26. /**
  27. * Returns true if there is a filter on the jetpack_active_modules option.
  28. *
  29. * @return bool Whether there is a filter on the jetpack_active_modules option.
  30. */
  31. public function do_overrides_exist() {
  32. return (bool) ( has_filter( 'option_jetpack_active_modules' ) || has_filter( 'jetpack_active_modules' ) );
  33. }
  34. /**
  35. * Gets the override for a given module.
  36. *
  37. * @param string $module_slug The module's slug.
  38. * @param boolean $use_cache Whether or not cached overrides should be used.
  39. *
  40. * @return bool|string False if no override for module. 'active' or 'inactive' if there is an override.
  41. */
  42. public function get_module_override( $module_slug, $use_cache = true ) {
  43. $overrides = $this->get_overrides( $use_cache );
  44. if ( ! isset( $overrides[ $module_slug ] ) ) {
  45. return false;
  46. }
  47. return $overrides[ $module_slug ];
  48. }
  49. /**
  50. * Returns an array of module overrides where the key is the module slug and the value
  51. * is true if the module is forced on and false if the module is forced off.
  52. *
  53. * @param bool $use_cache Whether or not cached overrides should be used.
  54. *
  55. * @return array The array of module overrides.
  56. */
  57. public function get_overrides( $use_cache = true ) {
  58. if ( $use_cache && ! is_null( $this->overrides ) ) {
  59. return $this->overrides;
  60. }
  61. if ( ! $this->do_overrides_exist() ) {
  62. return array();
  63. }
  64. $available_modules = Jetpack::get_available_modules();
  65. /**
  66. * First, let's get all modules that have been forced on.
  67. */
  68. /** This filter is documented in wp-includes/option.php */
  69. $filtered = apply_filters( 'option_jetpack_active_modules', array() );
  70. /** This filter is documented in class.jetpack.php */
  71. $filtered = apply_filters( 'jetpack_active_modules', $filtered );
  72. $forced_on = array_diff( $filtered, array() );
  73. /**
  74. * Second, let's get all modules forced off.
  75. */
  76. /** This filter is documented in wp-includes/option.php */
  77. $filtered = apply_filters( 'option_jetpack_active_modules', $available_modules );
  78. /** This filter is documented in class.jetpack.php */
  79. $filtered = apply_filters( 'jetpack_active_modules', $filtered );
  80. $forced_off = array_diff( $available_modules, $filtered );
  81. /**
  82. * Last, build the return value.
  83. */
  84. $return_value = array();
  85. foreach ( $forced_on as $on ) {
  86. $return_value[ $on ] = 'active';
  87. }
  88. foreach ( $forced_off as $off ) {
  89. $return_value[ $off ] = 'inactive';
  90. }
  91. $this->overrides = $return_value;
  92. return $return_value;
  93. }
  94. /**
  95. * A reference to an instance of this class.
  96. *
  97. * @var Jetpack_Modules_Overrides
  98. */
  99. private static $instance = null;
  100. /**
  101. * Returns the singleton instance of Jetpack_Modules_Overrides
  102. *
  103. * @return Jetpack_Modules_Overrides
  104. */
  105. public static function instance() {
  106. if ( is_null( self::$instance ) ) {
  107. self::$instance = new Jetpack_Modules_Overrides();
  108. }
  109. return self::$instance;
  110. }
  111. /**
  112. * Private construct to enforce singleton.
  113. */
  114. private function __construct() {
  115. }
  116. }
  117. Jetpack_Modules_Overrides::instance();