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
|
25 |
|
public function all() |
17
|
|
|
{ |
18
|
25 |
|
if ($settings = Arr::consolidate(glsr()->retrieve('settings'))) { |
19
|
25 |
|
return $settings; |
20
|
|
|
} |
21
|
|
|
return $this->reset(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param int $version |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
14 |
|
public static function databaseKey($version = null) |
29
|
|
|
{ |
30
|
14 |
|
if (1 == $version) { |
31
|
9 |
|
return 'geminilabs_site_reviews_settings'; |
32
|
|
|
} |
33
|
14 |
|
if (2 == $version) { |
34
|
14 |
|
return 'geminilabs_site_reviews-v2'; |
35
|
|
|
} |
36
|
14 |
|
if (null === $version) { |
37
|
14 |
|
$version = glsr()->version('major'); |
38
|
|
|
} |
39
|
14 |
|
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
|
25 |
|
public function get($path = '', $fallback = '', $cast = '') |
58
|
|
|
{ |
59
|
25 |
|
$option = Arr::get($this->all(), $path, $fallback); |
60
|
25 |
|
$path = ltrim(Str::removePrefix($path, 'settings'), '.'); |
61
|
25 |
|
if (!empty($path)) { |
62
|
24 |
|
$hook = 'option/'.str_replace('.', '/', $path); |
63
|
24 |
|
$option = glsr()->filter($hook, $option); |
64
|
|
|
} |
65
|
25 |
|
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
|
14 |
|
public function getWP($path, $fallback = '', $cast = '') |
105
|
|
|
{ |
106
|
14 |
|
$option = get_option($path, $fallback); |
107
|
14 |
|
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
|
14 |
|
public function reset() |
146
|
|
|
{ |
147
|
14 |
|
$settings = Arr::consolidate($this->getWP(static::databaseKey(), [])); |
148
|
14 |
|
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
|
14 |
|
glsr()->store('settings', $settings); |
154
|
14 |
|
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
|
|
|
|