Passed
Push — hotfix/fix-counts ( 4b43d1...cc9e05 )
by Paul
03:52
created

OptionManager::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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