| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings hook for settings |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace IvyForms\Services\Settings; |
| 8 |
|
| 9 |
// phpcs:disable PSR1.Files.SideEffects |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class SettingsService |
| 16 |
* |
| 17 |
* @package IvyForms\Services\Settings |
| 18 |
*/ |
| 19 |
class SettingsService |
| 20 |
{ |
| 21 |
/** @var array|mixed */ |
| 22 |
private $settingsCache; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array<string, string> // Define that both keys and values are strings |
| 26 |
* */ |
| 27 |
private static array $wpSettings = [ |
| 28 |
'dateFormat' => 'date_format', |
| 29 |
'timeFormat' => 'time_format', |
| 30 |
'startOfWeek' => 'start_of_week', |
| 31 |
'timeZoneString' => 'timezone_string', |
| 32 |
'gmtOffset' => 'gmt_offset' |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* SettingsService constructor. |
| 37 |
*/ |
| 38 |
public function __construct() |
| 39 |
{ |
| 40 |
$this->settingsCache = json_decode(get_option('ivyforms_settings'), true); |
| 41 |
|
| 42 |
foreach (self::$wpSettings as $setting => $wpSetting) { |
| 43 |
$this->settingsCache['wordpress'][$setting] = get_option($wpSetting); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string $settingCategoryKey |
| 49 |
* @param string $settingKey |
| 50 |
* |
| 51 |
* @return mixed |
| 52 |
*/ |
| 53 |
public function getSetting(string $settingCategoryKey, string $settingKey) |
| 54 |
{ |
| 55 |
return $this->settingsCache[$settingCategoryKey][$settingKey] ?? null; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @param string $settingCategoryKey |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function getCategorySettings(string $settingCategoryKey) |
| 64 |
{ |
| 65 |
return $this->settingsCache[$settingCategoryKey] ?? null; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return mixed[] |
| 70 |
*/ |
| 71 |
public function getAllSettings(): array |
| 72 |
{ |
| 73 |
return $this->settingsCache; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return settings for frontend |
| 78 |
* |
| 79 |
* @return mixed[] |
| 80 |
*/ |
| 81 |
public function getFrontendSettings(): array |
| 82 |
{ |
| 83 |
// $capabilities = []; |
| 84 |
// |
| 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 |
return [ |
| 101 |
//'capabilities' => $capabilities, |
| 102 |
'general' => [ |
| 103 |
'fullscreen' => $this->getSetting('general', 'fullscreen'), |
| 104 |
], |
| 105 |
'wordpress' => [ |
| 106 |
'dateFormat' => $this->getSetting('wordpress', 'dateFormat'), |
| 107 |
'timeFormat' => $this->getSetting('wordpress', 'timeFormat'), |
| 108 |
'startOfWeek' => (int)$this->getSetting('wordpress', 'startOfWeek'), |
| 109 |
'timezone' => $this->getSetting('wordpress', 'timeZoneString'), |
| 110 |
], |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param string $settingCategoryKey |
| 116 |
* @param string $settingKey |
| 117 |
* @param mixed $settingValue |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function setSetting(string $settingCategoryKey, string $settingKey, $settingValue) |
| 122 |
{ |
| 123 |
$this->settingsCache[$settingCategoryKey][$settingKey] = $settingValue; |
| 124 |
|
| 125 |
$settingsCopy = $this->settingsCache; |
| 126 |
|
| 127 |
unset($settingsCopy['wordpress']); |
| 128 |
|
| 129 |
update_option('ivyforms_settings', json_encode($settingsCopy)); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param string $settingCategoryKey |
| 134 |
* @param mixed $settingValue |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function setCategorySettings(string $settingCategoryKey, $settingValue) |
| 139 |
{ |
| 140 |
$this->settingsCache[$settingCategoryKey] = $settingValue; |
| 141 |
|
| 142 |
$settingsCopy = $this->settingsCache; |
| 143 |
|
| 144 |
unset($settingsCopy['wordpress']); |
| 145 |
|
| 146 |
update_option('ivyforms_settings', json_encode($settingsCopy)); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get all settings |
| 151 |
* |
| 152 |
* Specify that $settings is an associative array with string keys and mixed values |
| 153 |
* @param array<string, mixed> $settings |
| 154 |
* |
| 155 |
*/ |
| 156 |
public function setAllSettings(array $settings): void |
| 157 |
{ |
| 158 |
foreach ($settings as $settingCategoryKey => $settingValues) { |
| 159 |
$this->settingsCache[$settingCategoryKey] = $settingValues; |
| 160 |
} |
| 161 |
|
| 162 |
$settingsCopy = $this->settingsCache; |
| 163 |
|
| 164 |
unset($settingsCopy['wordpress']); |
| 165 |
|
| 166 |
update_option('ivyforms_settings', json_encode($settingsCopy)); |
| 167 |
} |
| 168 |
} |
| 169 |
|