Test Failed
Push — main ( 6435b1...db043e )
by Paul
16:23 queued 07:02
created

OptionManager::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0261
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
/**
12
 * @method array  getArray(string $path = '', $fallback = [])
13
 * @method bool   getBool(string $path = '', $fallback = false)
14
 * @method float  getFloat(string $path = '', $fallback = 0.0)
15
 * @method int    getInt(string $path = '', $fallback = 0)
16 34
 * @method string getString(string $path = '', $fallback = '')
17
 */
18 34
class OptionManager
19 34
{
20
    /**
21
     * @return mixed
22
     */
23
    public function __call(string $method, array $args = [])
24 31
    {
25
        if (!str_starts_with($method, 'get')) {
26 31
            throw new \BadMethodCallException("Method [$method] does not exist.");
27 31
        }
28 31
        $cast = strtolower((string) substr($method, 3));
29
        if (!in_array($cast, ['array', 'bool', 'float', 'int', 'string'])) {
30 31
            throw new \BadMethodCallException("Method [$method] does not exist.");
31 31
        }
32
        $path = Arr::getAs('string', $args, 0);
33
        $fallback = Arr::get($args, 1);
34
        return call_user_func([$this, 'get'], $path, $fallback, $cast);
35
    }
36 1
37
    public function all(): array
38 1
    {
39
        $settings = Arr::consolidate(glsr()->retrieve('settings'));
40
        if (empty($settings)) {
41 4
            $settings = $this->reset();
42
        }
43 4
        return $settings;
44 4
    }
45 4
46 4
    public static function databaseKey(?int $version = null): string
47 4
    {
48 4
        $versions = static::databaseKeys();
49
        if (null === $version) {
50
            $version = glsr()->version('major');
51 4
        }
52 4
        if (array_key_exists($version, $versions)) {
53
            return $versions[$version];
54
        }
55
        return '';
56
    }
57
58
    public static function databaseKeys(): array
59
    {
60
        $keys = [];
61
        $slug = Str::snakeCase(glsr()->id);
62 34
        $version = intval(glsr()->version('major')) + 1;
63
        while (--$version) {
64 34
            if ($version >= 7) {
65 34
                $keys[$version] = $slug; // remove version from settings key in versions >= 7.0
66 34
            } elseif (1 === $version) {
67 34
                $keys[$version] = sprintf('geminilabs_%s_settings', $slug);
68 34
            } elseif (2 === $version) {
69
                $keys[$version] = sprintf('geminilabs_%s-v%s', $slug, $version);
70 34
            } else {
71
                $keys[$version] = sprintf('%s_v%s', $slug, $version);
72
            }
73
        }
74
        return $keys;
75
    }
76
77
    public static function flushSettingsCache(): void
78
    {
79
        $alloptions = wp_load_alloptions(true);
80
        $flushed = false;
81
        foreach (static::databaseKeys() as $option) {
82
            if (isset($alloptions[$option])) {
83
                unset($alloptions[$option]);
84 24
                $flushed = true;
85
            }
86 24
        }
87
        if ($flushed) {
88
            wp_cache_set('alloptions', $alloptions, 'options');
89
        }
90
    }
91
92 21
    /**
93
     * @param mixed $fallback
94 21
     *
95
     * @return mixed
96
     */
97
    public function get(string $path = '', $fallback = '', string $cast = '')
98
    {
99
        $settings = $this->all();
100
        $option = Arr::get($settings, $path, $fallback);
101
        $path = ltrim(Str::removePrefix($path, 'settings'), '.');
102 31
        if (!empty($path)) {
103
            $hook = 'option/'.str_replace('.', '/', $path);
104 31
            $option = glsr()->filter($hook, $option, $settings, $path);
105 31
        }
106
        return Cast::to($cast, $option);
107
    }
108
109
    /**
110
     * This is used when exporting the settings.
111
     */
112
    public function json(): string
113
    {
114
        $all = $this->all();
115
        $all['extra'] = glsr()->filterArray('export/settings/extra', []); // allow addons to export additional data
116
        return (string) wp_json_encode($all, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
117
    }
118
119
    public function mergeDefaults(array $defaults): void
120
    {
121
        $saved = Arr::consolidate($this->wp(static::databaseKey(), []));
122
        $defaults = Arr::flatten(Arr::getAs('array', $defaults, 'settings'));
123
        $settings = Arr::flatten(Arr::getAs('array', $saved, 'settings'));
124
        if (empty($defaults) || empty(array_diff_key($defaults, $settings))) {
125
            return;
126
        }
127
        $settings = shortcode_atts($defaults, $settings);
128
        $settings = Arr::unflatten($settings);
129
        $settings['strings'] = Arr::consolidate(Arr::get($saved, 'settings.strings'));
130
        $saved['settings'] = $settings;
131
        $this->replace($saved);
132
    }
133
134
    public function normalize(array $data = []): array
135
    {
136
        $settings = $this->kses($data);
137
        if (!empty(glsr()->settings)) { // access the property directly to prevent an infinite loop
0 ignored issues
show
Bug Best Practice introduced by
The property $settings is declared protected in GeminiLabs\SiteReviews\Application. Since you implement __get, consider adding a @property or @property-read.
Loading history...
138
            $savedSettings = $settings;
139
            $defaults = glsr()->defaults(); // @phpstan-ignore-line
140
            $defaults = Arr::flatten($defaults);
141
            $settings = Arr::flatten($settings);
142
            $settings = shortcode_atts($defaults, $settings);
143
            $settings = Arr::unflatten($settings);
144
            $settings = $this->restoreOrphanedSettings($settings, $savedSettings);
145
        }
146
        return $settings;
147
    }
148
149
    public function previous(): array
150
    {
151
        static::flushSettingsCache();
152
        foreach (static::databaseKeys() as $version => $databaseKey) {
153 31
            if ($version === intval(glsr()->version('major'))) {
154
                continue;
155 31
            }
156 31
            $settings = Arr::consolidate(get_option($databaseKey));
157 1
            if (!empty(array_filter(Arr::getAs('array', $settings, 'settings')))) {
158 1
                return $settings;
159 1
            }
160
        }
161 31
        return [];
162 31
    }
163
164
    public function replace(array $settings): bool
165
    {
166
        if (empty($settings)) {
167
            return false;
168
        }
169 31
        $settings = $this->normalize($settings);
170
        if (!update_option(static::databaseKey(), $settings, true)) {
171 31
            return false;
172 31
        }
173
        $this->reset();
174 31
        return true;
175 31
    }
176
177 31
    public function reset(): array
178 31
    {
179 31
        $settings = Arr::consolidate($this->wp(static::databaseKey(), []));
180
        if (empty($settings)) {
181
            delete_option(static::databaseKey());
182
            // glsr(Migrate::class)->reset(); // Do this to migrate any previous version settings
183
        }
184 31
        $settings = $this->normalize($settings);
185
        glsr()->store('settings', $settings);
186 31
        return $settings;
187 31
    }
188 31
189 31
    /**
190 31
     * @param mixed $value
191 31
     */
192 31
    public function set(string $path, $value = ''): bool
193
    {
194 31
        $settings = $this->all();
195
        $settings = Arr::set($settings, $path, $value);
196
        $settings = $this->normalize($settings);
197 31
        if (!update_option(static::databaseKey(), $settings, true)) {
198
            return false;
199
        }
200
        glsr()->store('settings', $settings);
201
        return true;
202
    }
203
204
    public function updateVersion(): void
205
    {
206
        $version = $this->get('version', '0.0.0');
207
        if (glsr()->version !== $version) {
208
            $this->set('version', glsr()->version);
209
            $this->set('version_upgraded_from', $version);
210
        }
211
    }
212
213
    /**
214
     * @param mixed $fallback
215
     *
216
     * @return mixed
217
     */
218
    public function wp(string $path, $fallback = '', string $cast = '')
219
    {
220
        $option = get_option($path, $fallback);
221
        return Cast::to($cast, Helper::ifEmpty($option, $fallback, $strict = true));
222
    }
223
224
    public function kses(array $data): array
225
    {
226
        $data = Arr::flatten($data);
227
        array_walk($data, function (&$value) {
228
            if (is_string($value)) {
229
                $value = wp_kses($value, wp_kses_allowed_html('post'));
230
            }
231
        });
232
        $data = Arr::unflatten($data);
233
        return $data;
234
    }
235
236
    /**
237
     * This restores orphaned settings in cases where addons have been deactivated, etc.
238
     */
239
    protected function restoreOrphanedSettings(array $settings, array $saved): array
240
    {
241
        $defaults = glsr()->defaults();
242
        $settings = Arr::set($settings, 'settings.strings', Arr::get($saved, 'settings.strings', []));
243
        foreach (Arr::get($saved, 'settings.addons', []) as $addon => $values) {
244
            if (!isset($defaults['settings']['addons'][$addon])) {
245
                $settings['settings']['addons'][$addon] = $values;
246
            }
247
        }
248
        foreach (Arr::get($saved, 'settings.integrations', []) as $integration => $values) {
249
            if (!isset($defaults['settings']['integrations'][$integration])) {
250
                $settings['settings']['integrations'][$integration] = $values;
251
            }
252
        }
253
        foreach (Arr::get($saved, 'settings.licenses', []) as $addon => $value) {
254
            if (!isset($defaults['settings']['licenses'][$addon])) {
255
                $settings['settings']['licenses'][$addon] = $value;
256
            }
257
        }
258
        return $settings;
259
    }
260
}
261