Completed
Branch Gutenberg/master (eed722)
by
unknown
78:41 queued 66:19
created

CoreAssetManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 336
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 336
rs 10
wmc 20
lcom 1
cbo 9

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A addAssets() 0 5 1
A addJavascriptFiles() 0 11 1
A addStylesheetFiles() 0 4 1
A loadCoreJs() 0 57 2
B loadCoreCss() 0 20 5
A loadJqueryValidate() 0 16 1
B loadAccountingJs() 0 45 4
A loadQtipJs() 0 11 3
A registerAdminAssets() 0 17 1
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_Qtip_Loader;
10
use EventEspresso\core\domain\DomainInterface;
11
use EventEspresso\core\domain\values\assets\JavascriptAsset;
12
use EventEspresso\core\exceptions\InvalidDataTypeException;
13
use EventEspresso\core\exceptions\InvalidEntityException;
14
use EventEspresso\core\services\assets\AssetCollection;
15
use EventEspresso\core\services\assets\AssetManager;
16
use EventEspresso\core\services\assets\Registry;
17
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
18
use InvalidArgumentException;
19
20
/**
21
 * Class CoreAssetManager
22
 * Manager class for for Event Espresso core assets
23
 *
24
 * @package EventEspresso\core\services\assets
25
 * @author  Brent Christensen
26
 * @since   $VID:$
27
 */
28
class CoreAssetManager extends AssetManager
29
{
30
31
    // WordPress core / Third party JS asset handles
32
    const JS_HANDLE_JQUERY                = 'jquery';
33
34
    const JS_HANDLE_JQUERY_VALIDATE       = 'jquery-validate';
35
36
    const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods';
37
38
    const JS_HANDLE_UNDERSCORE            = 'underscore';
39
40
    const JS_HANDLE_ACCOUNTING_CORE       = 'ee-accounting-core';
41
42
    // EE JS assets handles
43
    const JS_HANDLE_EE_MANIFEST        = 'ee-manifest';
44
45
    const JS_HANDLE_EE_JS_CORE         = 'eejs-core';
46
47
    const JS_HANDLE_EE_VENDOR_REACT    = 'ee-vendor-react';
48
49
    const JS_HANDLE_EE_JS_API          = 'eejs-api';
50
51
    const JS_HANDLE_EE_CORE            = 'espresso_core';
52
53
    const JS_HANDLE_EE_I18N            = 'eei18n';
54
55
    const JS_HANDLE_EE_ACCOUNTING      = 'ee-accounting';
56
57
    const JS_HANDLE_EE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page';
58
59
    // EE CSS assets handles
60
    const CSS_HANDLE_EE_DEFAULT = 'espresso_default';
61
62
    const CSS_HANDLE_EE_CUSTOM  = 'espresso_custom_css';
63
64
    /**
65
     * @var EE_Currency_Config $currency_config
66
     */
67
    protected $currency_config;
68
69
    /**
70
     * @var EE_Template_Config $template_config
71
     */
72
    protected $template_config;
73
74
75
    /**
76
     * CoreAssetRegister constructor.
77
     *
78
     * @param AssetCollection    $assets
79
     * @param EE_Currency_Config $currency_config
80
     * @param EE_Template_Config $template_config
81
     * @param DomainInterface    $domain
82
     * @param Registry           $registry
83
     */
84
    public function __construct(
85
        AssetCollection $assets,
86
        EE_Currency_Config $currency_config,
87
        EE_Template_Config $template_config,
88
        DomainInterface $domain,
89
        Registry $registry
90
    ) {
91
        $this->currency_config = $currency_config;
92
        $this->template_config = $template_config;
93
        parent::__construct($domain, $assets, $registry);
94
    }
95
96
97
    /**
98
     * @since $VID:$
99
     * @throws DuplicateCollectionIdentifierException
100
     * @throws InvalidArgumentException
101
     * @throws InvalidDataTypeException
102
     * @throws InvalidEntityException
103
     */
104
    public function addAssets()
105
    {
106
        $this->addJavascriptFiles();
107
        $this->addStylesheetFiles();
108
    }
109
110
111
    /**
112
     * @since $VID:$
113
     * @throws DuplicateCollectionIdentifierException
114
     * @throws InvalidArgumentException
115
     * @throws InvalidDataTypeException
116
     * @throws InvalidEntityException
117
     */
118
    public function addJavascriptFiles()
119
    {
120
        $this->loadCoreJs();
121
        $this->loadJqueryValidate();
122
        $this->loadAccountingJs();
123
        add_action(
124
            'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
125
            array($this, 'loadQtipJs')
126
        );
127
        $this->registerAdminAssets();
128
    }
129
130
131
    /**
132
     * @since $VID:$
133
     * @throws DuplicateCollectionIdentifierException
134
     * @throws InvalidDataTypeException
135
     * @throws InvalidEntityException
136
     */
137
    public function addStylesheetFiles()
138
    {
139
        $this->loadCoreCss();
140
    }
141
142
143
    /**
144
     * core default javascript
145
     *
146
     * @since $VID:$
147
     * @throws DuplicateCollectionIdentifierException
148
     * @throws InvalidArgumentException
149
     * @throws InvalidDataTypeException
150
     * @throws InvalidEntityException
151
     */
152
    private function loadCoreJs()
153
    {
154
        $this->addJavascript(
155
            CoreAssetManager::JS_HANDLE_EE_MANIFEST,
156
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'manifest')
157
        );
158
159
        $this->addJavascript(
160
            CoreAssetManager::JS_HANDLE_EE_JS_CORE,
161
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'eejs'),
162
            array(CoreAssetManager::JS_HANDLE_EE_MANIFEST)
163
        )
164
        ->setRequiresTranslation()
165
        ->setHasLocalizedData();
166
167
        $this->addJavascript(
168
            CoreAssetManager::JS_HANDLE_EE_VENDOR_REACT,
169
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'reactVendor'),
170
            array(CoreAssetManager::JS_HANDLE_EE_JS_CORE)
171
        );
172
173
        global $wp_version;
174
        if (version_compare($wp_version, '4.4.0', '>')) {
175
            //js.api
176
            $this->addJavascript(
177
                CoreAssetManager::JS_HANDLE_EE_JS_API,
178
                EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js',
179
                array(
180
                    CoreAssetManager::JS_HANDLE_UNDERSCORE,
181
                    CoreAssetManager::JS_HANDLE_EE_JS_CORE
182
                )
183
            );
184
            $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest'));
185
            $this->registry->addData(
186
                'paths',
187
                array(
188
                    'rest_route' => rest_url('ee/v4.8.36/'),
189
                    'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(),
190
                )
191
            );
192
        }
193
194
        $this->addJavascript(
195
            CoreAssetManager::JS_HANDLE_EE_CORE,
196
            EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js',
197
            array(CoreAssetManager::JS_HANDLE_JQUERY)
198
        )
199
        ->setLocalizationCallback(
200
            function () {
201
                wp_localize_script(
202
                    CoreAssetManager::JS_HANDLE_EE_CORE,
203
                    CoreAssetManager::JS_HANDLE_EE_I18N,
204
                    EE_Registry::$i18n_js_strings
205
                );
206
            }
207
        );
208
    }
209
210
211
    /**
212
     * @since $VID:$
213
     * @throws DuplicateCollectionIdentifierException
214
     * @throws InvalidDataTypeException
215
     * @throws InvalidEntityException
216
     */
217
    private function loadCoreCss()
218
    {
219
        if ($this->template_config->enable_default_style && ! is_admin()) {
220
            $this->addStylesheet(
221
                CoreAssetManager::CSS_HANDLE_EE_DEFAULT,
222
                is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css')
223
                    ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css'
224
                    : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css',
225
                array('dashicons')
226
            );
227
            //Load custom style sheet if available
228
            if ($this->template_config->custom_style_sheet !== null) {
229
                $this->addStylesheet(
230
                    CoreAssetManager::CSS_HANDLE_EE_CUSTOM,
231
                    EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet,
232
                    array(CoreAssetManager::CSS_HANDLE_EE_DEFAULT)
233
                );
234
            }
235
        }
236
    }
237
238
239
    /**
240
     * jQuery Validate for form validation
241
     *
242
     * @since $VID:$
243
     * @throws DuplicateCollectionIdentifierException
244
     * @throws InvalidDataTypeException
245
     * @throws InvalidEntityException
246
     */
247
    private function loadJqueryValidate()
248
    {
249
        $this->addJavascript(
250
            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE,
251
            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js',
252
            array(CoreAssetManager::JS_HANDLE_JQUERY)
253
        )
254
        ->setVersion('1.15.0');
255
256
        $this->addJavascript(
257
            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA,
258
            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js',
259
            array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE)
260
        )
261
        ->setVersion('1.15.0');
262
    }
263
264
265
    /**
266
     * accounting.js for performing client-side calculations
267
     *
268
     * @since $VID:$
269
     * @throws DuplicateCollectionIdentifierException
270
     * @throws InvalidDataTypeException
271
     * @throws InvalidEntityException
272
     */
273
    private function loadAccountingJs()
274
    {
275
        //accounting.js library
276
        // @link http://josscrowcroft.github.io/accounting.js/
277
        $this->addJavascript(
278
            CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE,
279
            EE_THIRD_PARTY_URL . 'accounting/accounting.js',
280
            array(CoreAssetManager::JS_HANDLE_UNDERSCORE)
281
        )
282
        ->setVersion('0.3.2');
283
284
        $currency_config = $this->currency_config;
285
        $this->addJavascript(
286
            CoreAssetManager::JS_HANDLE_EE_ACCOUNTING,
287
            EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js',
288
            array(CoreAssetManager::JS_HANDLE_ACCOUNTING_CORE)
289
        )
290
        ->setLocalizationCallback(
291
            function () use ($currency_config) {
292
                 wp_localize_script(
293
                     CoreAssetManager::JS_HANDLE_EE_ACCOUNTING,
294
                     'EE_ACCOUNTING_CFG',
295
                     array(
296
                         'currency' => array(
297
                             'symbol'    => $currency_config->sign,
298
                             'format'    => array(
299
                                 'pos'  => $currency_config->sign_b4 ? '%s%v' : '%v%s',
300
                                 'neg'  => $currency_config->sign_b4 ? '- %s%v' : '- %v%s',
301
                                 'zero' => $currency_config->sign_b4 ? '%s--' : '--%s',
302
                             ),
303
                             'decimal'   => $currency_config->dec_mrk,
304
                             'thousand'  => $currency_config->thsnds,
305
                             'precision' => $currency_config->dec_plc,
306
                         ),
307
                         'number'   => array(
308
                             'precision' => $currency_config->dec_plc,
309
                             'thousand'  => $currency_config->thsnds,
310
                             'decimal'   => $currency_config->dec_mrk,
311
                         ),
312
                     )
313
                 );
314
            }
315
        )
316
        ->setVersion();
317
    }
318
319
320
    /**
321
     * registers assets for cleaning your ears
322
     *
323
     * @param JavascriptAsset $script
324
     */
325
    public function loadQtipJs(JavascriptAsset $script)
326
    {
327
        // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook,
328
        // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' );
329
        if (
330
            $script->handle() === CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE
331
            && apply_filters('FHEE_load_qtip', false)
332
        ) {
333
            EEH_Qtip_Loader::instance()->register_and_enqueue();
334
        }
335
    }
336
337
338
    /**
339
     * assets that are used in the WordPress admin
340
     *
341
     * @since $VID:$
342
     * @throws DuplicateCollectionIdentifierException
343
     * @throws InvalidDataTypeException
344
     * @throws InvalidEntityException
345
     */
346
    private function registerAdminAssets()
347
    {
348
        $this->addJavascript(
349
            CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE,
350
            $this->registry->getJsUrl($this->domain->assetNamespace(), 'wp-plugins-page'),
351
            array(
352
                CoreAssetManager::JS_HANDLE_JQUERY,
353
                CoreAssetManager::JS_HANDLE_EE_VENDOR_REACT,
354
            )
355
        )
356
        ->setRequiresTranslation();
357
358
        $this->addStylesheet(
359
            CoreAssetManager::JS_HANDLE_EE_WP_PLUGINS_PAGE,
360
            $this->registry->getCssUrl($this->domain->assetNamespace(), 'wp-plugins-page')
361
        );
362
    }
363
}
364