1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
|
8
|
|
|
class OptionManager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $options; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
7 |
|
public static function databaseKey($version = null) |
19
|
|
|
{ |
20
|
7 |
|
if (null === $version) { |
21
|
7 |
|
$version = explode('.', glsr()->version); |
22
|
7 |
|
$version = array_shift($version); |
23
|
|
|
} |
24
|
7 |
|
return glsr(Helper::class)->snakeCase( |
25
|
7 |
|
Application::ID.'-v'.intval($version) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
7 |
|
public function all() |
33
|
|
|
{ |
34
|
7 |
|
if (empty($this->options)) { |
35
|
|
|
$this->reset(); |
36
|
|
|
} |
37
|
7 |
|
return $this->options; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $path |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
1 |
|
public function delete($path) |
45
|
|
|
{ |
46
|
1 |
|
$keys = explode('.', $path); |
47
|
1 |
|
$last = array_pop($keys); |
48
|
1 |
|
$options = $this->all(); |
49
|
1 |
|
$pointer = &$options; |
50
|
1 |
|
foreach ($keys as $key) { |
51
|
1 |
|
if (!isset($pointer[$key]) || !is_array($pointer[$key])) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
1 |
|
$pointer = &$pointer[$key]; |
55
|
|
|
} |
56
|
1 |
|
unset($pointer[$last]); |
57
|
1 |
|
return $this->set($options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $path |
62
|
|
|
* @param mixed $fallback |
63
|
|
|
* @param string $cast |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
7 |
|
public function get($path = '', $fallback = '', $cast = '') |
67
|
|
|
{ |
68
|
7 |
|
$result = glsr(Helper::class)->dataGet($this->all(), $path, $fallback); |
69
|
7 |
|
return glsr(Helper::class)->castTo($cast, $result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $path |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
1 |
|
public function getBool($path) |
77
|
|
|
{ |
78
|
1 |
|
return glsr(Helper::class)->castTo('bool', $this->get($path)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $path |
83
|
|
|
* @param mixed $fallback |
84
|
|
|
* @param string $cast |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
7 |
|
public function getWP($path, $fallback = '', $cast = '') |
88
|
|
|
{ |
89
|
7 |
|
$option = get_option($path, $fallback); |
90
|
7 |
|
if (empty($option)) { |
91
|
|
|
$option = $fallback; |
92
|
|
|
} |
93
|
7 |
|
return glsr(Helper::class)->castTo($cast, $option); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function json() |
100
|
|
|
{ |
101
|
|
|
return json_encode($this->all()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function normalize(array $options = []) |
108
|
|
|
{ |
109
|
|
|
$options = wp_parse_args( |
110
|
|
|
glsr(Helper::class)->flattenArray($options), |
111
|
|
|
glsr(DefaultsManager::class)->defaults() |
112
|
|
|
); |
113
|
|
|
array_walk($options, function (&$value) { |
114
|
|
|
if (!is_string($value)) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
$value = wp_kses($value, wp_kses_allowed_html('post')); |
118
|
|
|
}); |
119
|
|
|
return glsr(Helper::class)->convertDotNotationArray($options); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
1 |
|
public function isRecaptchaEnabled() |
126
|
|
|
{ |
127
|
1 |
|
$integration = $this->get('settings.submissions.recaptcha.integration'); |
128
|
1 |
|
return 'all' == $integration || ('guest' == $integration && !is_user_logged_in()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
7 |
|
public function reset() |
135
|
|
|
{ |
136
|
7 |
|
$options = $this->getWP(static::databaseKey(), []); |
137
|
7 |
|
if (!is_array($options) || empty($options)) { |
138
|
|
|
delete_option(static::databaseKey()); |
139
|
|
|
$options = glsr()->defaults ?: []; |
140
|
|
|
} |
141
|
7 |
|
$this->options = $options; |
142
|
7 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string|array $pathOrOptions |
146
|
|
|
* @param mixed $value |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
7 |
|
public function set($pathOrOptions, $value = '') |
150
|
|
|
{ |
151
|
7 |
|
if (is_string($pathOrOptions)) { |
152
|
7 |
|
$pathOrOptions = glsr(Helper::class)->dataSet($this->all(), $pathOrOptions, $value); |
153
|
|
|
} |
154
|
7 |
|
if ($result = update_option(static::databaseKey(), (array) $pathOrOptions)) { |
155
|
7 |
|
$this->reset(); |
156
|
|
|
} |
157
|
7 |
|
return $result; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|