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 array |
91
|
|
|
*/ |
92
|
7 |
|
public function reset() |
93
|
|
|
{ |
94
|
7 |
|
$options = get_option( static::databaseKey(), [] ); |
95
|
7 |
|
if( !is_array( $options ) || empty( $options )) { |
96
|
|
|
delete_option( static::databaseKey() ); |
97
|
|
|
$options = wp_parse_args( glsr()->defaults, ['settings' => []] ); |
98
|
|
|
} |
99
|
7 |
|
$this->options = $options; |
100
|
7 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string|array $pathOrOptions |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
7 |
|
public function set( $pathOrOptions, $value = '' ) |
108
|
|
|
{ |
109
|
7 |
|
if( is_string( $pathOrOptions )) { |
110
|
6 |
|
$pathOrOptions = glsr( Helper::class )->setPathValue( $pathOrOptions, $value, $this->all() ); |
111
|
|
|
} |
112
|
7 |
|
if( $result = update_option( static::databaseKey(), (array)$pathOrOptions )) { |
113
|
7 |
|
$this->reset(); |
114
|
|
|
} |
115
|
7 |
|
return $result; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|