Brak opisu

class-plugins-handler.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * This file was automatically generated by automattic/jetpack-autoloader.
  4. *
  5. * @package automattic/jetpack-autoloader
  6. */
  7. namespace Automattic\Jetpack\Autoloader\jp7853afa8732e39f60cfddd564b056bf1;
  8. // phpcs:ignore
  9. /**
  10. * This class handles locating and caching all of the active plugins.
  11. */
  12. class Plugins_Handler {
  13. /**
  14. * The transient key for plugin paths.
  15. */
  16. const TRANSIENT_KEY = 'jetpack_autoloader_plugin_paths';
  17. /**
  18. * The locator for finding plugins in different locations.
  19. *
  20. * @var Plugin_Locator
  21. */
  22. private $plugin_locator;
  23. /**
  24. * The processor for transforming cached paths.
  25. *
  26. * @var Path_Processor
  27. */
  28. private $path_processor;
  29. /**
  30. * The constructor.
  31. *
  32. * @param Plugin_Locator $plugin_locator The locator for finding active plugins.
  33. * @param Path_Processor $path_processor The processor for transforming cached paths.
  34. */
  35. public function __construct( $plugin_locator, $path_processor ) {
  36. $this->plugin_locator = $plugin_locator;
  37. $this->path_processor = $path_processor;
  38. }
  39. /**
  40. * Gets all of the active plugins we can find.
  41. *
  42. * @param bool $include_deactivating When true, plugins deactivating this request will be considered active.
  43. * @param bool $record_unknown When true, the current plugin will be marked as active and recorded when unknown.
  44. *
  45. * @return string[]
  46. */
  47. public function get_active_plugins( $include_deactivating, $record_unknown ) {
  48. global $jetpack_autoloader_activating_plugins_paths;
  49. // We're going to build a unique list of plugins from a few different sources
  50. // to find all of our "active" plugins. While we need to return an integer
  51. // array, we're going to use an associative array internally to reduce
  52. // the amount of time that we're going to spend checking uniqueness
  53. // and merging different arrays together to form the output.
  54. $active_plugins = array();
  55. // Make sure that plugins which have activated this request are considered as "active" even though
  56. // they probably won't be present in any option.
  57. if ( is_array( $jetpack_autoloader_activating_plugins_paths ) ) {
  58. foreach ( $jetpack_autoloader_activating_plugins_paths as $path ) {
  59. $active_plugins[ $path ] = $path;
  60. }
  61. }
  62. // This option contains all of the plugins that have been activated.
  63. $plugins = $this->plugin_locator->find_using_option( 'active_plugins' );
  64. foreach ( $plugins as $path ) {
  65. $active_plugins[ $path ] = $path;
  66. }
  67. // This option contains all of the multisite plugins that have been activated.
  68. if ( is_multisite() ) {
  69. $plugins = $this->plugin_locator->find_using_option( 'active_sitewide_plugins', true );
  70. foreach ( $plugins as $path ) {
  71. $active_plugins[ $path ] = $path;
  72. }
  73. }
  74. // These actions contain plugins that are being activated/deactivated during this request.
  75. $plugins = $this->plugin_locator->find_using_request_action( array( 'activate', 'activate-selected', 'deactivate', 'deactivate-selected' ) );
  76. foreach ( $plugins as $path ) {
  77. $active_plugins[ $path ] = $path;
  78. }
  79. // When the current plugin isn't considered "active" there's a problem.
  80. // Since we're here, the plugin is active and currently being loaded.
  81. // We can support this case (mu-plugins and non-standard activation)
  82. // by adding the current plugin to the active list and marking it
  83. // as an unknown (activating) plugin. This also has the benefit
  84. // of causing a reset because the active plugins list has
  85. // been changed since it was saved in the global.
  86. $current_plugin = $this->plugin_locator->find_current_plugin();
  87. if ( $record_unknown && ! in_array( $current_plugin, $active_plugins, true ) ) {
  88. $active_plugins[ $current_plugin ] = $current_plugin;
  89. $jetpack_autoloader_activating_plugins_paths[] = $current_plugin;
  90. }
  91. // When deactivating plugins aren't desired we should entirely remove them from the active list.
  92. if ( ! $include_deactivating ) {
  93. // These actions contain plugins that are being deactivated during this request.
  94. $plugins = $this->plugin_locator->find_using_request_action( array( 'deactivate', 'deactivate-selected' ) );
  95. foreach ( $plugins as $path ) {
  96. unset( $active_plugins[ $path ] );
  97. }
  98. }
  99. // Transform the array so that we don't have to worry about the keys interacting with other array types later.
  100. return array_values( $active_plugins );
  101. }
  102. /**
  103. * Gets all of the cached plugins if there are any.
  104. *
  105. * @return string[]
  106. */
  107. public function get_cached_plugins() {
  108. $cached = get_transient( self::TRANSIENT_KEY );
  109. if ( ! is_array( $cached ) || empty( $cached ) ) {
  110. return array();
  111. }
  112. // We need to expand the tokens to an absolute path for this webserver.
  113. return array_map( array( $this->path_processor, 'untokenize_path_constants' ), $cached );
  114. }
  115. /**
  116. * Saves the plugin list to the cache.
  117. *
  118. * @param array $plugins The plugin list to save to the cache.
  119. */
  120. public function cache_plugins( $plugins ) {
  121. // We store the paths in a tokenized form so that that webservers with different absolute paths don't break.
  122. $plugins = array_map( array( $this->path_processor, 'tokenize_path_constants' ), $plugins );
  123. set_transient( self::TRANSIENT_KEY, $plugins );
  124. }
  125. /**
  126. * Checks to see whether or not the plugin list given has changed when compared to the
  127. * shared `$jetpack_autoloader_cached_plugin_paths` global. This allows us to deal
  128. * with cases where the active list may change due to filtering..
  129. *
  130. * @param string[] $plugins The plugins list to check against the global cache.
  131. *
  132. * @return bool True if the plugins have changed, otherwise false.
  133. */
  134. public function have_plugins_changed( $plugins ) {
  135. global $jetpack_autoloader_cached_plugin_paths;
  136. if ( $jetpack_autoloader_cached_plugin_paths !== $plugins ) {
  137. $jetpack_autoloader_cached_plugin_paths = $plugins;
  138. return true;
  139. }
  140. return false;
  141. }
  142. }