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