nextend-facebook-connect
Last commit date
NSL
3 years ago
admin
3 years ago
includes
3 years ago
js
3 years ago
languages
3 years ago
providers
3 years ago
template-parts
3 years ago
class-settings.php
6 years ago
compat.php
6 years ago
index.html
13 years ago
licence.txt
6 years ago
nextend-facebook-connect.php
3 years ago
nextend-social-login.php
3 years ago
readme.txt
3 years ago
widget.php
6 years ago
class-settings.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | class NextendSocialLoginSettings { |
| 4 | |
| 5 | protected $optionKey; |
| 6 | |
| 7 | protected $settings = array( |
| 8 | 'default' => array(), |
| 9 | 'stored' => array(), |
| 10 | 'final' => array() |
| 11 | ); |
| 12 | |
| 13 | /** |
| 14 | * NextendSocialLoginSettings constructor. |
| 15 | * |
| 16 | * @param $optionKey string |
| 17 | * @param $defaultSettings array |
| 18 | */ |
| 19 | public function __construct($optionKey, $defaultSettings) { |
| 20 | $this->optionKey = $optionKey; |
| 21 | |
| 22 | $this->settings['default'] = $defaultSettings; |
| 23 | |
| 24 | |
| 25 | $storedSettings = get_option($this->optionKey); |
| 26 | if ($storedSettings !== false) { |
| 27 | $storedSettings = (array)maybe_unserialize($storedSettings); |
| 28 | } else { |
| 29 | $storedSettings = array(); |
| 30 | } |
| 31 | |
| 32 | $this->settings['stored'] = array_merge($this->settings['default'], $storedSettings); |
| 33 | |
| 34 | $this->settings['final'] = apply_filters('nsl_finalize_settings_' . $optionKey, $this->settings['stored']); |
| 35 | } |
| 36 | |
| 37 | public function get($key, $storage = 'final') { |
| 38 | if (!isset($this->settings[$storage][$key])) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | return $this->settings[$storage][$key]; |
| 43 | } |
| 44 | |
| 45 | public function set($key, $value) { |
| 46 | $this->settings['stored'][$key] = $value; |
| 47 | $this->storeSettings(); |
| 48 | } |
| 49 | |
| 50 | public function getAll($storage = 'final') { |
| 51 | return $this->settings[$storage]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param array $postedData |
| 56 | * |
| 57 | * @return bool |
| 58 | */ |
| 59 | public function update($postedData) { |
| 60 | if (is_array($postedData)) { |
| 61 | $newData = array(); |
| 62 | $newData = apply_filters('nsl_update_settings_validate_' . $this->optionKey, $newData, $postedData); |
| 63 | |
| 64 | if (count($newData)) { |
| 65 | |
| 66 | $isChanged = false; |
| 67 | foreach ($newData AS $key => $value) { |
| 68 | if ($this->settings['stored'][$key] != $value) { |
| 69 | $this->settings['stored'][$key] = $value; |
| 70 | $isChanged = true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ($isChanged) { |
| 75 | $allowedKeys = array_keys($this->settings['default']); |
| 76 | $this->settings['stored'] = array_intersect_key($this->settings['stored'], array_flip($allowedKeys)); |
| 77 | |
| 78 | $this->storeSettings(); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | protected function storeSettings() { |
| 89 | update_option($this->optionKey, maybe_serialize($this->settings['stored'])); |
| 90 | |
| 91 | $this->settings['final'] = apply_filters('nsl_finalize_settings_' . $this->optionKey, $this->settings['stored']); |
| 92 | } |
| 93 | } |