Completed
Branch Gutenberg/master (ddf824)
by
unknown
52:49 queued 39:46
created

CoreAssetManager::getAccountingSettings()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\assets;
4
5
use EE_Currency_Config;
6
use EE_Registry;
7
use EE_Template_Config;
8
use EED_Core_Rest_Api;
9
use EEH_DTT_Helper;
10
use EEH_Qtip_Loader;
11
use EventEspresso\core\domain\DomainInterface;
12
use EventEspresso\core\domain\values\assets\JavascriptAsset;
13
use EventEspresso\core\exceptions\InvalidDataTypeException;
14
use EventEspresso\core\exceptions\InvalidEntityException;
15
use EventEspresso\core\services\assets\AssetCollection;
16
use EventEspresso\core\services\assets\AssetManager;
17
use EventEspresso\core\services\assets\Registry;
18
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
19
use InvalidArgumentException;
20
21
/**
22
 * Class CoreAssetManager
23
 * Manager class for for Event Espresso core assets
24
 *
25
 * @package EventEspresso\core\services\assets
26
 * @author  Brent Christensen
27
 * @since   4.9.62.p
28
 */
29
class CoreAssetManager extends AssetManager
30
{
31
32
    // WordPress core / Third party JS asset handles
33
    const JS_HANDLE_JQUERY = 'jquery';
34
35
    const JS_HANDLE_JQUERY_VALIDATE = 'jquery-validate';
36
37
    const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods';
38
39
    const JS_HANDLE_UNDERSCORE = 'underscore';
40
41
    const JS_HANDLE_ACCOUNTING_CORE = 'ee-accounting-core';
42
43
    // EE JS assets handles
44
    const JS_HANDLE_EE_MANIFEST = 'ee-manifest';
45
46
    const JS_HANDLE_EE_JS_CORE = 'eejs-core';
47
48
    const JS_HANDLE_EE_VENDOR = 'eventespresso-vendor';
49
50
    const JS_HANDLE_EE_DATA_STORES = 'eventespresso-data-stores';
51
52
    const JS_HANDLE_EE_HELPERS = 'eventespresso-helpers';
53
54
    const JS_HANDLE_EE_MODEL = 'eventespresso-model';
55
56
    const JS_HANDLE_EE_VALUE_OBJECTS = 'eventespresso-value-objects';
57
58
    const JS_HANDLE_EE_HOC_COMPONENTS = 'eventespresso-hoc-components';
59
60
    const JS_HANDLE_EE_COMPONENTS = 'eventespresso-components';
61
62
    const JS_HANDLE_EE_JS_API = 'eejs-api';
63
64
    const JS_HANDLE_EE_CORE = 'espresso_core';
65
66
    const JS_HANDLE_EE_I18N = 'eei18n';
67
68
    const JS_HANDLE_EE_ACCOUNTING = 'ee-accounting';
69
70
    const JS_HANDLE_EE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page';
71
72
    // EE CSS assets handles
73
    const CSS_HANDLE_EE_DEFAULT = 'espresso_default';
74
75
    const CSS_HANDLE_EE_CUSTOM = 'espresso_custom_css';
76
77
    const CSS_HANDLE_EE_COMPONENTS = 'eventespresso-components';
78
79
    /**
80
     * @var EE_Currency_Config $currency_config
81
     */
82
    protected $currency_config;
83
84
    /**
85
     * @var EE_Template_Config $template_config
86
     */
87
    protected $template_config;
88
89
90
    /**
91
     * CoreAssetRegister constructor.
92
     *
93
     * @param AssetCollection    $assets
94
     * @param EE_Currency_Config $currency_config
95
     * @param EE_Template_Config $template_config
96
     * @param DomainInterface    $domain
97
     * @param Registry           $registry
98
     */
99
    public function __construct(
100
        AssetCollection $assets,
101
        EE_Currency_Config $currency_config,
102
        EE_Template_Config $template_config,
103
        DomainInterface $domain,
104
        Registry $registry
105
    ) {
106
        $this->currency_config = $currency_config;
107
        $this->template_config = $template_config;
108
        parent::__construct($domain, $assets, $registry);
109
    }
110
111
112
    /**
113
     * @since 4.9.62.p
114
     * @throws DuplicateCollectionIdentifierException
115
     * @throws InvalidArgumentException
116
     * @throws InvalidDataTypeException
117
     * @throws InvalidEntityException
118
     */
119
    public function addAssets()
120
    {
121
        $this->addJavascriptFiles();
122
        $this->addStylesheetFiles();
123
    }
124
125
126
    /**
127
     * @since 4.9.62.p
128
     * @throws DuplicateCollectionIdentifierException
129
     * @throws InvalidArgumentException
130
     * @throws InvalidDataTypeException
131
     * @throws InvalidEntityException
132
     */
133
    public function addJavascriptFiles()
134
    {
135
        $this->loadCoreJs();
136
        $this->loadJqueryValidate();
137
        $this->loadAccountingJs();
138
        add_action(
139
            'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
140
            array($this, 'loadQtipJs')
141
        );
142
        $this->registerAdminAssets();
143
    }
144
145
146
    /**
147
     * @since 4.9.62.p
148
     * @throws DuplicateCollectionIdentifierException
149
     * @throws InvalidDataTypeException
150
     * @throws InvalidEntityException
151
     */
152
    public function addStylesheetFiles()
153
    {
154
        $this->loadCoreCss();
155
    }
156
157
158
    /**
159
     * core default javascript
160
     *
161
     * @since 4.9.62.p
162
     * @throws DuplicateCollectionIdentifierException
163
     * @throws InvalidArgumentException
164
     * @throws InvalidDataTypeException
165
     * @throws InvalidEntityException
166
     */
167
    private function loadCoreJs()
168
    {
169
        $this->addJavascript(
170
            CoreAssetManager::JS_HANDLE_EE_MANIFEST,
171
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'manifest')
172
        );
173
174
        $this->addJavascript(
175
            CoreAssetManager::JS_HANDLE_EE_JS_CORE,
176
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'eejs'),
177
            array(CoreAssetManager::JS_HANDLE_EE_MANIFEST)
178
        )
179
        ->setHasInlineData();
180
181
        $this->addJavascript(
182
            CoreAssetManager::JS_HANDLE_EE_VENDOR,
183
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'vendor'),
184
            array(CoreAssetManager::JS_HANDLE_EE_JS_CORE)
185
        );
186
187
        $this->addJavascript(
188
            CoreAssetManager::JS_HANDLE_EE_DATA_STORES,
189
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'data-stores'),
190
            array(CoreAssetManager::JS_HANDLE_EE_VENDOR, 'wp-data', 'wp-api-request')
191
        )
192
        ->setRequiresTranslation();
193
194
        $this->addJavascript(
195
            CoreAssetManager::JS_HANDLE_EE_HELPERS,
196
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'helpers')
197
        )->setRequiresTranslation();
198
199
        $this->addJavascript(
200
            CoreAssetManager::JS_HANDLE_EE_MODEL,
201
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'model'),
202
            array(
203
                CoreAssetManager::JS_HANDLE_EE_HELPERS
204
            )
205
        )->setRequiresTranslation();
206
207
        $this->addJavascript(
208
            CoreAssetManager::JS_HANDLE_EE_VALUE_OBJECTS,
209
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'valueObjects'),
210
            array(
211
                CoreAssetManager::JS_HANDLE_EE_MODEL
212
            )
213
        )->setRequiresTranslation();
214
215
        $this->addJavascript(
216
            CoreAssetManager::JS_HANDLE_EE_HOC_COMPONENTS,
217
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'hocComponents'),
218
            array(
219
                CoreAssetManager::JS_HANDLE_EE_DATA_STORES,
220
                CoreAssetManager::JS_HANDLE_EE_VALUE_OBJECTS,
221
                'wp-components',
222
            )
223
        )->setRequiresTranslation();
224
225
        $this->addJavascript(
226
            CoreAssetManager::JS_HANDLE_EE_COMPONENTS,
227
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'components'),
228
            array(
229
                CoreAssetManager::JS_HANDLE_EE_DATA_STORES,
230
                CoreAssetManager::JS_HANDLE_EE_VALUE_OBJECTS,
231
                'wp-components',
232
            )
233
        )
234
        ->setRequiresTranslation();
235
236
        global $wp_version;
237
        if (version_compare($wp_version, '4.4.0', '>')) {
238
            //js.api
239
            $this->addJavascript(
240
                CoreAssetManager::JS_HANDLE_EE_JS_API,
241
                EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js',
242
                array(
243
                    CoreAssetManager::JS_HANDLE_UNDERSCORE,
244
                    CoreAssetManager::JS_HANDLE_EE_JS_CORE
245
                )
246
            );
247
            $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest'));
248
            $this->registry->addData(
249
                'paths',
250
                array(
251
                    'rest_route' => rest_url('ee/v4.8.36/'),
252
                    'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(),
253
                    'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName(),
254
					'site_url' => site_url('/'),
255
                    'admin_url' => admin_url('/'),
256
                )
257
            );
258
            /** site formatting values **/
259
            $this->registry->addData(
260
                'site_formats',
261
                array(
262
                    'date_formats' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats()
263
                )
264
            );
265
            /** currency data **/
266
            $this->registry->addData(
267
                'currency_config',
268
                $this->getCurrencySettings()
269
            );
270
        }
271
272
        $this->addJavascript(
273
            CoreAssetManager::JS_HANDLE_EE_CORE,
274
            EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js',
275
            array(CoreAssetManager::JS_HANDLE_JQUERY)
276
        )
277
        ->setInlineDataCallback(
278
            function () {
279
                wp_localize_script(
280
                    CoreAssetManager::JS_HANDLE_EE_CORE,
281
                    CoreAssetManager::JS_HANDLE_EE_I18N,
282
                    EE_Registry::$i18n_js_strings
283
                );
284
            }
285
        );
286
    }
287
288
289
    /**
290
     * Returns configuration data for the accounting-js library.
291
     * @since $VID:$
292
     * @return array
293
     */
294
    private function getAccountingSettings() {
295
        return array(
296
            'currency' => array(
297
                'symbol'    => $this->currency_config->sign,
298
                'format'    => array(
299
                    'pos'  => $this->currency_config->sign_b4 ? '%s%v' : '%v%s',
300
                    'neg'  => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s',
301
                    'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s',
302
                ),
303
                'decimal'   => $this->currency_config->dec_mrk,
304
                'thousand'  => $this->currency_config->thsnds,
305
                'precision' => $this->currency_config->dec_plc,
306
            ),
307
            'number'   => array(
308
                'precision' => $this->currency_config->dec_plc,
309
                'thousand'  => $this->currency_config->thsnds,
310
                'decimal'   => $this->currency_config->dec_mrk,
311
            ),
312
        );
313
    }
314
315
316
    /**
317
     * Returns configuration data for the js Currency VO.
318
     * @sinc $VID:$
319
     * @return array
320
     */
321
    private function getCurrencySettings()
322
    {
323
        return array(
324
            'code' => $this->currency_config->code,
325
            'singularLabel' => $this->currency_config->name,
326
            'pluralLabel' => $this->currency_config->plural,
327
            'sign' => $this->currency_config->sign,
328
            'signB4' => $this->currency_config->sign_b4,
329
            'decimalPlaces' => $this->currency_config->dec_plc,
330
            'decimalMark' => $this->currency_config->dec_mrk,
331
            'thousandsSeparator' => $this->currency_config->thsnds,
332
        );
333
    }
334
335
336
    /**
337
     * @since 4.9.62.p
338
     * @throws DuplicateCollectionIdentifierException
339
     * @throws InvalidDataTypeException
340
     * @throws InvalidEntityException
341
     */
342
    private function loadCoreCss()
343
    {
344
        if ($this->template_config->enable_default_style && ! is_admin()) {
345
            $this->addStylesheet(
346
                CoreAssetManager::CSS_HANDLE_EE_DEFAULT,
347
                is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css')
348
                    ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css'
349
                    : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css',
350
                array('dashicons')
351
            );
352
            //Load custom style sheet if available
353
            if ($this->template_config->custom_style_sheet !== null) {
354
                $this->addStylesheet(
355
                    CoreAssetManager::CSS_HANDLE_EE_CUSTOM,
356
                    EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet,
357
                    array(CoreAssetManager::CSS_HANDLE_EE_DEFAULT)
358
                );
359
            }
360
        }
361
        $this->addStylesheet(
362
            CoreAssetManager::CSS_HANDLE_EE_COMPONENTS,
363
            $this->registry->getCssUrl(
364
                $this->domain->assetNamespace(),
365
                'components'
366
            )
367
        );
368
    }
369
370
371
    /**
372
     * jQuery Validate for form validation
373
     *
374
     * @since 4.9.62.p
375
     * @throws DuplicateCollectionIdentifierException
376
     * @throws InvalidDataTypeException
377
     * @throws InvalidEntityException
378
     */
379
    private function loadJqueryValidate()
380
    {
381
        $this->addJavascript(
382
            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE,
383
            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js',
384
            array(CoreAssetManager::JS_HANDLE_JQUERY)
385
        )
386
        ->setVersion('1.15.0');
387
388
        $this->addJavascript(
389
            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA,
390
            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js',
391
            array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE)
392
        )
393
        ->setVersion('1.15.0');
394
    }
395
396
397
    /**
398
     * accounting.js for performing client-side calculations
399
     *
400
     * @since 4.9.62.p
401
     * @throws DuplicateCollectionIdentifierException
402
     * @throws InvalidDataTypeException
403
     * @throws InvalidEntityException
404
     */
405
    private function loadAccountingJs()
406
    {
407
        //accounting.js library
408
        // @link http://josscrowcroft.github.io/accounting.js/
409
        $this->addJavascript(
410
            CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE,
411
            EE_THIRD_PARTY_URL . 'accounting/accounting.js',
412
            array(CoreAssetManager::JS_HANDLE_UNDERSCORE)
413
        )
414
        ->setVersion('0.3.2');
415
416
        $this->addJavascript(
417
            CoreAssetManager::JS_HANDLE_EE_ACCOUNTING,
418
            EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js',
419
            array(CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE)
420
        )
421
        ->setInlineDataCallback(
422
            function () {
423
                 wp_localize_script(
424
                     CoreAssetManager::JS_HANDLE_EE_ACCOUNTING,
425
                     'EE_ACCOUNTING_CFG',
426
                     $this->getAccountingSettings()
427
                 );
428
            }
429
        )
430
        ->setVersion();
431
    }
432
433
434
    /**
435
     * registers assets for cleaning your ears
436
     *
437
     * @param JavascriptAsset $script
438
     */
439
    public function loadQtipJs(JavascriptAsset $script)
440
    {
441
        // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook,
442
        // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' );
443
        if (
444
            $script->handle() === CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE
445
            && apply_filters('FHEE_load_qtip', false)
446
        ) {
447
            EEH_Qtip_Loader::instance()->register_and_enqueue();
448
        }
449
    }
450
451
452
    /**
453
     * assets that are used in the WordPress admin
454
     *
455
     * @since 4.9.62.p
456
     * @throws DuplicateCollectionIdentifierException
457
     * @throws InvalidDataTypeException
458
     * @throws InvalidEntityException
459
     */
460
    private function registerAdminAssets()
461
    {
462
        $this->addJavascript(
463
            CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE,
464
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'wp-plugins-page'),
465
            array(
466
                CoreAssetManager::JS_HANDLE_JQUERY,
467
                CoreAssetManager::JS_HANDLE_EE_VENDOR,
468
            )
469
        )
470
        ->setRequiresTranslation();
471
472
        $this->addStylesheet(
473
            CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE,
474
            $this->registry->getCssUrl($this->domain->assetNamespace(), 'wp-plugins-page')
475
        );
476
    }
477
}
478