Completed
Branch BUG/country-settings-fatal (64681e)
by
unknown
19:15 queued 09:28
created

CoreAssetManager::loadCoreJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

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