Passed
Push — master ( ccb079...7906b4 )
by Paul
04:39
created

SettingsController::sanitizeLicenses()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 12
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use Exception;
6
use GeminiLabs\SiteReviews\Addons\Updater;
7
use GeminiLabs\SiteReviews\Application;
8
use GeminiLabs\SiteReviews\Database\OptionManager;
9
use GeminiLabs\SiteReviews\Helpers\Arr;
10
use GeminiLabs\SiteReviews\Modules\Multilingual;
11
use GeminiLabs\SiteReviews\Modules\Notice;
12
13
class SettingsController extends Controller
14
{
15
    /**
16
     * @param mixed $input
17
     * @return array
18
     * @callback register_setting
19
     */
20 1
    public function callbackRegisterSettings($input)
21
    {
22 1
        $settings = Arr::consolidate($input);
23 1
        if (1 === count($settings) && array_key_exists('settings', $settings)) {
24
            $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input);
25
            $options = $this->sanitizeGeneral($input, $options);
26
            $options = $this->sanitizeLicenses($input, $options);
27
            $options = $this->sanitizeSubmissions($input, $options);
28
            $options = $this->sanitizeTranslations($input, $options);
29
            $options = apply_filters('site-reviews/settings/callback', $options, $settings);
30
            if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') {
31
                glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews'));
32
            }
33
            return $options;
34
        }
35 1
        return $input;
36
    }
37
38
    /**
39
     * @return void
40
     * @action admin_init
41
     */
42 1
    public function registerSettings()
43
    {
44 1
        register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [
45 1
            'sanitize_callback' => [$this, 'callbackRegisterSettings'],
46
        ]);
47 1
    }
48
49
    /**
50
     * @return array
51
     */
52
    protected function sanitizeGeneral(array $input, array $options)
53
    {
54
        $key = 'settings.general';
55
        $inputForm = Arr::get($input, $key);
56
        if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) {
57
            $options = Arr::set($options, $key.'.multilingual', '');
58
        }
59
        if ('' == trim(Arr::get($inputForm, 'notification_message'))) {
60
            $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message');
61
            $options = Arr::set($options, $key.'.notification_message', $defaultValue);
62
        }
63
        $defaultValue = Arr::get($inputForm, 'notifications', []);
64
        $options = Arr::set($options, $key.'.notifications', $defaultValue);
65
        return $options;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function sanitizeLicenses(array $input, array $options)
72
    {
73
        $key = 'settings.licenses';
74
        $licenses = Arr::consolidate(Arr::get($input, $key));
75
        foreach ($licenses as $slug => &$license) {
76
            if (empty($license)) {
77
                continue;
78
            }
79
            $license = $this->verifyLicense($license, $slug);
80
        }
81
        $options = Arr::set($options, $key, $licenses);
82
        return $options;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    protected function sanitizeSubmissions(array $input, array $options)
89
    {
90
        $key = 'settings.submissions';
91
        $inputForm = Arr::get($input, $key);
92
        $defaultValue = isset($inputForm['required'])
93
            ? $inputForm['required']
94
            : [];
95
        $options = Arr::set($options, $key.'.required', $defaultValue);
96
        return $options;
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    protected function sanitizeTranslations(array $input, array $options)
103
    {
104
        $key = 'settings.strings';
105
        $inputForm = Arr::consolidate(Arr::get($input, $key));
106
        if (!empty($inputForm)) {
107
            $options = Arr::set($options, $key, array_values(array_filter($inputForm)));
108
            $allowedTags = [
109
                'a' => ['class' => [], 'href' => [], 'target' => []],
110
                'span' => ['class' => []],
111
            ];
112
            array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) {
113
                if (isset($string['s2'])) {
114
                    $string['s2'] = wp_kses($string['s2'], $allowedTags);
115
                }
116
                if (isset($string['p2'])) {
117
                    $string['p2'] = wp_kses($string['p2'], $allowedTags);
118
                }
119
            });
120
        }
121
        return $options;
122
    }
123
124
    /**
125
     * @param string $integrationSlug
126
     * @return bool
127
     */
128
    protected function hasMultilingualIntegration($integrationSlug)
129
    {
130
        $integration = glsr(Multilingual::class)->getIntegration($integrationSlug);
131
        if (!$integration) {
132
            return false;
133
        }
134
        if (!$integration->isActive()) {
135
            glsr(Notice::class)->addError(sprintf(
136
                __('Please install/activate the %s plugin to enable integration.', 'site-reviews'),
137
                $integration->pluginName
138
            ));
139
            return false;
140
        } elseif (!$integration->isSupported()) {
141
            glsr(Notice::class)->addError(sprintf(
142
                __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'),
143
                $integration->pluginName,
144
                $integration->supportedVersion
145
            ));
146
            return false;
147
        }
148
        return true;
149
    }
150
151
    /**
152
     * @param string $license
153
     * @param string $slug
154
     * @return string
155
     */
156
    protected function verifyLicense($license, $slug)
157
    {
158
        try {
159
            $addon = glsr($slug);
160
            $updater = new Updater($addon->update_url, $addon->file, [
161
                'license' => $license,
162
                'testedTo' => $addon->testedTo,
163
            ]);
164
            if (!$updater->isLicenseValid()) {
165
                throw new Exception('Invalid license: '.$license.' ('.$addon->id.')');
166
            }
167
        } catch (Exception $e) {
168
            $license = '';
169
            glsr_log()->debug($e->getMessage());
170
            glsr(Notice::class)->addError(__('A license you entered was invalid.', 'site-reviews'));
171
        }
172
        return $license;
173
    }
174
}
175