Completed
Branch EDTR/master (50dd13)
by
unknown
25:34 queued 17:04
created

CoreAssetManager::loadJqueryValidate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 18
rs 9.6666
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_Template_Config;
8
use EED_Core_Rest_Api;
9
use EEH_DTT_Helper;
10
use EventEspresso\core\domain\Domain;
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\exceptions\InvalidInterfaceException;
16
use EventEspresso\core\services\assets\AssetCollection;
17
use EventEspresso\core\services\assets\AssetManager;
18
use EventEspresso\core\services\assets\Registry;
19
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
20
use InvalidArgumentException;
21
22
/**
23
 * Class CoreAssetManager
24
 * Manager class for for Event Espresso core assets
25
 *
26
 * @package EventEspresso\core\services\assets
27
 * @author  Brent Christensen
28
 * @since   4.9.62.p
29
 */
30
class CoreAssetManager extends AssetManager
31
{
32
33
    // WordPress core / Third party JS asset handles
34
    const JS_HANDLE_JS_CORE = 'eejs-core';
35
36
    const JS_HANDLE_CORE = 'espresso_core';
37
38
    const JS_HANDLE_I18N = 'eei18n';
39
40
    const JS_HANDLE_VENDOR = 'eventespresso-vendor';
41
42
    // EE CSS assets handles
43
    const CSS_HANDLE_DEFAULT = 'espresso_default';
44
45
    const CSS_HANDLE_CUSTOM = 'espresso_custom_css';
46
47
    /**
48
     * @var EE_Currency_Config $currency_config
49
     */
50
    protected $currency_config;
51
52
    /**
53
     * @var EE_Template_Config $template_config
54
     */
55
    protected $template_config;
56
57
58
    /**
59
     * CoreAssetRegister constructor.
60
     *
61
     * @param AssetCollection    $assets
62
     * @param EE_Currency_Config $currency_config
63
     * @param EE_Template_Config $template_config
64
     * @param DomainInterface    $domain
65
     * @param Registry           $registry
66
     */
67
    public function __construct(
68
        AssetCollection $assets,
69
        EE_Currency_Config $currency_config,
70
        EE_Template_Config $template_config,
71
        DomainInterface $domain,
72
        Registry $registry
73
    ) {
74
        $this->currency_config = $currency_config;
75
        $this->template_config = $template_config;
76
        parent::__construct($domain, $assets, $registry);
77
    }
78
79
80
    /**
81
     * @since 4.9.62.p
82
     * @throws DomainException
83
     * @throws DuplicateCollectionIdentifierException
84
     * @throws InvalidArgumentException
85
     * @throws InvalidDataTypeException
86
     * @throws InvalidEntityException
87
     * @throws InvalidInterfaceException
88
     */
89
    public function addAssets()
90
    {
91
        $this->addJavascriptFiles();
92
        $this->addStylesheetFiles();
93
    }
94
95
96
    /**
97
     * @since 4.9.62.p
98
     * @throws DomainException
99
     * @throws DuplicateCollectionIdentifierException
100
     * @throws InvalidArgumentException
101
     * @throws InvalidDataTypeException
102
     * @throws InvalidEntityException
103
     * @throws InvalidInterfaceException
104
     */
105
    public function addJavascriptFiles()
106
    {
107
        $this->addJs(CoreAssetManager::JS_HANDLE_VENDOR);
108
        $this->addJs(CoreAssetManager::JS_HANDLE_JS_CORE)->setHasInlineData();
109
        // todo verify that the following data is no longer being used anywhere and remove
110
        $this->registry->addData('eejs_api_nonce', wp_create_nonce('wp_rest'));
111
        $this->registry->addData(
112
            'paths',
113
            array(
114
                'base_rest_route' => rest_url(),
115
                'rest_route' => rest_url('ee/v4.8.36/'),
116
                'collection_endpoints' => EED_Core_Rest_Api::getCollectionRoutesIndexedByModelName(),
117
                'primary_keys' => EED_Core_Rest_Api::getPrimaryKeyNamesIndexedByModelName(),
118
                'site_url' => site_url('/'),
119
                'admin_url' => admin_url('/'),
120
            )
121
        );
122
        // Event Espresso brand name
123
        $this->registry->addData('brandName', Domain::brandName());
124
        /** site formatting values **/
125
        $this->registry->addData(
126
            'site_formats',
127
            array(
128
                'date_formats' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats()
129
            )
130
        );
131
        /** currency data **/
132
        $this->registry->addData(
133
            'currency_config',
134
            $this->getCurrencySettings()
135
        );
136
        /** site timezone */
137
        $this->registry->addData(
138
            'default_timezone',
139
            array(
140
                'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(),
141
                'string' => get_option('timezone_string'),
142
                'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(),
143
            )
144
        );
145
        /** site locale (user locale if user logged in) */
146
        $this->registry->addData(
147
            'locale',
148
            array(
149
                'user' => get_user_locale(),
150
                'site' => get_locale()
151
            )
152
        );
153
154
        $this->addJavascript(
155
            CoreAssetManager::JS_HANDLE_CORE,
156
            EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js',
157
            array(JqueryAssetManager::JS_HANDLE_JQUERY)
158
        );
159
    }
160
161
162
    /**
163
     * @throws DuplicateCollectionIdentifierException
164
     * @throws InvalidDataTypeException
165
     * @throws InvalidEntityException
166
     * @throws DomainException
167
     * @since 4.9.62.p
168
     */
169
    public function addStylesheetFiles()
170
    {
171
        if ($this->template_config->enable_default_style && ! is_admin()) {
172
            $this->addStylesheet(
173
                CoreAssetManager::CSS_HANDLE_DEFAULT,
174
                is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css')
175
                    ? EVENT_ESPRESSO_UPLOAD_URL . 'css/espresso_default.css'
176
                    : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css',
177
                array('dashicons')
178
            );
179
            //Load custom style sheet if available
180
            if ($this->template_config->custom_style_sheet !== null) {
181
                $this->addStylesheet(
182
                    CoreAssetManager::CSS_HANDLE_CUSTOM,
183
                    EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet,
184
                    array(CoreAssetManager::CSS_HANDLE_DEFAULT)
185
                );
186
            }
187
        }
188
    }
189
190
191
192
    /**
193
     * Returns configuration data for the js Currency VO.
194
     * @since 4.9.71.p
195
     * @return array
196
     */
197
    private function getCurrencySettings()
198
    {
199
        return array(
200
            'code' => $this->currency_config->code,
201
            'singularLabel' => $this->currency_config->name,
202
            'pluralLabel' => $this->currency_config->plural,
203
            'sign' => $this->currency_config->sign,
204
            'signB4' => $this->currency_config->sign_b4,
205
            'decimalPlaces' => $this->currency_config->dec_plc,
206
            'decimalMark' => $this->currency_config->dec_mrk,
207
            'thousandsSeparator' => $this->currency_config->thsnds,
208
        );
209
    }
210
211
212
    /**
213
     * @param JavascriptAsset $script
214
     * @deprecated $VID:$
215
     */
216
    public function loadQtipJs(JavascriptAsset $script)
217
    {
218
        // replacement:
219
        // EventEspresso\core\domain\services\assets\EspressoLegacyAdminAssetManager::loadQtipJs()
220
    }
221
}
222