| 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 |
$input = $this->cleanup_input($_POST); |
| 103 |
$errors = $this->validate_input($input); |
| 104 |
|
| 105 |
if (empty($errors)) { |
| 106 |
$this->preupdate($input); |
| 107 |
$this->status['dataupdate'] = true; |
| 108 |
$current_values = get_option($option_key); |
| 109 |
$new_option = array_merge($current_values, $input); |
| 110 |
update_option($option_key, $new_option); |
| 111 |
$this->data = pixcustomify::instance('PixCustomifyMeta', $input); |
| 112 |
$this->postupdate($input); |
| 113 |
} |
| 114 |
else { // got errors |
| 115 |
$this->status['errors'] = $errors; |
| 116 |
$this->load_data_from_database($option_key); |
| 117 |
$this->data->overwritemeta($input); |
| 118 |
} |
| 119 |
} |
| 120 |
else { // GET request |
| 121 |
$this->load_data_from_database($option_key); |
| 122 |
} |
| 123 |
} |
| 124 |
catch (Exception $e) { |
| 125 |
if ($this->meta->get('debug', false)) { |
| 126 |
throw $e; |
| 127 |
} |
| 128 |
|
| 129 |
$this->status['state'] = 'error'; |
| 130 |
$this->status['message'] = $e->getMessage(); |
| 131 |
} |
| 132 |
|
| 133 |
return $this; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @return static $this |
| 138 |
*/ |
| 139 |
protected function load_data_from_database($option_key) { |
| 140 |
$dbconfig = get_option($option_key); |
| 141 |
|
| 142 |
if ($dbconfig === false) { |
| 143 |
throw new Exception('Unable to retrieve options.'); |
| 144 |
} |
| 145 |
|
| 146 |
$this->data = pixcustomify::instance('PixCustomifyMeta', $dbconfig); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @param array input |
| 151 |
* @return array cleaned up input |
| 152 |
*/ |
| 153 |
protected function cleanup_input($input) { |
| 154 |
$defaults = pixcustomify::defaults(); |
| 155 |
$plugin_cleanup = $this->meta->get('cleanup', array()); |
| 156 |
|
| 157 |
foreach ($this->fields->metadata_array() as $key => $field) { |
| 158 |
|
| 159 |
// ensure a value is present |
| 160 |
if ( ! isset($input[$key])) { |
| 161 |
$input[$key] = null; |
| 162 |
} |
| 163 |
|
| 164 |
// Calculate cleanup rules |
| 165 |
// ----------------------- |
| 166 |
|
| 167 |
$cleanup = array(); |
| 168 |
// check pixcustomify defaults |
| 169 |
if (isset($defaults['cleanup'][$field['type']])) { |
| 170 |
$cleanup = $defaults['cleanup'][$field['type']]; |
| 171 |
} |
| 172 |
// check plugin defaults |
| 173 |
if (isset($plugin_cleanup[$field['type']])) { |
| 174 |
$cleanup = array_merge($cleanup, $plugin_cleanup[$field['type']]); |
| 175 |
} |
| 176 |
// check field presets |
| 177 |
if (isset($field['cleanup'])) { |
| 178 |
$cleanup = array_merge($cleanup, $field['cleanup']); |
| 179 |
} |
| 180 |
|
| 181 |
// Perform Cleanup |
| 182 |
// --------------- |
| 183 |
|
| 184 |
foreach ($cleanup as $rule) { |
| 185 |
$callback = pixcustomify::callback($rule, $this->meta); |
| 186 |
$input[$key] = call_user_func($callback, $input[$key], $field, $this); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $input; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @param array input |
| 195 |
* @return array |
| 196 |
*/ |
| 197 |
protected function validate_input($input) { |
| 198 |
$validator = pixcustomify::instance('PixCustomifyValidator', $this->meta, $this->fields); |
| 199 |
return $validator->validate($input); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* @return boolean |
| 204 |
*/ |
| 205 |
protected function form_was_submitted() { |
| 206 |
return $_SERVER['REQUEST_METHOD'] === 'POST'; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* @return array |
| 211 |
*/ |
| 212 |
function status() { |
| 213 |
if ($this->status === null) { |
| 214 |
$this->run(); |
| 215 |
} |
| 216 |
|
| 217 |
return $this->status; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* @return PixCustomifyMeta current data (influenced by user submitted data) |
| 222 |
*/ |
| 223 |
function data() { |
| 224 |
if ($this->status === null) { |
| 225 |
$this->run(); |
| 226 |
} |
| 227 |
|
| 228 |
return $this->data; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Shorthand. |
| 233 |
* |
| 234 |
* @return array |
| 235 |
*/ |
| 236 |
function errors() { |
| 237 |
if ($this->status === null) { |
| 238 |
$this->run(); |
| 239 |
} |
| 240 |
|
| 241 |
return $this->status['errors']; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Shorthand. |
| 246 |
* |
| 247 |
* @return boolean |
| 248 |
*/ |
| 249 |
function performed_update() { |
| 250 |
if ($this->status === null) { |
| 251 |
$this->run(); |
| 252 |
} |
| 253 |
|
| 254 |
return $this->status['dataupdate']; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* @return boolean true if state is nominal |
| 259 |
*/ |
| 260 |
function ok() { |
| 261 |
if ($this->status === null) { |
| 262 |
$this->run(); |
| 263 |
} |
| 264 |
|
| 265 |
return $this->status['state'] == 'nominal'; |
| 266 |
} |
| 267 |
|
| 268 |
// ------------------------------------------------------------------------ |
| 269 |
// Hooks |
| 270 |
|
| 271 |
/** |
| 272 |
* Execute preupdate hooks on input. |
| 273 |
*/ |
| 274 |
protected function preupdate($input) |
| 275 |
{ |
| 276 |
$defaults = pixcustomify::defaults(); |
| 277 |
$plugin_hooks = $this->meta->get('processor', array('preupdate' => array(), 'postupdate' => array())); |
| 278 |
|
| 279 |
// Calculate hooks |
| 280 |
// --------------- |
| 281 |
|
| 282 |
$hooks = array(); |
| 283 |
// check pixcustomify defaults |
| 284 |
if (isset($defaults['processor']['preupdate'])) { |
| 285 |
$hooks = $defaults['processor']['preupdate']; |
| 286 |
} |
| 287 |
// check plugin defaults |
| 288 |
if (isset($plugin_hooks['preupdate'])) { |
| 289 |
$hooks = array_merge($hooks, $plugin_hooks['preupdate']); |
| 290 |
} |
| 291 |
|
| 292 |
// Execute hooks |
| 293 |
// ------------- |
| 294 |
|
| 295 |
foreach ($hooks as $rule) { |
| 296 |
$callback = pixcustomify::callback($rule, $this->meta); |
| 297 |
call_user_func($callback, $input, $this); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Execute postupdate hooks on input. |
| 303 |
*/ |
| 304 |
protected function postupdate($input) |
| 305 |
{ |
| 306 |
$defaults = pixcustomify::defaults(); |
| 307 |
$plugin_hooks = $this->meta->get('processor', array('preupdate' => array(), 'postupdate' => array())); |
| 308 |
|
| 309 |
// Calculate hooks |
| 310 |
// --------------- |
| 311 |
|
| 312 |
$hooks = array(); |
| 313 |
// check pixcustomify defaults |
| 314 |
if (isset($defaults['processor']['postupdate'])) { |
| 315 |
$hooks = $defaults['processor']['postupdate']; |
| 316 |
} |
| 317 |
// check plugin defaults |
| 318 |
if (isset($plugin_hooks['postupdate'])) { |
| 319 |
$hooks = array_merge($hooks, $plugin_hooks['postupdate']); |
| 320 |
} |
| 321 |
|
| 322 |
// Execute hooks |
| 323 |
// ------------- |
| 324 |
|
| 325 |
foreach ($hooks as $rule) { |
| 326 |
$callback = pixcustomify::callback($rule, $this->meta); |
| 327 |
call_user_func($callback, $input, $this); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
} # class |
| 332 |
|