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
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
7 |
|
public static function databaseKey() |
15
|
|
|
{ |
16
|
7 |
|
return glsr( Helper::class )->snakeCase( |
17
|
7 |
|
Application::ID.'-v'.explode( '.', glsr()->version )[0] |
18
|
|
|
); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
7 |
|
public function all() |
25
|
|
|
{ |
26
|
7 |
|
$options = get_option( static::databaseKey(), [] ); |
27
|
7 |
|
if( !is_array( $options )) { |
28
|
|
|
delete_option( static::databaseKey() ); |
29
|
|
|
$options = ['settings' => []]; |
30
|
|
|
} |
31
|
7 |
|
return $options; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $path |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function delete( $path ) |
39
|
|
|
{ |
40
|
|
|
$keys = explode( '.', $path ); |
41
|
|
|
$last = array_pop( $keys ); |
42
|
|
|
$options = $this->all(); |
43
|
|
|
$pointer = &$options; |
44
|
|
|
foreach( $keys as $key ) { |
45
|
|
|
if( !isset( $pointer[$key] ) || !is_array( $pointer[$key] ))continue; |
46
|
|
|
$pointer = &$pointer[$key]; |
47
|
|
|
} |
48
|
|
|
unset( $pointer[$last] ); |
49
|
|
|
return $this->set( $options ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $path |
54
|
|
|
* @param mixed $fallback |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
7 |
|
public function get( $path = '', $fallback = '' ) |
58
|
|
|
{ |
59
|
7 |
|
return glsr( Helper::class )->getPathValue( $path, $this->all(), $fallback ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function json() |
66
|
|
|
{ |
67
|
|
|
return json_encode( $this->all() ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function normalize( array $options = [] ) |
74
|
|
|
{ |
75
|
|
|
$options = wp_parse_args( |
76
|
|
|
glsr( Helper::class )->flattenArray( $options ), |
77
|
|
|
glsr( DefaultsManager::class )->defaults() |
78
|
|
|
); |
79
|
|
|
array_walk( $options, function( &$value, $key ) { |
|
|
|
|
80
|
|
|
if( !is_string( $value ))return; |
81
|
|
|
$value = wp_kses( $value, wp_kses_allowed_html( 'post' )); |
82
|
|
|
}); |
83
|
|
|
return glsr( Helper::class )->convertDotNotationArray( $options ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|array $pathOrOptions |
88
|
|
|
* @param mixed $value |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
7 |
|
public function set( $pathOrOptions, $value = '' ) |
92
|
|
|
{ |
93
|
7 |
|
if( is_string( $pathOrOptions )) { |
94
|
7 |
|
$pathOrOptions = glsr( Helper::class )->setPathValue( $pathOrOptions, $value, $this->all() ); |
95
|
|
|
} |
96
|
7 |
|
return update_option( static::databaseKey(), (array)$pathOrOptions ); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.