|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Migrate; |
|
10
|
|
|
|
|
11
|
|
|
class OptionManager |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
24 |
|
public function all() |
|
17
|
|
|
{ |
|
18
|
24 |
|
if ($settings = Arr::consolidate(glsr()->retrieve('settings'))) { |
|
19
|
24 |
|
return $settings; |
|
20
|
|
|
} |
|
21
|
1 |
|
return $this->reset(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param int $version |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
15 |
|
public static function databaseKey($version = null) |
|
29
|
|
|
{ |
|
30
|
15 |
|
if (1 == $version) { |
|
31
|
9 |
|
return 'geminilabs_site_reviews_settings'; |
|
32
|
|
|
} |
|
33
|
15 |
|
if (2 == $version) { |
|
34
|
14 |
|
return 'geminilabs_site_reviews-v2'; |
|
35
|
|
|
} |
|
36
|
15 |
|
if (null === $version) { |
|
37
|
15 |
|
$version = glsr()->version('major'); |
|
38
|
|
|
} |
|
39
|
15 |
|
return Str::snakeCase(glsr()->id.'-v'.intval($version)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $path |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
14 |
|
public function delete($path) |
|
47
|
|
|
{ |
|
48
|
14 |
|
return $this->set(Arr::remove($this->all(), $path)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
* @param mixed $fallback |
|
54
|
|
|
* @param string $cast |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
24 |
|
public function get($path = '', $fallback = '', $cast = '') |
|
58
|
|
|
{ |
|
59
|
24 |
|
$option = Arr::get($this->all(), $path, $fallback); |
|
60
|
24 |
|
$path = ltrim(Str::removePrefix($path, 'settings'), '.'); |
|
61
|
24 |
|
if (!empty($path)) { |
|
62
|
24 |
|
$hook = 'option/'.str_replace('.', '/', $path); |
|
63
|
24 |
|
$option = glsr()->filter($hook, $option); |
|
64
|
|
|
} |
|
65
|
24 |
|
return Cast::to($cast, $option); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $path |
|
70
|
|
|
* @param array $fallback |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getArray($path, $fallback = []) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->get($path, $fallback, 'array'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $path |
|
80
|
|
|
* @param string|int|bool $fallback |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
14 |
|
public function getBool($path, $fallback = false) |
|
84
|
|
|
{ |
|
85
|
14 |
|
return $this->get($path, $fallback, 'bool'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $path |
|
90
|
|
|
* @param int $fallback |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
14 |
|
public function getInt($path, $fallback = 0) |
|
94
|
|
|
{ |
|
95
|
14 |
|
return $this->get($path, $fallback, 'int'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $path |
|
100
|
|
|
* @param mixed $fallback |
|
101
|
|
|
* @param string $cast |
|
102
|
|
|
* @return mixed |
|
103
|
|
|
*/ |
|
104
|
15 |
|
public function getWP($path, $fallback = '', $cast = '') |
|
105
|
|
|
{ |
|
106
|
15 |
|
$option = get_option($path, $fallback); |
|
107
|
15 |
|
return Cast::to($cast, Helper::ifEmpty($option, $fallback, $strict = true)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function isRecaptchaEnabled() |
|
114
|
|
|
{ |
|
115
|
1 |
|
$integration = $this->get('settings.submissions.recaptcha.integration'); |
|
116
|
1 |
|
return 'all' == $integration || ('guest' == $integration && !is_user_logged_in()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function json() |
|
123
|
|
|
{ |
|
124
|
|
|
return json_encode($this->all(), JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Restricts the provided settings keys to the defaults |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function normalize(array $settings = []) |
|
132
|
|
|
{ |
|
133
|
|
|
$settings = shortcode_atts(glsr(DefaultsManager::class)->defaults(), Arr::flatten($settings)); |
|
134
|
|
|
array_walk($settings, function (&$value) { |
|
135
|
|
|
if (is_string($value)) { |
|
136
|
|
|
$value = wp_kses($value, wp_kses_allowed_html('post')); |
|
137
|
|
|
} |
|
138
|
|
|
}); |
|
139
|
|
|
return Arr::convertFromDotNotation($settings); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
15 |
|
public function reset() |
|
146
|
|
|
{ |
|
147
|
15 |
|
$settings = Arr::consolidate($this->getWP(static::databaseKey(), [])); |
|
148
|
15 |
|
if (empty($settings)) { |
|
149
|
|
|
delete_option(static::databaseKey()); |
|
150
|
|
|
$settings = Arr::consolidate(glsr()->defaults); |
|
151
|
|
|
glsr(Migrate::class)->reset(); // Do this to migrate any previous version settings |
|
152
|
|
|
} |
|
153
|
15 |
|
glsr()->store('settings', $settings); |
|
154
|
15 |
|
return $settings; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string|array $pathOrArray |
|
159
|
|
|
* @param mixed $value |
|
160
|
|
|
* @return bool |
|
161
|
|
|
*/ |
|
162
|
14 |
|
public function set($pathOrArray, $value = '') |
|
163
|
|
|
{ |
|
164
|
14 |
|
if (is_string($pathOrArray)) { |
|
165
|
14 |
|
$pathOrArray = Arr::set($this->all(), $pathOrArray, $value); |
|
166
|
|
|
} |
|
167
|
14 |
|
if ($settings = Arr::consolidate($pathOrArray)) { |
|
168
|
14 |
|
$result = update_option(static::databaseKey(), $settings); |
|
169
|
|
|
} |
|
170
|
14 |
|
if (!empty($result)) { |
|
171
|
14 |
|
$this->reset(); |
|
172
|
14 |
|
return true; |
|
173
|
|
|
} |
|
174
|
14 |
|
return false; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|