PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Settings / SettingsService.php

SettingsService.php in The Innovative Form Builder – IvyForms 0.2.1, at backend/src/Services/Settings/SettingsService.php

160 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ];
103 }
104
105 /**
106 * @param string $settingCategoryKey
107 * @param string $settingKey
108 * @param mixed $settingValue
109 *
110 * @return void
111 */
112 public function setSetting(string $settingCategoryKey, string $settingKey, $settingValue)
113 {
114 $this->settingsCache[$settingCategoryKey][$settingKey] = $settingValue;
115
116 $settingsCopy = $this->settingsCache;
117
118 unset($settingsCopy['wordpress']);
119
120 update_option('ivyforms_settings', json_encode($settingsCopy));
121 }
122
123 /**
124 * @param string $settingCategoryKey
125 * @param mixed $settingValue
126 *
127 * @return void
128 */
129 public function setCategorySettings(string $settingCategoryKey, $settingValue)
130 {
131 $this->settingsCache[$settingCategoryKey] = $settingValue;
132
133 $settingsCopy = $this->settingsCache;
134
135 unset($settingsCopy['wordpress']);
136
137 update_option('ivyforms_settings', json_encode($settingsCopy));
138 }
139
140 /**
141 * Get all settings
142 *
143 * Specify that $settings is an associative array with string keys and mixed values
144 * @param array<string, mixed> $settings
145 *
146 */
147 public function setAllSettings(array $settings): void
148 {
149 foreach ($settings as $settingCategoryKey => $settingValues) {
150 $this->settingsCache[$settingCategoryKey] = $settingValues;
151 }
152
153 $settingsCopy = $this->settingsCache;
154
155 unset($settingsCopy['wordpress']);
156
157 update_option('ivyforms_settings', json_encode($settingsCopy));
158 }
159 }
160