| @@ -5,13 +5,8 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | namespace IvyForms\Services\Settings; |
| 8 | 8 | |
| 9 | -// phpcs:disable PSR1.Files.SideEffects | |
| 10 | -if (!defined('ABSPATH')) { | |
| 11 | - exit; // Exit if accessed directly | |
| 12 | -} | |
| 13 | - | |
| 14 | 9 | /** |
| 15 | 10 | * Class SettingsService |
| 16 | 11 | * |
| 17 | 12 | * @package IvyForms\Services\Settings |
| @@ -37,9 +32,39 @@ | ||
| 37 | 32 | */ |
| 38 | 33 | public function __construct() |
| 39 | 34 | { |
| 40 | 35 | $this->settingsCache = json_decode(get_option('ivyforms_settings'), true); |
| 36 | + if (!is_array($this->settingsCache)) { | |
| 37 | + $this->settingsCache = []; | |
| 38 | + } | |
| 41 | 39 | |
| 40 | + $this->refreshWordPressSettingsCache(); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @return array<string, mixed> | |
| 45 | + */ | |
| 46 | + private function readStoredSettings(): array | |
| 47 | + { | |
| 48 | + $stored = json_decode((string) get_option('ivyforms_settings'), true); | |
| 49 | + | |
| 50 | + return is_array($stored) ? $stored : []; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * @param array<string, mixed> $stored | |
| 55 | + */ | |
| 56 | + private function writeStoredSettings(array $stored): void | |
| 57 | + { | |
| 58 | + update_option('ivyforms_settings', json_encode($stored)); | |
| 59 | + } | |
| 60 | + | |
| 61 | + private function refreshWordPressSettingsCache(): void | |
| 62 | + { | |
| 63 | + if (!isset($this->settingsCache['wordpress']) || !is_array($this->settingsCache['wordpress'])) { | |
| 64 | + $this->settingsCache['wordpress'] = []; | |
| 65 | + } | |
| 66 | + | |
| 42 | 67 | foreach (self::$wpSettings as $setting => $wpSetting) { |
| 43 | 68 | $this->settingsCache['wordpress'][$setting] = get_option($wpSetting); |
| 44 | 69 | } |
| 45 | 70 | } |
| @@ -79,27 +104,36 @@ | ||
| 79 | 104 | * @return mixed[] |
| 80 | 105 | */ |
| 81 | 106 | public function getFrontendSettings(): array |
| 82 | 107 | { |
| 83 | - $capabilities = []; | |
| 108 | +// $capabilities = []; | |
| 109 | +// | |
| 110 | +// if (is_admin()) { | |
| 111 | +// $currentScreenId = get_current_screen()->id; | |
| 112 | +// | |
| 113 | +// $currentScreen = substr($currentScreenId, strrpos($currentScreenId, '-') + 1); | |
| 114 | +// | |
| 115 | +// $capabilities = [ | |
| 116 | +// 'canRead' => current_user_can('ivyforms_read_' . $currentScreen), | |
| 117 | +// 'canReadOthers' => current_user_can('ivyforms_read_others_' . $currentScreen), | |
| 118 | +// 'canWrite' => current_user_can('ivyforms_write_' . $currentScreen), | |
| 119 | +// 'canWriteOthers' => current_user_can('ivyforms_write_others_' . $currentScreen), | |
| 120 | +// 'canDelete' => current_user_can('ivyforms_delete_' . $currentScreen), | |
| 121 | +// 'canWriteStatus' => current_user_can('ivyforms_write_status_' . $currentScreen), | |
| 122 | +// ]; | |
| 123 | +// } | |
| 84 | 124 | |
| 85 | - if (is_admin()) { | |
| 86 | - $currentScreenId = get_current_screen()->id; | |
| 87 | - | |
| 88 | - $currentScreen = substr($currentScreenId, strrpos($currentScreenId, '-') + 1); | |
| 89 | - | |
| 90 | - $capabilities = [ | |
| 91 | - 'canRead' => current_user_can('ivyforms_read_' . $currentScreen), | |
| 92 | - 'canReadOthers' => current_user_can('ivyforms_read_others_' . $currentScreen), | |
| 93 | - 'canWrite' => current_user_can('ivyforms_write_' . $currentScreen), | |
| 94 | - 'canWriteOthers' => current_user_can('ivyforms_write_others_' . $currentScreen), | |
| 95 | - 'canDelete' => current_user_can('ivyforms_delete_' . $currentScreen), | |
| 96 | - 'canWriteStatus' => current_user_can('ivyforms_write_status_' . $currentScreen), | |
| 97 | - ]; | |
| 98 | - } | |
| 99 | - | |
| 100 | 125 | return [ |
| 101 | - 'capabilities' => $capabilities, | |
| 126 | + //'capabilities' => $capabilities, | |
| 127 | + 'general' => [ | |
| 128 | + 'fullscreen' => $this->getSetting('general', 'fullscreen'), | |
| 129 | + ], | |
| 130 | + 'wordpress' => [ | |
| 131 | + 'dateFormat' => $this->getSetting('wordpress', 'dateFormat'), | |
| 132 | + 'timeFormat' => $this->getSetting('wordpress', 'timeFormat'), | |
| 133 | + 'startOfWeek' => (int)$this->getSetting('wordpress', 'startOfWeek'), | |
| 134 | + 'timezone' => $this->getSetting('wordpress', 'timeZoneString'), | |
| 135 | + ], | |
| 102 | 136 | ]; |
| 103 | 137 | } |
| 104 | 138 | |
| 105 | 139 | /** |
| @@ -110,15 +144,25 @@ | ||
| 110 | 144 | * @return void |
| 111 | 145 | */ |
| 112 | 146 | public function setSetting(string $settingCategoryKey, string $settingKey, $settingValue) |
| 113 | 147 | { |
| 114 | - $this->settingsCache[$settingCategoryKey][$settingKey] = $settingValue; | |
| 148 | + if ($settingCategoryKey === 'integrations' && is_array($settingValue)) { | |
| 149 | + $existing = $this->getSetting($settingCategoryKey, $settingKey); | |
| 150 | + if (is_array($existing)) { | |
| 151 | + $settingValue = array_merge($existing, $settingValue); | |
| 152 | + } | |
| 153 | + } | |
| 115 | 154 | |
| 116 | - $settingsCopy = $this->settingsCache; | |
| 155 | + $stored = $this->readStoredSettings(); | |
| 117 | 156 | |
| 118 | - unset($settingsCopy['wordpress']); | |
| 157 | + if (!isset($stored[$settingCategoryKey]) || !is_array($stored[$settingCategoryKey])) { | |
| 158 | + $stored[$settingCategoryKey] = []; | |
| 159 | + } | |
| 119 | 160 | |
| 120 | - update_option('ivyforms_settings', json_encode($settingsCopy)); | |
| 161 | + $stored[$settingCategoryKey][$settingKey] = $settingValue; | |
| 162 | + $this->settingsCache = $stored; | |
| 163 | + $this->refreshWordPressSettingsCache(); | |
| 164 | + $this->writeStoredSettings($stored); | |
| 121 | 165 | } |
| 122 | 166 | |
| 123 | 167 | /** |
| 124 | 168 | * @param string $settingCategoryKey |
| @@ -127,15 +171,13 @@ | ||
| 127 | 171 | * @return void |
| 128 | 172 | */ |
| 129 | 173 | public function setCategorySettings(string $settingCategoryKey, $settingValue) |
| 130 | 174 | { |
| 131 | - $this->settingsCache[$settingCategoryKey] = $settingValue; | |
| 132 | - | |
| 133 | - $settingsCopy = $this->settingsCache; | |
| 134 | - | |
| 135 | - unset($settingsCopy['wordpress']); | |
| 136 | - | |
| 137 | - update_option('ivyforms_settings', json_encode($settingsCopy)); | |
| 175 | + $stored = $this->readStoredSettings(); | |
| 176 | + $stored[$settingCategoryKey] = $settingValue; | |
| 177 | + $this->settingsCache = $stored; | |
| 178 | + $this->refreshWordPressSettingsCache(); | |
| 179 | + $this->writeStoredSettings($stored); | |
| 138 | 180 | } |
| 139 | 181 | |
| 140 | 182 | /** |
| 141 | 183 | * Get all settings |
| @@ -145,15 +187,15 @@ | ||
| 145 | 187 | * |
| 146 | 188 | */ |
| 147 | 189 | public function setAllSettings(array $settings): void |
| 148 | 190 | { |
| 191 | + $stored = $this->readStoredSettings(); | |
| 192 | + | |
| 149 | 193 | foreach ($settings as $settingCategoryKey => $settingValues) { |
| 150 | - $this->settingsCache[$settingCategoryKey] = $settingValues; | |
| 194 | + $stored[$settingCategoryKey] = $settingValues; | |
| 151 | 195 | } |
| 152 | 196 | |
| 153 | - $settingsCopy = $this->settingsCache; | |
| 154 | - | |
| 155 | - unset($settingsCopy['wordpress']); | |
| 156 | - | |
| 157 | - update_option('ivyforms_settings', json_encode($settingsCopy)); | |
| 197 | + $this->settingsCache = $stored; | |
| 198 | + $this->refreshWordPressSettingsCache(); | |
| 199 | + $this->writeStoredSettings($stored); | |
| 158 | 200 | } |
| 159 | 201 | } |