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