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
|
<?php namespace StellarWP\Assets;
use RuntimeException;
class Config { /** * @var string */ protected static string $hook_prefix = '';
/** * @var string */ protected static string $relative_asset_path = 'src/assets/';
/** * @var string */ protected static string $root_path = '';
/** * @var array<string, array<string, string>> */ protected static array $group_paths = [];
/** * @var string */ protected static string $version = '';
/** * @var array<string, string> */ protected static array $path_urls = [];
/** * Gets the hook prefix. * * @return string */ public static function get_hook_prefix(): string { if ( static::$hook_prefix === '' ) { $class = __CLASS__; throw new RuntimeException( "You must specify a hook prefix for your project with {$class}::set_hook_prefix()" ); } return static::$hook_prefix; }
/** * Gets the root path of a group. * * @since 1.4.0 * * @return string */ public static function get_path_of_group_path( string $group ): string { return ( static::$group_paths[ $group ] ?? [] )['root'] ?? ''; }
/** * Gets the relative path of a group. * * @since 1.4.0 * * @return string */ public static function get_relative_path_of_group_path( string $group ): string { return ( static::$group_paths[ $group ] ?? [] )['relative'] ?? ''; }
/** * Gets whether the group is using the asset directory prefix. * * @since 1.4.2 * * @return bool */ public static function is_group_path_using_asset_directory_prefix( string $group ): bool { return ( static::$group_paths[ $group ] ?? [] )['prefix'] ?? false; }
/** * Adds a group path. * * @since 1.4.0 * @since 1.4.2 Added the ability to specify whether the group path is using the asset directory prefix. * * @throws RuntimeException If the root or relative path is not specified. * * @param string $group_path_slug The slug of the group path. * @param string $root The root path of the group. * @param string $relative The relative path of the group. * @param bool $is_using_asset_directory_prefix Whether the group path is using the asset directory prefix. * * @return void */ public static function add_group_path( string $group_path_slug, string $root, string $relative, bool $is_using_asset_directory_prefix = false ): void { static::$group_paths[ $group_path_slug ] = [ 'root' => self::normalize_path( $root ), 'relative' => trailingslashit( $relative ), 'prefix' => $is_using_asset_directory_prefix, ]; }
/** * Gets the root path of the project. * * @return string */ public static function get_path(): string { if ( static::$root_path === '' ) { $class = __CLASS__; throw new RuntimeException( "You must specify a path to the root of you project with {$class}::set_path()" ); } return static::$root_path; }
/** * Gets the relative asset path of the project. * * @return string */ public static function get_relative_asset_path(): string { return static::$relative_asset_path; }
/** * Gets the root path of the project. * * @return string */ public static function get_url( $path ): string { $path = wp_normalize_path( $path ); $key = Utils::get_runtime_cache_key( [ $path ] );
if ( empty( static::$path_urls[ $key ] ) ) { $bases = Utils::get_bases(); static::$path_urls[ $key ] = trailingslashit( str_replace( wp_list_pluck( $bases, 'base_dir' ), wp_list_pluck( $bases, 'base_url' ), $path ) ); }
return static::$path_urls[ $key ]; }
/** * Gets the version of the project. * * @return string */ public static function get_version(): string { return static::$version; }
/** * Resets this class back to the defaults. */ public static function reset() { static::$hook_prefix = ''; static::$relative_asset_path = 'src/assets/'; static::$root_path = ''; static::$path_urls = []; static::$version = ''; Utils::clear_runtime_cache(); }
/** * Sets the hook prefix. * * @param string $prefix The prefix to add to hooks. * * @return void */ public static function set_hook_prefix( string $prefix ) { static::$hook_prefix = $prefix; }
/** * Sets the relative asset path of the project. * * @param string $path The root path of the project. * * @return void */ public static function set_relative_asset_path( string $path ) { static::$relative_asset_path = trailingslashit( $path ); }
/** * Sets the root path of the project. * * @param string $path The root path of the project. * * @return void */ public static function set_path( string $path ) { static::$root_path = self::normalize_path( $path ); }
/** * Sets the version of the project. * * @param string $version The version of the project. * * @return void */ public static function set_version( string $version ) { static::$version = $version; }
/** * Normalizes a path. * * @since 1.4.0 * @since 1.4.1 Allow for paths that are not in the plugin or theme directory. * * @param string $path The path to normalize. * * @return string */ protected static function normalize_path( string $path ): string { $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); $path = wp_normalize_path( $path );
$plugins_content_dir_position = $plugin_dir ? strpos( $path, $plugin_dir ) : false; $themes_content_dir_position = strpos( $path, wp_normalize_path( get_theme_root() ) );
if ( $plugins_content_dir_position === false && $themes_content_dir_position === false && strpos( $path, '/' ) !== 0 ) { // Default to plugins if a relative path is provided. $path = trailingslashit( $plugin_dir ) . $path; } elseif ( $plugins_content_dir_position !== false ) { $path = substr( $path, $plugins_content_dir_position ); } elseif ( $themes_content_dir_position !== false ) { $path = substr( $path, $themes_content_dir_position ); }
return trailingslashit( $path ); } }
|