1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\assets; |
4
|
|
|
|
5
|
|
|
use EE_Currency_Config; |
6
|
|
|
use EE_Registry; |
7
|
|
|
use EE_Template_Config; |
8
|
|
|
use EEH_Qtip_Loader; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Used for registering assets used in EE. |
17
|
|
|
* |
18
|
|
|
* @package EventEspresso |
19
|
|
|
* @subpackage services\assets |
20
|
|
|
* @author Darren Ethier |
21
|
|
|
* @since 4.9.24.rc.004 |
22
|
|
|
*/ |
23
|
|
|
class Registry |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EE_Template_Config $template_config |
28
|
|
|
*/ |
29
|
|
|
protected $template_config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EE_Currency_Config $currency_config |
33
|
|
|
*/ |
34
|
|
|
protected $currency_config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $jsdata = array(); |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Registry constructor. |
47
|
|
|
* Hooking into WP actions for script registry. |
48
|
|
|
* |
49
|
|
|
* @param EE_Template_Config $template_config |
50
|
|
|
* @param EE_Currency_Config $currency_config |
51
|
|
|
*/ |
52
|
|
|
public function __construct(EE_Template_Config $template_config, EE_Currency_Config $currency_config) |
53
|
|
|
{ |
54
|
|
|
$this->template_config = $template_config; |
55
|
|
|
$this->currency_config = $currency_config; |
56
|
|
|
add_action('wp_enqueue_scripts', array($this, 'scripts'), 1); |
57
|
|
|
add_action('admin_enqueue_scripts', array($this, 'scripts'), 1); |
58
|
|
|
add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
59
|
|
|
add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Callback for the WP script actions. |
66
|
|
|
* Used to register globally accessible core scripts. |
67
|
|
|
* Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
68
|
|
|
*/ |
69
|
|
|
public function scripts() |
70
|
|
|
{ |
71
|
|
|
global $wp_version; |
72
|
|
|
wp_register_script( |
73
|
|
|
'eejs-core', |
74
|
|
|
EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js', |
75
|
|
|
array(), |
76
|
|
|
EVENT_ESPRESSO_VERSION, |
77
|
|
|
true |
78
|
|
|
); |
79
|
|
|
//only run this if WordPress 4.4.0 > is in use. |
80
|
|
|
if (version_compare($wp_version, '4.4.0', '>')) { |
81
|
|
|
//js.api |
82
|
|
|
wp_register_script( |
83
|
|
|
'eejs-api', |
84
|
|
|
EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
85
|
|
|
array('underscore', 'eejs-core'), |
86
|
|
|
EVENT_ESPRESSO_VERSION, |
87
|
|
|
true |
88
|
|
|
); |
89
|
|
|
$this->jsdata['eejs_api_nonce'] = wp_create_nonce('wp_rest'); |
90
|
|
|
$this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/')); |
91
|
|
|
} |
92
|
|
|
if (! is_admin()) { |
93
|
|
|
$this->loadCoreCss(); |
94
|
|
|
} |
95
|
|
|
$this->loadCoreJs(); |
96
|
|
|
$this->loadJqueryValidate(); |
97
|
|
|
$this->loadAccountingJs(); |
98
|
|
|
$this->loadQtipJs(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Call back for the script print in frontend and backend. |
105
|
|
|
* Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
106
|
|
|
* |
107
|
|
|
* @since 4.9.31.rc.015 |
108
|
|
|
*/ |
109
|
|
|
public function enqueueData() |
110
|
|
|
{ |
111
|
|
|
wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata)); |
112
|
|
|
wp_localize_script('espresso_core', 'eei18n', EE_Registry::$i18n_js_strings); |
113
|
|
|
$this->localizeAccountingJs(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Used to add data to eejs.data object. |
120
|
|
|
* Note: Overriding existing data is not allowed. |
121
|
|
|
* Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
122
|
|
|
* If the data you add is something like this: |
123
|
|
|
* $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
124
|
|
|
* It will be exposed in the page source as: |
125
|
|
|
* eejs.data.my_plugin_data.foo == gar |
126
|
|
|
* |
127
|
|
|
* @param string $key Key used to access your data |
128
|
|
|
* @param string|array $value Value to attach to key |
129
|
|
|
* @throws InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function addData($key, $value) |
132
|
|
|
{ |
133
|
|
|
if ($this->verifyDataNotExisting($key)) { |
134
|
|
|
$this->jsdata[$key] = $value; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Similar to addData except this allows for users to push values to an existing key where the values on key are |
142
|
|
|
* elements in an array. |
143
|
|
|
* When you use this method, the value you include will be appended to the end of an array on $key. |
144
|
|
|
* So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
145
|
|
|
* object like this, eejs.data.test = [ my_data, |
146
|
|
|
* ] |
147
|
|
|
* If there has already been a scalar value attached to the data object given key, then |
148
|
|
|
* this will throw an exception. |
149
|
|
|
* |
150
|
|
|
* @param string $key Key to attach data to. |
151
|
|
|
* @param string|array $value Value being registered. |
152
|
|
|
* @throws InvalidArgumentException |
153
|
|
|
*/ |
154
|
|
|
public function pushData($key, $value) |
155
|
|
|
{ |
156
|
|
View Code Duplication |
if (isset($this->jsdata[$key]) |
157
|
|
|
&& ! is_array($this->jsdata[$key]) |
158
|
|
|
) { |
159
|
|
|
throw new invalidArgumentException( |
160
|
|
|
sprintf( |
161
|
|
|
__( |
162
|
|
|
'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
163
|
|
|
push values to this data element when it is an array.', |
164
|
|
|
'event_espresso' |
165
|
|
|
), |
166
|
|
|
$key, |
167
|
|
|
__METHOD__ |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
$this->jsdata[$key][] = $value; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Used to set content used by javascript for a template. |
178
|
|
|
* Note: Overrides of existing registered templates are not allowed. |
179
|
|
|
* |
180
|
|
|
* @param string $template_reference |
181
|
|
|
* @param string $template_content |
182
|
|
|
* @throws InvalidArgumentException |
183
|
|
|
*/ |
184
|
|
|
public function addTemplate($template_reference, $template_content) |
185
|
|
|
{ |
186
|
|
|
if (! isset($this->jsdata['templates'])) { |
187
|
|
|
$this->jsdata['templates'] = array(); |
188
|
|
|
} |
189
|
|
|
//no overrides allowed. |
190
|
|
|
if (isset($this->jsdata['templates'][$template_reference])) { |
191
|
|
|
throw new invalidArgumentException( |
192
|
|
|
sprintf( |
193
|
|
|
__( |
194
|
|
|
'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
195
|
|
|
'event_espresso' |
196
|
|
|
), |
197
|
|
|
$template_reference |
198
|
|
|
) |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
$this->jsdata['templates'][$template_reference] = $template_content; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Retrieve the template content already registered for the given reference. |
208
|
|
|
* |
209
|
|
|
* @param string $template_reference |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function getTemplate($template_reference) |
213
|
|
|
{ |
214
|
|
|
return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference]) |
215
|
|
|
? $this->jsdata['templates'][$template_reference] |
216
|
|
|
: ''; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Retrieve registered data. |
223
|
|
|
* |
224
|
|
|
* @param string $key Name of key to attach data to. |
225
|
|
|
* @return mixed If there is no for the given key, then false is returned. |
226
|
|
|
*/ |
227
|
|
|
public function getData($key) |
228
|
|
|
{ |
229
|
|
|
return isset($this->jsdata[$key]) |
230
|
|
|
? $this->jsdata[$key] |
231
|
|
|
: false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Verifies whether the given data exists already on the jsdata array. |
238
|
|
|
* Overriding data is not allowed. |
239
|
|
|
* |
240
|
|
|
* @param string $key Index for data. |
241
|
|
|
* @return bool If valid then return true. |
242
|
|
|
* @throws InvalidArgumentException if data already exists. |
243
|
|
|
*/ |
244
|
|
|
protected function verifyDataNotExisting($key) |
245
|
|
|
{ |
246
|
|
|
if (isset($this->jsdata[$key])) { |
247
|
|
View Code Duplication |
if (is_array($this->jsdata[$key])) { |
248
|
|
|
throw new InvalidArgumentException( |
249
|
|
|
sprintf( |
250
|
|
|
__( |
251
|
|
|
'The value for %1$s already exists in the Registry::eejs object. |
252
|
|
|
Overrides are not allowed. Since the value of this data is an array, you may want to use the |
253
|
|
|
%2$s method to push your value to the array.', |
254
|
|
|
'event_espresso' |
255
|
|
|
), |
256
|
|
|
$key, |
257
|
|
|
'pushData()' |
258
|
|
|
) |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
throw new InvalidArgumentException( |
262
|
|
|
sprintf( |
263
|
|
|
__( |
264
|
|
|
'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
265
|
|
|
allowed. Consider attaching your value to a different key', |
266
|
|
|
'event_espresso' |
267
|
|
|
), |
268
|
|
|
$key |
269
|
|
|
) |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
return true; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* registers core default stylesheets |
279
|
|
|
*/ |
280
|
|
|
private function loadCoreCss() |
281
|
|
|
{ |
282
|
|
|
if ($this->template_config->enable_default_style) { |
283
|
|
|
$default_stylesheet_path = is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css') |
284
|
|
|
? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css' |
285
|
|
|
: EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css'; |
286
|
|
|
wp_register_style( |
287
|
|
|
'espresso_default', |
288
|
|
|
$default_stylesheet_path, |
289
|
|
|
array('dashicons'), |
290
|
|
|
EVENT_ESPRESSO_VERSION |
291
|
|
|
); |
292
|
|
|
//Load custom style sheet if available |
293
|
|
|
if ($this->template_config->custom_style_sheet !== null) { |
294
|
|
|
wp_register_style( |
295
|
|
|
'espresso_custom_css', |
296
|
|
|
EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet, |
297
|
|
|
array('espresso_default'), |
298
|
|
|
EVENT_ESPRESSO_VERSION |
299
|
|
|
); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* registers core default javascript |
308
|
|
|
*/ |
309
|
|
|
private function loadCoreJs() |
310
|
|
|
{ |
311
|
|
|
// load core js |
312
|
|
|
wp_register_script( |
313
|
|
|
'espresso_core', |
314
|
|
|
EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
315
|
|
|
array('jquery'), |
316
|
|
|
EVENT_ESPRESSO_VERSION, |
317
|
|
|
true |
318
|
|
|
); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* registers jQuery Validate for form validation |
325
|
|
|
*/ |
326
|
|
|
private function loadJqueryValidate() |
327
|
|
|
{ |
328
|
|
|
// register jQuery Validate and additional methods |
329
|
|
|
wp_register_script( |
330
|
|
|
'jquery-validate', |
331
|
|
|
EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js', |
332
|
|
|
array('jquery'), |
333
|
|
|
'1.15.0', |
334
|
|
|
true |
335
|
|
|
); |
336
|
|
|
wp_register_script( |
337
|
|
|
'jquery-validate-extra-methods', |
338
|
|
|
EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js', |
339
|
|
|
array('jquery', 'jquery-validate'), |
340
|
|
|
'1.15.0', |
341
|
|
|
true |
342
|
|
|
); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* registers accounting.js for performing client-side calculations |
349
|
|
|
*/ |
350
|
|
|
private function loadAccountingJs() |
351
|
|
|
{ |
352
|
|
|
//accounting.js library |
353
|
|
|
// @link http://josscrowcroft.github.io/accounting.js/ |
354
|
|
|
wp_register_script( |
355
|
|
|
'ee-accounting-core', |
356
|
|
|
EE_THIRD_PARTY_URL . 'accounting/accounting.js', |
357
|
|
|
array('underscore'), |
358
|
|
|
'0.3.2', |
359
|
|
|
true |
360
|
|
|
); |
361
|
|
|
wp_register_script( |
362
|
|
|
'ee-accounting', |
363
|
|
|
EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js', |
364
|
|
|
array('ee-accounting-core'), |
365
|
|
|
EVENT_ESPRESSO_VERSION, |
366
|
|
|
true |
367
|
|
|
); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* registers accounting.js for performing client-side calculations |
374
|
|
|
*/ |
375
|
|
|
private function localizeAccountingJs() |
376
|
|
|
{ |
377
|
|
|
wp_localize_script( |
378
|
|
|
'ee-accounting', |
379
|
|
|
'EE_ACCOUNTING_CFG', |
380
|
|
|
array( |
381
|
|
|
'currency' => array( |
382
|
|
|
'symbol' => $this->currency_config->sign, |
383
|
|
|
'format' => array( |
384
|
|
|
'pos' => $this->currency_config->sign_b4 ? '%s%v' : '%v%s', |
385
|
|
|
'neg' => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s', |
386
|
|
|
'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s', |
387
|
|
|
), |
388
|
|
|
'decimal' => $this->currency_config->dec_mrk, |
389
|
|
|
'thousand' => $this->currency_config->thsnds, |
390
|
|
|
'precision' => $this->currency_config->dec_plc, |
391
|
|
|
), |
392
|
|
|
'number' => array( |
393
|
|
|
'precision' => 0, |
394
|
|
|
'thousand' => $this->currency_config->thsnds, |
395
|
|
|
'decimal' => $this->currency_config->dec_mrk, |
396
|
|
|
), |
397
|
|
|
) |
398
|
|
|
); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
|
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* registers assets for cleaning your ears |
405
|
|
|
*/ |
406
|
|
|
private function loadQtipJs() |
407
|
|
|
{ |
408
|
|
|
// qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook, |
409
|
|
|
// can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' ); |
410
|
|
|
if (apply_filters('FHEE_load_qtip', false)) { |
411
|
|
|
EEH_Qtip_Loader::instance()->register_and_enqueue(); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
|
417
|
|
|
|
418
|
|
|
} |
419
|
|
|
|