Passed
Push — master ( d98594...cee2ee )
by Paul
07:20 queued 03:47
created

OptionManager::delete()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
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
	 * @param string $path
67
	 * @return bool
68
	 */
69 1
	public function getBool( $path )
70
	{
71 1
		return $this->get( $path ) == 'yes'
72
			? true
73 1
			: false;
74
	}
75
76
	/**
77
	 * @return string
78
	 */
79
	public function json()
80
	{
81
		return json_encode( $this->all() );
82
	}
83
84
	/**
85
	 * @return array
86
	 */
87
	public function normalize( array $options = [] )
88
	{
89
		$options = wp_parse_args(
90
			glsr( Helper::class )->flattenArray( $options ),
91
			glsr( DefaultsManager::class )->defaults()
92
		);
93
		array_walk( $options, function( &$value ) {
94
			if( !is_string( $value ))return;
95
			$value = wp_kses( $value, wp_kses_allowed_html( 'post' ));
96
		});
97
		return glsr( Helper::class )->convertDotNotationArray( $options );
98
	}
99
100
	/**
101
	 * @return bool
102
	 */
103 1
	public function isRecaptchaEnabled()
104
	{
105 1
		$integration = $this->get( 'settings.submissions.recaptcha.integration' );
106 1
		return $integration == 'all' || ( $integration == 'guest' && !is_user_logged_in() );
107
	}
108
109
	/**
110
	 * @return array
111
	 */
112 7
	public function reset()
113
	{
114 7
		$options = get_option( static::databaseKey(), [] );
115 7
		if( !is_array( $options ) || empty( $options )) {
116
			delete_option( static::databaseKey() );
117
			$options = wp_parse_args( glsr()->defaults, ['settings' => []] );
118
		}
119 7
		$this->options = $options;
120 7
	}
121
122
	/**
123
	 * @param string|array $pathOrOptions
124
	 * @param mixed $value
125
	 * @return bool
126
	 */
127 7
	public function set( $pathOrOptions, $value = '' )
128
	{
129 7
		if( is_string( $pathOrOptions )) {
130 7
			$pathOrOptions = glsr( Helper::class )->setPathValue( $pathOrOptions, $value, $this->all() );
131
		}
132 7
		if( $result = update_option( static::databaseKey(), (array)$pathOrOptions )) {
133 7
			$this->reset();
134
		}
135 7
		return $result;
136
	}
137
}
138