PluginProbe
Customify / 1.2.6
Customify v1.2.6
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 / core / classes / Processor.php

Processor.php in Customify 1.2.6, at core/classes/Processor.php

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