Nav apraksta

class-jetpack-modules-overrides.php 3.6KB

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