1
|
|
|
<?php |
2
|
|
|
/* HEADER */ // phpcs:ignore |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This class handles dealing with paths for the autoloader. |
6
|
|
|
*/ |
7
|
|
|
class Path_Processor { |
8
|
|
|
/** |
9
|
|
|
* Given a path this will replace any of the path constants with a token to represent it. |
10
|
|
|
* |
11
|
|
|
* @param string $path The path we want to process. |
12
|
|
|
* @return string The tokenized path. |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
public function tokenize_path_constants( $path ) { |
15
|
|
|
$path = wp_normalize_path( $path ); |
16
|
|
|
|
17
|
|
|
$constants = $this->get_normalized_constants(); |
18
|
|
|
foreach ( $constants as $constant => $constant_path ) { |
19
|
|
|
$len = strlen( $constant_path ); |
20
|
|
|
if ( substr( $path, 0, $len ) !== $constant_path ) { |
21
|
|
|
continue; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return substr_replace( $path, '{{' . $constant . '}}', 0, $len ); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $path; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Given a path this will replace any of the path constant tokens with the expanded path. |
32
|
|
|
* |
33
|
|
|
* @param string $tokenized_path The path we want to process. |
34
|
|
|
* @return string The expanded path. |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function untokenize_path_constants( $tokenized_path ) { |
37
|
|
|
$tokenized_path = wp_normalize_path( $tokenized_path ); |
38
|
|
|
|
39
|
|
|
$constants = $this->get_normalized_constants(); |
40
|
|
|
foreach ( $constants as $constant => $constant_path ) { |
41
|
|
|
$constant = '{{' . $constant . '}}'; |
42
|
|
|
|
43
|
|
|
$len = strlen( $constant ); |
44
|
|
|
if ( substr( $tokenized_path, 0, $len ) !== $constant ) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return substr_replace( $tokenized_path, $constant_path, 0, $len ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $tokenized_path; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Given a file and an array of places it might be, this will find the absolute path and return it. |
56
|
|
|
* |
57
|
|
|
* @param string $file The plugin or theme file to resolve. |
58
|
|
|
* @param array $directories_to_check The directories we should check for the file if it isn't an absolute path. |
59
|
|
|
* @return string|false Returns the absolute path to the directory, otherwise false. |
60
|
|
|
*/ |
61
|
|
|
public function find_directory_with_autoloader( $file, $directories_to_check ) { |
62
|
|
|
$file = wp_normalize_path( $file ); |
63
|
|
|
|
64
|
|
|
// We need an absolute path to the plugin directory to check for an autoloader. |
65
|
|
|
if ( $this->is_absolute_path( $file ) ) { |
66
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
67
|
|
|
$directory = @is_file( $file ) ? dirname( $file ) : $file; |
68
|
|
|
} else { |
69
|
|
|
$directory = $this->find_plugin_directory( $file, $directories_to_check ); |
70
|
|
|
if ( ! isset( $directory ) ) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
76
|
|
|
if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $directory; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Fetches an array of normalized paths keyed by the constant they came from. |
85
|
|
|
* |
86
|
|
|
* @return string[] The normalized paths keyed by the constant. |
87
|
|
|
*/ |
88
|
|
|
private function get_normalized_constants() { |
89
|
|
|
$raw_constants = array( |
90
|
|
|
// Order the constants from most-specific to least-specific. |
91
|
|
|
'WP_PLUGIN_DIR', |
92
|
|
|
'WPMU_PLUGIN_DIR', |
93
|
|
|
'WP_CONTENT_DIR', |
94
|
|
|
'ABSPATH', |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$constants = array(); |
98
|
|
|
foreach ( $raw_constants as $raw ) { |
99
|
|
|
if ( ! defined( $raw ) ) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$path = wp_normalize_path( constant( $raw ) ); |
104
|
|
|
if ( isset( $path ) ) { |
105
|
|
|
$constants[ $raw ] = $path; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $constants; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Indicates whether or not a path is absolute. |
114
|
|
|
* |
115
|
|
|
* @param string $path The path to check. |
116
|
|
|
* @return bool True if the path is absolute, otherwise false. |
117
|
|
|
*/ |
118
|
|
|
private function is_absolute_path( $path ) { |
119
|
|
|
if ( 0 === strlen( $path ) || '.' === $path[0] ) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Absolute paths on Windows may begin with a drive letter. |
124
|
|
|
if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) { |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// A path starting with / or \ is absolute; anything else is relative. |
129
|
|
|
return ( '/' === $path[0] || '\\' === $path[0] ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Given a file and a list of directories to check, this method will try to figure out |
134
|
|
|
* the absolute path to the file in question. |
135
|
|
|
* |
136
|
|
|
* @param string $normalized_path The normalized path to the plugin or theme file to resolve. |
137
|
|
|
* @param array $directories_to_check The directories we should check for the file if it isn't an absolute path. |
138
|
|
|
* |
139
|
|
|
* @return string|null The absolute path to the plugin directory, otherwise null. |
140
|
|
|
*/ |
141
|
|
|
private function find_plugin_directory( $normalized_path, $directories_to_check ) { |
142
|
|
|
// We're only able to find the absolute path for plugin/theme PHP files. |
143
|
|
|
if ( ! is_string( $normalized_path ) || '.php' !== substr( $normalized_path, -4 ) ) { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
foreach ( $directories_to_check as $directory ) { |
148
|
|
|
$normalized_check = wp_normalize_path( trailingslashit( $directory ) ) . $normalized_path; |
149
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
150
|
|
|
if ( @is_file( $normalized_check ) ) { |
151
|
|
|
return dirname( $normalized_check ); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|