Test Failed
Push — main ( 6646cf...aabacf )
by Paul
14:17 queued 05:58
created

OptionManager::clean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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