Passed
Push — master ( a9546d...6aabd8 )
by Paul
05:58
created

OptionManager::getWP()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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