Repository navigation
Expand file tree
/
Copy pathAsset.php
More file actions
363 lines (322 loc) · 8.28 KB
/
Copy pathAsset.php
File metadata and controls
363 lines (322 loc) · 8.28 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Asset class file.
*
* @package Mantle
*/
namespace Mantle\Assets;
use Alley\WP\Asset_Manager\Scripts as Asset_Manager_Scripts;
use Alley\WP\Asset_Manager\Styles as Asset_Manager_Styles;
use Mantle\Contracts\Assets\Load_Hook;
use Mantle\Contracts\Assets\Load_Method;
use function Mantle\Support\Helpers\hook_callable;
/**
* Asset Manager
*/
class Asset {
/**
* Asset style media.
*
* @var string
*/
protected $media;
/**
* Enqueue on frontend.
*/
protected bool $frontend = true;
/**
* Enqueue in the admin area.
*/
protected bool $admin = true;
/**
* Enqueue in the block editor area.
*/
protected bool $block_editor = false;
/**
* Constructor.
*
* @param string $type Asset type (script/style).
* @param string $handle Asset handle.
* @param string $src Asset URL.
* @param array<string> $deps Asset dependencies.
* @param array<string>|string $condition Condition to load.
* @param string $load_method Load method.
* @param string $load_hook Load hook.
* @param string|null $version Asset version.
* @param bool $infer_from_loader Infer the asset from the loader if the source is not provided.
*/
public function __construct(
protected string $type,
protected string $handle,
protected ?string $src = null,
protected array $deps = [],
protected string|array $condition = 'global',
protected string $load_method = Load_Method::SYNC,
protected string $load_hook = Load_Hook::HEADER,
protected ?string $version = null,
bool $infer_from_loader = true,
) {
if ( ! $src && $infer_from_loader ) {
$this->infer_from_asset_loader();
}
}
/**
* Set an asset to load with async.
*/
public function async(): Asset {
if ( Load_Method::DEFER === $this->load_method ) {
$this->load_method = Load_Method::ASYNC_DEFER;
} else {
$this->load_method = Load_Method::ASYNC;
}
return $this;
}
/**
* Set an asset to load with async.
*/
public function sync(): Asset {
$this->load_method = Load_Method::SYNC;
return $this;
}
/**
* Defer a script.
*/
public function defer(): Asset {
if ( Load_Method::ASYNC === $this->load_method || Load_Method::ASYNC_DEFER === $this->load_method ) {
$this->load_method = Load_Method::ASYNC_DEFER;
} else {
$this->load_method = Load_Method::DEFER;
}
return $this;
}
/**
* Condition to load the asset.
*
* @param string|string[] $condition Condition to load.
*/
public function condition( string|array $condition ): Asset {
$this->condition = $condition;
return $this;
}
/**
* Load the asset in the header.
*/
public function header(): Asset {
$this->load_hook = Load_Hook::HEADER;
return $this;
}
/**
* Load the asset in the footer.
*/
public function footer(): Asset {
$this->load_hook = Load_Hook::FOOTER;
return $this;
}
/**
* Load the asset on a specific hook.
*
* @param string $hook Hook to load on.
*/
public function hook( string $hook ): Asset {
$this->load_hook = $hook;
return $this;
}
/**
* Set the version of the asset.
*
* @param string|null $version Version to set.
*/
public function version( ?string $version ): Asset {
$this->version = $version;
return $this;
}
/**
* Set the asset dependencies.
*
* @param string[] $dependencies Dependencies to set.
*/
public function dependencies( array $dependencies ): Asset {
$this->deps = $dependencies;
return $this;
}
/**
* Add a dependency to the asset.
*
* @param string $dependency Dependency to add.
*/
public function add_dependency( string $dependency ): Asset {
$this->deps[] = $dependency;
return $this;
}
/**
* Set the asset handle.
*
* @param string $handle Handle to set.
*/
public function handle( string $handle ): Asset {
$this->handle = $handle;
return $this;
}
/**
* Set the asset URL.
*
* @param string $src URL to set.
*/
public function src( string $src = '' ): Asset {
$this->src = $src;
return $this;
}
/**
* Set the media to use for style assets.
*
* @param string $media Media to set.
*/
public function media( string $media = '' ): Asset {
$this->media = $media;
return $this;
}
/**
* Tell the asset whether or not to load on the frontend.
*
* @param bool $load True if this should load on the frontend of the site.
*/
public function frontend( bool $load ): Asset {
$this->frontend = $load;
return $this;
}
/**
* Tell the asset to only load on the front-end
*/
public function only_frontend(): Asset {
return $this
->frontend( true )
->admin( false )
->block_editor( false );
}
/**
* Tell the asset whether or not to load in the admin area.
*
* @param bool $load True if this should load in the admin area of the site.
*/
public function admin( bool $load ): Asset {
$this->admin = $load;
return $this;
}
/**
* Tell the asset to only load in the admin area.
*/
public function only_admin(): Asset {
return $this
->frontend( false )
->admin( true )
->block_editor( false );
}
/**
* Tell the asset whether or not to load in the block editor
*
* @param bool $load True if this should load in the block editor.
*/
public function block_editor( bool $load ): Asset {
$this->block_editor = $load;
return $this;
}
/**
* Tell the asset to only load in the block editor.
*/
public function only_block_editor(): Asset {
return $this
->frontend( false )
->admin( false )
->block_editor( true );
}
/**
* Register the asset with asset manager. If called before
* 'wp_enqueue_scripts', it will defer registration until then.
*/
public function register(): void {
if ( $this->frontend ) {
hook_callable( 'wp_enqueue_scripts', $this->register_asset( ... ) );
}
if ( $this->admin ) {
hook_callable( 'admin_enqueue_scripts', $this->register_asset( ... ) );
}
if ( $this->block_editor ) {
hook_callable( 'enqueue_block_editor_assets', $this->register_asset( ... ) );
}
}
/**
* Register the asset with asset manager.
*
* @return void
*/
protected function register_asset() {
if ( 'script' === $this->type ) {
Asset_Manager_Scripts::instance()->add_asset(
[
'handle' => $this->handle,
'src' => $this->src,
'deps' => $this->deps,
'condition' => $this->condition,
'load_method' => Asset_Manager::resolve_load_method( $this->load_method ),
'version' => $this->version,
'load_hook' => $this->load_hook,
]
);
} elseif ( 'style' === $this->type ) {
Asset_Manager_Styles::instance()->add_asset(
[
'handle' => $this->handle,
'src' => $this->src,
'deps' => $this->deps,
'condition' => $this->condition,
'load_method' => Asset_Manager::resolve_load_method( $this->load_method ),
'version' => $this->version,
'load_hook' => $this->load_hook,
'media' => $this->media,
]
);
}
}
/**
* Register the asset on destruction.
*
* Allows for a fluent interface call without having to call register.
*/
public function __destruct() {
$this->register();
}
/**
* Attempt to automatically infer the asset src from the asset loader.
*
* For example, if /folder/app.js is passed as the handle with no src,
* this will attempt to load the asset from the asset loader. If it
* is found, it will set the src to the URL of the asset and rename the handle to
* a sanitized version (folder-app-js).
*/
protected function infer_from_asset_loader(): void {
// Bail if the src is set OR if the handle doesn't include an extension.
if ( $this->src || false === strpos( $this->handle, '.' ) ) {
return;
}
$asset_loader = asset_loader();
// Bail if the asset doesn't exist.
if ( ! $asset_loader->exists( $this->handle ) ) {
return;
}
$this
->src( $asset_loader->url( $this->handle ) ?: '' )
->dependencies(
array_merge(
$this->deps,
$asset_loader->dependencies( $this->handle ),
)
);
$handle = str_replace( [ '/', '.' ], '-', (string) esc_attr( $this->handle ) );
// Ensure the handle doesn't start with a dash.
if ( str_starts_with( $handle, '-' ) ) {
$handle = substr( $handle, 1 );
}
// Update the handle with a sanitized version.
$this->handle( $handle );
}
}