Completed
Pull Request — master (#990)
by Darren
26:52 queued 17:58
created
core/domain/services/assets/CoreAssetManager.php 1 patch
Indentation   +534 added lines, -534 removed lines patch added patch discarded remove patch
@@ -31,557 +31,557 @@
 block discarded – undo
31 31
 class CoreAssetManager extends AssetManager
32 32
 {
33 33
 
34
-    // WordPress core / Third party JS asset handles
35
-    const JS_HANDLE_JQUERY = 'jquery';
34
+	// WordPress core / Third party JS asset handles
35
+	const JS_HANDLE_JQUERY = 'jquery';
36 36
 
37
-    const JS_HANDLE_JQUERY_VALIDATE = 'jquery-validate';
37
+	const JS_HANDLE_JQUERY_VALIDATE = 'jquery-validate';
38 38
 
39
-    const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods';
39
+	const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods';
40 40
 
41
-    const JS_HANDLE_UNDERSCORE = 'underscore';
41
+	const JS_HANDLE_UNDERSCORE = 'underscore';
42 42
 
43
-    const JS_HANDLE_ACCOUNTING_CORE = 'ee-accounting-core';
43
+	const JS_HANDLE_ACCOUNTING_CORE = 'ee-accounting-core';
44 44
 
45
-    /**
46
-     * @since 4.9.71.p
47
-     */
48
-    const JS_HANDLE_REACT = 'react';
45
+	/**
46
+	 * @since 4.9.71.p
47
+	 */
48
+	const JS_HANDLE_REACT = 'react';
49 49
 
50
-    /**
51
-     * @since 4.9.71.p
52
-     */
53
-    const JS_HANDLE_REACT_DOM = 'react-dom';
50
+	/**
51
+	 * @since 4.9.71.p
52
+	 */
53
+	const JS_HANDLE_REACT_DOM = 'react-dom';
54 54
 
55
-    /**
56
-     * @since 4.9.71.p
57
-     */
58
-    const JS_HANDLE_LODASH = 'lodash';
55
+	/**
56
+	 * @since 4.9.71.p
57
+	 */
58
+	const JS_HANDLE_LODASH = 'lodash';
59 59
 
60
-    // EE JS assets handles
61
-    const JS_HANDLE_MANIFEST = 'ee-manifest';
60
+	// EE JS assets handles
61
+	const JS_HANDLE_MANIFEST = 'ee-manifest';
62 62
 
63
-    const JS_HANDLE_JS_CORE = 'eejs-core';
63
+	const JS_HANDLE_JS_CORE = 'eejs-core';
64 64
 
65
-    const JS_HANDLE_VENDOR = 'eventespresso-vendor';
65
+	const JS_HANDLE_VENDOR = 'eventespresso-vendor';
66 66
 
67
-    const JS_HANDLE_DATA_STORES = 'eventespresso-data-stores';
67
+	const JS_HANDLE_DATA_STORES = 'eventespresso-data-stores';
68 68
 
69
-    const JS_HANDLE_HELPERS = 'eventespresso-helpers';
69
+	const JS_HANDLE_HELPERS = 'eventespresso-helpers';
70 70
 
71
-    const JS_HANDLE_MODEL = 'eventespresso-model';
71
+	const JS_HANDLE_MODEL = 'eventespresso-model';
72 72
 
73
-    const JS_HANDLE_VALUE_OBJECTS = 'eventespresso-value-objects';
73
+	const JS_HANDLE_VALUE_OBJECTS = 'eventespresso-value-objects';
74 74
 
75
-    const JS_HANDLE_HOCS = 'eventespresso-hocs';
75
+	const JS_HANDLE_HOCS = 'eventespresso-hocs';
76 76
 
77
-    const JS_HANDLE_COMPONENTS = 'eventespresso-components';
78
-
79
-    const JS_HANDLE_EDITOR_HOCS = 'eventespresso-editor-hocs';
77
+	const JS_HANDLE_COMPONENTS = 'eventespresso-components';
78
+
79
+	const JS_HANDLE_EDITOR_HOCS = 'eventespresso-editor-hocs';
80 80
 
81
-    const JS_HANDLE_VALIDATORS = 'eventespresso-validators';
81
+	const JS_HANDLE_VALIDATORS = 'eventespresso-validators';
82 82
 
83
-    const JS_HANDLE_CORE = 'espresso_core';
83
+	const JS_HANDLE_CORE = 'espresso_core';
84 84
 
85
-    const JS_HANDLE_I18N = 'eei18n';
86
-
87
-    const JS_HANDLE_ACCOUNTING = 'ee-accounting';
88
-
89
-    const JS_HANDLE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page';
90
-
91
-    // EE CSS assets handles
92
-    const CSS_HANDLE_DEFAULT = 'espresso_default';
93
-
94
-    const CSS_HANDLE_CUSTOM = 'espresso_custom_css';
95
-
96
-    const CSS_HANDLE_COMPONENTS = 'eventespresso-components';
97
-
98
-    /**
99
-     * @var EE_Currency_Config $currency_config
100
-     */
101
-    protected $currency_config;
102
-
103
-    /**
104
-     * @var EE_Template_Config $template_config
105
-     */
106
-    protected $template_config;
107
-
108
-
109
-    /**
110
-     * CoreAssetRegister constructor.
111
-     *
112
-     * @param AssetCollection    $assets
113
-     * @param EE_Currency_Config $currency_config
114
-     * @param EE_Template_Config $template_config
115
-     * @param DomainInterface    $domain
116
-     * @param Registry           $registry
117
-     */
118
-    public function __construct(
119
-        AssetCollection $assets,
120
-        EE_Currency_Config $currency_config,
121
-        EE_Template_Config $template_config,
122
-        DomainInterface $domain,
123
-        Registry $registry
124
-    ) {
125
-        $this->currency_config = $currency_config;
126
-        $this->template_config = $template_config;
127
-        parent::__construct($domain, $assets, $registry);
128
-    }
129
-
130
-
131
-    /**
132
-     * @since 4.9.62.p
133
-     * @throws DomainException
134
-     * @throws DuplicateCollectionIdentifierException
135
-     * @throws InvalidArgumentException
136
-     * @throws InvalidDataTypeException
137
-     * @throws InvalidEntityException
138
-     * @throws InvalidInterfaceException
139
-     */
140
-    public function addAssets()
141
-    {
142
-        $this->addJavascriptFiles();
143
-        $this->addStylesheetFiles();
144
-    }
145
-
146
-
147
-    /**
148
-     * @since 4.9.62.p
149
-     * @throws DomainException
150
-     * @throws DuplicateCollectionIdentifierException
151
-     * @throws InvalidArgumentException
152
-     * @throws InvalidDataTypeException
153
-     * @throws InvalidEntityException
154
-     * @throws InvalidInterfaceException
155
-     */
156
-    public function addJavascriptFiles()
157
-    {
158
-        $this->loadCoreJs();
159
-        $this->loadJqueryValidate();
160
-        $this->loadAccountingJs();
161
-        add_action(
162
-            'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
163
-            array($this, 'loadQtipJs')
164
-        );
165
-        $this->registerAdminAssets();
166
-    }
167
-
168
-
169
-    /**
170
-     * @since 4.9.62.p
171
-     * @throws DuplicateCollectionIdentifierException
172
-     * @throws InvalidDataTypeException
173
-     * @throws InvalidEntityException
174
-     */
175
-    public function addStylesheetFiles()
176
-    {
177
-        $this->loadCoreCss();
178
-    }
179
-
180
-
181
-    /**
182
-     * core default javascript
183
-     *
184
-     * @since 4.9.62.p
185
-     * @throws DomainException
186
-     * @throws DuplicateCollectionIdentifierException
187
-     * @throws InvalidArgumentException
188
-     * @throws InvalidDataTypeException
189
-     * @throws InvalidEntityException
190
-     * @throws InvalidInterfaceException
191
-     */
192
-    private function loadCoreJs()
193
-    {
194
-        // conditionally load third-party libraries that WP core MIGHT have.
195
-        $this->registerWpAssets();
196
-
197
-        $this->addJavascript(
198
-            CoreAssetManager::JS_HANDLE_MANIFEST,
199
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'manifest')
200
-        );
201
-
202
-        $this->addJavascript(
203
-            CoreAssetManager::JS_HANDLE_JS_CORE,
204
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'eejs'),
205
-            array(CoreAssetManager::JS_HANDLE_MANIFEST)
206
-        )
207
-        ->setHasInlineData();
208
-
209
-        $this->addJavascript(
210
-            CoreAssetManager::JS_HANDLE_VENDOR,
211
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'vendor'),
212
-            array(
213
-                CoreAssetManager::JS_HANDLE_JS_CORE,
214
-                CoreAssetManager::JS_HANDLE_REACT,
215
-                CoreAssetManager::JS_HANDLE_REACT_DOM,
216
-                CoreAssetManager::JS_HANDLE_LODASH,
217
-            )
218
-        );
219
-
220
-        $this->addJavascript(
221
-            CoreAssetManager::JS_HANDLE_VALIDATORS,
222
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'validators')
223
-        )->setRequiresTranslation();
224
-
225
-        $this->addJavascript(
226
-            CoreAssetManager::JS_HANDLE_HELPERS,
227
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'helpers'),
228
-            array(
229
-                CoreAssetManager::JS_HANDLE_VALIDATORS
230
-            )
231
-        )->setRequiresTranslation();
232
-
233
-        $this->addJavascript(
234
-            CoreAssetManager::JS_HANDLE_MODEL,
235
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'model'),
236
-            array(
237
-                CoreAssetManager::JS_HANDLE_HELPERS,
238
-                CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
239
-            )
240
-        )->setRequiresTranslation();
241
-
242
-        $this->addJavascript(
243
-            CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
244
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'valueObjects'),
245
-            array(
246
-                CoreAssetManager::JS_HANDLE_VALIDATORS,
247
-                CoreAssetManager::JS_HANDLE_HELPERS,
248
-            )
249
-        )->setRequiresTranslation();
250
-
251
-        $this->addJavascript(
252
-            CoreAssetManager::JS_HANDLE_DATA_STORES,
253
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'data-stores'),
254
-            array(
255
-                CoreAssetManager::JS_HANDLE_VENDOR,
256
-                'wp-data',
257
-                'wp-api-fetch',
258
-                CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
259
-                CoreAssetManager::JS_HANDLE_MODEL,
260
-            )
261
-        )
262
-             ->setRequiresTranslation()
263
-             ->setInlineDataCallback(
264
-                 function() {
265
-                     wp_add_inline_script(
266
-                         CoreAssetManager::JS_HANDLE_DATA_STORES,
267
-                         is_admin()
268
-                             ? 'wp.apiFetch.use( eejs.middleWares.apiFetch.capsMiddleware( eejs.middleWares.apiFetch.CONTEXT_CAPS_EDIT ) )'
269
-                             : 'wp.apiFetch.use( eejs.middleWares.apiFetch.capsMiddleware )'
270
-                     );
271
-                 }
272
-             );
273
-
274
-        $this->addJavascript(
275
-            CoreAssetManager::JS_HANDLE_HOCS,
276
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'hocs'),
277
-            array(
278
-                CoreAssetManager::JS_HANDLE_DATA_STORES,
279
-                CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
280
-                'wp-components',
281
-            )
282
-        )->setRequiresTranslation();
283
-
284
-        $this->addJavascript(
285
-            CoreAssetManager::JS_HANDLE_COMPONENTS,
286
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'components'),
287
-            array(
288
-                CoreAssetManager::JS_HANDLE_DATA_STORES,
289
-                CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
290
-                'wp-components',
291
-            )
292
-        )
293
-        ->setRequiresTranslation();
294
-
295
-        $this->addJavascript(
296
-            CoreAssetManager::JS_HANDLE_EDITOR_HOCS,
297
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'editor-hocs'),
298
-            array(
299
-                CoreAssetManager::JS_HANDLE_COMPONENTS
300
-            )
301
-        )->setRequiresTranslation();
302
-
303
-        $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest'));
304
-        $this->registry->addData(
305
-            'paths',
306
-            array(
307
-                'base_rest_route' => rest_url(),
308
-                'rest_route' => rest_url('ee/v4.8.36/'),
309
-                'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(),
310
-                'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName(),
311
-                'site_url' => site_url('/'),
312
-                'admin_url' => admin_url('/'),
313
-            )
314
-        );
315
-        /** site formatting values **/
316
-        $this->registry->addData(
317
-            'site_formats',
318
-            array(
319
-                'date_formats' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats()
320
-            )
321
-        );
322
-        /** currency data **/
323
-        $this->registry->addData(
324
-            'currency_config',
325
-            $this->getCurrencySettings()
326
-        );
327
-        /** site timezone */
328
-        $this->registry->addData(
329
-            'default_timezone',
330
-            array(
331
-                'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(),
332
-                'string' => get_option('timezone_string'),
333
-                'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(),
334
-            )
335
-        );
336
-        /** site locale (user locale if user logged in) */
337
-        $this->registry->addData(
338
-            'locale',
339
-            array(
340
-                'user' => get_user_locale(),
341
-                'site' => get_locale()
342
-            )
343
-        );
344
-
345
-        $this->addJavascript(
346
-            CoreAssetManager::JS_HANDLE_CORE,
347
-            EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js',
348
-            array(CoreAssetManager::JS_HANDLE_JQUERY)
349
-        )
350
-        ->setInlineDataCallback(
351
-            function () {
352
-                wp_localize_script(
353
-                    CoreAssetManager::JS_HANDLE_CORE,
354
-                    CoreAssetManager::JS_HANDLE_I18N,
355
-                    EE_Registry::$i18n_js_strings
356
-                );
357
-            }
358
-        );
359
-    }
360
-
361
-
362
-    /**
363
-     * Registers vendor files that are bundled with a later version WP but might not be for the current version of
364
-     * WordPress in the running environment.
365
-     *
366
-     * @throws DuplicateCollectionIdentifierException
367
-     * @throws InvalidDataTypeException
368
-     * @throws InvalidEntityException
369
-     * @throws DomainException
370
-     * @since 4.9.71.p
371
-     */
372
-    private function registerWpAssets()
373
-    {
374
-        global $wp_version;
375
-        if (version_compare($wp_version, '5.0.beta', '>=')) {
376
-            return;
377
-        }
378
-        $this->addVendorJavascript(CoreAssetManager::JS_HANDLE_REACT)
379
-            ->setVersion('16.6.0');
380
-        $this->addVendorJavascript(
381
-            CoreAssetManager::JS_HANDLE_REACT_DOM,
382
-            array(CoreAssetManager::JS_HANDLE_REACT)
383
-        )->setVersion('16.6.0');
384
-        $this->addVendorJavascript(CoreAssetManager::JS_HANDLE_LODASH)
385
-            ->setInlineDataCallback(
386
-                function() {
387
-                    wp_add_inline_script(
388
-                        CoreAssetManager::JS_HANDLE_LODASH,
389
-                        'window.lodash = _.noConflict();'
390
-                    );
391
-                }
392
-            )
393
-            ->setVersion('4.17.11');
394
-    }
395
-
396
-
397
-    /**
398
-     * Returns configuration data for the accounting-js library.
399
-     * @since 4.9.71.p
400
-     * @return array
401
-     */
402
-    private function getAccountingSettings() {
403
-        return array(
404
-            'currency' => array(
405
-                'symbol'    => $this->currency_config->sign,
406
-                'format'    => array(
407
-                    'pos'  => $this->currency_config->sign_b4 ? '%s%v' : '%v%s',
408
-                    'neg'  => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s',
409
-                    'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s',
410
-                ),
411
-                'decimal'   => $this->currency_config->dec_mrk,
412
-                'thousand'  => $this->currency_config->thsnds,
413
-                'precision' => $this->currency_config->dec_plc,
414
-            ),
415
-            'number'   => array(
416
-                'precision' => $this->currency_config->dec_plc,
417
-                'thousand'  => $this->currency_config->thsnds,
418
-                'decimal'   => $this->currency_config->dec_mrk,
419
-            ),
420
-        );
421
-    }
422
-
423
-
424
-    /**
425
-     * Returns configuration data for the js Currency VO.
426
-     * @since 4.9.71.p
427
-     * @return array
428
-     */
429
-    private function getCurrencySettings()
430
-    {
431
-        return array(
432
-            'code' => $this->currency_config->code,
433
-            'singularLabel' => $this->currency_config->name,
434
-            'pluralLabel' => $this->currency_config->plural,
435
-            'sign' => $this->currency_config->sign,
436
-            'signB4' => $this->currency_config->sign_b4,
437
-            'decimalPlaces' => $this->currency_config->dec_plc,
438
-            'decimalMark' => $this->currency_config->dec_mrk,
439
-            'thousandsSeparator' => $this->currency_config->thsnds,
440
-        );
441
-    }
442
-
443
-
444
-    /**
445
-     * @since 4.9.62.p
446
-     * @throws DuplicateCollectionIdentifierException
447
-     * @throws InvalidDataTypeException
448
-     * @throws InvalidEntityException
449
-     */
450
-    private function loadCoreCss()
451
-    {
452
-        if ($this->template_config->enable_default_style && ! is_admin()) {
453
-            $this->addStylesheet(
454
-                CoreAssetManager::CSS_HANDLE_DEFAULT,
455
-                is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css')
456
-                    ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css'
457
-                    : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css',
458
-                array('dashicons')
459
-            );
460
-            //Load custom style sheet if available
461
-            if ($this->template_config->custom_style_sheet !== null) {
462
-                $this->addStylesheet(
463
-                    CoreAssetManager::CSS_HANDLE_CUSTOM,
464
-                    EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet,
465
-                    array(CoreAssetManager::CSS_HANDLE_DEFAULT)
466
-                );
467
-            }
468
-        }
469
-        $this->addStylesheet(
470
-            CoreAssetManager::CSS_HANDLE_COMPONENTS,
471
-            $this->registry->getCssUrl(
472
-                $this->domain->assetNamespace(),
473
-                'components'
474
-            )
475
-        );
476
-    }
477
-
478
-
479
-    /**
480
-     * jQuery Validate for form validation
481
-     *
482
-     * @since 4.9.62.p
483
-     * @throws DomainException
484
-     * @throws DuplicateCollectionIdentifierException
485
-     * @throws InvalidDataTypeException
486
-     * @throws InvalidEntityException
487
-     */
488
-    private function loadJqueryValidate()
489
-    {
490
-        $this->addJavascript(
491
-            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE,
492
-            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js',
493
-            array(CoreAssetManager::JS_HANDLE_JQUERY)
494
-        )
495
-        ->setVersion('1.15.0');
496
-
497
-        $this->addJavascript(
498
-            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA,
499
-            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js',
500
-            array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE)
501
-        )
502
-        ->setVersion('1.15.0');
503
-    }
504
-
505
-
506
-    /**
507
-     * accounting.js for performing client-side calculations
508
-     *
509
-     * @since 4.9.62.p
510
-     * @throws DomainException
511
-     * @throws DuplicateCollectionIdentifierException
512
-     * @throws InvalidDataTypeException
513
-     * @throws InvalidEntityException
514
-     */
515
-    private function loadAccountingJs()
516
-    {
517
-        //accounting.js library
518
-        // @link http://josscrowcroft.github.io/accounting.js/
519
-        $this->addJavascript(
520
-            CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE,
521
-            EE_THIRD_PARTY_URL . 'accounting/accounting.js',
522
-            array(CoreAssetManager::JS_HANDLE_UNDERSCORE)
523
-        )
524
-        ->setVersion('0.3.2');
525
-
526
-        $this->addJavascript(
527
-            CoreAssetManager::JS_HANDLE_ACCOUNTING,
528
-            EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js',
529
-            array(CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE)
530
-        )
531
-        ->setInlineDataCallback(
532
-            function () {
533
-                 wp_localize_script(
534
-                     CoreAssetManager::JS_HANDLE_ACCOUNTING,
535
-                     'EE_ACCOUNTING_CFG',
536
-                     $this->getAccountingSettings()
537
-                 );
538
-            }
539
-        )
540
-        ->setVersion();
541
-    }
542
-
543
-
544
-    /**
545
-     * registers assets for cleaning your ears
546
-     *
547
-     * @param JavascriptAsset $script
548
-     */
549
-    public function loadQtipJs(JavascriptAsset $script)
550
-    {
551
-        // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook,
552
-        // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' );
553
-        if (
554
-            $script->handle() === CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE
555
-            && apply_filters('FHEE_load_qtip', false)
556
-        ) {
557
-            EEH_Qtip_Loader::instance()->register_and_enqueue();
558
-        }
559
-    }
560
-
561
-
562
-    /**
563
-     * assets that are used in the WordPress admin
564
-     *
565
-     * @since 4.9.62.p
566
-     * @throws DuplicateCollectionIdentifierException
567
-     * @throws InvalidDataTypeException
568
-     * @throws InvalidEntityException
569
-     */
570
-    private function registerAdminAssets()
571
-    {
572
-        $this->addJavascript(
573
-            CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE,
574
-            $this->registry->getJsUrl($this->domain->assetNamespace(), 'wp-plugins-page'),
575
-            array(
576
-                CoreAssetManager::JS_HANDLE_JQUERY,
577
-                CoreAssetManager::JS_HANDLE_VENDOR,
578
-            )
579
-        )
580
-        ->setRequiresTranslation();
581
-
582
-        $this->addStylesheet(
583
-            CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE,
584
-            $this->registry->getCssUrl($this->domain->assetNamespace(), 'wp-plugins-page')
585
-        );
586
-    }
85
+	const JS_HANDLE_I18N = 'eei18n';
86
+
87
+	const JS_HANDLE_ACCOUNTING = 'ee-accounting';
88
+
89
+	const JS_HANDLE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page';
90
+
91
+	// EE CSS assets handles
92
+	const CSS_HANDLE_DEFAULT = 'espresso_default';
93
+
94
+	const CSS_HANDLE_CUSTOM = 'espresso_custom_css';
95
+
96
+	const CSS_HANDLE_COMPONENTS = 'eventespresso-components';
97
+
98
+	/**
99
+	 * @var EE_Currency_Config $currency_config
100
+	 */
101
+	protected $currency_config;
102
+
103
+	/**
104
+	 * @var EE_Template_Config $template_config
105
+	 */
106
+	protected $template_config;
107
+
108
+
109
+	/**
110
+	 * CoreAssetRegister constructor.
111
+	 *
112
+	 * @param AssetCollection    $assets
113
+	 * @param EE_Currency_Config $currency_config
114
+	 * @param EE_Template_Config $template_config
115
+	 * @param DomainInterface    $domain
116
+	 * @param Registry           $registry
117
+	 */
118
+	public function __construct(
119
+		AssetCollection $assets,
120
+		EE_Currency_Config $currency_config,
121
+		EE_Template_Config $template_config,
122
+		DomainInterface $domain,
123
+		Registry $registry
124
+	) {
125
+		$this->currency_config = $currency_config;
126
+		$this->template_config = $template_config;
127
+		parent::__construct($domain, $assets, $registry);
128
+	}
129
+
130
+
131
+	/**
132
+	 * @since 4.9.62.p
133
+	 * @throws DomainException
134
+	 * @throws DuplicateCollectionIdentifierException
135
+	 * @throws InvalidArgumentException
136
+	 * @throws InvalidDataTypeException
137
+	 * @throws InvalidEntityException
138
+	 * @throws InvalidInterfaceException
139
+	 */
140
+	public function addAssets()
141
+	{
142
+		$this->addJavascriptFiles();
143
+		$this->addStylesheetFiles();
144
+	}
145
+
146
+
147
+	/**
148
+	 * @since 4.9.62.p
149
+	 * @throws DomainException
150
+	 * @throws DuplicateCollectionIdentifierException
151
+	 * @throws InvalidArgumentException
152
+	 * @throws InvalidDataTypeException
153
+	 * @throws InvalidEntityException
154
+	 * @throws InvalidInterfaceException
155
+	 */
156
+	public function addJavascriptFiles()
157
+	{
158
+		$this->loadCoreJs();
159
+		$this->loadJqueryValidate();
160
+		$this->loadAccountingJs();
161
+		add_action(
162
+			'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
163
+			array($this, 'loadQtipJs')
164
+		);
165
+		$this->registerAdminAssets();
166
+	}
167
+
168
+
169
+	/**
170
+	 * @since 4.9.62.p
171
+	 * @throws DuplicateCollectionIdentifierException
172
+	 * @throws InvalidDataTypeException
173
+	 * @throws InvalidEntityException
174
+	 */
175
+	public function addStylesheetFiles()
176
+	{
177
+		$this->loadCoreCss();
178
+	}
179
+
180
+
181
+	/**
182
+	 * core default javascript
183
+	 *
184
+	 * @since 4.9.62.p
185
+	 * @throws DomainException
186
+	 * @throws DuplicateCollectionIdentifierException
187
+	 * @throws InvalidArgumentException
188
+	 * @throws InvalidDataTypeException
189
+	 * @throws InvalidEntityException
190
+	 * @throws InvalidInterfaceException
191
+	 */
192
+	private function loadCoreJs()
193
+	{
194
+		// conditionally load third-party libraries that WP core MIGHT have.
195
+		$this->registerWpAssets();
196
+
197
+		$this->addJavascript(
198
+			CoreAssetManager::JS_HANDLE_MANIFEST,
199
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'manifest')
200
+		);
201
+
202
+		$this->addJavascript(
203
+			CoreAssetManager::JS_HANDLE_JS_CORE,
204
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'eejs'),
205
+			array(CoreAssetManager::JS_HANDLE_MANIFEST)
206
+		)
207
+		->setHasInlineData();
208
+
209
+		$this->addJavascript(
210
+			CoreAssetManager::JS_HANDLE_VENDOR,
211
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'vendor'),
212
+			array(
213
+				CoreAssetManager::JS_HANDLE_JS_CORE,
214
+				CoreAssetManager::JS_HANDLE_REACT,
215
+				CoreAssetManager::JS_HANDLE_REACT_DOM,
216
+				CoreAssetManager::JS_HANDLE_LODASH,
217
+			)
218
+		);
219
+
220
+		$this->addJavascript(
221
+			CoreAssetManager::JS_HANDLE_VALIDATORS,
222
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'validators')
223
+		)->setRequiresTranslation();
224
+
225
+		$this->addJavascript(
226
+			CoreAssetManager::JS_HANDLE_HELPERS,
227
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'helpers'),
228
+			array(
229
+				CoreAssetManager::JS_HANDLE_VALIDATORS
230
+			)
231
+		)->setRequiresTranslation();
232
+
233
+		$this->addJavascript(
234
+			CoreAssetManager::JS_HANDLE_MODEL,
235
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'model'),
236
+			array(
237
+				CoreAssetManager::JS_HANDLE_HELPERS,
238
+				CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
239
+			)
240
+		)->setRequiresTranslation();
241
+
242
+		$this->addJavascript(
243
+			CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
244
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'valueObjects'),
245
+			array(
246
+				CoreAssetManager::JS_HANDLE_VALIDATORS,
247
+				CoreAssetManager::JS_HANDLE_HELPERS,
248
+			)
249
+		)->setRequiresTranslation();
250
+
251
+		$this->addJavascript(
252
+			CoreAssetManager::JS_HANDLE_DATA_STORES,
253
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'data-stores'),
254
+			array(
255
+				CoreAssetManager::JS_HANDLE_VENDOR,
256
+				'wp-data',
257
+				'wp-api-fetch',
258
+				CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
259
+				CoreAssetManager::JS_HANDLE_MODEL,
260
+			)
261
+		)
262
+			 ->setRequiresTranslation()
263
+			 ->setInlineDataCallback(
264
+				 function() {
265
+					 wp_add_inline_script(
266
+						 CoreAssetManager::JS_HANDLE_DATA_STORES,
267
+						 is_admin()
268
+							 ? 'wp.apiFetch.use( eejs.middleWares.apiFetch.capsMiddleware( eejs.middleWares.apiFetch.CONTEXT_CAPS_EDIT ) )'
269
+							 : 'wp.apiFetch.use( eejs.middleWares.apiFetch.capsMiddleware )'
270
+					 );
271
+				 }
272
+			 );
273
+
274
+		$this->addJavascript(
275
+			CoreAssetManager::JS_HANDLE_HOCS,
276
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'hocs'),
277
+			array(
278
+				CoreAssetManager::JS_HANDLE_DATA_STORES,
279
+				CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
280
+				'wp-components',
281
+			)
282
+		)->setRequiresTranslation();
283
+
284
+		$this->addJavascript(
285
+			CoreAssetManager::JS_HANDLE_COMPONENTS,
286
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'components'),
287
+			array(
288
+				CoreAssetManager::JS_HANDLE_DATA_STORES,
289
+				CoreAssetManager::JS_HANDLE_VALUE_OBJECTS,
290
+				'wp-components',
291
+			)
292
+		)
293
+		->setRequiresTranslation();
294
+
295
+		$this->addJavascript(
296
+			CoreAssetManager::JS_HANDLE_EDITOR_HOCS,
297
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'editor-hocs'),
298
+			array(
299
+				CoreAssetManager::JS_HANDLE_COMPONENTS
300
+			)
301
+		)->setRequiresTranslation();
302
+
303
+		$this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest'));
304
+		$this->registry->addData(
305
+			'paths',
306
+			array(
307
+				'base_rest_route' => rest_url(),
308
+				'rest_route' => rest_url('ee/v4.8.36/'),
309
+				'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(),
310
+				'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName(),
311
+				'site_url' => site_url('/'),
312
+				'admin_url' => admin_url('/'),
313
+			)
314
+		);
315
+		/** site formatting values **/
316
+		$this->registry->addData(
317
+			'site_formats',
318
+			array(
319
+				'date_formats' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats()
320
+			)
321
+		);
322
+		/** currency data **/
323
+		$this->registry->addData(
324
+			'currency_config',
325
+			$this->getCurrencySettings()
326
+		);
327
+		/** site timezone */
328
+		$this->registry->addData(
329
+			'default_timezone',
330
+			array(
331
+				'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(),
332
+				'string' => get_option('timezone_string'),
333
+				'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(),
334
+			)
335
+		);
336
+		/** site locale (user locale if user logged in) */
337
+		$this->registry->addData(
338
+			'locale',
339
+			array(
340
+				'user' => get_user_locale(),
341
+				'site' => get_locale()
342
+			)
343
+		);
344
+
345
+		$this->addJavascript(
346
+			CoreAssetManager::JS_HANDLE_CORE,
347
+			EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js',
348
+			array(CoreAssetManager::JS_HANDLE_JQUERY)
349
+		)
350
+		->setInlineDataCallback(
351
+			function () {
352
+				wp_localize_script(
353
+					CoreAssetManager::JS_HANDLE_CORE,
354
+					CoreAssetManager::JS_HANDLE_I18N,
355
+					EE_Registry::$i18n_js_strings
356
+				);
357
+			}
358
+		);
359
+	}
360
+
361
+
362
+	/**
363
+	 * Registers vendor files that are bundled with a later version WP but might not be for the current version of
364
+	 * WordPress in the running environment.
365
+	 *
366
+	 * @throws DuplicateCollectionIdentifierException
367
+	 * @throws InvalidDataTypeException
368
+	 * @throws InvalidEntityException
369
+	 * @throws DomainException
370
+	 * @since 4.9.71.p
371
+	 */
372
+	private function registerWpAssets()
373
+	{
374
+		global $wp_version;
375
+		if (version_compare($wp_version, '5.0.beta', '>=')) {
376
+			return;
377
+		}
378
+		$this->addVendorJavascript(CoreAssetManager::JS_HANDLE_REACT)
379
+			->setVersion('16.6.0');
380
+		$this->addVendorJavascript(
381
+			CoreAssetManager::JS_HANDLE_REACT_DOM,
382
+			array(CoreAssetManager::JS_HANDLE_REACT)
383
+		)->setVersion('16.6.0');
384
+		$this->addVendorJavascript(CoreAssetManager::JS_HANDLE_LODASH)
385
+			->setInlineDataCallback(
386
+				function() {
387
+					wp_add_inline_script(
388
+						CoreAssetManager::JS_HANDLE_LODASH,
389
+						'window.lodash = _.noConflict();'
390
+					);
391
+				}
392
+			)
393
+			->setVersion('4.17.11');
394
+	}
395
+
396
+
397
+	/**
398
+	 * Returns configuration data for the accounting-js library.
399
+	 * @since 4.9.71.p
400
+	 * @return array
401
+	 */
402
+	private function getAccountingSettings() {
403
+		return array(
404
+			'currency' => array(
405
+				'symbol'    => $this->currency_config->sign,
406
+				'format'    => array(
407
+					'pos'  => $this->currency_config->sign_b4 ? '%s%v' : '%v%s',
408
+					'neg'  => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s',
409
+					'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s',
410
+				),
411
+				'decimal'   => $this->currency_config->dec_mrk,
412
+				'thousand'  => $this->currency_config->thsnds,
413
+				'precision' => $this->currency_config->dec_plc,
414
+			),
415
+			'number'   => array(
416
+				'precision' => $this->currency_config->dec_plc,
417
+				'thousand'  => $this->currency_config->thsnds,
418
+				'decimal'   => $this->currency_config->dec_mrk,
419
+			),
420
+		);
421
+	}
422
+
423
+
424
+	/**
425
+	 * Returns configuration data for the js Currency VO.
426
+	 * @since 4.9.71.p
427
+	 * @return array
428
+	 */
429
+	private function getCurrencySettings()
430
+	{
431
+		return array(
432
+			'code' => $this->currency_config->code,
433
+			'singularLabel' => $this->currency_config->name,
434
+			'pluralLabel' => $this->currency_config->plural,
435
+			'sign' => $this->currency_config->sign,
436
+			'signB4' => $this->currency_config->sign_b4,
437
+			'decimalPlaces' => $this->currency_config->dec_plc,
438
+			'decimalMark' => $this->currency_config->dec_mrk,
439
+			'thousandsSeparator' => $this->currency_config->thsnds,
440
+		);
441
+	}
442
+
443
+
444
+	/**
445
+	 * @since 4.9.62.p
446
+	 * @throws DuplicateCollectionIdentifierException
447
+	 * @throws InvalidDataTypeException
448
+	 * @throws InvalidEntityException
449
+	 */
450
+	private function loadCoreCss()
451
+	{
452
+		if ($this->template_config->enable_default_style && ! is_admin()) {
453
+			$this->addStylesheet(
454
+				CoreAssetManager::CSS_HANDLE_DEFAULT,
455
+				is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css')
456
+					? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css'
457
+					: EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css',
458
+				array('dashicons')
459
+			);
460
+			//Load custom style sheet if available
461
+			if ($this->template_config->custom_style_sheet !== null) {
462
+				$this->addStylesheet(
463
+					CoreAssetManager::CSS_HANDLE_CUSTOM,
464
+					EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet,
465
+					array(CoreAssetManager::CSS_HANDLE_DEFAULT)
466
+				);
467
+			}
468
+		}
469
+		$this->addStylesheet(
470
+			CoreAssetManager::CSS_HANDLE_COMPONENTS,
471
+			$this->registry->getCssUrl(
472
+				$this->domain->assetNamespace(),
473
+				'components'
474
+			)
475
+		);
476
+	}
477
+
478
+
479
+	/**
480
+	 * jQuery Validate for form validation
481
+	 *
482
+	 * @since 4.9.62.p
483
+	 * @throws DomainException
484
+	 * @throws DuplicateCollectionIdentifierException
485
+	 * @throws InvalidDataTypeException
486
+	 * @throws InvalidEntityException
487
+	 */
488
+	private function loadJqueryValidate()
489
+	{
490
+		$this->addJavascript(
491
+			CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE,
492
+			EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js',
493
+			array(CoreAssetManager::JS_HANDLE_JQUERY)
494
+		)
495
+		->setVersion('1.15.0');
496
+
497
+		$this->addJavascript(
498
+			CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA,
499
+			EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js',
500
+			array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE)
501
+		)
502
+		->setVersion('1.15.0');
503
+	}
504
+
505
+
506
+	/**
507
+	 * accounting.js for performing client-side calculations
508
+	 *
509
+	 * @since 4.9.62.p
510
+	 * @throws DomainException
511
+	 * @throws DuplicateCollectionIdentifierException
512
+	 * @throws InvalidDataTypeException
513
+	 * @throws InvalidEntityException
514
+	 */
515
+	private function loadAccountingJs()
516
+	{
517
+		//accounting.js library
518
+		// @link http://josscrowcroft.github.io/accounting.js/
519
+		$this->addJavascript(
520
+			CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE,
521
+			EE_THIRD_PARTY_URL . 'accounting/accounting.js',
522
+			array(CoreAssetManager::JS_HANDLE_UNDERSCORE)
523
+		)
524
+		->setVersion('0.3.2');
525
+
526
+		$this->addJavascript(
527
+			CoreAssetManager::JS_HANDLE_ACCOUNTING,
528
+			EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js',
529
+			array(CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE)
530
+		)
531
+		->setInlineDataCallback(
532
+			function () {
533
+				 wp_localize_script(
534
+					 CoreAssetManager::JS_HANDLE_ACCOUNTING,
535
+					 'EE_ACCOUNTING_CFG',
536
+					 $this->getAccountingSettings()
537
+				 );
538
+			}
539
+		)
540
+		->setVersion();
541
+	}
542
+
543
+
544
+	/**
545
+	 * registers assets for cleaning your ears
546
+	 *
547
+	 * @param JavascriptAsset $script
548
+	 */
549
+	public function loadQtipJs(JavascriptAsset $script)
550
+	{
551
+		// qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook,
552
+		// can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' );
553
+		if (
554
+			$script->handle() === CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE
555
+			&& apply_filters('FHEE_load_qtip', false)
556
+		) {
557
+			EEH_Qtip_Loader::instance()->register_and_enqueue();
558
+		}
559
+	}
560
+
561
+
562
+	/**
563
+	 * assets that are used in the WordPress admin
564
+	 *
565
+	 * @since 4.9.62.p
566
+	 * @throws DuplicateCollectionIdentifierException
567
+	 * @throws InvalidDataTypeException
568
+	 * @throws InvalidEntityException
569
+	 */
570
+	private function registerAdminAssets()
571
+	{
572
+		$this->addJavascript(
573
+			CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE,
574
+			$this->registry->getJsUrl($this->domain->assetNamespace(), 'wp-plugins-page'),
575
+			array(
576
+				CoreAssetManager::JS_HANDLE_JQUERY,
577
+				CoreAssetManager::JS_HANDLE_VENDOR,
578
+			)
579
+		)
580
+		->setRequiresTranslation();
581
+
582
+		$this->addStylesheet(
583
+			CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE,
584
+			$this->registry->getCssUrl($this->domain->assetNamespace(), 'wp-plugins-page')
585
+		);
586
+	}
587 587
 }
Please login to merge, or discard this patch.