| 1 |
<?php |
| 2 |
|
| 3 |
class VP_Option_Control_Set |
| 4 |
{ |
| 5 |
|
| 6 |
const SAVE_SUCCESS = 1; |
| 7 |
|
| 8 |
const SAVE_NOCHANGES = 2; |
| 9 |
|
| 10 |
const SAVE_FAILED = 3; |
| 11 |
|
| 12 |
private $_menus; |
| 13 |
|
| 14 |
private $_title; |
| 15 |
|
| 16 |
private $_logo; |
| 17 |
|
| 18 |
private $_layout; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->_menus = array(); |
| 23 |
} |
| 24 |
|
| 25 |
public function render() |
| 26 |
{ |
| 27 |
// Setup data |
| 28 |
$data = array('set' => $this); |
| 29 |
return VP_View::instance()->load('option/set', $data); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get Option Set Title |
| 34 |
* |
| 35 |
* @return String Option set title |
| 36 |
*/ |
| 37 |
public function get_title() { |
| 38 |
return $this->_title; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Set Option Set title |
| 43 |
* |
| 44 |
* @param String $_title Option set title |
| 45 |
*/ |
| 46 |
public function set_title($_title) { |
| 47 |
$this->_title = $_title; |
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Set _layout |
| 53 |
* |
| 54 |
* @return String _layout |
| 55 |
*/ |
| 56 |
public function get_layout() |
| 57 |
{ |
| 58 |
return $this->_layout; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get _layout |
| 63 |
* |
| 64 |
* @param String $_layout _layout |
| 65 |
*/ |
| 66 |
public function set_layout($_layout) |
| 67 |
{ |
| 68 |
$this->_layout = $_layout; |
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get logo |
| 74 |
* |
| 75 |
* @return String Logo URL |
| 76 |
*/ |
| 77 |
public function get_logo() { |
| 78 |
return $this->_logo; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Set logo |
| 83 |
* |
| 84 |
* @param String $_logo Logo URL |
| 85 |
*/ |
| 86 |
public function set_logo($_logo) { |
| 87 |
$this->_logo = $_logo; |
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
public function add_menu($menu) |
| 92 |
{ |
| 93 |
$this->_menus[] = $menu; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Getter of $_menus |
| 98 |
* |
| 99 |
* @return Array Collection of menus object |
| 100 |
*/ |
| 101 |
public function get_menus() { |
| 102 |
return $this->_menus; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Setter of $_menus |
| 107 |
* |
| 108 |
* @param Array $_menus Collection of menus object |
| 109 |
*/ |
| 110 |
public function set_menus($_menus) { |
| 111 |
$this->_menus = $_menus; |
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
public function get_fields($include_section = false) |
| 116 |
{ |
| 117 |
if(!function_exists('loop_controls')) |
| 118 |
{ |
| 119 |
function loop_controls($menu, $include_section) |
| 120 |
{ |
| 121 |
$fields = array(); |
| 122 |
foreach ( $menu->get_controls() as $control ) |
| 123 |
{ |
| 124 |
if( get_class($control) === 'VP_Option_Control_Group_Section' ) |
| 125 |
{ |
| 126 |
if($include_section) |
| 127 |
{ |
| 128 |
$fields[$control->get_name()] = $control; |
| 129 |
} |
| 130 |
foreach ( $control->get_fields() as $field ) |
| 131 |
{ |
| 132 |
if( VP_Util_Reflection::field_type_from_class(get_class($field)) != 'impexp' ) |
| 133 |
{ |
| 134 |
$fields[$field->get_name()] = $field; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
if( VP_Util_Reflection::field_type_from_class(get_class($control)) != 'impexp' ) |
| 141 |
{ |
| 142 |
$fields[$control->get_name()] = $control; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return $fields; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$fields = array(); |
| 151 |
|
| 152 |
foreach ( $this->_menus as $menu ) |
| 153 |
{ |
| 154 |
$submenus = $menu->get_menus(); |
| 155 |
if( !empty($submenus) ) |
| 156 |
{ |
| 157 |
foreach ( $submenus as $submenu ) |
| 158 |
{ |
| 159 |
$fields = array_merge($fields, loop_controls($submenu, $include_section)); |
| 160 |
} |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
$fields = array_merge($fields, loop_controls($menu, $include_section)); |
| 165 |
} |
| 166 |
} |
| 167 |
return $fields; |
| 168 |
} |
| 169 |
|
| 170 |
public function get_field_types() |
| 171 |
{ |
| 172 |
$fields = $this->get_fields(); |
| 173 |
$types = array(); |
| 174 |
foreach ($fields as $field) |
| 175 |
{ |
| 176 |
$type = VP_Util_Reflection::field_type_from_class(get_class($field)); |
| 177 |
if(!in_array($type, $types)) |
| 178 |
$types[] = $type; |
| 179 |
} |
| 180 |
return $types; |
| 181 |
} |
| 182 |
|
| 183 |
public function get_field($name) |
| 184 |
{ |
| 185 |
$fields = $this->get_fields(); |
| 186 |
if(array_key_exists($name, $fields)) |
| 187 |
{ |
| 188 |
return $fields[$name]; |
| 189 |
} |
| 190 |
return null; |
| 191 |
} |
| 192 |
|
| 193 |
public function process_binding() |
| 194 |
{ |
| 195 |
|
| 196 |
$fields = $this->get_fields(); |
| 197 |
|
| 198 |
foreach ($fields as $field) |
| 199 |
{ |
| 200 |
$bind = $field->get_binding(); |
| 201 |
$val = $field->get_value(); |
| 202 |
if(!empty($bind) and is_null($val)) |
| 203 |
{ |
| 204 |
$bind = explode('|', $bind); |
| 205 |
$func = $bind[0]; |
| 206 |
$params = $bind[1]; |
| 207 |
$params = preg_split('/[\s,]+/', $params); |
| 208 |
$values = array(); |
| 209 |
foreach ($params as $param) |
| 210 |
{ |
| 211 |
if(array_key_exists($param, $fields)) |
| 212 |
{ |
| 213 |
$values[] = $fields[$param]->get_value(); |
| 214 |
} |
| 215 |
} |
| 216 |
$result = call_user_func_array($func, $values); |
| 217 |
|
| 218 |
if(VP_Util_Reflection::is_multiselectable($field)) |
| 219 |
{ |
| 220 |
$result = (array) $result; |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
if(is_array($result)) |
| 225 |
{ |
| 226 |
$result = reset($result); |
| 227 |
} |
| 228 |
$result = (String) $result; |
| 229 |
} |
| 230 |
$field->set_value($result); |
| 231 |
} |
| 232 |
|
| 233 |
if($field instanceof VP_Control_FieldMulti) |
| 234 |
{ |
| 235 |
$bind = $field->get_items_binding(); |
| 236 |
if(!empty($bind)) |
| 237 |
{ |
| 238 |
$bind = explode('|', $bind); |
| 239 |
$func = $bind[0]; |
| 240 |
$params = $bind[1]; |
| 241 |
$params = preg_split('/[\s,]+/', $params); |
| 242 |
$values = array(); |
| 243 |
foreach ($params as $param) |
| 244 |
{ |
| 245 |
if(array_key_exists($param, $fields)) |
| 246 |
{ |
| 247 |
$values[] = $fields[$param]->get_value(); |
| 248 |
} |
| 249 |
} |
| 250 |
$items = call_user_func_array($func, $values); |
| 251 |
if(is_array($items) && !empty($items)) |
| 252 |
{ |
| 253 |
$field->set_items(array()); |
| 254 |
$field->add_items_from_array($items); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
public function process_dependencies() |
| 262 |
{ |
| 263 |
$fields = $this->get_fields(true); |
| 264 |
|
| 265 |
foreach ($fields as $field) |
| 266 |
{ |
| 267 |
$dependency = $field->get_dependency(); |
| 268 |
if(!empty($dependency)) |
| 269 |
{ |
| 270 |
$dependency = explode('|', $dependency); |
| 271 |
$func = $dependency[0]; |
| 272 |
$params = $dependency[1]; |
| 273 |
$params = preg_split('/[\s,]+/', $params); |
| 274 |
$values = array(); |
| 275 |
foreach ($params as $param) |
| 276 |
{ |
| 277 |
if(array_key_exists($param, $fields)) |
| 278 |
{ |
| 279 |
$values[] = $fields[$param]->get_value(); |
| 280 |
} |
| 281 |
} |
| 282 |
$result = call_user_func_array($func, $values); |
| 283 |
if(!$result) |
| 284 |
{ |
| 285 |
$field->add_container_extra_classes('vp-dep-inactive'); |
| 286 |
$field->is_hidden(true); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
public function normalize_values($opt_arr) |
| 294 |
{ |
| 295 |
$fields = $this->get_fields(); |
| 296 |
|
| 297 |
foreach ($opt_arr as $key => $value) |
| 298 |
{ |
| 299 |
if(array_key_exists($key, $fields)) |
| 300 |
{ |
| 301 |
$is_multi = VP_Util_Reflection::is_multiselectable($fields[$key]); |
| 302 |
if( $is_multi and !is_array($value) ) |
| 303 |
{ |
| 304 |
$opt_arr[$key] = array($value); |
| 305 |
} |
| 306 |
if( !$is_multi and is_array($value)) |
| 307 |
{ |
| 308 |
$opt_arr[$key] = ''; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
return $opt_arr; |
| 313 |
} |
| 314 |
|
| 315 |
public function get_defaults() |
| 316 |
{ |
| 317 |
$defaults = array(); |
| 318 |
$fields = $this->get_fields(); |
| 319 |
foreach ( $fields as $field ) |
| 320 |
{ |
| 321 |
$defaults[$field->get_name()] = $field->get_default(); |
| 322 |
} |
| 323 |
return $defaults; |
| 324 |
} |
| 325 |
|
| 326 |
public function get_values() |
| 327 |
{ |
| 328 |
$values = array(); |
| 329 |
$fields = $this->get_fields(); |
| 330 |
foreach ( $fields as $field ) |
| 331 |
{ |
| 332 |
$values[$field->get_name()] = $field->get_value(); |
| 333 |
} |
| 334 |
return $values; |
| 335 |
} |
| 336 |
|
| 337 |
public function setup($options) |
| 338 |
{ |
| 339 |
// populate option to fields' values |
| 340 |
$this->populate_values($options); |
| 341 |
|
| 342 |
// process binding |
| 343 |
$this->process_binding(); |
| 344 |
|
| 345 |
// process dependencies |
| 346 |
$this->process_dependencies(); |
| 347 |
} |
| 348 |
|
| 349 |
public function save($option_key) |
| 350 |
{ |
| 351 |
$opt = $this->get_values(); |
| 352 |
|
| 353 |
do_action('vp_option_set_before_save', $opt); |
| 354 |
|
| 355 |
if(update_option($option_key, $opt)) |
| 356 |
{ |
| 357 |
$result['status'] = true; |
| 358 |
$result['code'] = self::SAVE_SUCCESS; |
| 359 |
$result['message'] = __('Saving successful', 'wp-ultimate-recipe'); |
| 360 |
$curr_opt = get_option($option_key, array()); |
| 361 |
} |
| 362 |
else |
| 363 |
{ |
| 364 |
$curr_opt = get_option($option_key, array()); |
| 365 |
$changed = $opt !== $curr_opt; |
| 366 |
if($changed) |
| 367 |
{ |
| 368 |
$result['status'] = false; |
| 369 |
$result['code'] = self::SAVE_FAILED; |
| 370 |
$result['message'] = __('Saving failed', 'wp-ultimate-recipe'); |
| 371 |
} |
| 372 |
else |
| 373 |
{ |
| 374 |
$result['status'] = true; |
| 375 |
$result['code'] = self::SAVE_NOCHANGES; |
| 376 |
$result['message'] = __('No changes made', 'wp-ultimate-recipe'); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
do_action('vp_option_set_after_save', $curr_opt, $result['status'], $option_key); |
| 381 |
|
| 382 |
return $result; |
| 383 |
} |
| 384 |
|
| 385 |
public function populate_values($opt, $force_update = false) |
| 386 |
{ |
| 387 |
$fields = $this->get_fields(); |
| 388 |
foreach ( $fields as $field ) |
| 389 |
{ |
| 390 |
$is_multi = VP_Util_Reflection::is_multiselectable($field); |
| 391 |
if( array_key_exists($field->get_name(), $opt) ) |
| 392 |
{ |
| 393 |
if( $is_multi and is_array($opt[$field->get_name()]) ) |
| 394 |
{ |
| 395 |
$field->set_value($opt[$field->get_name()]); |
| 396 |
} |
| 397 |
if( !$is_multi and !is_array($opt[$field->get_name()]) ) |
| 398 |
{ |
| 399 |
$field->set_value($opt[$field->get_name()]); |
| 400 |
} |
| 401 |
} |
| 402 |
else |
| 403 |
{ |
| 404 |
if($force_update) |
| 405 |
{ |
| 406 |
if($is_multi) |
| 407 |
{ |
| 408 |
$field->set_value(array()); |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
$field->set_value(''); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* EOF |
| 423 |
*/ |