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() |
||
31 | { |
||
32 | add_action('wp_enqueue_scripts', array($this, 'scripts'), 100); |
||
33 | add_action('admin_enqueue_scripts', array($this, 'scripts'), 100); |
||
34 | add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
35 | add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
36 | } |
||
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() |
||
45 | { |
||
46 | global $wp_version; |
||
47 | wp_register_script( |
||
48 | 'eejs-core', |
||
49 | EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js', |
||
50 | array(), |
||
51 | espresso_version(), |
||
52 | true |
||
53 | ); |
||
54 | //only run this if WordPress 4.4.0 > is in use. |
||
55 | if (version_compare($wp_version, '4.4.0', '>')) { |
||
56 | //js.api |
||
57 | wp_register_script( |
||
58 | 'eejs-api', |
||
59 | EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
||
60 | array('underscore', 'eejs-core'), |
||
61 | espresso_version(), |
||
62 | true |
||
63 | ); |
||
64 | $this->jsdata['eejs_api_nonce'] = wp_create_nonce('wp_rest'); |
||
65 | $this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/')); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Call back for the script print in frontend and backend. |
||
72 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hookpoint. |
||
73 | * @since 4.9.31.rc.015 |
||
74 | */ |
||
75 | public function enqueueData() |
||
76 | { |
||
77 | wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata)); |
||
78 | } |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Used to add data to eejs.data object. |
||
83 | * |
||
84 | * Note: Overriding existing data is not allowed. |
||
85 | * |
||
86 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
87 | * If the data you add is something like this: |
||
88 | * |
||
89 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
90 | * |
||
91 | * It will be exposed in the page source as: |
||
92 | * |
||
93 | * eejs.data.my_plugin_data.foo == gar |
||
94 | * |
||
95 | * @param string $key Key used to access your data |
||
96 | * @param string|array $value Value to attach to key |
||
97 | * @throws InvalidArgumentException |
||
98 | */ |
||
99 | public function addData($key, $value) |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
109 | * elements in an array. |
||
110 | * |
||
111 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
112 | * |
||
113 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object |
||
114 | * like this, |
||
115 | * |
||
116 | * eejs.data.test = [ |
||
117 | * my_data, |
||
118 | * ] |
||
119 | * |
||
120 | * If there has already been a scalar value attached to the data object given key, then |
||
121 | * this will throw an exception. |
||
122 | * |
||
123 | * @param string $key Key to attach data to. |
||
124 | * @param string|array $value Value being registered. |
||
125 | * @throws InvalidArgumentException |
||
126 | */ |
||
127 | public function pushData($key, $value) |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Used to set content used by javascript for a template. |
||
151 | * Note: Overrides of existing registered templates are not allowed. |
||
152 | * |
||
153 | * @param string $template_reference |
||
154 | * @param string $template_content |
||
155 | * @throws InvalidArgumentException |
||
156 | */ |
||
157 | public function addTemplate($template_reference, $template_content) |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Retrieve the template content already registered for the given reference. |
||
182 | * @param string $template_reference |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getTemplate($template_reference) |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Retrieve registered data. |
||
195 | * |
||
196 | * @param string $key Name of key to attach data to. |
||
197 | * @return mixed If there is no for the given key, then false is returned. |
||
198 | */ |
||
199 | public function getData($key) |
||
205 | |||
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * Verifies whether the given data exists already on the jsdata array. |
||
211 | * |
||
212 | * Overriding data is not allowed. |
||
213 | * |
||
214 | * @param string $key Index for data. |
||
215 | * @return bool If valid then return true. |
||
216 | * @throws InvalidArgumentException if data already exists. |
||
217 | */ |
||
218 | protected function verifyDataNotExisting($key) |
||
249 | } |
||
250 |