Completed
Branch Gutenberg/master (a6a049)
by
unknown
14:44
created

CoreAssetManager::loadQtipJs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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