1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\assets; |
4
|
|
|
|
5
|
|
|
use DomainException; |
6
|
|
|
use EE_Error; |
7
|
|
|
use EventEspresso\core\domain\services\assets\CoreAssetManager; |
8
|
|
|
use EventEspresso\core\domain\values\assets\Asset; |
9
|
|
|
use EventEspresso\core\domain\values\assets\JavascriptAsset; |
10
|
|
|
use EventEspresso\core\domain\values\assets\StylesheetAsset; |
11
|
|
|
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay; |
12
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
13
|
|
|
use EventEspresso\core\exceptions\InvalidFilePathException; |
14
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
15
|
|
|
use Exception; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Used for registering assets used in EE. |
20
|
|
|
* |
21
|
|
|
* @package EventEspresso |
22
|
|
|
* @subpackage services\assets |
23
|
|
|
* @author Darren Ethier |
24
|
|
|
* @since 4.9.24.rc.004 |
25
|
|
|
*/ |
26
|
|
|
class Registry |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AssetCollection $assets |
33
|
|
|
*/ |
34
|
|
|
protected $assets; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var I18nRegistry |
38
|
|
|
*/ |
39
|
|
|
private $i18n_registry; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $jsdata = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
50
|
|
|
* page source. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $script_handles_with_data = array(); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Holds the manifest data obtained from registered manifest files. |
59
|
|
|
* Manifests are maps of asset chunk name to actual built asset file names. |
60
|
|
|
* Shape of this array is: |
61
|
|
|
* array( |
62
|
|
|
* 'some_namespace_slug' => array( |
63
|
|
|
* 'some_chunk_name' => array( |
64
|
|
|
* 'js' => 'filename.js' |
65
|
|
|
* 'css' => 'filename.js' |
66
|
|
|
* ), |
67
|
|
|
* 'url_base' => 'https://baseurl.com/to/assets |
68
|
|
|
* ) |
69
|
|
|
* ) |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
private $manifest_data = array(); |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Holds any dependency data obtained from registered dependency map json. |
78
|
|
|
* Dependency map json is generated via the @wordpress/dependency-extraction-webpack-plugin via the webpack config. |
79
|
|
|
* @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
private $dependencies_data = []; |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our |
88
|
|
|
* build process. Currently the dependency export process in webpack does not consider css imports, so we derive |
89
|
|
|
* them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that |
90
|
|
|
* are used for both js and css. |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
private $wp_css_handle_dependencies = [ |
94
|
|
|
'wp-components', |
95
|
|
|
'wp-block-editor', |
96
|
|
|
'wp-block-library', |
97
|
|
|
'wp-edit-post', |
98
|
|
|
'wp-edit-widgets', |
99
|
|
|
'wp-editor', |
100
|
|
|
'wp-format-library', |
101
|
|
|
'wp-list-reusable-blocks', |
102
|
|
|
'wp-nux', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Registry constructor. |
108
|
|
|
* Hooking into WP actions for script registry. |
109
|
|
|
* |
110
|
|
|
* @param AssetCollection $assets |
111
|
|
|
* @param I18nRegistry $i18n_registry |
112
|
|
|
* @throws InvalidArgumentException |
113
|
|
|
* @throws InvalidDataTypeException |
114
|
|
|
* @throws InvalidInterfaceException |
115
|
|
|
*/ |
116
|
|
|
public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
117
|
|
|
{ |
118
|
|
|
$this->assets = $assets; |
119
|
|
|
$this->i18n_registry = $i18n_registry; |
120
|
|
|
add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
121
|
|
|
add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
122
|
|
|
add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
123
|
|
|
add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
124
|
|
|
add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
125
|
|
|
add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
126
|
|
|
add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
127
|
|
|
add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
133
|
|
|
* translation handling. |
134
|
|
|
* |
135
|
|
|
* @return I18nRegistry |
136
|
|
|
*/ |
137
|
|
|
public function getI18nRegistry() |
138
|
|
|
{ |
139
|
|
|
return $this->i18n_registry; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Callback for the wp_enqueue_scripts actions used to register assets. |
145
|
|
|
* |
146
|
|
|
* @since 4.9.62.p |
147
|
|
|
* @throws Exception |
148
|
|
|
*/ |
149
|
|
|
public function registerScriptsAndStyles() |
150
|
|
|
{ |
151
|
|
|
try { |
152
|
|
|
$this->registerScripts($this->assets->getJavascriptAssets()); |
153
|
|
|
$this->registerStyles($this->assets->getStylesheetAssets()); |
154
|
|
|
} catch (Exception $exception) { |
155
|
|
|
new ExceptionStackTraceDisplay($exception); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Registers JS assets with WP core |
162
|
|
|
* |
163
|
|
|
* @param JavascriptAsset[] $scripts |
164
|
|
|
* @throws AssetRegistrationException |
165
|
|
|
* @throws InvalidDataTypeException |
166
|
|
|
* @throws DomainException |
167
|
|
|
* @since 4.9.62.p |
168
|
|
|
*/ |
169
|
|
|
public function registerScripts(array $scripts) |
170
|
|
|
{ |
171
|
|
|
foreach ($scripts as $script) { |
172
|
|
|
// skip to next script if this has already been done |
173
|
|
|
if ($script->isRegistered()) { |
174
|
|
|
continue; |
175
|
|
|
} |
176
|
|
|
do_action( |
177
|
|
|
'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
178
|
|
|
$script |
179
|
|
|
); |
180
|
|
|
$registered = wp_register_script( |
181
|
|
|
$script->handle(), |
182
|
|
|
$script->source(), |
183
|
|
|
$script->dependencies(), |
184
|
|
|
$script->version(), |
185
|
|
|
$script->loadInFooter() |
186
|
|
|
); |
187
|
|
|
if (! $registered && $this->debug()) { |
188
|
|
|
throw new AssetRegistrationException($script->handle()); |
189
|
|
|
} |
190
|
|
|
$script->setRegistered($registered); |
191
|
|
|
if ($script->requiresTranslation()) { |
192
|
|
|
$this->registerTranslation($script->handle()); |
193
|
|
|
} |
194
|
|
|
do_action( |
195
|
|
|
'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
196
|
|
|
$script |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Registers CSS assets with WP core |
204
|
|
|
* |
205
|
|
|
* @param StylesheetAsset[] $styles |
206
|
|
|
* @throws InvalidDataTypeException |
207
|
|
|
* @throws DomainException |
208
|
|
|
* @since 4.9.62.p |
209
|
|
|
*/ |
210
|
|
|
public function registerStyles(array $styles) |
211
|
|
|
{ |
212
|
|
|
foreach ($styles as $style) { |
213
|
|
|
// skip to next style if this has already been done |
214
|
|
|
if ($style->isRegistered()) { |
215
|
|
|
continue; |
216
|
|
|
} |
217
|
|
|
do_action( |
218
|
|
|
'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
219
|
|
|
$style |
220
|
|
|
); |
221
|
|
|
wp_register_style( |
222
|
|
|
$style->handle(), |
223
|
|
|
$style->source(), |
224
|
|
|
$style->dependencies(), |
225
|
|
|
$style->version(), |
226
|
|
|
$style->media() |
227
|
|
|
); |
228
|
|
|
$style->setRegistered(); |
229
|
|
|
do_action( |
230
|
|
|
'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
231
|
|
|
$style |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Call back for the script print in frontend and backend. |
239
|
|
|
* Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
240
|
|
|
* |
241
|
|
|
* @since 4.9.31.rc.015 |
242
|
|
|
*/ |
243
|
|
|
public function enqueueData() |
244
|
|
|
{ |
245
|
|
|
$this->removeAlreadyRegisteredDataForScriptHandles(); |
246
|
|
|
wp_add_inline_script( |
247
|
|
|
'eejs-core', |
248
|
|
|
'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
249
|
|
|
'before' |
250
|
|
|
); |
251
|
|
|
$scripts = $this->assets->getJavascriptAssetsWithData(); |
252
|
|
|
foreach ($scripts as $script) { |
253
|
|
|
$this->addRegisteredScriptHandlesWithData($script->handle()); |
254
|
|
|
if ($script->hasInlineDataCallback()) { |
255
|
|
|
$localize = $script->inlineDataCallback(); |
256
|
|
|
$localize(); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Used to add data to eejs.data object. |
264
|
|
|
* Note: Overriding existing data is not allowed. |
265
|
|
|
* Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
266
|
|
|
* If the data you add is something like this: |
267
|
|
|
* $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
268
|
|
|
* It will be exposed in the page source as: |
269
|
|
|
* eejs.data.my_plugin_data.foo == gar |
270
|
|
|
* |
271
|
|
|
* @param string $key Key used to access your data |
272
|
|
|
* @param string|array $value Value to attach to key |
273
|
|
|
* @throws InvalidArgumentException |
274
|
|
|
*/ |
275
|
|
|
public function addData($key, $value) |
276
|
|
|
{ |
277
|
|
|
if ($this->verifyDataNotExisting($key)) { |
278
|
|
|
$this->jsdata[ $key ] = $value; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Similar to addData except this allows for users to push values to an existing key where the values on key are |
285
|
|
|
* elements in an array. |
286
|
|
|
* |
287
|
|
|
* When you use this method, the value you include will be merged with the array on $key. |
288
|
|
|
* So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
289
|
|
|
* object like this, eejs.data.test = [ my_data, |
290
|
|
|
* ] |
291
|
|
|
* If there has already been a scalar value attached to the data object given key (via addData for instance), then |
292
|
|
|
* this will throw an exception. |
293
|
|
|
* |
294
|
|
|
* Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
295
|
|
|
* key potentially overriding the existing data on merge (specifically with associative arrays). |
296
|
|
|
* |
297
|
|
|
* @param string $key Key to attach data to. |
298
|
|
|
* @param string|array $value Value being registered. |
299
|
|
|
* @throws InvalidArgumentException |
300
|
|
|
*/ |
301
|
|
|
public function pushData($key, $value) |
302
|
|
|
{ |
303
|
|
|
if (isset($this->jsdata[ $key ]) |
304
|
|
|
&& ! is_array($this->jsdata[ $key ]) |
305
|
|
|
) { |
306
|
|
|
if (! $this->debug()) { |
307
|
|
|
return; |
308
|
|
|
} |
309
|
|
|
throw new InvalidArgumentException( |
310
|
|
|
sprintf( |
311
|
|
|
__( |
312
|
|
|
'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
313
|
|
|
push values to this data element when it is an array.', |
314
|
|
|
'event_espresso' |
315
|
|
|
), |
316
|
|
|
$key, |
317
|
|
|
__METHOD__ |
318
|
|
|
) |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
if ( ! isset( $this->jsdata[ $key ] ) ) { |
322
|
|
|
$this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
323
|
|
|
} else { |
324
|
|
|
$this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Used to set content used by javascript for a template. |
331
|
|
|
* Note: Overrides of existing registered templates are not allowed. |
332
|
|
|
* |
333
|
|
|
* @param string $template_reference |
334
|
|
|
* @param string $template_content |
335
|
|
|
* @throws InvalidArgumentException |
336
|
|
|
*/ |
337
|
|
|
public function addTemplate($template_reference, $template_content) |
338
|
|
|
{ |
339
|
|
|
if (! isset($this->jsdata['templates'])) { |
340
|
|
|
$this->jsdata['templates'] = array(); |
341
|
|
|
} |
342
|
|
|
//no overrides allowed. |
343
|
|
View Code Duplication |
if (isset($this->jsdata['templates'][ $template_reference ])) { |
344
|
|
|
if (! $this->debug()) { |
345
|
|
|
return; |
346
|
|
|
} |
347
|
|
|
throw new InvalidArgumentException( |
348
|
|
|
sprintf( |
349
|
|
|
__( |
350
|
|
|
'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
351
|
|
|
'event_espresso' |
352
|
|
|
), |
353
|
|
|
$template_reference |
354
|
|
|
) |
355
|
|
|
); |
356
|
|
|
} |
357
|
|
|
$this->jsdata['templates'][ $template_reference ] = $template_content; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Retrieve the template content already registered for the given reference. |
363
|
|
|
* |
364
|
|
|
* @param string $template_reference |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
public function getTemplate($template_reference) |
368
|
|
|
{ |
369
|
|
|
return isset($this->jsdata['templates'][ $template_reference ]) |
370
|
|
|
? $this->jsdata['templates'][ $template_reference ] |
371
|
|
|
: ''; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Retrieve registered data. |
377
|
|
|
* |
378
|
|
|
* @param string $key Name of key to attach data to. |
379
|
|
|
* @return mixed If there is no for the given key, then false is returned. |
380
|
|
|
*/ |
381
|
|
|
public function getData($key) |
382
|
|
|
{ |
383
|
|
|
return isset($this->jsdata[ $key ]) |
384
|
|
|
? $this->jsdata[ $key ] |
385
|
|
|
: false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Verifies whether the given data exists already on the jsdata array. |
391
|
|
|
* Overriding data is not allowed. |
392
|
|
|
* |
393
|
|
|
* @param string $key Index for data. |
394
|
|
|
* @return bool If valid then return true. |
395
|
|
|
* @throws InvalidArgumentException if data already exists. |
396
|
|
|
*/ |
397
|
|
|
protected function verifyDataNotExisting($key) |
398
|
|
|
{ |
399
|
|
|
if (isset($this->jsdata[ $key ])) { |
400
|
|
|
if (! $this->debug()) { |
401
|
|
|
return false; |
402
|
|
|
} |
403
|
|
|
if (is_array($this->jsdata[ $key ])) { |
404
|
|
|
throw new InvalidArgumentException( |
405
|
|
|
sprintf( |
406
|
|
|
__( |
407
|
|
|
'The value for %1$s already exists in the Registry::eejs object. |
408
|
|
|
Overrides are not allowed. Since the value of this data is an array, you may want to use the |
409
|
|
|
%2$s method to push your value to the array.', |
410
|
|
|
'event_espresso' |
411
|
|
|
), |
412
|
|
|
$key, |
413
|
|
|
'pushData()' |
414
|
|
|
) |
415
|
|
|
); |
416
|
|
|
} |
417
|
|
|
throw new InvalidArgumentException( |
418
|
|
|
sprintf( |
419
|
|
|
__( |
420
|
|
|
'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
421
|
|
|
allowed. Consider attaching your value to a different key', |
422
|
|
|
'event_espresso' |
423
|
|
|
), |
424
|
|
|
$key |
425
|
|
|
) |
426
|
|
|
); |
427
|
|
|
} |
428
|
|
|
return true; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Get the actual asset path for asset manifests. |
434
|
|
|
* If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
435
|
|
|
* |
436
|
|
|
* @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
437
|
|
|
* asset file location. |
438
|
|
|
* @param string $chunk_name |
439
|
|
|
* @param string $asset_type |
440
|
|
|
* @return string |
441
|
|
|
* @since 4.9.59.p |
442
|
|
|
*/ |
443
|
|
|
public function getAssetUrl($namespace, $chunk_name, $asset_type) |
444
|
|
|
{ |
445
|
|
|
$url = isset( |
446
|
|
|
$this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
447
|
|
|
$this->manifest_data[ $namespace ]['url_base'] |
448
|
|
|
) |
449
|
|
|
? $this->manifest_data[ $namespace ]['url_base'] |
450
|
|
|
. $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
451
|
|
|
: $chunk_name; |
452
|
|
|
|
453
|
|
|
return apply_filters( |
454
|
|
|
'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
455
|
|
|
$url, |
456
|
|
|
$namespace, |
457
|
|
|
$chunk_name, |
458
|
|
|
$asset_type |
459
|
|
|
); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
|
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Return the url to a js file for the given namespace and chunk name. |
466
|
|
|
* |
467
|
|
|
* @param string $namespace |
468
|
|
|
* @param string $chunk_name |
469
|
|
|
* @return string |
470
|
|
|
*/ |
471
|
|
|
public function getJsUrl($namespace, $chunk_name) |
472
|
|
|
{ |
473
|
|
|
return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Return the url to a css file for the given namespace and chunk name. |
479
|
|
|
* |
480
|
|
|
* @param string $namespace |
481
|
|
|
* @param string $chunk_name |
482
|
|
|
* @return string |
483
|
|
|
*/ |
484
|
|
|
public function getCssUrl($namespace, $chunk_name) |
485
|
|
|
{ |
486
|
|
|
return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Return the dependencies array and version string for a given asset $chunk_name |
492
|
|
|
* |
493
|
|
|
* @param string $namespace |
494
|
|
|
* @param string $chunk_name |
495
|
|
|
* @param string $asset_type |
496
|
|
|
* @return array |
497
|
|
|
* @since 4.9.82.p |
498
|
|
|
*/ |
499
|
|
|
private function getDetailsForAsset($namespace, $chunk_name, $asset_type) |
500
|
|
|
{ |
501
|
|
|
$asset_index = $chunk_name . '.' . $asset_type; |
502
|
|
|
if (! isset( $this->dependencies_data[ $namespace ][ $asset_index ])) { |
503
|
|
|
$path = isset($this->manifest_data[ $namespace ]['path']) |
504
|
|
|
? $this->manifest_data[ $namespace ]['path'] |
505
|
|
|
: ''; |
506
|
|
|
$dependencies_index = $chunk_name . '.' . Asset::TYPE_PHP; |
507
|
|
|
$file_path = isset($this->manifest_data[ $namespace ][ $dependencies_index ]) |
508
|
|
|
? $path . $this->manifest_data[ $namespace ][ $dependencies_index ] |
509
|
|
|
: |
510
|
|
|
''; |
511
|
|
|
$this->dependencies_data[ $namespace ][ $asset_index ] = $file_path !== '' && file_exists($file_path) |
512
|
|
|
? $this->getDetailsForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
513
|
|
|
: []; |
514
|
|
|
} |
515
|
|
|
$details = $this->dependencies_data[ $namespace ][ $asset_index ]; |
516
|
|
|
return $details; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Return dependencies array and version string according to asset type. |
522
|
|
|
* For css assets, this filters the auto generated dependencies by css type. |
523
|
|
|
* |
524
|
|
|
* @param string $namespace |
525
|
|
|
* @param string $asset_type |
526
|
|
|
* @param string $file_path |
527
|
|
|
* @param string $chunk_name |
528
|
|
|
* @return array |
529
|
|
|
* @since 4.9.82.p |
530
|
|
|
*/ |
531
|
|
|
private function getDetailsForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
532
|
|
|
{ |
533
|
|
|
// $asset_dependencies = json_decode(file_get_contents($file_path), true); |
534
|
|
|
$asset_details = require($file_path); |
535
|
|
|
$asset_details['dependencies'] = isset($asset_details['dependencies']) |
536
|
|
|
? $asset_details['dependencies'] |
537
|
|
|
: []; |
538
|
|
|
$asset_details['version'] = isset($asset_details['version']) |
539
|
|
|
? $asset_details['version'] |
540
|
|
|
: ''; |
541
|
|
|
if ($asset_type === Asset::TYPE_JS) { |
542
|
|
|
$asset_details['dependencies'] = $chunk_name === 'eejs-core' |
543
|
|
|
? $asset_details['dependencies'] |
544
|
|
|
: $asset_details['dependencies'] + [ CoreAssetManager::JS_HANDLE_JS_CORE ]; |
545
|
|
|
return $asset_details; |
546
|
|
|
} |
547
|
|
|
// for css we need to make sure there is actually a css file related to this chunk. |
548
|
|
|
if (isset($this->manifest_data[ $namespace ])) { |
549
|
|
|
// array of css chunk files for ee. |
550
|
|
|
$css_chunks = array_map( |
551
|
|
|
static function ($value) { |
552
|
|
|
return str_replace('.css', '', $value); |
553
|
|
|
}, |
554
|
|
|
array_filter( |
555
|
|
|
array_keys($this->manifest_data[ $namespace ]), |
556
|
|
|
static function ($value) { |
557
|
|
|
return strpos($value, '.css') !== false; |
558
|
|
|
} |
559
|
|
|
) |
560
|
|
|
); |
561
|
|
|
// add known wp chunks with css |
562
|
|
|
$css_chunks = array_merge( $css_chunks, $this->wp_css_handle_dependencies); |
563
|
|
|
// flip for easier search |
564
|
|
|
$css_chunks = array_flip($css_chunks); |
565
|
|
|
// now let's filter the dependencies for the incoming chunk to actual chunks that have styles |
566
|
|
|
$asset_details['dependencies'] = array_filter( |
567
|
|
|
$asset_details['dependencies'], |
568
|
|
|
static function ($chunk_name) use ($css_chunks) { |
569
|
|
|
return isset($css_chunks[ $chunk_name ]); |
570
|
|
|
} |
571
|
|
|
); |
572
|
|
|
return $asset_details; |
573
|
|
|
} |
574
|
|
|
return ['dependencies' => [], 'version' => '']; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Get the dependencies array and version string for the given js asset chunk name |
580
|
|
|
* |
581
|
|
|
* @param string $namespace |
582
|
|
|
* @param string $chunk_name |
583
|
|
|
* @return array |
584
|
|
|
* @since $VID:$ |
585
|
|
|
*/ |
586
|
|
|
public function getJsAssetDetails($namespace, $chunk_name) |
587
|
|
|
{ |
588
|
|
|
return $this->getDetailsForAsset($namespace, $chunk_name, Asset::TYPE_JS); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Get the dependencies array and version string for the given css asset chunk name |
594
|
|
|
* |
595
|
|
|
* @param string $namespace |
596
|
|
|
* @param string $chunk_name |
597
|
|
|
* @return array |
598
|
|
|
* @since $VID:$ |
599
|
|
|
*/ |
600
|
|
|
public function getCssAssetDetails($namespace, $chunk_name) |
601
|
|
|
{ |
602
|
|
|
return $this->getDetailsForAsset($namespace, $chunk_name, Asset::TYPE_CSS); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* @since 4.9.62.p |
608
|
|
|
* @throws InvalidArgumentException |
609
|
|
|
* @throws InvalidFilePathException |
610
|
|
|
*/ |
611
|
|
|
public function registerManifestFiles() |
612
|
|
|
{ |
613
|
|
|
$manifest_files = $this->assets->getManifestFiles(); |
614
|
|
|
foreach ($manifest_files as $manifest_file) { |
615
|
|
|
$this->registerManifestFile( |
616
|
|
|
$manifest_file->assetNamespace(), |
617
|
|
|
$manifest_file->urlBase(), |
618
|
|
|
$manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST, |
619
|
|
|
$manifest_file->filepath() |
620
|
|
|
); |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Used to register a js/css manifest file with the registered_manifest_files property. |
627
|
|
|
* |
628
|
|
|
* @param string $namespace Provided to associate the manifest file with a specific namespace. |
629
|
|
|
* @param string $url_base The url base for the manifest file location. |
630
|
|
|
* @param string $manifest_file The absolute path to the manifest file. |
631
|
|
|
* @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
632
|
|
|
* default to `plugin_root/assets/dist`. |
633
|
|
|
* @throws InvalidArgumentException |
634
|
|
|
* @throws InvalidFilePathException |
635
|
|
|
* @since 4.9.59.p |
636
|
|
|
*/ |
637
|
|
|
public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
638
|
|
|
{ |
639
|
|
View Code Duplication |
if (isset($this->manifest_data[ $namespace ])) { |
640
|
|
|
if (! $this->debug()) { |
641
|
|
|
return; |
642
|
|
|
} |
643
|
|
|
throw new InvalidArgumentException( |
644
|
|
|
sprintf( |
645
|
|
|
esc_html__( |
646
|
|
|
'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
647
|
|
|
'event_espresso' |
648
|
|
|
), |
649
|
|
|
$namespace |
650
|
|
|
) |
651
|
|
|
); |
652
|
|
|
} |
653
|
|
|
if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
654
|
|
|
if (is_admin()) { |
655
|
|
|
EE_Error::add_error( |
656
|
|
|
sprintf( |
657
|
|
|
esc_html__( |
658
|
|
|
'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
659
|
|
|
'event_espresso' |
660
|
|
|
), |
661
|
|
|
'Event Espresso', |
662
|
|
|
$url_base, |
663
|
|
|
'plugins_url', |
664
|
|
|
'WP_PLUGIN_URL' |
665
|
|
|
), |
666
|
|
|
__FILE__, |
667
|
|
|
__FUNCTION__, |
668
|
|
|
__LINE__ |
669
|
|
|
); |
670
|
|
|
} |
671
|
|
|
return; |
672
|
|
|
} |
673
|
|
|
$this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
674
|
|
|
if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
675
|
|
|
$this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
676
|
|
|
} |
677
|
|
|
if (! isset($this->manifest_data[ $namespace ]['path'])) { |
678
|
|
|
$this->manifest_data[ $namespace ]['path'] = $manifest_file_path; |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* Decodes json from the provided manifest file. |
685
|
|
|
* |
686
|
|
|
* @since 4.9.59.p |
687
|
|
|
* @param string $manifest_file Path to manifest file. |
688
|
|
|
* @return array |
689
|
|
|
* @throws InvalidFilePathException |
690
|
|
|
*/ |
691
|
|
|
private function decodeManifestFile($manifest_file) |
692
|
|
|
{ |
693
|
|
|
if (! file_exists($manifest_file)) { |
694
|
|
|
throw new InvalidFilePathException($manifest_file); |
695
|
|
|
} |
696
|
|
|
return json_decode(file_get_contents($manifest_file), true); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* This is used to set registered script handles that have data. |
702
|
|
|
* |
703
|
|
|
* @param string $script_handle |
704
|
|
|
*/ |
705
|
|
|
private function addRegisteredScriptHandlesWithData($script_handle) |
706
|
|
|
{ |
707
|
|
|
$this->script_handles_with_data[ $script_handle ] = $script_handle; |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
|
711
|
|
|
/**i |
712
|
|
|
* Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
713
|
|
|
* Dependency stored in WP_Scripts if its set. |
714
|
|
|
*/ |
715
|
|
|
private function removeAlreadyRegisteredDataForScriptHandles() |
716
|
|
|
{ |
717
|
|
|
if (empty($this->script_handles_with_data)) { |
718
|
|
|
return; |
719
|
|
|
} |
720
|
|
|
foreach ($this->script_handles_with_data as $script_handle) { |
721
|
|
|
$this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Removes any data dependency registered in WP_Scripts if its set. |
728
|
|
|
* |
729
|
|
|
* @param string $script_handle |
730
|
|
|
*/ |
731
|
|
|
private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
732
|
|
|
{ |
733
|
|
|
if (isset($this->script_handles_with_data[ $script_handle ])) { |
734
|
|
|
global $wp_scripts; |
735
|
|
|
$unset_handle = false; |
736
|
|
View Code Duplication |
if ($wp_scripts->get_data($script_handle, 'data')) { |
737
|
|
|
unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
738
|
|
|
$unset_handle = true; |
739
|
|
|
} |
740
|
|
|
//deal with inline_scripts |
741
|
|
View Code Duplication |
if ($wp_scripts->get_data($script_handle, 'before')) { |
742
|
|
|
unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
743
|
|
|
$unset_handle = true; |
744
|
|
|
} |
745
|
|
|
if ($wp_scripts->get_data($script_handle, 'after')) { |
746
|
|
|
unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
747
|
|
|
} |
748
|
|
|
if ($unset_handle) { |
749
|
|
|
unset($this->script_handles_with_data[ $script_handle ]); |
750
|
|
|
} |
751
|
|
|
} |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
|
755
|
|
|
/** |
756
|
|
|
* register translations for a registered script |
757
|
|
|
* |
758
|
|
|
* @param string $handle |
759
|
|
|
*/ |
760
|
|
|
public function registerTranslation($handle) |
761
|
|
|
{ |
762
|
|
|
$this->i18n_registry->registerScriptI18n($handle); |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* @since 4.9.63.p |
768
|
|
|
* @return bool |
769
|
|
|
*/ |
770
|
|
|
private function debug() |
771
|
|
|
{ |
772
|
|
|
return apply_filters( |
773
|
|
|
'FHEE__EventEspresso_core_services_assets_Registry__debug', |
774
|
|
|
defined('EE_DEBUG') && EE_DEBUG |
775
|
|
|
); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Get the dependencies array for the given js asset chunk name |
781
|
|
|
* |
782
|
|
|
* @param string $namespace |
783
|
|
|
* @param string $chunk_name |
784
|
|
|
* @return array |
785
|
|
|
* @deprecated $VID:$ |
786
|
|
|
* @since 4.9.82.p |
787
|
|
|
*/ |
788
|
|
|
public function getJsDependencies($namespace, $chunk_name) |
789
|
|
|
{ |
790
|
|
|
$details = $this->getJsAssetDetails($namespace, $chunk_name); |
791
|
|
|
return isset($details['dependencies']) ? $details['dependencies'] : []; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
|
795
|
|
|
/** |
796
|
|
|
* Get the dependencies array for the given css asset chunk name |
797
|
|
|
* |
798
|
|
|
* @param string $namespace |
799
|
|
|
* @param string $chunk_name |
800
|
|
|
* @return array |
801
|
|
|
* @deprecated $VID:$ |
802
|
|
|
* @since 4.9.82.p |
803
|
|
|
*/ |
804
|
|
|
public function getCssDependencies($namespace, $chunk_name) |
805
|
|
|
{ |
806
|
|
|
$details = $this->getCssAssetDetails($namespace, $chunk_name); |
807
|
|
|
return isset($details['dependencies']) ? $details['dependencies'] : []; |
808
|
|
|
} |
809
|
|
|
} |
810
|
|
|
|