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() |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Used to add data to eejs.data object. |
||
57 | * |
||
58 | * Note: Overriding existing data is not allowed. To help enforce uniqueness, key/value pairs are added as an element |
||
59 | * of the method/function name for the caller. So for instance, if this coded is called from a class, something like |
||
60 | * this would be done: |
||
61 | |||
62 | * EE_Registry::instance()->AssetsRegistry->addData(__METHOD__, 'my_key', 'my_value'); |
||
63 | * |
||
64 | * If this is being called within a function then you'd do: |
||
65 | * |
||
66 | * EE_Registry::instance()->AssetsRegistry->addData(__FUNCTION__, 'my_key', 'my_value'); |
||
67 | * |
||
68 | * This method WILL validate whether the incoming method/function name is an actual function or method. |
||
69 | * |
||
70 | * Since `::` and `\` is not accessible via javascript object dot notation. Umlauts (::) will be escaped with a |
||
71 | * double underscore (__) and if the method belongs to a namespaced class, the backslashes will be escaped with a |
||
72 | * single underscore (_). So if your method was a part of namespaced class resulting in something like this: |
||
73 | * EventEspresso\some\namespace\Class::myMethod |
||
74 | * |
||
75 | * ...then you would be able to access all the data attached to it in your js via: |
||
76 | * |
||
77 | * var myData = eejs.data.EventEspresso_some_namespace_Class__myMethod |
||
78 | * |
||
79 | * |
||
80 | * @param string $method_name Name of actual method or function. |
||
81 | * @param string $key Key used to access your data |
||
82 | * @param string|array $value Value to attach to key |
||
83 | * @throws \InvalidArgumentException |
||
84 | */ |
||
85 | public function addData($method_name, $key, $value) |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
99 | * elements in an array. |
||
100 | * |
||
101 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
102 | * |
||
103 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object |
||
104 | * like this, |
||
105 | * |
||
106 | * eejs.data.function_name.test = [ |
||
107 | * my_data, |
||
108 | * ] |
||
109 | * |
||
110 | * If there has already been a scalar value attached to the data object for the given method_name, then |
||
111 | * this will throw an exception. |
||
112 | * |
||
113 | * @see Registry::addData for more info on purpose of $method_name |
||
114 | * |
||
115 | * @param string $method_name Name of actual method or function. |
||
116 | * @param string $key Key to attach data to. |
||
117 | * @param string|array $value Value being registered. |
||
118 | * @throws InvalidArgumentException |
||
119 | */ |
||
120 | public function pushData($method_name, $key, $value) |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Retrieve registered data. |
||
147 | * |
||
148 | * @param string $method_name Name of actual method or function. |
||
149 | * @param string $key Name of key to attach data to. |
||
150 | * @return mixed If there is no for the given key, then false is returned. |
||
151 | */ |
||
152 | public function getData($method_name, $key) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Verifies the given string represents an actual function or method. |
||
163 | * @param string $method_name |
||
164 | * @throws InvalidArgumentException |
||
165 | */ |
||
166 | protected function verifyMethod($method_name) |
||
178 | |||
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * Verifies whether the given data exists already on the jsdata array. |
||
184 | * |
||
185 | * Overriding data is not allowed. |
||
186 | * |
||
187 | * @param string $method_name Name representing actual function or method. |
||
188 | * @param string $key Index for data. |
||
189 | * @param string|array $value Value being stored. |
||
190 | * @return bool If valid then return true. |
||
191 | * @throws InvalidArgumentException if data already exists. |
||
192 | */ |
||
193 | protected function verifyDataNotExisting($method_name, $key, $value) |
||
226 | |||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Escapes backslash and umlauts existing in given string. |
||
232 | * |
||
233 | * '::' becomes '__' |
||
234 | * '\' becomes '_' |
||
235 | * |
||
236 | * @param string $method_name The string to escape. |
||
237 | * @return string |
||
238 | */ |
||
239 | protected function methodNameToJsFriendlyString($method_name) |
||
247 | } |
||
248 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.