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
|
|
|
const AUTOLOAD_GENERATOR_CLASS_NAME = 'Automattic\Jetpack\Autoloader\AutoloadGenerator'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The Plugins_Handler object. |
23
|
|
|
* |
24
|
|
|
* @var Plugins_Handler |
25
|
|
|
*/ |
26
|
|
|
private $plugins_handler = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Version_Selector object. |
30
|
|
|
* |
31
|
|
|
* @var Version_Selector |
32
|
|
|
*/ |
33
|
|
|
private $version_selector = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The constructor. |
37
|
|
|
* |
38
|
|
|
* @param Plugins_Handler $plugins_handler The Plugins_Handler object. |
39
|
|
|
* @param Version_Selector $version_selector The Version_Selector object. |
40
|
|
|
*/ |
41
|
|
|
public function __construct( $plugins_handler, $version_selector ) { |
42
|
|
|
$this->plugins_handler = $plugins_handler; |
43
|
|
|
$this->version_selector = $version_selector; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Finds the latest installed autoloader. |
48
|
|
|
*/ |
49
|
|
|
public function find_latest_autoloader() { |
50
|
|
|
global $jetpack_autoloader_latest_version; |
51
|
|
|
|
52
|
|
|
$current_autoloader_path = trailingslashit( dirname( __FILE__ ) ) . 'autoload_packages.php'; |
53
|
|
|
$current_autoloader_path = str_replace( '\\', '/', $current_autoloader_path ); |
54
|
|
|
|
55
|
|
|
$selected_autoloader_version = null; |
56
|
|
|
$selected_autoloader_path = null; |
57
|
|
|
|
58
|
|
|
$active_plugins_paths = $this->plugins_handler->get_all_active_plugins_paths(); |
59
|
|
|
|
60
|
|
|
foreach ( $active_plugins_paths as $plugin_path ) { |
61
|
|
|
$classmap_path = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php'; |
62
|
|
|
|
63
|
|
|
if ( file_exists( $classmap_path ) ) { |
64
|
|
|
$packages = require $classmap_path; |
65
|
|
|
|
66
|
|
|
$compare_version = $packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version']; |
67
|
|
|
$compare_path = trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php'; |
68
|
|
|
|
69
|
|
|
if ( $this->version_selector->is_version_update_required( $selected_autoloader_version, $compare_version ) ) { |
70
|
|
|
if ( ! $this->check_for_v1_files( $compare_path ) ) { |
71
|
|
|
$selected_autoloader_version = $compare_version; |
72
|
|
|
$selected_autoloader_path = $compare_path; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$jetpack_autoloader_latest_version = $selected_autoloader_version; |
79
|
|
|
|
80
|
|
|
// $current_autoloader_path is already loaded |
81
|
|
|
if ( $current_autoloader_path !== $selected_autoloader_path ) { |
82
|
|
|
require $selected_autoloader_path; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check the plugins directory for autoloader v1.x files. If the plugin's directory contains |
88
|
|
|
* v1.x autoloader files, we will ignore this plugins autoloader. |
89
|
|
|
* |
90
|
|
|
* This situation can occur if a plugin has a v2.x autoloader and a backup containing a version |
91
|
|
|
* of the plugin with a v1.x autoloader is restored. The v2.x autolaoder files remain in the |
92
|
|
|
* plugin directory. |
93
|
|
|
* |
94
|
|
|
* @param string $autoload_packages_path The path to the plugin's vendor/autoload_packages.php file. |
95
|
|
|
* |
96
|
|
|
* @return boolean True if the plugin directory contains a v1.x autoloader, false otherwise. |
97
|
|
|
*/ |
98
|
|
|
private function check_for_v1_files( $autoload_packages_path ) { |
99
|
|
|
$v1_classmap_path = trailingslashit( dirname( $autoload_packages_path ) ) . 'composer/autoload_classmap_package.php'; |
100
|
|
|
|
101
|
|
|
clearstatcache( true, $v1_classmap_path ); |
102
|
|
|
|
103
|
|
|
if ( ! file_exists( $v1_classmap_path ) ) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
clearstatcache( true, $autoload_packages_path ); |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* Use the filesize of autoload_packages.php to determine if it's a v1 or v2 file. |
111
|
|
|
* The v2.x file is small, less than 1 KB. The v1.x file is much larger, > 6 KB. |
112
|
|
|
*/ |
113
|
|
|
if ( 2000 < filesize( $autoload_packages_path ) ) { |
114
|
|
|
// The autoload_package.php file is a v1.x, so try to delete v2.x files. |
115
|
|
|
$vendor_dir = dirname( $autoload_packages_path ); |
116
|
|
View Code Duplication |
if ( file_exists( trailingslashit( $vendor_dir ) . 'autoload_functions.php' ) |
117
|
|
|
&& is_writable( $vendor_dir ) |
118
|
|
|
&& is_executable( $vendor_dir ) ) { |
119
|
|
|
unlink( trailingslashit( $vendor_dir ) . 'autoload_functions.php' ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$jetpack_autoloader_dir = trailingslashit( $vendor_dir ) . 'jetpack-autoloader'; |
123
|
|
View Code Duplication |
if ( file_exists( trailingslashit( $jetpack_autoloader_dir ) . 'autoload_functions.php' ) |
124
|
|
|
&& is_writable( $jetpack_autoloader_dir ) |
125
|
|
|
&& is_executable( $jetpack_autoloader_dir ) ) { |
126
|
|
|
unlink( trailingslashit( $jetpack_autoloader_dir ) . 'autoload_functions.php' ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$composer_dir = trailingslashit( $vendor_dir ) . 'composer'; |
130
|
|
View Code Duplication |
if ( file_exists( trailingslashit( $composer_dir ) . 'jetpack_autoload_classmap.php' ) |
131
|
|
|
&& is_writable( $composer_dir ) |
132
|
|
|
&& is_executable( $composer_dir ) ) { |
133
|
|
|
unlink( trailingslashit( $composer_dir ) . 'jetpack_autoload_classmap.php' ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Try to delete v1.x classmap file. |
140
|
|
|
if ( is_writable( dirname( $v1_classmap_path ) ) && is_executable( dirname( $v1_classmap_path ) ) ) { |
141
|
|
|
unlink( $v1_classmap_path ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get this autoloader's package version. |
149
|
|
|
* |
150
|
|
|
* @return String The autoloader's package version. |
151
|
|
|
*/ |
152
|
|
|
public function get_current_autoloader_version() { |
153
|
|
|
$classmap_file = trailingslashit( dirname( __FILE__ ) ) . 'composer/jetpack_autoload_classmap.php'; |
154
|
|
|
$autoloader_packages = require $classmap_file; |
155
|
|
|
|
156
|
|
|
return $autoloader_packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Updates the spl autoloader chain: |
162
|
|
|
* - Registers this namespace's autoloader function. |
163
|
|
|
* - If a v1 autoloader function is registered, moves it to the end of the chain. |
164
|
|
|
* - Removes any other v2 autoloader functions that have already been registered. This |
165
|
|
|
* can occur when the autoloader is being reset by an activating plugin. |
166
|
|
|
*/ |
167
|
|
|
public function update_autoloader_chain() { |
168
|
|
|
spl_autoload_register( __NAMESPACE__ . '\autoloader' ); |
169
|
|
|
|
170
|
|
|
$autoload_chain = spl_autoload_functions(); |
171
|
|
|
|
172
|
|
|
foreach ( $autoload_chain as $autoloader ) { |
173
|
|
|
if ( ! is_string( $autoloader ) ) { |
174
|
|
|
/* |
175
|
|
|
* The Jetpack Autoloader functions are registered as strings, so |
176
|
|
|
* just continue if $autoloader isn't a string. |
177
|
|
|
*/ |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ( self::V1_AUTOLOADER_NAME === $autoloader ) { |
182
|
|
|
// Move the v1.* autoloader function to the end of the spl autoloader chain. |
183
|
|
|
spl_autoload_unregister( $autoloader ); |
184
|
|
|
spl_autoload_register( $autoloader ); |
185
|
|
|
|
186
|
|
|
} elseif ( |
187
|
|
|
self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) ) |
188
|
|
|
&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) ) |
189
|
|
|
) { |
190
|
|
|
// Unregister any other v2.* autoloader functions if they're in the chain. |
191
|
|
|
spl_autoload_unregister( $autoloader ); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|