| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- class NextendSocialLoginSettings {
- protected $optionKey;
- protected $settings = array(
- 'default' => array(),
- 'stored' => array(),
- 'final' => array()
- );
- /**
- * NextendSocialLoginSettings constructor.
- *
- * @param $optionKey string
- * @param $defaultSettings array
- */
- public function __construct($optionKey, $defaultSettings) {
- $this->optionKey = $optionKey;
- $this->settings['default'] = $defaultSettings;
- $storedSettings = get_option($this->optionKey);
- if ($storedSettings !== false) {
- $storedSettings = (array)maybe_unserialize($storedSettings);
- } else {
- $storedSettings = array();
- }
- $this->settings['stored'] = array_merge($this->settings['default'], $storedSettings);
- $this->settings['final'] = apply_filters('nsl_finalize_settings_' . $optionKey, $this->settings['stored']);
- }
- public function get($key, $storage = 'final') {
- if (!isset($this->settings[$storage][$key])) {
- return false;
- }
- return $this->settings[$storage][$key];
- }
- public function set($key, $value) {
- $this->settings['stored'][$key] = $value;
- $this->storeSettings();
- }
- public function getAll($storage = 'final') {
- return $this->settings[$storage];
- }
- /**
- * @param array $postedData
- *
- * @return bool
- */
- public function update($postedData) {
- if (is_array($postedData)) {
- $newData = array();
- $newData = apply_filters('nsl_update_settings_validate_' . $this->optionKey, $newData, $postedData);
- if (count($newData)) {
- $isChanged = false;
- foreach ($newData AS $key => $value) {
- if ($this->settings['stored'][$key] != $value) {
- $this->settings['stored'][$key] = $value;
- $isChanged = true;
- }
- }
- if ($isChanged) {
- $allowedKeys = array_keys($this->settings['default']);
- $this->settings['stored'] = array_intersect_key($this->settings['stored'], array_flip($allowedKeys));
- $this->storeSettings();
- return true;
- }
- }
- }
- return false;
- }
- protected function storeSettings() {
- update_option($this->optionKey, maybe_serialize($this->settings['stored']));
- $this->settings['final'] = apply_filters('nsl_finalize_settings_' . $this->optionKey, $this->settings['stored']);
- }
- }
|