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