EmailRepository.php
11 months ago
LevelOrderRepository.php
11 months ago
LevelRepository.php
11 months ago
MemberActivityRepository.php
11 months ago
MembershipChangeRepository.php
11 months ago
MembershipRepository.php
11 months ago
PageRepository.php
11 months ago
Repository.php
11 months ago
SettingsRepository.php
11 months ago
UserRepository.php
11 months ago
SettingsRepository.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Repository; |
| 4 | |
| 5 | use FapiMember\Model\Enums\Keys\OptionKey; |
| 6 | use FapiMember\Model\Settings; |
| 7 | |
| 8 | class SettingsRepository extends Repository |
| 9 | { |
| 10 | |
| 11 | public function createSettingsIfNeeded(): void |
| 12 | { |
| 13 | if ($this->getSettings() === null) { |
| 14 | add_option(OptionKey::SETTINGS, []); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public function getSettings(): Settings |
| 19 | { |
| 20 | $option = get_option(OptionKey::SETTINGS); |
| 21 | |
| 22 | return $this->optionToSettings($option); |
| 23 | } |
| 24 | |
| 25 | public function getSetting(string $key): mixed |
| 26 | { |
| 27 | $options = get_option(OptionKey::SETTINGS); |
| 28 | |
| 29 | if ($options === false) { |
| 30 | $options = []; |
| 31 | } |
| 32 | |
| 33 | return (isset($options[$key])) ? $options[$key] : null; |
| 34 | } |
| 35 | |
| 36 | public function updateSettings(Settings $settings): void |
| 37 | { |
| 38 | update_option(OptionKey::SETTINGS, $settings->toArray()); |
| 39 | } |
| 40 | |
| 41 | private function optionToSettings(array|bool $option): Settings |
| 42 | { |
| 43 | if ($option === false) { |
| 44 | return new Settings([]); |
| 45 | } |
| 46 | |
| 47 | return new Settings($option); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 |