| @@ -1,0 +1,201 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Settings hook for settings | |
| 5 | + */ | |
| 6 | + | |
| 7 | +namespace IvyForms\Services\Settings; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Class SettingsService | |
| 11 | + * | |
| 12 | + * @package IvyForms\Services\Settings | |
| 13 | + */ | |
| 14 | +class SettingsService | |
| 15 | +{ | |
| 16 | + /** @var array|mixed */ | |
| 17 | + private $settingsCache; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @var array<string, string> // Define that both keys and values are strings | |
| 21 | + * */ | |
| 22 | + private static array $wpSettings = [ | |
| 23 | + 'dateFormat' => 'date_format', | |
| 24 | + 'timeFormat' => 'time_format', | |
| 25 | + 'startOfWeek' => 'start_of_week', | |
| 26 | + 'timeZoneString' => 'timezone_string', | |
| 27 | + 'gmtOffset' => 'gmt_offset' | |
| 28 | + ]; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * SettingsService constructor. | |
| 32 | + */ | |
| 33 | + public function __construct() | |
| 34 | + { | |
| 35 | + $this->settingsCache = json_decode(get_option('ivyforms_settings'), true); | |
| 36 | + if (!is_array($this->settingsCache)) { | |
| 37 | + $this->settingsCache = []; | |
| 38 | + } | |
| 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 | + | |
| 67 | + foreach (self::$wpSettings as $setting => $wpSetting) { | |
| 68 | + $this->settingsCache['wordpress'][$setting] = get_option($wpSetting); | |
| 69 | + } | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @param string $settingCategoryKey | |
| 74 | + * @param string $settingKey | |
| 75 | + * | |
| 76 | + * @return mixed | |
| 77 | + */ | |
| 78 | + public function getSetting(string $settingCategoryKey, string $settingKey) | |
| 79 | + { | |
| 80 | + return $this->settingsCache[$settingCategoryKey][$settingKey] ?? null; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * @param string $settingCategoryKey | |
| 85 | + * | |
| 86 | + * @return mixed | |
| 87 | + */ | |
| 88 | + public function getCategorySettings(string $settingCategoryKey) | |
| 89 | + { | |
| 90 | + return $this->settingsCache[$settingCategoryKey] ?? null; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * @return mixed[] | |
| 95 | + */ | |
| 96 | + public function getAllSettings(): array | |
| 97 | + { | |
| 98 | + return $this->settingsCache; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Return settings for frontend | |
| 103 | + * | |
| 104 | + * @return mixed[] | |
| 105 | + */ | |
| 106 | + public function getFrontendSettings(): array | |
| 107 | + { | |
| 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 | +// } | |
| 124 | + | |
| 125 | + return [ | |
| 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 | + ], | |
| 136 | + ]; | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * @param string $settingCategoryKey | |
| 141 | + * @param string $settingKey | |
| 142 | + * @param mixed $settingValue | |
| 143 | + * | |
| 144 | + * @return void | |
| 145 | + */ | |
| 146 | + public function setSetting(string $settingCategoryKey, string $settingKey, $settingValue) | |
| 147 | + { | |
| 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 | + } | |
| 154 | + | |
| 155 | + $stored = $this->readStoredSettings(); | |
| 156 | + | |
| 157 | + if (!isset($stored[$settingCategoryKey]) || !is_array($stored[$settingCategoryKey])) { | |
| 158 | + $stored[$settingCategoryKey] = []; | |
| 159 | + } | |
| 160 | + | |
| 161 | + $stored[$settingCategoryKey][$settingKey] = $settingValue; | |
| 162 | + $this->settingsCache = $stored; | |
| 163 | + $this->refreshWordPressSettingsCache(); | |
| 164 | + $this->writeStoredSettings($stored); | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * @param string $settingCategoryKey | |
| 169 | + * @param mixed $settingValue | |
| 170 | + * | |
| 171 | + * @return void | |
| 172 | + */ | |
| 173 | + public function setCategorySettings(string $settingCategoryKey, $settingValue) | |
| 174 | + { | |
| 175 | + $stored = $this->readStoredSettings(); | |
| 176 | + $stored[$settingCategoryKey] = $settingValue; | |
| 177 | + $this->settingsCache = $stored; | |
| 178 | + $this->refreshWordPressSettingsCache(); | |
| 179 | + $this->writeStoredSettings($stored); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Get all settings | |
| 184 | + * | |
| 185 | + * Specify that $settings is an associative array with string keys and mixed values | |
| 186 | + * @param array<string, mixed> $settings | |
| 187 | + * | |
| 188 | + */ | |
| 189 | + public function setAllSettings(array $settings): void | |
| 190 | + { | |
| 191 | + $stored = $this->readStoredSettings(); | |
| 192 | + | |
| 193 | + foreach ($settings as $settingCategoryKey => $settingValues) { | |
| 194 | + $stored[$settingCategoryKey] = $settingValues; | |
| 195 | + } | |
| 196 | + | |
| 197 | + $this->settingsCache = $stored; | |
| 198 | + $this->refreshWordPressSettingsCache(); | |
| 199 | + $this->writeStoredSettings($stored); | |
| 200 | + } | |
| 201 | +} | |