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() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
237 | } |
||
238 |