1
|
|
|
<?php |
2
|
|
|
/* HEADER */ // phpcs:ignore |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This class selects the package version for the autoloader. |
6
|
|
|
*/ |
7
|
|
|
class Autoloader_Handler { |
8
|
|
|
|
9
|
|
|
// The name of the autoloader function registered by v1.* autoloaders. |
10
|
|
|
const V1_AUTOLOADER_NAME = 'Automattic\Jetpack\Autoloader\autoloader'; |
11
|
|
|
|
12
|
|
|
/* |
13
|
|
|
* The autoloader function for v2.* autoloaders is named __NAMESPACE__ . \autoloader. |
14
|
|
|
* The namespace is defined in AutoloadGenerator as |
15
|
|
|
* 'Automattic\Jetpack\Autoloader\jp' plus a unique suffix. |
16
|
|
|
*/ |
17
|
|
|
const V2_AUTOLOADER_BASE = 'Automattic\Jetpack\Autoloader\jp'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The current plugin path. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $current_plugin_path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The paths for all of the active plugins. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $active_plugin_paths; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The Autoloader_Locator object. |
35
|
|
|
* |
36
|
|
|
* @var Autoloader_Locator |
37
|
|
|
*/ |
38
|
|
|
private $autoloader_locator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The Version_Selector object. |
42
|
|
|
* |
43
|
|
|
* @var Version_Selector |
44
|
|
|
*/ |
45
|
|
|
private $version_selector; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The constructor. |
49
|
|
|
* |
50
|
|
|
* @param string $current_plugin_path The current plugin path. |
51
|
|
|
* @param array $active_plugin_paths The active plugin paths. |
52
|
|
|
* @param Autoloader_Locator $autoloader_locator The Autoloader_Locator object. |
53
|
|
|
* @param Version_Selector $version_selector The Version_Selector object. |
54
|
|
|
*/ |
55
|
|
|
public function __construct( $current_plugin_path, $active_plugin_paths, $autoloader_locator, $version_selector ) { |
56
|
|
|
$this->current_plugin_path = $current_plugin_path; |
57
|
|
|
$this->active_plugin_paths = $active_plugin_paths; |
58
|
|
|
$this->autoloader_locator = $autoloader_locator; |
59
|
|
|
$this->version_selector = $version_selector; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Finds the latest installed autoloader. |
64
|
|
|
* |
65
|
|
|
* @return bool True if this autoloader is the latest, false otherwise. |
66
|
|
|
*/ |
67
|
|
|
public function is_latest_autoloader() { |
68
|
|
|
global $jetpack_autoloader_latest_version; |
69
|
|
|
|
70
|
|
|
if ( isset( $jetpack_autoloader_latest_version ) ) { |
71
|
|
|
return $jetpack_autoloader_latest_version === $this->autoloader_locator->get_autoloader_version( $this->current_plugin_path ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$latest_plugin = $this->autoloader_locator->find_latest_autoloader( $this->active_plugin_paths, $jetpack_autoloader_latest_version ); |
75
|
|
|
if ( ! isset( $latest_plugin ) ) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ( $latest_plugin !== $this->current_plugin_path ) { |
80
|
|
|
require $this->autoloader_locator->get_autoloader_path( $latest_plugin ); |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Checks whether the autoloader should be reset. The autoloader should be reset |
89
|
|
|
* when a plugin is activating via a method other than a request, for example |
90
|
|
|
* using WP-CLI. When this occurs, the activating plugin was not known when |
91
|
|
|
* the autoloader selected the package versions for the classmap and filemap |
92
|
|
|
* globals, so the autoloader must reselect the versions. |
93
|
|
|
* |
94
|
|
|
* If the current plugin is not already known, this method will add it to the |
95
|
|
|
* $jetpack_autoloader_activating_plugins_paths global. |
96
|
|
|
* |
97
|
|
|
* @return boolean True if the autoloder must be reset, else false. |
98
|
|
|
*/ |
99
|
|
|
public function should_autoloader_reset() { |
100
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
101
|
|
|
if ( ! isset( $jetpack_autoloader_activating_plugins_paths ) ) { |
102
|
|
|
$jetpack_autoloader_activating_plugins_paths = array(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$plugin_unknown = ! in_array( $this->current_plugin_path, $this->active_plugin_paths, true ); |
106
|
|
|
if ( $plugin_unknown ) { |
107
|
|
|
// If the current plugin isn't known, add it to the activating plugins list. |
108
|
|
|
$jetpack_autoloader_activating_plugins_paths[] = $this->current_plugin_path; |
109
|
|
|
$this->active_plugin_paths[] = $this->current_plugin_path; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $plugin_unknown; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Builds the Version_Autoloader class that is used for autoloading. |
117
|
|
|
* |
118
|
|
|
* @return Version_Loader |
119
|
|
|
*/ |
120
|
|
|
public function build_autoloader() { |
121
|
|
|
$manifest_handler = new Manifest_Handler( $this->active_plugin_paths, $this->version_selector ); |
122
|
|
|
|
123
|
|
|
global $jetpack_packages_psr4; |
124
|
|
|
$jetpack_packages_psr4 = array(); |
125
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_psr4.php', $jetpack_packages_psr4 ); |
126
|
|
|
|
127
|
|
|
global $jetpack_packages_classmap; |
128
|
|
|
$jetpack_packages_classmap = array(); |
129
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $jetpack_packages_classmap ); |
130
|
|
|
|
131
|
|
|
global $jetpack_packages_filemap; |
132
|
|
|
$jetpack_packages_filemap = array(); |
133
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_filemap.php', $jetpack_packages_filemap ); |
134
|
|
|
|
135
|
|
|
// Store the generated autoloader data in the loader so we can use it. |
136
|
|
|
return new Version_Loader( |
137
|
|
|
$this->version_selector, |
138
|
|
|
$jetpack_packages_classmap, |
139
|
|
|
$jetpack_packages_psr4, |
140
|
|
|
$jetpack_packages_filemap |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Updates the spl autoloader chain: |
146
|
|
|
* - Registers this namespace's autoloader function. |
147
|
|
|
* - If a v1 autoloader function is registered, moves it to the end of the chain. |
148
|
|
|
* - Removes any other v2 autoloader functions that have already been registered. This |
149
|
|
|
* can occur when the autoloader is being reset by an activating plugin. |
150
|
|
|
*/ |
151
|
|
|
public function update_autoloader_chain() { |
152
|
|
|
spl_autoload_register( __NAMESPACE__ . '\autoloader' ); |
153
|
|
|
|
154
|
|
|
$autoload_chain = spl_autoload_functions(); |
155
|
|
|
|
156
|
|
|
foreach ( $autoload_chain as $autoloader ) { |
157
|
|
|
if ( ! is_string( $autoloader ) ) { |
158
|
|
|
/* |
159
|
|
|
* The Jetpack Autoloader functions are registered as strings, so |
160
|
|
|
* just continue if $autoloader isn't a string. |
161
|
|
|
*/ |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ( self::V1_AUTOLOADER_NAME === $autoloader ) { |
166
|
|
|
// Move the v1.* autoloader function to the end of the spl autoloader chain. |
167
|
|
|
spl_autoload_unregister( $autoloader ); |
168
|
|
|
spl_autoload_register( $autoloader ); |
169
|
|
|
|
170
|
|
|
} elseif ( |
171
|
|
|
self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) ) |
172
|
|
|
&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) ) |
173
|
|
|
) { |
174
|
|
|
// Unregister any other v2.* autoloader functions if they're in the chain. |
175
|
|
|
spl_autoload_unregister( $autoloader ); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|