Passed
Push — master ( 2da942...d633d7 )
by Paul
15:03 queued 05:40
created

OptionManager::databaseKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Modules\Migrate;
10
11
class OptionManager
12
{
13
    /**
14
     * @return array
15
     */
16 24
    public function all()
17
    {
18 24
        if ($settings = Arr::consolidate(glsr()->retrieve('settings'))) {
19 24
            return $settings;
20
        }
21 1
        return $this->reset();
22
    }
23
24
    /**
25
     * @param int $version
26
     * @return string
27
     */
28 15
    public static function databaseKey($version = null)
29
    {
30 15
        if (1 == $version) {
31 7
            return 'geminilabs_site_reviews_settings';
32
        }
33 15
        if (2 == $version) {
34 7
            return 'geminilabs_site_reviews-v2';
35
        }
36 15
        if (null === $version) {
37 15
            $version = explode('.', glsr()->version);
38 15
            $version = array_shift($version);
39
        }
40 15
        return Str::snakeCase(glsr()->id.'-v'.intval($version));
41
    }
42
43
    /**
44
     * @param string $path
45
     * @return bool
46
     */
47 7
    public function delete($path)
48
    {
49 7
        return $this->set(Arr::remove($this->all(), $path));
50
    }
51
52
    /**
53
     * @param string $path
54
     * @param mixed $fallback
55
     * @param string $cast
56
     * @return mixed
57
     */
58 24
    public function get($path = '', $fallback = '', $cast = '')
59
    {
60 24
        $option = Arr::get($this->all(), $path, $fallback);
61 24
        $path = ltrim(Str::removePrefix($path, 'settings'), '.');
62 24
        if (!empty($path)) {
63 24
            $hook = 'option/'.str_replace('.', '/', $path);
64 24
            $option = glsr()->filter($hook, $option);
65
        }
66 24
        return Cast::to($cast, $option);
67
    }
68
69
    /**
70
     * @param string $path
71
     * @param string|int|bool $fallback
72
     * @return bool
73
     */
74 14
    public function getBool($path, $fallback = false)
75
    {
76 14
        return $this->get($path, $fallback, 'bool');
77
    }
78
79
    /**
80
     * @param string $path
81
     * @param mixed $fallback
82
     * @param string $cast
83
     * @return mixed
84
     */
85 14
    public function getWP($path, $fallback = '', $cast = '')
86
    {
87 14
        $option = get_option($path, $fallback);
88 14
        return Cast::to($cast, Helper::ifEmpty($option, $fallback, $strict = true));
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 1
    public function isRecaptchaEnabled()
95
    {
96 1
        $integration = $this->get('settings.submissions.recaptcha.integration');
97 1
        return 'all' == $integration || ('guest' == $integration && !is_user_logged_in());
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function json()
104
    {
105
        return json_encode($this->all(), JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
106
    }
107
108
    /**
109
     * Restricts the provided settings keys to the defaults
110
     * @return array
111
     */
112
    public function normalize(array $settings = [])
113
    {
114
        $settings = shortcode_atts(glsr(DefaultsManager::class)->defaults(), Arr::flatten($settings));
115
        array_walk($settings, function (&$value) {
116
            if (is_string($value)) {
117
                $value = wp_kses($value, wp_kses_allowed_html('post'));
118
            }
119
        });
120
        return Arr::convertFromDotNotation($settings);
121
    }
122
123
    /**
124
     * @return array
125
     */
126 12
    public function reset()
127
    {
128 12
        $settings = Arr::consolidate($this->getWP(static::databaseKey(), []));
129 12
        if (empty($settings)) {
130
            glsr(Migrate::class)->reset(); // @todo why do we this here again? It should only run if this is not a fresh install.
131
            delete_option(static::databaseKey());
132
            $settings = Arr::consolidate(glsr()->defaults);
133
        }
134 12
        glsr()->store('settings', $settings);
135 12
        return $settings;
136
    }
137
138
    /**
139
     * @param string|array $pathOrArray
140
     * @param mixed $value
141
     * @return bool
142
     */
143 11
    public function set($pathOrArray, $value = '')
144
    {
145 11
        if (is_string($pathOrArray)) {
146 11
            $pathOrArray = Arr::set($this->all(), $pathOrArray, $value);
147
        }
148 11
        if ($settings = Arr::consolidate($pathOrArray)) {
149 11
            $result = update_option(static::databaseKey(), $settings);
150
        }
151 11
        if (!empty($result)) {
152 11
            $this->reset();
153 11
            return true;
154
        }
155 7
        return false;
156
    }
157
}
158