1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
<?php /** * Handles various file locations */ class Loco_fs_Locations extends ArrayObject {
/** * Singleton of WordPress root directory * @var Loco_fs_Locations */ private static $roots;
/** * Singleton of wp-content directory * @var Loco_fs_Locations */ private static $conts;
/** * Singleton of global languages directories * @var Loco_fs_Locations */ private static $langs;
/** * Singleton of registered theme paths * @var Loco_fs_Locations */ private static $theme;
/** * Singleton of registered plugin locations * @var Loco_fs_Locations */ private static $plugin;
/** * Clear static caches */ public static function clear(){ self::$roots = null; self::$conts = null; self::$langs = null; self::$theme = null; self::$plugin = null; }
/** * @return Loco_fs_Locations */ public static function getRoot(){ if( ! self::$roots ){ self::$roots = new Loco_fs_Locations( [ loco_constant('ABSPATH'), ] ); } return self::$roots; }
/** * @return Loco_fs_Locations */ public static function getContent(){ if( ! self::$conts ){ self::$conts = new Loco_fs_Locations( [ loco_constant('WP_CONTENT_DIR'), // <- defined WP_CONTENT_DIR trailingslashit(ABSPATH).'wp-content', // <- default /wp-content ] ); } return self::$conts; }
/** * @deprecated Use getLang * @return Loco_fs_Locations */ public static function getGlobal(){ return self::getLangs(); }
/** * @return Loco_fs_Locations */ public static function getLangs(){ if( ! self::$langs ){ self::$langs = new Loco_fs_Locations( [ loco_constant('WP_LANG_DIR'), ] ); } return self::$langs; }
/** * @return Loco_fs_Locations */ public static function getThemes(){ if( ! self::$theme ){ $roots = isset($GLOBALS['wp_theme_directories']) ? $GLOBALS['wp_theme_directories'] : []; if( ! $roots ){ $roots[] = trailingslashit( loco_constant('WP_CONTENT_DIR') ).'themes'; } self::$theme = new Loco_fs_Locations( $roots ); } return self::$theme; }
/** * @return Loco_fs_Locations */ public static function getPlugins(){ if( ! self::$plugin ){ self::$plugin = new Loco_fs_Locations( [ loco_constant('WPMU_PLUGIN_DIR'), loco_constant('WP_PLUGIN_DIR'), ] ); } return self::$plugin; }
/** * Create instance from list of locations */ public function __construct( array $paths ){ parent::__construct( [] ); foreach( $paths as $path ){ $this->add( $path ); } }
/** * @param string $path normalized absolute path * @return Loco_fs_Locations */ public function add( $path ){ foreach( $this->expand($path) as $path ){ // path must have trailing slash, otherwise "/plugins/foobar" would match "/plugins/foo/" $this->offsetSet( $path, strlen($path) ); } return $this; }
/** * Check if a given path begins with any of the registered ones * @param string $path absolute path * @return bool whether path matched */ public function check( $path ){ foreach( $this->expand($path) as $path ){ foreach( $this as $prefix => $length ){ if( $prefix === $path || substr($path,0,$length) === $prefix ){ return true; } } } return false; } /** * Match location and return the relative subpath. * Note that exact match is returned as "." indicating self * @param string $path * @return string | null */ public function rel( $path ){ foreach( $this->expand($path) as $path ){ foreach( $this as $prefix => $length ){ if( $prefix === $path ){ return '.'; } if( substr($path,0,$length) === $prefix ){ return untrailingslashit( substr($path,$length) ); } } } return null; }
/** * Like rel() but returns base directory also * @return string[] */ public function split( $path ){ foreach( $this->expand($path) as $path ){ foreach( $this as $prefix => $length ){ if( $prefix === $path ){ return [$prefix,'.']; } if( substr($path,0,$length) === $prefix ){ return [ $prefix, untrailingslashit( substr($path,$length) ) ]; } } } return null; }
/* * Opposite of rel, takes a relative path and constructs the first full path that exists * public function abs( $rel ){ foreach( $this as $prefix => $length ){ $path = realpath( $prefix.$rel ); if( $path ){ return $path; } } return null; }*/
/** * @param string $rel * @return string[] */ public function expand( $rel ){ if( '' === $rel ){ //Loco_error_AdminNotices::debug('Expanding empty path to empty array'); return []; } $path = Loco_fs_File::abs($rel); if( '' === $path ){ //throw new InvalidArgumentException('Failed on abs('.var_export($rel,true).')'); return []; } $paths = [ trailingslashit($path) ]; // add real path if differs $real = realpath($path); if( $real && $real !== $path ){ $paths[] = trailingslashit($real); } return $paths; }
/** * @return string[] */ public function apply( $suffix = '' ){ $paths = []; foreach( $this->getArrayCopy() as $prefix => $length ){ $paths[] = $prefix.$suffix; } return $paths; }
}
|