|
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
|
|
|
* @param bool $include_deactivating When true, plugins deactivating this request will be considered active. Default true. |
|
42
|
|
|
* @return string[] |
|
43
|
|
|
*/ |
|
44
|
|
|
public function get_active_plugins( $include_deactivating = true ) { |
|
45
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
|
46
|
|
|
global $jetpack_autoloader_including_latest; |
|
47
|
|
|
|
|
48
|
|
|
// We're going to build a unique list of plugins from a few different sources |
|
49
|
|
|
// to find all of our "active" plugins. While we need to return an integer |
|
50
|
|
|
// array, we're going to use an associative array internally to reduce |
|
51
|
|
|
// the amount of time that we're going to spend checking uniqueness |
|
52
|
|
|
// and merging different arrays together to form the output. |
|
53
|
|
|
$active_plugins = array(); |
|
54
|
|
|
|
|
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
|
|
|
|
|
63
|
|
|
// This option contains all of the plugins that have been activated. |
|
64
|
|
|
$plugins = $this->plugin_locator->find_using_option( 'active_plugins' ); |
|
65
|
|
|
foreach ( $plugins as $path ) { |
|
66
|
|
|
$active_plugins[ $path ] = $path; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// This option contains all of the multisite plugins that have been activated. |
|
70
|
|
|
if ( is_multisite() ) { |
|
71
|
|
|
$plugins = $this->plugin_locator->find_using_option( 'active_sitewide_plugins', true ); |
|
72
|
|
|
foreach ( $plugins as $path ) { |
|
73
|
|
|
$active_plugins[ $path ] = $path; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// These actions contain plugins that are being activated during this request. |
|
78
|
|
|
$plugins = $this->plugin_locator->find_using_request_action( array( 'activate', 'activate-selected' ) ); |
|
79
|
|
|
foreach ( $plugins as $path ) { |
|
80
|
|
|
$active_plugins[ $path ] = $path; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// While it's true that the deactivating plugins are more than likely already in the active list |
|
84
|
|
|
// at this point, we should make sure in order to avoid any strange misbehavior. |
|
85
|
|
View Code Duplication |
if ( $include_deactivating ) { |
|
86
|
|
|
// These actions contain plugins that are being deactivated during this request. |
|
87
|
|
|
$plugins = $this->plugin_locator->find_using_request_action( array( 'deactivate', 'deactivate-selected' ) ); |
|
88
|
|
|
foreach ( $plugins as $path ) { |
|
89
|
|
|
$active_plugins[ $path ] = $path; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// When the current plugin isn't considered "active" there's a problem. |
|
94
|
|
|
// Since we're here, the plugin is active and currently being loaded. |
|
95
|
|
|
// We can support this case (mu-plugins and non-standard activation) |
|
96
|
|
|
// by adding the current plugin to the active list and marking it |
|
97
|
|
|
// as an unknown (activating) plugin. This also has the benefit |
|
98
|
|
|
// of causing a reset because the active plugins list has |
|
99
|
|
|
// been changed since it was saved in the global. |
|
100
|
|
|
$current_plugin = $this->plugin_locator->find_current_plugin(); |
|
101
|
|
|
if ( ! in_array( $current_plugin, $active_plugins, true ) && ! $jetpack_autoloader_including_latest ) { |
|
102
|
|
|
$active_plugins[ $current_plugin ] = $current_plugin; |
|
103
|
|
|
$jetpack_autoloader_activating_plugins_paths[] = $current_plugin; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// When deactivating plugins aren't desired we should entirely remove them from the active list. |
|
107
|
|
View Code Duplication |
if ( ! $include_deactivating ) { |
|
108
|
|
|
// These actions contain plugins that are being deactivated during this request. |
|
109
|
|
|
$plugins = $this->plugin_locator->find_using_request_action( array( 'deactivate', 'deactivate-selected' ) ); |
|
110
|
|
|
foreach ( $plugins as $path ) { |
|
111
|
|
|
unset( $active_plugins[ $path ] ); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Transform the array so that we don't have to worry about the keys interacting with other array types later. |
|
116
|
|
|
return array_values( $active_plugins ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Gets all of the cached plugins if there are any. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string[] |
|
123
|
|
|
*/ |
|
124
|
|
|
public function get_cached_plugins() { |
|
125
|
|
|
$cached = get_transient( self::TRANSIENT_KEY ); |
|
126
|
|
|
if ( false === $cached ) { |
|
127
|
|
|
return array(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// We need to expand the tokens to an absolute path for this webserver. |
|
131
|
|
|
return array_map( array( $this->path_processor, 'untokenize_path_constants' ), $cached ); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Saves the plugin list to the cache. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array $plugins The plugin list to save to the cache. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function cache_plugins( $plugins ) { |
|
140
|
|
|
// We store the paths in a tokenized form so that that webservers with different absolute paths don't break. |
|
141
|
|
|
$plugins = array_map( array( $this->path_processor, 'tokenize_path_constants' ), $plugins ); |
|
142
|
|
|
|
|
143
|
|
|
set_transient( self::TRANSIENT_KEY, $plugins ); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Checks to see whether or not the plugin list given has changed when compared to the |
|
148
|
|
|
* shared `$jetpack_autoloader_cached_plugin_paths` global. This allows us to deal |
|
149
|
|
|
* with cases where the active list may change due to filtering.. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string[] $plugins The plugins list to check against the global cache. |
|
152
|
|
|
* |
|
153
|
|
|
* @return bool True if the plugins have changed, otherwise false. |
|
154
|
|
|
*/ |
|
155
|
|
|
public function have_plugins_changed( $plugins ) { |
|
156
|
|
|
global $jetpack_autoloader_cached_plugin_paths; |
|
157
|
|
|
|
|
158
|
|
|
// When no autoloader has executed there is nothing to have changed. |
|
159
|
|
|
if ( ! isset( $jetpack_autoloader_cached_plugin_paths ) ) { |
|
160
|
|
|
$jetpack_autoloader_cached_plugin_paths = $plugins; |
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ( $jetpack_autoloader_cached_plugin_paths !== $plugins ) { |
|
165
|
|
|
$jetpack_autoloader_cached_plugin_paths = $plugins; |
|
166
|
|
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return false; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|