|
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 Plugins_Handler object. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Plugins_Handler |
|
23
|
|
|
*/ |
|
24
|
|
|
private $plugins_handler = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Plugins_Handler $plugins_handler The plugins_handler object. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( $plugins_handler ) { |
|
32
|
|
|
$this->plugins_handler = $plugins_handler; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Finds the latest installed autoloader. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function find_latest_autoloader() { |
|
39
|
|
|
global $jetpack_autoloader_latest_version; |
|
40
|
|
|
|
|
41
|
|
|
$current_autoloader_path = trailingslashit( dirname( __FILE__ ) ) . 'autoload_packages.php'; |
|
42
|
|
|
|
|
43
|
|
|
$selected_autoloader_version = null; |
|
44
|
|
|
$selected_autoloader_path = null; |
|
45
|
|
|
|
|
46
|
|
|
$active_plugins = $this->plugins_handler->get_all_active_plugins(); |
|
47
|
|
|
|
|
48
|
|
|
foreach ( $active_plugins as $plugin ) { |
|
49
|
|
|
$plugin_path = plugin_dir_path( trailingslashit( WP_PLUGIN_DIR ) . $plugin ); |
|
50
|
|
|
$classmap_path = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php'; |
|
51
|
|
|
|
|
52
|
|
|
if ( file_exists( $classmap_path ) ) { |
|
53
|
|
|
$packages = require $classmap_path; |
|
54
|
|
|
|
|
55
|
|
|
$compare_version = $packages['Automattic\\Jetpack\\Autoloader\\AutoloadGenerator']['version']; |
|
56
|
|
|
$compare_path = trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php'; |
|
57
|
|
|
|
|
58
|
|
|
// TODO: This comparison needs to properly handle dev versions. |
|
59
|
|
|
if ( version_compare( $selected_autoloader_version, $compare_version, '<' ) ) { |
|
60
|
|
|
$selected_autoloader_version = $compare_version; |
|
61
|
|
|
$selected_autoloader_path = $compare_path; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$jetpack_autoloader_latest_version = $selected_autoloader_version; |
|
67
|
|
|
if ( $current_autoloader_path !== $selected_autoloader_path ) { |
|
68
|
|
|
require $selected_autoloader_path; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get this autoloader's package version. |
|
74
|
|
|
* |
|
75
|
|
|
* @return String The autoloader's package version. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function get_current_autoloader_version() { |
|
78
|
|
|
$classmap_file = trailingslashit( dirname( __FILE__ ) ) . 'composer/jetpack_autoload_classmap.php'; |
|
79
|
|
|
$autoloader_packages = require $classmap_file; |
|
80
|
|
|
|
|
81
|
|
|
return $autoloader_packages['Automattic\\Jetpack\\Autoloader\\AutoloadGenerator']['version']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Updates the spl autoloader chain: |
|
87
|
|
|
* - Registers this namespace's autoloader function. |
|
88
|
|
|
* - If a v1 autoloader function is registered, moves it to the end of the chain. |
|
89
|
|
|
* - Removes any other v2 autoloader functions that have already been registered. This |
|
90
|
|
|
* can occur when the autoloader is being reset by an activating plugin. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function update_autoloader_chain() { |
|
93
|
|
|
spl_autoload_register( __NAMESPACE__ . '\autoloader' ); |
|
94
|
|
|
|
|
95
|
|
|
$autoload_chain = spl_autoload_functions(); |
|
96
|
|
|
|
|
97
|
|
|
foreach ( $autoload_chain as $autoloader ) { |
|
98
|
|
|
if ( self::V1_AUTOLOADER_NAME === $autoloader ) { |
|
99
|
|
|
// Move the v1.* autoloader function to the end of the spl autoloader chain. |
|
100
|
|
|
spl_autoload_unregister( $autoloader ); |
|
101
|
|
|
spl_autoload_register( $autoloader ); |
|
102
|
|
|
|
|
103
|
|
|
} elseif ( |
|
104
|
|
|
is_string( $autoloader ) |
|
105
|
|
|
&& self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) ) |
|
106
|
|
|
&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) ) |
|
107
|
|
|
) { |
|
108
|
|
|
// Unregister any other v2.* autoloader functions if they're in the chain. |
|
109
|
|
|
spl_autoload_unregister( $autoloader ); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|