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() |
||
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() |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Used to add data to eejs.data object. |
||
68 | * |
||
69 | * Note: Overriding existing data is not allowed. |
||
70 | * |
||
71 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
72 | * If the data you add is something like this: |
||
73 | * |
||
74 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
75 | * |
||
76 | * It will be exposed in the page source as: |
||
77 | * |
||
78 | * eejs.data.my_plugin_data.foo == gar |
||
79 | * |
||
80 | * @param string $key Key used to access your data |
||
81 | * @param string|array $value Value to attach to key |
||
82 | * @throws \InvalidArgumentException |
||
83 | */ |
||
84 | public function addData($key, $value) |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
94 | * elements in an array. |
||
95 | * |
||
96 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
97 | * |
||
98 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object |
||
99 | * like this, |
||
100 | * |
||
101 | * eejs.data.test = [ |
||
102 | * my_data, |
||
103 | * ] |
||
104 | * |
||
105 | * If there has already been a scalar value attached to the data object given key, then |
||
106 | * this will throw an exception. |
||
107 | * |
||
108 | * @param string $key Key to attach data to. |
||
109 | * @param string|array $value Value being registered. |
||
110 | * @throws InvalidArgumentException |
||
111 | */ |
||
112 | public function pushData($key, $value) |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Used to set content used by javascript for a template. |
||
136 | * |
||
137 | * Note: Overrides of existing registered templates are not allowed. |
||
138 | * |
||
139 | * @param string $template_reference |
||
140 | * @param string $template_content |
||
141 | */ |
||
142 | public function addTemplate($template_reference, $template_content) |
||
163 | |||
164 | |||
165 | /** |
||
166 | * Retrieve the template content already registered for the given reference. |
||
167 | * @param string $template_reference |
||
168 | * @return '' |
||
|
|||
169 | */ |
||
170 | public function getTemplate($template_reference) |
||
176 | |||
177 | |||
178 | /** |
||
179 | * Retrieve registered data. |
||
180 | * |
||
181 | * @param string $key Name of key to attach data to. |
||
182 | * @return mixed If there is no for the given key, then false is returned. |
||
183 | */ |
||
184 | public function getData($key) |
||
190 | |||
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * Verifies whether the given data exists already on the jsdata array. |
||
196 | * |
||
197 | * Overriding data is not allowed. |
||
198 | * |
||
199 | * @param string $key Index for data. |
||
200 | * @return bool If valid then return true. |
||
201 | * @throws InvalidArgumentException if data already exists. |
||
202 | */ |
||
203 | protected function verifyDataNotExisting($key) |
||
234 | } |
||
235 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.