Completed
Branch EDTR/master (dbc914)
by
unknown
09:15 queued 40s
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 DomainException;
6
use EE_Currency_Config;
7
use EE_Registry;
8
use EE_Template_Config;
9
use EED_Core_Rest_Api;
10
use EEH_DTT_Helper;
11
use EEH_Qtip_Loader;
12
use EventEspresso\core\domain\Domain;
13
use EventEspresso\core\domain\DomainInterface;
14
use EventEspresso\core\domain\values\assets\JavascriptAsset;
15
use EventEspresso\core\exceptions\InvalidDataTypeException;
16
use EventEspresso\core\exceptions\InvalidEntityException;
17
use EventEspresso\core\exceptions\InvalidInterfaceException;
18
use EventEspresso\core\services\assets\AssetCollection;
19
use EventEspresso\core\services\assets\AssetManager;
20
use EventEspresso\core\services\assets\Registry;
21
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
22
use InvalidArgumentException;
23
24
/**
25
 * Class CoreAssetManager
26
 * Manager class for for Event Espresso core assets
27
 *
28
 * @package EventEspresso\core\services\assets
29
 * @author  Brent Christensen
30
 * @since   4.9.62.p
31
 */
32
class CoreAssetManager extends AssetManager
33
{
34
35
    // WordPress core / Third party JS asset handles
36
    const JS_HANDLE_JQUERY = 'jquery';
37
38
    const JS_HANDLE_JQUERY_VALIDATE = 'jquery-validate';
39
40
    const JS_HANDLE_JQUERY_VALIDATE_EXTRA = 'jquery-validate-extra-methods';
41
42
    const JS_HANDLE_JS_CORE = 'eejs-core';
43
44
    const JS_HANDLE_CORE = 'espresso_core';
45
46
    const JS_HANDLE_I18N = 'eei18n';
47
48
    const JS_HANDLE_VENDOR = 'eventespresso-vendor';
49
50
    const JS_HANDLE_WP_PLUGINS_PAGE = 'ee-wp-plugins-page';
51
52
    // EE CSS assets handles
53
    const CSS_HANDLE_DEFAULT = 'espresso_default';
54
55
    const CSS_HANDLE_CUSTOM = 'espresso_custom_css';
56
57
    /**
58
     * @var EE_Currency_Config $currency_config
59
     */
60
    protected $currency_config;
61
62
    /**
63
     * @var EE_Template_Config $template_config
64
     */
65
    protected $template_config;
66
67
68
    /**
69
     * CoreAssetRegister constructor.
70
     *
71
     * @param AssetCollection    $assets
72
     * @param EE_Currency_Config $currency_config
73
     * @param EE_Template_Config $template_config
74
     * @param DomainInterface    $domain
75
     * @param Registry           $registry
76
     */
77
    public function __construct(
78
        AssetCollection $assets,
79
        EE_Currency_Config $currency_config,
80
        EE_Template_Config $template_config,
81
        DomainInterface $domain,
82
        Registry $registry
83
    ) {
84
        $this->currency_config = $currency_config;
85
        $this->template_config = $template_config;
86
        parent::__construct($domain, $assets, $registry);
87
    }
88
89
90
    /**
91
     * @since 4.9.62.p
92
     * @throws DomainException
93
     * @throws DuplicateCollectionIdentifierException
94
     * @throws InvalidArgumentException
95
     * @throws InvalidDataTypeException
96
     * @throws InvalidEntityException
97
     * @throws InvalidInterfaceException
98
     */
99
    public function addAssets()
100
    {
101
        $this->addJavascriptFiles();
102
        $this->addStylesheetFiles();
103
    }
104
105
106
    /**
107
     * @since 4.9.62.p
108
     * @throws DomainException
109
     * @throws DuplicateCollectionIdentifierException
110
     * @throws InvalidArgumentException
111
     * @throws InvalidDataTypeException
112
     * @throws InvalidEntityException
113
     * @throws InvalidInterfaceException
114
     */
115
    public function addJavascriptFiles()
116
    {
117
        $this->loadCoreJs();
118
        $this->loadJqueryValidate();
119
        add_action(
120
            'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
121
            array($this, 'loadQtipJs')
122
        );
123
        $this->registerAdminAssets();
124
    }
125
126
127
    /**
128
     * @throws DuplicateCollectionIdentifierException
129
     * @throws InvalidDataTypeException
130
     * @throws InvalidEntityException
131
     * @throws DomainException
132
     * @since 4.9.62.p
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 DomainException
145
     * @throws DuplicateCollectionIdentifierException
146
     * @throws InvalidArgumentException
147
     * @throws InvalidDataTypeException
148
     * @throws InvalidEntityException
149
     * @throws InvalidInterfaceException
150
     */
151
    private function loadCoreJs()
152
    {
153
        $this->addJs(CoreAssetManager::JS_HANDLE_VENDOR);
154
        $this->addJs(CoreAssetManager::JS_HANDLE_JS_CORE)->setHasInlineData();
155
        $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest'));
156
        $this->registry->addData(
157
            'paths',
158
            array(
159
                'base_rest_route' => rest_url(),
160
                'rest_route' => rest_url('ee/v4.8.36/'),
161
                'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(),
162
                'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName(),
163
                'site_url' => site_url('/'),
164
                'admin_url' => admin_url('/'),
165
            )
166
        );
167
        // Event Espresso brand name
168
        $this->registry->addData('brandName', Domain::brandName());
169
        /** site formatting values **/
170
        $this->registry->addData(
171
            'site_formats',
172
            array(
173
                'date_formats' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats()
174
            )
175
        );
176
        /** currency data **/
177
        $this->registry->addData(
178
            'currency_config',
179
            $this->getCurrencySettings()
180
        );
181
        /** site timezone */
182
        $this->registry->addData(
183
            'default_timezone',
184
            array(
185
                'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(),
186
                'string' => get_option('timezone_string'),
187
                'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(),
188
            )
189
        );
190
        /** site locale (user locale if user logged in) */
191
        $this->registry->addData(
192
            'locale',
193
            array(
194
                'user' => get_user_locale(),
195
                'site' => get_locale()
196
            )
197
        );
198
199
        $this->addJavascript(
200
            CoreAssetManager::JS_HANDLE_CORE,
201
            EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js',
202
            array(CoreAssetManager::JS_HANDLE_JQUERY)
203
        )
204
        ->setInlineDataCallback(
205
            static function () {
206
                wp_localize_script(
207
                    CoreAssetManager::JS_HANDLE_CORE,
208
                    CoreAssetManager::JS_HANDLE_I18N,
209
                    EE_Registry::$i18n_js_strings
210
                );
211
            }
212
        );
213
    }
214
215
216
217
    /**
218
     * Returns configuration data for the js Currency VO.
219
     * @since 4.9.71.p
220
     * @return array
221
     */
222
    private function getCurrencySettings()
223
    {
224
        return array(
225
            'code' => $this->currency_config->code,
226
            'singularLabel' => $this->currency_config->name,
227
            'pluralLabel' => $this->currency_config->plural,
228
            'sign' => $this->currency_config->sign,
229
            'signB4' => $this->currency_config->sign_b4,
230
            'decimalPlaces' => $this->currency_config->dec_plc,
231
            'decimalMark' => $this->currency_config->dec_mrk,
232
            'thousandsSeparator' => $this->currency_config->thsnds,
233
        );
234
    }
235
236
237
    /**
238
     * @throws DuplicateCollectionIdentifierException
239
     * @throws InvalidDataTypeException
240
     * @throws InvalidEntityException
241
     * @throws DomainException
242
     * @since 4.9.62.p
243
     */
244
    private function loadCoreCss()
245
    {
246
        if ($this->template_config->enable_default_style && ! is_admin()) {
247
            $this->addStylesheet(
248
                CoreAssetManager::CSS_HANDLE_DEFAULT,
249
                is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css')
250
                    ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css'
251
                    : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css',
252
                array('dashicons')
253
            );
254
            //Load custom style sheet if available
255
            if ($this->template_config->custom_style_sheet !== null) {
256
                $this->addStylesheet(
257
                    CoreAssetManager::CSS_HANDLE_CUSTOM,
258
                    EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet,
259
                    array(CoreAssetManager::CSS_HANDLE_DEFAULT)
260
                );
261
            }
262
        }
263
    }
264
265
266
    /**
267
     * jQuery Validate for form validation
268
     *
269
     * @since 4.9.62.p
270
     * @throws DomainException
271
     * @throws DuplicateCollectionIdentifierException
272
     * @throws InvalidDataTypeException
273
     * @throws InvalidEntityException
274
     */
275
    private function loadJqueryValidate()
276
    {
277
        $this->addJavascript(
278
            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE,
279
            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js',
280
            array(CoreAssetManager::JS_HANDLE_JQUERY),
281
            true,
282
            '1.15.0'
283
        );
284
285
        $this->addJavascript(
286
            CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE_EXTRA,
287
            EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js',
288
            array(CoreAssetManager::JS_HANDLE_JQUERY_VALIDATE),
289
            true,
290
            '1.15.0'
291
        );
292
    }
293
294
295
    /**
296
     * registers assets for cleaning your ears
297
     *
298
     * @param JavascriptAsset $script
299
     */
300
    public function loadQtipJs(JavascriptAsset $script)
301
    {
302
        // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook,
303
        // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' );
304
        if (
305
            $script->handle() === CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE
306
            && apply_filters('FHEE_load_qtip', false)
307
        ) {
308
            EEH_Qtip_Loader::instance()->register_and_enqueue();
309
        }
310
    }
311
312
313
    /**
314
     * assets that are used in the WordPress admin
315
     *
316
     * @throws DuplicateCollectionIdentifierException
317
     * @throws InvalidDataTypeException
318
     * @throws InvalidEntityException
319
     * @throws DomainException
320
     * @since 4.9.62.p
321
     */
322
    private function registerAdminAssets()
323
    {
324
        $this->addJs(CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE)->setRequiresTranslation();
325
        // note usage of the "JS_HANDLE.." constant is intentional here because css uses the same handle.
326
        $this->addCss(CoreAssetManager::JS_HANDLE_WP_PLUGINS_PAGE);
327
    }
328
}
329