Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

Plugins_Handler::have_plugins_changed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class handles locating and caching all of the active plugins.
6
 */
7
class Plugins_Handler {
8
	/**
9
	 * The transient key for plugin paths.
10
	 */
11
	const TRANSIENT_KEY = 'jetpack_autoloader_plugin_paths';
12
13
	/**
14
	 * The locator for finding plugins in different locations.
15
	 *
16
	 * @var Plugin_Locator
17
	 */
18
	private $plugin_locator;
19
20
	/**
21
	 * The processor for transforming cached paths.
22
	 *
23
	 * @var Path_Processor
24
	 */
25
	private $path_processor;
26
27
	/**
28
	 * The constructor.
29
	 *
30
	 * @param Plugin_Locator $plugin_locator The locator for finding active plugins.
31
	 * @param Path_Processor $path_processor The processor for transforming cached paths.
32
	 */
33
	public function __construct( $plugin_locator, $path_processor ) {
34
		$this->plugin_locator = $plugin_locator;
35
		$this->path_processor = $path_processor;
36
	}
37
38
	/**
39
	 * Gets all of the active plugins we can find.
40
	 *
41
	 * @return string[]
42
	 */
43
	public function get_active_plugins() {
44
		global $jetpack_autoloader_activating_plugins_paths;
45
		global $jetpack_autoloader_including_latest;
46
47
		// We're going to build a unique list of plugins from a few different sources
48
		// to find all of our "active" plugins. While we need to return an integer
49
		// array, we're going to use an associative array internally to reduce
50
		// the amount of time that we're going to spend checking uniqueness
51
		// and merging different arrays together to form the output.
52
		$active_plugins = array();
53
54
		// Make sure that plugins which have activated this request are considered as "active" even though
55
		// they probably won't be present in any option.
56
		if ( is_array( $jetpack_autoloader_activating_plugins_paths ) ) {
57
			foreach ( $jetpack_autoloader_activating_plugins_paths as $path ) {
58
				$active_plugins[ $path ] = $path;
59
			}
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
68
		// This option contains all of the multisite plugins that have been activated.
69
		if ( is_multisite() ) {
70
			$plugins = $this->plugin_locator->find_using_option( 'active_sitewide_plugins', true );
71
			foreach ( $plugins as $path ) {
72
				$active_plugins[ $path ] = $path;
73
			}
74
		}
75
76
		$plugins = $this->plugin_locator->find_activating_this_request();
77
		foreach ( $plugins as $path ) {
78
			$active_plugins[ $path ] = $path;
79
		}
80
81
		// When the current plugin isn't considered "active" there's a problem.
82
		// Since we're here, the plugin is active and currently being loaded.
83
		// We can support this case (mu-plugins and non-standard activation)
84
		// by adding the current plugin to the active list and marking it
85
		// as an unknown (activating) plugin. This also has the benefit
86
		// of causing a reset because the active plugins list has
87
		// been changed since it was saved in the global.
88
		$current_plugin = $this->plugin_locator->find_current_plugin();
89
		if ( ! in_array( $current_plugin, $active_plugins, true ) && ! $jetpack_autoloader_including_latest ) {
90
			$active_plugins[ $current_plugin ]             = $current_plugin;
91
			$jetpack_autoloader_activating_plugins_paths[] = $current_plugin;
92
		}
93
94
		// Transform the array so that we don't have to worry about the keys interacting with other array types later.
95
		return array_values( $active_plugins );
96
	}
97
98
	/**
99
	 * Gets all of the cached plugins if there are any.
100
	 *
101
	 * @return string[]
102
	 */
103
	public function get_cached_plugins() {
104
		$cached = get_transient( self::TRANSIENT_KEY );
105
		if ( false === $cached ) {
106
			return array();
107
		}
108
109
		// We need to expand the tokens to an absolute path for this webserver.
110
		return array_map( array( $this->path_processor, 'untokenize_path_constants' ), $cached );
111
	}
112
113
	/**
114
	 * Saves the plugin list to the cache.
115
	 *
116
	 * @param array $plugins The plugin list to save to the cache.
117
	 */
118
	public function cache_plugins( $plugins ) {
119
		// We store the paths in a tokenized form so that that webservers with different absolute paths don't break.
120
		$plugins = array_map( array( $this->path_processor, 'tokenize_path_constants' ), $plugins );
121
122
		set_transient( self::TRANSIENT_KEY, $plugins );
123
	}
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
137
		// When no autoloader has executed there is nothing to have changed.
138
		if ( ! isset( $jetpack_autoloader_cached_plugin_paths ) ) {
139
			$jetpack_autoloader_cached_plugin_paths = $plugins;
140
			return false;
141
		}
142
143
		if ( $jetpack_autoloader_cached_plugin_paths !== $plugins ) {
144
			$jetpack_autoloader_cached_plugin_paths = $plugins;
145
			return true;
146
		}
147
148
		return false;
149
	}
150
}
151