PluginProbe
Customify / 2.10.9
Customify v2.10.9
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / includes / admin-settings / core / classes / Processor.php

Processor.php in Customify 2.10.9, at includes/admin-settings/core/classes/Processor.php

333 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die;
2
3 /**
4 * @package pixcustomify
5 * @category core
6 * @author Pixelgrade Team
7 * @copyright (c) 2013, Pixelgrade
8 */
9 class PixCustomifyProcessorImpl implements PixCustomifyProcessor {
10
11 /** @var PixCustomifyMeta plugin configuration */
12 protected $meta = null;
13
14 /** @var PixCustomifyMeta field information */
15 protected $fields = null;
16
17 /**
18 * @param array config
19 */
20 static function instance($config = null) {
21 $i = new self;
22 $i->configure($config);
23 return $i;
24 }
25
26 /**
27 * Apply configuration.
28 */
29 protected function configure($config = null) {
30 $this->meta = pixcustomify::instance('PixCustomifyMeta', $config);
31
32 // extract fields from configuration
33 $fields = $this->extract($this->meta->get('fields', array()));
34 $this->fields = pixcustomify::instance('PixCustomifyMeta', $fields);
35 }
36
37 /**
38 * Extracts fields from raw fields configuration returning an array with
39 * the fields in a flat plain.
40 *
41 * Fields are extracted as follows: if an array has the key "type" it's a
42 * field configuration and the key above it is considered the field name
43 * unless a "name" key is also available.
44 *
45 * @param array raw fields
46 * @return array flat field list
47 */
48 protected function extract($rawfields) {
49 $fields = array();
50
51 foreach ($rawfields as $key => $value) {
52 if (is_array($value)) {
53 if (isset($value['type'])) {
54 if (is_string($key)) {
55 $fields[$key] = $value;
56 }
57 else if (isset($value['name'])) {
58 $fields[$value['name']] = $value;
59 }
60 # else: assume rendering sugar or other
61 }
62
63 // search deeper for embeded fields
64 $embeded_fields = $this->extract($value);
65 $fields = array_merge($fields, $embeded_fields);
66 }
67 }
68
69 return $fields;
70 }
71
72 /** @var array status */
73 protected $status = null;
74
75 /** @var PixCustomifyMeta current data; including submitted data */
76 protected $data = null;
77
78 /**
79 * @return static $this
80 */
81 function run() {
82 // if the status has been generated we skip execution
83 if ($this->status !== null) {
84 return $this;
85 }
86
87 $this->status = array
88 (
89 'state' => 'nominal',
90 'errors' => array(),
91 'dataupdate' => false,
92 );
93
94 try {
95 $option_key = $this->meta->get('settings-key', null);
96
97 if ($option_key === null) {
98 throw new Exception('Missing option_key in plugin configuration.');
99 }
100
101 if ($this->form_was_submitted()) {
102 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- The settings page verifies the nonce before this processor runs.
103 $input = $this->cleanup_input( wp_unslash( $_POST ) );
104 $errors = $this->validate_input($input);
105
106 if (empty($errors)) {
107 $this->preupdate($input);
108 $this->status['dataupdate'] = true;
109 $current_values = get_option($option_key);
110 $new_option = array_merge($current_values, $input);
111 update_option($option_key, $new_option);
112 $this->data = pixcustomify::instance('PixCustomifyMeta', $input);
113 $this->postupdate($input);
114 }
115 else { // got errors
116 $this->status['errors'] = $errors;
117 $this->load_data_from_database($option_key);
118 $this->data->overwritemeta($input);
119 }
120 }
121 else { // GET request
122 $this->load_data_from_database($option_key);
123 }
124 }
125 catch (Exception $e) {
126 if ($this->meta->get('debug', false)) {
127 throw $e;
128 }
129
130 $this->status['state'] = 'error';
131 $this->status['message'] = $e->getMessage();
132 }
133
134 return $this;
135 }
136
137 /**
138 * @return static $this
139 */
140 protected function load_data_from_database($option_key) {
141 $dbconfig = get_option($option_key);
142
143 if ($dbconfig === false) {
144 throw new Exception('Unable to retrieve options.');
145 }
146
147 $this->data = pixcustomify::instance('PixCustomifyMeta', $dbconfig);
148 }
149
150 /**
151 * @param array input
152 * @return array cleaned up input
153 */
154 protected function cleanup_input($input) {
155 $defaults = pixcustomify::defaults();
156 $plugin_cleanup = $this->meta->get('cleanup', array());
157
158 foreach ($this->fields->metadata_array() as $key => $field) {
159
160 // ensure a value is present
161 if ( ! isset($input[$key])) {
162 $input[$key] = null;
163 }
164
165 // Calculate cleanup rules
166 // -----------------------
167
168 $cleanup = array();
169 // check pixcustomify defaults
170 if (isset($defaults['cleanup'][$field['type']])) {
171 $cleanup = $defaults['cleanup'][$field['type']];
172 }
173 // check plugin defaults
174 if (isset($plugin_cleanup[$field['type']])) {
175 $cleanup = array_merge($cleanup, $plugin_cleanup[$field['type']]);
176 }
177 // check field presets
178 if (isset($field['cleanup'])) {
179 $cleanup = array_merge($cleanup, $field['cleanup']);
180 }
181
182 // Perform Cleanup
183 // ---------------
184
185 foreach ($cleanup as $rule) {
186 $callback = pixcustomify::callback($rule, $this->meta);
187 $input[$key] = call_user_func($callback, $input[$key], $field, $this);
188 }
189 }
190
191 return $input;
192 }
193
194 /**
195 * @param array input
196 * @return array
197 */
198 protected function validate_input($input) {
199 $validator = pixcustomify::instance('PixCustomifyValidator', $this->meta, $this->fields);
200 return $validator->validate($input);
201 }
202
203 /**
204 * @return boolean
205 */
206 protected function form_was_submitted() {
207 return isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) );
208 }
209
210 /**
211 * @return array
212 */
213 function status() {
214 if ($this->status === null) {
215 $this->run();
216 }
217
218 return $this->status;
219 }
220
221 /**
222 * @return PixCustomifyMeta current data (influenced by user submitted data)
223 */
224 function data() {
225 if ($this->status === null) {
226 $this->run();
227 }
228
229 return $this->data;
230 }
231
232 /**
233 * Shorthand.
234 *
235 * @return array
236 */
237 function errors() {
238 if ($this->status === null) {
239 $this->run();
240 }
241
242 return $this->status['errors'];
243 }
244
245 /**
246 * Shorthand.
247 *
248 * @return boolean
249 */
250 function performed_update() {
251 if ($this->status === null) {
252 $this->run();
253 }
254
255 return $this->status['dataupdate'];
256 }
257
258 /**
259 * @return boolean true if state is nominal
260 */
261 function ok() {
262 if ($this->status === null) {
263 $this->run();
264 }
265
266 return $this->status['state'] == 'nominal';
267 }
268
269 // ------------------------------------------------------------------------
270 // Hooks
271
272 /**
273 * Execute preupdate hooks on input.
274 */
275 protected function preupdate($input)
276 {
277 $defaults = pixcustomify::defaults();
278 $plugin_hooks = $this->meta->get('processor', array('preupdate' => array(), 'postupdate' => array()));
279
280 // Calculate hooks
281 // ---------------
282
283 $hooks = array();
284 // check pixcustomify defaults
285 if (isset($defaults['processor']['preupdate'])) {
286 $hooks = $defaults['processor']['preupdate'];
287 }
288 // check plugin defaults
289 if (isset($plugin_hooks['preupdate'])) {
290 $hooks = array_merge($hooks, $plugin_hooks['preupdate']);
291 }
292
293 // Execute hooks
294 // -------------
295
296 foreach ($hooks as $rule) {
297 $callback = pixcustomify::callback($rule, $this->meta);
298 call_user_func($callback, $input, $this);
299 }
300 }
301
302 /**
303 * Execute postupdate hooks on input.
304 */
305 protected function postupdate($input)
306 {
307 $defaults = pixcustomify::defaults();
308 $plugin_hooks = $this->meta->get('processor', array('preupdate' => array(), 'postupdate' => array()));
309
310 // Calculate hooks
311 // ---------------
312
313 $hooks = array();
314 // check pixcustomify defaults
315 if (isset($defaults['processor']['postupdate'])) {
316 $hooks = $defaults['processor']['postupdate'];
317 }
318 // check plugin defaults
319 if (isset($plugin_hooks['postupdate'])) {
320 $hooks = array_merge($hooks, $plugin_hooks['postupdate']);
321 }
322
323 // Execute hooks
324 // -------------
325
326 foreach ($hooks as $rule) {
327 $callback = pixcustomify::callback($rule, $this->meta);
328 call_user_func($callback, $input, $this);
329 }
330 }
331
332 } # class
333