Completed
Branch GDPR/user-data-export (1838dd)
by
unknown
53:17 queued 40:13
created

CoreAssetManager::loadJqueryValidate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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