settings.php
| 1 | <?php |
| 2 | |
| 3 | acf_register_store('acfe/settings'); |
| 4 | |
| 5 | if(!class_exists('acfe_settings')): |
| 6 | |
| 7 | class acfe_settings{ |
| 8 | |
| 9 | public $settings = array(); |
| 10 | |
| 11 | public $upgrades = array( |
| 12 | '0_8_5' => '0.8.5', |
| 13 | '0_8_6' => '0.8.6', |
| 14 | ); |
| 15 | |
| 16 | public $model = array( |
| 17 | |
| 18 | // Version |
| 19 | 'version' => ACFE_VERSION, |
| 20 | |
| 21 | // Modules |
| 22 | 'modules' => array( |
| 23 | |
| 24 | 'author' => array( |
| 25 | 'active' => true, |
| 26 | ), |
| 27 | |
| 28 | 'dev' => array( |
| 29 | 'active' => false, |
| 30 | ), |
| 31 | |
| 32 | 'meta' => array( |
| 33 | 'active' => false, |
| 34 | ), |
| 35 | |
| 36 | 'option' => array( |
| 37 | 'active' => true, |
| 38 | ), |
| 39 | |
| 40 | 'ui' => array( |
| 41 | 'active' => true, |
| 42 | ), |
| 43 | |
| 44 | 'dynamic_block_type' => array( |
| 45 | 'active' => true, |
| 46 | 'settings' => array(), |
| 47 | 'data' => array() |
| 48 | ), |
| 49 | |
| 50 | 'dynamic_form' => array( |
| 51 | 'active' => true, |
| 52 | 'settings' => array(), |
| 53 | 'data' => array() |
| 54 | ), |
| 55 | |
| 56 | 'dynamic_option' => array( |
| 57 | 'active' => true, |
| 58 | 'settings' => array(), |
| 59 | 'data' => array() |
| 60 | ), |
| 61 | |
| 62 | 'dynamic_post_type' => array( |
| 63 | 'active' => true, |
| 64 | 'settings' => array(), |
| 65 | 'data' => array() |
| 66 | ), |
| 67 | |
| 68 | 'dynamic_taxonomy' => array( |
| 69 | 'active' => true, |
| 70 | 'settings' => array(), |
| 71 | 'data' => array() |
| 72 | ), |
| 73 | |
| 74 | ), |
| 75 | |
| 76 | // Upgrades |
| 77 | 'upgrades' => array(), |
| 78 | ); |
| 79 | |
| 80 | function __construct(){ |
| 81 | |
| 82 | $this->settings = acf_get_store('acfe/settings'); |
| 83 | |
| 84 | if(empty($this->settings->get_data())){ |
| 85 | |
| 86 | $option = get_option('acfe', array()); |
| 87 | |
| 88 | if(!empty($option)){ |
| 89 | |
| 90 | $this->settings->set($option); |
| 91 | |
| 92 | $this->version(); |
| 93 | |
| 94 | }else{ |
| 95 | |
| 96 | $this->reset(); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | function get($selector = false){ |
| 105 | |
| 106 | return $this->array_get($this->settings->get(), $selector); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | function set($selector = false, $value = null, $update = true, $append = false){ |
| 111 | |
| 112 | if($value === null) |
| 113 | return false; |
| 114 | |
| 115 | $rows = $this->settings->get(); |
| 116 | |
| 117 | if($append){ |
| 118 | |
| 119 | $this->array_append($rows, $selector, $value); |
| 120 | |
| 121 | }else{ |
| 122 | |
| 123 | $this->array_set($rows, $selector, $value); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | $this->settings->set($rows); |
| 128 | |
| 129 | if($update) |
| 130 | $this->update(); |
| 131 | |
| 132 | return $this; |
| 133 | |
| 134 | } |
| 135 | |
| 136 | function clear($selector = false, $update = true){ |
| 137 | |
| 138 | $rows = $this->settings->get(); |
| 139 | |
| 140 | $this->array_clear($rows, $selector); |
| 141 | |
| 142 | $this->settings->set($rows); |
| 143 | |
| 144 | if($update) |
| 145 | $this->update(); |
| 146 | |
| 147 | return $this; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | function delete($selector = false, $update = true){ |
| 152 | |
| 153 | // Single |
| 154 | if(strpos($selector, '.') === false){ |
| 155 | |
| 156 | $this->settings->remove($selector); |
| 157 | |
| 158 | // Array |
| 159 | }else{ |
| 160 | |
| 161 | $rows = $this->settings->get(); |
| 162 | |
| 163 | $this->array_remove($rows, $selector); |
| 164 | |
| 165 | $this->settings->set($rows); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | if($update) |
| 170 | $this->update(); |
| 171 | |
| 172 | return $this; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | function append($selector = false, $value = null, $update = true){ |
| 177 | |
| 178 | if($selector === false && $value === null) |
| 179 | return false; |
| 180 | |
| 181 | // Allow simple append without selector |
| 182 | if($value === null){ |
| 183 | |
| 184 | $value = $selector; |
| 185 | $selector = false; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | return $this->set($selector, $value, $update, true); |
| 190 | |
| 191 | } |
| 192 | |
| 193 | function array_get($array, $key, $default = null) { |
| 194 | |
| 195 | if(empty($key)) |
| 196 | return $array; |
| 197 | |
| 198 | if(!is_array($key)) |
| 199 | $key = explode('.', $key); |
| 200 | |
| 201 | $count = count($key); |
| 202 | $i=-1; |
| 203 | |
| 204 | foreach($key as $segment){ |
| 205 | |
| 206 | $i++; |
| 207 | |
| 208 | if(!isset($array[$segment])) |
| 209 | continue; |
| 210 | |
| 211 | if($i+1 === $count){ |
| 212 | |
| 213 | return $array[$segment]; |
| 214 | |
| 215 | } |
| 216 | |
| 217 | unset($key[$i]); |
| 218 | |
| 219 | return $this->array_get($array[$segment], $key, $default); |
| 220 | |
| 221 | |
| 222 | } |
| 223 | |
| 224 | return $default; |
| 225 | |
| 226 | } |
| 227 | |
| 228 | function array_set(&$array, $key, $value){ |
| 229 | |
| 230 | if(empty($key)) |
| 231 | return $array = $value; |
| 232 | |
| 233 | $keys = explode('.', $key); |
| 234 | |
| 235 | while(count($keys) > 1){ |
| 236 | |
| 237 | $key = array_shift($keys); |
| 238 | |
| 239 | if(!isset($array[$key]) || !is_array($array[$key])){ |
| 240 | |
| 241 | $array[$key] = array(); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | $array =& $array[$key]; |
| 246 | |
| 247 | } |
| 248 | |
| 249 | $array[array_shift($keys)] = $value; |
| 250 | |
| 251 | return $array; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | function array_append(&$array, $key, $value){ |
| 256 | |
| 257 | $get = $this->array_get($array, $key); |
| 258 | |
| 259 | $old = acf_get_array($get); |
| 260 | $value = acf_get_array($value); |
| 261 | |
| 262 | $value = array_merge($old, $value); |
| 263 | |
| 264 | $this->array_set($array, $key, $value); |
| 265 | |
| 266 | return $array; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | function array_clear(&$array, $key){ |
| 271 | |
| 272 | $get = $this->array_get($array, $key); |
| 273 | |
| 274 | if($get === null) |
| 275 | return $array; |
| 276 | |
| 277 | $value = null; |
| 278 | |
| 279 | if(is_array($get)) |
| 280 | $value = array(); |
| 281 | |
| 282 | $this->array_set($array, $key, $value); |
| 283 | |
| 284 | return $array; |
| 285 | |
| 286 | } |
| 287 | |
| 288 | function array_remove(&$array, $keys){ |
| 289 | |
| 290 | $original =& $array; |
| 291 | |
| 292 | foreach((array)$keys as $key){ |
| 293 | |
| 294 | $parts = explode('.', $key); |
| 295 | |
| 296 | while(count($parts) > 1){ |
| 297 | |
| 298 | $part = array_shift($parts); |
| 299 | |
| 300 | if(isset($array[$part]) && is_array($array[$part])){ |
| 301 | |
| 302 | $array =& $array[$part]; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | } |
| 307 | |
| 308 | unset($array[array_shift($parts)]); |
| 309 | |
| 310 | // clean up after each pass |
| 311 | $array =& $original; |
| 312 | |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
| 317 | function reset(){ |
| 318 | |
| 319 | $this->model['upgrades'] = $this->upgrades; |
| 320 | |
| 321 | $this->set('', $this->model, true); |
| 322 | |
| 323 | new acfe_upgrades(); |
| 324 | |
| 325 | add_action('init', array($this, 'reset_modules')); |
| 326 | |
| 327 | } |
| 328 | |
| 329 | function reset_modules(){ |
| 330 | |
| 331 | // Reset Post Types |
| 332 | $post_types = get_posts(array( |
| 333 | 'post_type' => 'acfe-dpt', |
| 334 | 'posts_per_page' => -1, |
| 335 | 'fields' => 'ids' |
| 336 | )); |
| 337 | |
| 338 | if(!empty($post_types)){ |
| 339 | |
| 340 | foreach($post_types as $post_id){ |
| 341 | |
| 342 | acfe_dpt_filter_save($post_id); |
| 343 | |
| 344 | acf_log('[ACF Extended] Reset: Dynamic Post Type "' . get_post_field('post_title', $post_id) . '"'); |
| 345 | |
| 346 | } |
| 347 | |
| 348 | } |
| 349 | |
| 350 | // Reset Taxonomies |
| 351 | $taxonomies = get_posts(array( |
| 352 | 'post_type' => 'acfe-dt', |
| 353 | 'posts_per_page' => -1, |
| 354 | 'fields' => 'ids' |
| 355 | )); |
| 356 | |
| 357 | if(!empty($taxonomies)){ |
| 358 | |
| 359 | foreach($taxonomies as $post_id){ |
| 360 | |
| 361 | acfe_dt_filter_save($post_id); |
| 362 | |
| 363 | acf_log('[ACF Extended] Reset: Dynamic Taxonomy "' . get_post_field('post_title', $post_id) . '"'); |
| 364 | |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | |
| 369 | // Reset Block Types |
| 370 | $block_types = get_posts(array( |
| 371 | 'post_type' => 'acfe-dbt', |
| 372 | 'posts_per_page' => -1, |
| 373 | 'fields' => 'ids' |
| 374 | )); |
| 375 | |
| 376 | if(!empty($block_types)){ |
| 377 | |
| 378 | foreach($block_types as $post_id){ |
| 379 | |
| 380 | acfe_dbt_filter_save($post_id); |
| 381 | |
| 382 | acf_log('[ACF Extended] Reset: Dynamic Block Type "' . get_post_field('post_title', $post_id) . '"'); |
| 383 | |
| 384 | } |
| 385 | |
| 386 | } |
| 387 | |
| 388 | // Reset Options Pages |
| 389 | $options_pages = get_posts(array( |
| 390 | 'post_type' => 'acfe-dop', |
| 391 | 'posts_per_page' => -1, |
| 392 | 'fields' => 'ids' |
| 393 | )); |
| 394 | |
| 395 | if(!empty($options_pages)){ |
| 396 | |
| 397 | foreach($options_pages as $post_id){ |
| 398 | |
| 399 | acfe_dop_filter_save($post_id); |
| 400 | |
| 401 | acf_log('[ACF Extended] Reset: Dynamic Options Page "' . get_post_field('post_title', $post_id) . '"'); |
| 402 | |
| 403 | } |
| 404 | |
| 405 | } |
| 406 | |
| 407 | } |
| 408 | |
| 409 | function version(){ |
| 410 | |
| 411 | $version = $this->get('version'); |
| 412 | |
| 413 | if(acf_version_compare($version, '<', ACFE_VERSION)){ |
| 414 | |
| 415 | if(!empty($this->upgrades)){ |
| 416 | |
| 417 | $do_upgrades = false; |
| 418 | |
| 419 | foreach($this->upgrades as $function => $v){ |
| 420 | |
| 421 | if(acf_version_compare($v, '<=', $version)) |
| 422 | continue; |
| 423 | |
| 424 | $do_upgrades = true; |
| 425 | |
| 426 | $this->model['upgrades'][$function] = true; |
| 427 | |
| 428 | } |
| 429 | |
| 430 | } |
| 431 | |
| 432 | $data = $this->get(); |
| 433 | $model = $this->model; |
| 434 | |
| 435 | $new_model = $this->parse_args_r($data, $model); |
| 436 | |
| 437 | $new_model['version'] = ACFE_VERSION; |
| 438 | |
| 439 | $this->set('', $new_model, true); |
| 440 | |
| 441 | if($do_upgrades){ |
| 442 | |
| 443 | new acfe_upgrades(); |
| 444 | |
| 445 | } |
| 446 | |
| 447 | } |
| 448 | |
| 449 | } |
| 450 | |
| 451 | function update(){ |
| 452 | |
| 453 | $settings = $this->settings->get(); |
| 454 | |
| 455 | update_option('acfe', $settings, 'true'); |
| 456 | |
| 457 | } |
| 458 | |
| 459 | function parse_args_r(&$a, $b){ |
| 460 | |
| 461 | $a = (array) $a; |
| 462 | $b = (array) $b; |
| 463 | $r = $b; |
| 464 | |
| 465 | foreach($a as $k => &$v){ |
| 466 | |
| 467 | if(is_array($v) && isset($r[ $k ])){ |
| 468 | $r[$k] = $this->parse_args_r($v, $r[ $k ]); |
| 469 | }else{ |
| 470 | $r[$k] = $v; |
| 471 | } |
| 472 | |
| 473 | } |
| 474 | |
| 475 | return $r; |
| 476 | |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | |
| 481 | endif; |
| 482 | |
| 483 | function acfe_settings($selector = null, $value = null, $update = true){ |
| 484 | |
| 485 | $instance = acf_get_instance('acfe_settings'); |
| 486 | |
| 487 | // Set |
| 488 | if($selector !== null && $value !== null){ |
| 489 | |
| 490 | return $instance->set($selector, $value, $update); |
| 491 | |
| 492 | } |
| 493 | |
| 494 | // Get |
| 495 | elseif($selector !== null && $value === null){ |
| 496 | |
| 497 | return $instance->get($selector); |
| 498 | |
| 499 | } |
| 500 | |
| 501 | return $instance; |
| 502 | |
| 503 | } |