Passed
Push — feature/rebusify ( 495106...67b08f )
by Paul
06:10 queued 02:03
created

Upgrade_3_0_0::getOldSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Upgrader;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helper;
8
9
class Upgrade_3_0_0
10
{
11
    const MAPPED_SETTINGS = [
12
        'settings.general.notification' => 'settings.general.notifications', // array
13
        'settings.general.notification_email' => 'settings.general.notification_email',
14
        'settings.general.notification_message' => 'settings.general.notification_message',
15
        'settings.general.require.approval' => 'settings.general.require.approval',
16
        'settings.general.require.login' => 'settings.general.require.login',
17
        'settings.general.require.login_register' => 'settings.general.require.login_register',
18
        'settings.general.webhook_url' => 'settings.general.notification_slack',
19
        'settings.reviews-form.akismet' => 'settings.submissions.akismet',
20
        'settings.reviews-form.blacklist.action' => 'settings.submissions.blacklist.action',
21
        'settings.reviews-form.blacklist.entries' => 'settings.submissions.blacklist.entries',
22
        'settings.reviews-form.recaptcha.integration' => 'settings.submissions.recaptcha.integration',
23
        'settings.reviews-form.recaptcha.key' => 'settings.submissions.recaptcha.key',
24
        'settings.reviews-form.recaptcha.position' => 'settings.submissions.recaptcha.position',
25
        'settings.reviews-form.recaptcha.secret' => 'settings.submissions.recaptcha.secret',
26
        'settings.reviews-form.required' => 'settings.submissions.required', // array
27
        'settings.reviews.assigned_links.enabled' => 'settings.reviews.assigned_links',
28
        'settings.reviews.avatars.enabled' => 'settings.reviews.avatars',
29
        'settings.reviews.date.custom' => 'settings.reviews.date.custom',
30
        'settings.reviews.date.format' => 'settings.reviews.date.format',
31
        'settings.reviews.excerpt.enabled' => 'settings.reviews.excerpts',
32
        'settings.reviews.excerpt.length' => 'settings.reviews.excerpts_length',
33
        'settings.reviews.schema.address' => 'settings.schema.address',
34
        'settings.reviews.schema.description.custom' => 'settings.schema.description.custom',
35
        'settings.reviews.schema.description.default' => 'settings.schema.description.default',
36
        'settings.reviews.schema.highprice' => 'settings.schema.highprice',
37
        'settings.reviews.schema.image.custom' => 'settings.schema.image.custom',
38
        'settings.reviews.schema.image.default' => 'settings.schema.image.default',
39
        'settings.reviews.schema.lowprice' => 'settings.schema.lowprice',
40
        'settings.reviews.schema.name.custom' => 'settings.schema.name.custom',
41
        'settings.reviews.schema.name.default' => 'settings.schema.name.default',
42
        'settings.reviews.schema.pricecurrency' => 'settings.schema.pricecurrency',
43
        'settings.reviews.schema.pricerange' => 'settings.schema.pricerange',
44
        'settings.reviews.schema.telephone' => 'settings.schema.telephone',
45
        'settings.reviews.schema.type.custom' => 'settings.schema.type.custom',
46
        'settings.reviews.schema.type.default' => 'settings.schema.type.default',
47
        'settings.reviews.schema.url.custom' => 'settings.schema.url.custom',
48
        'settings.reviews.schema.url.default' => 'settings.schema.url.default',
49
        'version' => 'version_upgraded_from',
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    protected $newSettings;
56
57
    /**
58
     * @var array
59
     */
60
    protected $oldSettings;
61
62
    public function __construct()
63
    {
64
        $this->migrateSettings();
65
    }
66
67
    /**
68
     * @return void
69
     */
70
    public function migrateSettings()
71
    {
72
        $this->newSettings = $this->getNewSettings();
73
        $this->oldSettings = $this->getOldSettings();
74
        if (empty($this->oldSettings)) {
75
            return;
76
        }
77
        foreach (static::MAPPED_SETTINGS as $old => $new) {
78
            if (empty($this->oldSettings[$old])) {
79
                continue;
80
            }
81
            $this->newSettings[$new] = $this->oldSettings[$old];
82
        }
83
        $this->migrateNotificationSettings();
84
        $this->migrateRecaptchaSettings();
85
        $this->migrateRequiredSettings();
86
        $oldSettings = glsr(Helper::class)->convertDotNotationArray($this->oldSettings);
87
        $newSettings = glsr(Helper::class)->convertDotNotationArray($this->newSettings);
88
        if (isset($oldSettings['settings']['strings']) && is_array($oldSettings['settings']['strings'])) {
89
            $newSettings['settings']['strings'] = $oldSettings['settings']['strings'];
90
        }
91
        glsr(OptionManager::class)->set($newSettings);
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    protected function getNewSettings()
98
    {
99
        return wp_parse_args(
100
            glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()),
101
            glsr(DefaultsManager::class)->defaults()
102
        );
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    protected function getOldSettings()
109
    {
110
        $defaults = array_fill_keys(array_keys(static::MAPPED_SETTINGS), '');
111
        $settings = glsr(Helper::class)->flattenArray(get_option('geminilabs_site_reviews-v2', []));
112
        if (!empty($settings)) {
113
            $settings = wp_parse_args($settings, $defaults);
114
        }
115
        return $settings;
116
    }
117
118
    /**
119
     * @return void
120
     */
121
    protected function migrateNotificationSettings()
122
    {
123
        $notifications = [
124
            'custom' => 'custom',
125
            'default' => 'admin',
126
            'webhook' => 'slack',
127
        ];
128
        $this->newSettings['settings.general.notifications'] = [];
129
        foreach ($notifications as $old => $new) {
130
            if ($this->oldSettings['settings.general.notification'] != $old) {
131
                continue;
132
            }
133
            $this->newSettings['settings.general.notifications'][] = $new;
134
        }
135
    }
136
137
    /**
138
     * @return void
139
     */
140
    protected function migrateRecaptchaSettings()
141
    {
142
        $recaptcha = [
143
            'BadgePosition' => $this->oldSettings['settings.reviews-form.recaptcha.position'],
144
            'SecretKey' => $this->oldSettings['settings.reviews-form.recaptcha.secret'],
145
            'SiteKey' => $this->oldSettings['settings.reviews-form.recaptcha.key'],
146
        ];
147
        if (in_array($this->oldSettings['settings.reviews-form.recaptcha.integration'], ['custom', 'invisible-recaptcha'])) {
148
            $this->newSettings['settings.submissions.recaptcha.integration'] = 'all';
149
        }
150
        if ('invisible-recaptcha' == $this->oldSettings['settings.reviews-form.recaptcha.integration']) {
151
            $recaptcha = wp_parse_args((array) get_site_option('ic-settings', [], false), $recaptcha);
152
        }
153
        $this->newSettings['settings.submissions.recaptcha.key'] = $recaptcha['SiteKey'];
154
        $this->newSettings['settings.submissions.recaptcha.secret'] = $recaptcha['SecretKey'];
155
        $this->newSettings['settings.submissions.recaptcha.position'] = $recaptcha['BadgePosition'];
156
    }
157
158
    /**
159
     * @return void
160
     */
161
    protected function migrateRequiredSettings()
162
    {
163
        $this->newSettings['settings.submissions.required'] = array_filter((array) $this->oldSettings['settings.reviews-form.required']);
164
        $this->newSettings['settings.submissions.required'][] = 'rating';
165
        $this->newSettings['settings.submissions.required'][] = 'terms';
166
    }
167
}
168