Nenhuma Descrição

class-settings.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class NextendSocialLoginSettings {
  3. protected $optionKey;
  4. protected $settings = array(
  5. 'default' => array(),
  6. 'stored' => array(),
  7. 'final' => array()
  8. );
  9. /**
  10. * NextendSocialLoginSettings constructor.
  11. *
  12. * @param $optionKey string
  13. * @param $defaultSettings array
  14. */
  15. public function __construct($optionKey, $defaultSettings) {
  16. $this->optionKey = $optionKey;
  17. $this->settings['default'] = $defaultSettings;
  18. $storedSettings = get_option($this->optionKey);
  19. if ($storedSettings !== false) {
  20. $storedSettings = (array)maybe_unserialize($storedSettings);
  21. } else {
  22. $storedSettings = array();
  23. }
  24. $this->settings['stored'] = array_merge($this->settings['default'], $storedSettings);
  25. $this->settings['final'] = apply_filters('nsl_finalize_settings_' . $optionKey, $this->settings['stored']);
  26. }
  27. public function get($key, $storage = 'final') {
  28. if (!isset($this->settings[$storage][$key])) {
  29. return false;
  30. }
  31. return $this->settings[$storage][$key];
  32. }
  33. public function set($key, $value) {
  34. $this->settings['stored'][$key] = $value;
  35. $this->storeSettings();
  36. }
  37. public function getAll($storage = 'final') {
  38. return $this->settings[$storage];
  39. }
  40. /**
  41. * @param array $postedData
  42. *
  43. * @return bool
  44. */
  45. public function update($postedData) {
  46. if (is_array($postedData)) {
  47. $newData = array();
  48. $newData = apply_filters('nsl_update_settings_validate_' . $this->optionKey, $newData, $postedData);
  49. if (count($newData)) {
  50. $isChanged = false;
  51. foreach ($newData AS $key => $value) {
  52. if ($this->settings['stored'][$key] != $value) {
  53. $this->settings['stored'][$key] = $value;
  54. $isChanged = true;
  55. }
  56. }
  57. if ($isChanged) {
  58. $allowedKeys = array_keys($this->settings['default']);
  59. $this->settings['stored'] = array_intersect_key($this->settings['stored'], array_flip($allowedKeys));
  60. $this->storeSettings();
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. protected function storeSettings() {
  68. update_option($this->optionKey, maybe_serialize($this->settings['stored']));
  69. $this->settings['final'] = apply_filters('nsl_finalize_settings_' . $this->optionKey, $this->settings['stored']);
  70. }
  71. }