Passed
Push — develop ( c34e50...445d6d )
by Paul
06:42
created

OptionManager::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 10
cc 2
nc 2
nop 2
crap 2.0078
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
 * @method string getString(string $path = '', $fallback = '')
17
 */
18
class OptionManager
19
{
20
    /**
21
     * @return mixed
22
     */
23 22
    public function __call(string $method, array $args = [])
24
    {
25 22
        if (!str_starts_with($method, 'get')) {
26
            throw new \BadMethodCallException("Method [$method] does not exist.");
27
        }
28 22
        $cast = strtolower((string) substr($method, 3));
29 22
        if (!in_array($cast, ['array', 'bool', 'float', 'int', 'string'])) {
30
            throw new \BadMethodCallException("Method [$method] does not exist.");
31
        }
32 22
        $path = Arr::getAs('string', $args, 0);
33 22
        $fallback = Arr::get($args, 1);
34 22
        return call_user_func([$this, 'get'], $path, $fallback, $cast);
35
    }
36
37 66
    public function all(): array
38
    {
39 66
        $settings = Arr::consolidate(glsr()->retrieve('settings'));
40 66
        if (empty($settings)) {
41
            $settings = $this->reset();
42
        }
43 66
        return $settings;
44
    }
45
46 36
    public function clean(array $data = []): array
47
    {
48 36
        $settings = $this->kses($data);
49 36
        if (!empty(glsr()->settings)) { // access the property directly to prevent an infinite loop
50
            $savedSettings = $settings;
51
            $defaults = glsr()->defaults(); // @phpstan-ignore-line
52
            $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 36
        return $settings;
59
    }
60
61 118
    public static function databaseKey(?int $version = null): string
62
    {
63 118
        $versions = static::databaseKeys();
64 118
        if (null === $version) {
65 118
            $version = glsr()->version('major');
66
        }
67 118
        if (array_key_exists($version, $versions)) {
68 118
            return $versions[$version];
69
        }
70
        return '';
71
    }
72
73 118
    public static function databaseKeys(): array
74
    {
75 118
        $keys = [];
76 118
        $slug = Str::snakeCase(glsr()->id);
77 118
        $version = intval(glsr()->version('major')) + 1;
78 118
        while (--$version) {
79 118
            if ($version >= 7) {
80 118
                $keys[$version] = $slug; // remove version from settings key in versions >= 7.0
81 118
            } elseif (1 === $version) {
82 118
                $keys[$version] = sprintf('geminilabs_%s_settings', $slug);
83 118
            } elseif (2 === $version) {
84 118
                $keys[$version] = sprintf('geminilabs_%s-v%s', $slug, $version);
85
            } else {
86 118
                $keys[$version] = sprintf('%s_v%s', $slug, $version);
87
            }
88
        }
89 118
        return $keys;
90
    }
91
92
    public static function flushSettingsCache(): void
93
    {
94
        $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
        if ($flushed) {
103
            wp_cache_set('alloptions', $alloptions, 'options');
104
        }
105
    }
106
107
    /**
108
     * @param mixed $fallback
109
     *
110
     * @return mixed
111
     */
112 66
    public function get(string $path = '', $fallback = '', string $cast = '')
113
    {
114 66
        $settings = $this->all();
115 66
        $option = Arr::get($settings, $path, $fallback);
116 66
        $path = ltrim(Str::removePrefix($path, 'settings'), '.');
117 66
        if (!empty($path)) {
118 66
            $hook = 'option/'.str_replace('.', '/', $path);
119 66
            $option = glsr()->filter($hook, $option, $settings, $path);
120
        }
121 66
        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 36
    public function normalize(array $data = []): array
150
    {
151 36
        $settings = $this->kses($data);
152 36
        if (!empty(glsr()->settings)) { // access the property directly to prevent an infinite loop
153
            $defaults = glsr()->defaults(); // @phpstan-ignore-line
154
            $defaults = Arr::flatten($defaults);
155
            $settings = Arr::flatten($settings);
156
            $settings = wp_parse_args($settings, $defaults);
157
            $settings = Arr::unflatten($settings);
158
        }
159 36
        return $settings;
160
    }
161
162
    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
            $settings = Arr::consolidate(get_option($databaseKey));
170
            if (!empty(array_filter(Arr::getAs('array', $settings, 'settings')))) {
171
                return $settings;
172
            }
173
        }
174
        return [];
175
    }
176
177 36
    public function replace(array $settings): bool
178
    {
179 36
        if (empty($settings)) {
180
            return false;
181
        }
182 36
        $settings = $this->normalize($settings);
183 36
        if (!update_option(static::databaseKey(), $settings, true)) {
184
            return false;
185
        }
186 36
        $this->reset();
187 36
        return true;
188
    }
189
190 36
    public function reset(): array
191
    {
192 36
        $settings = Arr::consolidate($this->wp(static::databaseKey(), []));
193 36
        if (empty($settings)) {
194
            delete_option(static::databaseKey());
195
        }
196 36
        $settings = $this->normalize($settings);
197 36
        glsr()->store('settings', $settings);
198 36
        return $settings;
199
    }
200
201
    /**
202
     * @param mixed $value
203
     */
204 6
    public function set(string $path, $value = ''): bool
205
    {
206 6
        $settings = $this->all();
207 6
        $settings = Arr::set($settings, $path, $value);
208 6
        $settings = $this->normalize($settings);
209 6
        if (!update_option(static::databaseKey(), $settings, true)) {
210
            return false;
211
        }
212 6
        glsr()->store('settings', $settings);
213 6
        return true;
214
    }
215
216
    public function updateVersion(): void
217
    {
218
        $version = $this->get('version', '0.0.0');
219
        if (glsr()->version !== $version) {
220
            $this->set('version', glsr()->version);
221
            $this->set('version_upgraded_from', $version);
222
        }
223
    }
224
225
    /**
226
     * @param mixed $fallback
227
     *
228
     * @return mixed
229
     */
230 36
    public function wp(string $path, $fallback = '', string $cast = '')
231
    {
232 36
        $option = get_option($path, $fallback);
233 36
        return Cast::to($cast, Helper::ifEmpty($option, $fallback, $strict = true));
234
    }
235
236 36
    public function kses(array $data): array
237
    {
238 36
        $data = Arr::flatten($data);
239 36
        array_walk($data, function (&$value) {
240 36
            if (is_string($value)) {
241 36
                $value = wp_kses($value, wp_kses_allowed_html('post'));
242
            }
243 36
        });
244 36
        $data = Arr::unflatten($data);
245 36
        return $data;
246
    }
247
248
    /**
249
     * This restores orphaned settings in cases where addons have been deactivated, etc.
250
     */
251
    protected function restoreOrphanedSettings(array $settings, array $saved): array
252
    {
253
        $defaults = glsr()->defaults();
254
        $settings = Arr::set($settings, 'settings.strings', Arr::get($saved, 'settings.strings', []));
255
        foreach (Arr::get($saved, 'settings.addons', []) as $addon => $values) {
256
            if (!isset($defaults['settings']['addons'][$addon])) {
257
                $settings['settings']['addons'][$addon] = $values;
258
            }
259
        }
260
        foreach (Arr::get($saved, 'settings.integrations', []) as $integration => $values) {
261
            if (!isset($defaults['settings']['integrations'][$integration])) {
262
                $settings['settings']['integrations'][$integration] = $values;
263
            }
264
        }
265
        foreach (Arr::get($saved, 'settings.licenses', []) as $addon => $value) {
266
            if (!isset($defaults['settings']['licenses'][$addon])) {
267
                $settings['settings']['licenses'][$addon] = $value;
268
            }
269
        }
270
        return $settings;
271
    }
272
}
273