1 | <?php |
||
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() |
||
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() |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Call back for the script print in frontend and backend. |
||
71 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hookpoint. |
||
72 | * @since 4.9.31.rc.015 |
||
73 | */ |
||
74 | public function enqueueData() |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Used to add data to eejs.data object. |
||
82 | * |
||
83 | * Note: Overriding existing data is not allowed. |
||
84 | * |
||
85 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
86 | * If the data you add is something like this: |
||
87 | * |
||
88 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
89 | * |
||
90 | * It will be exposed in the page source as: |
||
91 | * |
||
92 | * eejs.data.my_plugin_data.foo == gar |
||
93 | * |
||
94 | * @param string $key Key used to access your data |
||
95 | * @param string|array $value Value to attach to key |
||
96 | * @throws InvalidArgumentException |
||
97 | */ |
||
98 | public function addData($key, $value) |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
108 | * elements in an array. |
||
109 | * |
||
110 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
111 | * |
||
112 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object |
||
113 | * like this, |
||
114 | * |
||
115 | * eejs.data.test = [ |
||
116 | * my_data, |
||
117 | * ] |
||
118 | * |
||
119 | * If there has already been a scalar value attached to the data object given key, then |
||
120 | * this will throw an exception. |
||
121 | * |
||
122 | * @param string $key Key to attach data to. |
||
123 | * @param string|array $value Value being registered. |
||
124 | * @throws InvalidArgumentException |
||
125 | */ |
||
126 | public function pushData($key, $value) |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Used to set content used by javascript for a template. |
||
150 | * Note: Overrides of existing registered templates are not allowed. |
||
151 | * |
||
152 | * @param string $template_reference |
||
153 | * @param string $template_content |
||
154 | * @throws InvalidArgumentException |
||
155 | */ |
||
156 | public function addTemplate($template_reference, $template_content) |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Retrieve the template content already registered for the given reference. |
||
181 | * @param string $template_reference |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getTemplate($template_reference) |
||
190 | |||
191 | |||
192 | /** |
||
193 | * Retrieve registered data. |
||
194 | * |
||
195 | * @param string $key Name of key to attach data to. |
||
196 | * @return mixed If there is no for the given key, then false is returned. |
||
197 | */ |
||
198 | public function getData($key) |
||
204 | |||
205 | |||
206 | |||
207 | |||
208 | /** |
||
209 | * Verifies whether the given data exists already on the jsdata array. |
||
210 | * |
||
211 | * Overriding data is not allowed. |
||
212 | * |
||
213 | * @param string $key Index for data. |
||
214 | * @return bool If valid then return true. |
||
215 | * @throws InvalidArgumentException if data already exists. |
||
216 | */ |
||
217 | protected function verifyDataNotExisting($key) |
||
248 | } |
||
249 |