1
|
|
|
<?php |
2
|
|
|
namespace EventEspresso\core\services\assets; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Used for registering assets used in EE. |
10
|
|
|
* |
11
|
|
|
* @package EventEspresso |
12
|
|
|
* @subpackage services\assets |
13
|
|
|
* @author Darren Ethier |
14
|
|
|
* @since 4.9.24.rc.004 |
15
|
|
|
*/ |
16
|
|
|
class Registry |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $jsdata = array(); |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Registry constructor. |
28
|
|
|
* Hooking into WP actions for script registry. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
add_action('wp_enqueue_scripts', array($this, 'scripts'), 100); |
33
|
|
|
add_action('admin_enqueue_scripts', array($this, 'scripts'), 100); |
34
|
|
|
add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
35
|
|
|
add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Callback for the WP script actions. |
41
|
|
|
* Used to register globally accessible core scripts. |
42
|
|
|
* Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
43
|
|
|
*/ |
44
|
|
|
public function scripts() |
45
|
|
|
{ |
46
|
|
|
global $wp_version; |
47
|
|
|
wp_register_script( |
48
|
|
|
'eejs-core', |
49
|
|
|
EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js', |
50
|
|
|
array(), |
51
|
|
|
espresso_version(), |
52
|
|
|
true |
53
|
|
|
); |
54
|
|
|
//only run this if WordPress 4.4.0 > is in use. |
55
|
|
|
if (version_compare($wp_version, '4.4.0', '>')) { |
56
|
|
|
//js.api |
57
|
|
|
wp_register_script( |
58
|
|
|
'eejs-api', |
59
|
|
|
EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
60
|
|
|
array('underscore', 'eejs-core'), |
61
|
|
|
espresso_version(), |
62
|
|
|
true |
63
|
|
|
); |
64
|
|
|
$this->jsdata['eejs_api_nonce'] = wp_create_nonce('wp_rest'); |
65
|
|
|
$this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/')); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Call back for the script print in frontend and backend. |
72
|
|
|
* Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hookpoint. |
73
|
|
|
* @since 4.9.31.rc.015 |
74
|
|
|
*/ |
75
|
|
|
public function enqueueData() |
76
|
|
|
{ |
77
|
|
|
wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Used to add data to eejs.data object. |
83
|
|
|
* |
84
|
|
|
* Note: Overriding existing data is not allowed. |
85
|
|
|
* |
86
|
|
|
* Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
87
|
|
|
* If the data you add is something like this: |
88
|
|
|
* |
89
|
|
|
* $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
90
|
|
|
* |
91
|
|
|
* It will be exposed in the page source as: |
92
|
|
|
* |
93
|
|
|
* eejs.data.my_plugin_data.foo == gar |
94
|
|
|
* |
95
|
|
|
* @param string $key Key used to access your data |
96
|
|
|
* @param string|array $value Value to attach to key |
97
|
|
|
* @throws InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function addData($key, $value) |
100
|
|
|
{ |
101
|
|
|
if ($this->verifyDataNotExisting($key)) { |
102
|
|
|
$this->jsdata[$key] = $value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Similar to addData except this allows for users to push values to an existing key where the values on key are |
109
|
|
|
* elements in an array. |
110
|
|
|
* |
111
|
|
|
* When you use this method, the value you include will be appended to the end of an array on $key. |
112
|
|
|
* |
113
|
|
|
* So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object |
114
|
|
|
* like this, |
115
|
|
|
* |
116
|
|
|
* eejs.data.test = [ |
117
|
|
|
* my_data, |
118
|
|
|
* ] |
119
|
|
|
* |
120
|
|
|
* If there has already been a scalar value attached to the data object given key, then |
121
|
|
|
* this will throw an exception. |
122
|
|
|
* |
123
|
|
|
* @param string $key Key to attach data to. |
124
|
|
|
* @param string|array $value Value being registered. |
125
|
|
|
* @throws InvalidArgumentException |
126
|
|
|
*/ |
127
|
|
|
public function pushData($key, $value) |
128
|
|
|
{ |
129
|
|
|
if (isset($this->jsdata[$key]) |
130
|
|
|
&& ! is_array($this->jsdata[$key]) |
131
|
|
|
) { |
132
|
|
|
throw new invalidArgumentException( |
133
|
|
|
sprintf( |
134
|
|
|
__( |
135
|
|
|
'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
136
|
|
|
push values to this data element when it is an array.', |
137
|
|
|
'event_espresso' |
138
|
|
|
), |
139
|
|
|
$key, |
140
|
|
|
__METHOD__ |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->jsdata[$key][] = $value; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Used to set content used by javascript for a template. |
151
|
|
|
* Note: Overrides of existing registered templates are not allowed. |
152
|
|
|
* |
153
|
|
|
* @param string $template_reference |
154
|
|
|
* @param string $template_content |
155
|
|
|
* @throws InvalidArgumentException |
156
|
|
|
*/ |
157
|
|
|
public function addTemplate($template_reference, $template_content) |
158
|
|
|
{ |
159
|
|
|
if (! isset($this->jsdata['templates'])) { |
160
|
|
|
$this->jsdata['templates'] = array(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
//no overrides allowed. |
164
|
|
|
if (isset($this->jsdata['templates'][$template_reference])) { |
165
|
|
|
throw new invalidArgumentException( |
166
|
|
|
sprintf( |
167
|
|
|
__( |
168
|
|
|
'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
169
|
|
|
'event_espresso' |
170
|
|
|
), |
171
|
|
|
$template_reference |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
} else { |
175
|
|
|
$this->jsdata['templates'][$template_reference] = $template_content; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Retrieve the template content already registered for the given reference. |
182
|
|
|
* @param string $template_reference |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getTemplate($template_reference) |
186
|
|
|
{ |
187
|
|
|
return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference]) |
188
|
|
|
? $this->jsdata['templates'][$template_reference] |
189
|
|
|
: ''; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Retrieve registered data. |
195
|
|
|
* |
196
|
|
|
* @param string $key Name of key to attach data to. |
197
|
|
|
* @return mixed If there is no for the given key, then false is returned. |
198
|
|
|
*/ |
199
|
|
|
public function getData($key) |
200
|
|
|
{ |
201
|
|
|
return isset($this->jsdata[$key]) |
202
|
|
|
? $this->jsdata[$key] |
203
|
|
|
: false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Verifies whether the given data exists already on the jsdata array. |
211
|
|
|
* |
212
|
|
|
* Overriding data is not allowed. |
213
|
|
|
* |
214
|
|
|
* @param string $key Index for data. |
215
|
|
|
* @return bool If valid then return true. |
216
|
|
|
* @throws InvalidArgumentException if data already exists. |
217
|
|
|
*/ |
218
|
|
|
protected function verifyDataNotExisting($key) |
219
|
|
|
{ |
220
|
|
|
if (isset($this->jsdata[$key])) { |
221
|
|
|
if (is_array($this->jsdata[$key])) { |
222
|
|
|
throw new InvalidArgumentException( |
223
|
|
|
sprintf( |
224
|
|
|
__( |
225
|
|
|
'The value for %1$s already exists in the Registry::eejs object. |
226
|
|
|
Overrides are not allowed. Since the value of this data is an array, you may want to use the |
227
|
|
|
%2$s method to push your value to the array.', |
228
|
|
|
'event_espresso' |
229
|
|
|
), |
230
|
|
|
$key, |
231
|
|
|
'pushData()' |
232
|
|
|
) |
233
|
|
|
); |
234
|
|
|
} else { |
235
|
|
|
throw new InvalidArgumentException( |
236
|
|
|
sprintf( |
237
|
|
|
__( |
238
|
|
|
'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
239
|
|
|
allowed. Consider attaching your value to a different key', |
240
|
|
|
'event_espresso' |
241
|
|
|
), |
242
|
|
|
$key |
243
|
|
|
) |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
return true; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|