Passed
Push — master ( 4db9fd...925e6d )
by Paul
08:15 queued 04:12
created

SettingsController::sanitizeSubmissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Notice;
9
10
class SettingsController extends Controller
11
{
12
    /**
13
     * @param mixed $input
14
     * @return array
15
     * @callback register_setting
16
     */
17 1
    public function callbackRegisterSettings($input)
18
    {
19 1
        $settings = Arr::consolidateArray($input);
20 1
        if (1 === count($settings) && array_key_exists('settings', $settings)) {
21
            $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input);
22
            $options = $this->sanitizeGeneral($input, $options);
23
            $options = $this->sanitizeSubmissions($input, $options);
24
            $options = $this->sanitizeTranslations($input, $options);
25
            $options = apply_filters('site-reviews/settings/callback', $options, $settings);
26
            if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') {
27
                glsr(Notice::class)->addSuccess(__('Settings updated.', 'site-reviews'));
28
            }
29
            return $options;
30
        }
31 1
        return $input;
32
    }
33
34
    /**
35
     * @return void
36
     * @action admin_init
37
     */
38 1
    public function registerSettings()
39
    {
40 1
        register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [
41 1
            'sanitize_callback' => [$this, 'callbackRegisterSettings'],
42
        ]);
43 1
    }
44
45
    /**
46
     * @return array
47
     */
48
    protected function sanitizeGeneral(array $input, array $options)
49
    {
50
        $key = 'settings.general';
51
        $inputForm = Arr::get($input, $key);
52
        if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) {
53
            $options = Arr::set($options, $key.'.multilingual', '');
54
        }
55
        if ('' == trim(Arr::get($inputForm, 'notification_message'))) {
56
            $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message');
57
            $options = Arr::set($options, $key.'.notification_message', $defaultValue);
58
        }
59
        $defaultValue = Arr::get($inputForm, 'notifications', []);
60
        $options = Arr::set($options, $key.'.notifications', $defaultValue);
61
        return $options;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    protected function sanitizeSubmissions(array $input, array $options)
68
    {
69
        $key = 'settings.submissions';
70
        $inputForm = Arr::get($input, $key);
71
        $defaultValue = isset($inputForm['required'])
72
            ? $inputForm['required']
73
            : [];
74
        $options = Arr::set($options, $key.'.required', $defaultValue);
75
        return $options;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    protected function sanitizeTranslations(array $input, array $options)
82
    {
83
        $key = 'settings.strings';
84
        $inputForm = Arr::consolidateArray(Arr::get($input, $key));
85
        if (!empty($inputForm)) {
86
            $options = Arr::set($options, $key, array_values(array_filter($inputForm)));
87
            $allowedTags = [
88
                'a' => ['class' => [], 'href' => [], 'target' => []],
89
                'span' => ['class' => []],
90
            ];
91
            array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) {
92
                if (isset($string['s2'])) {
93
                    $string['s2'] = wp_kses($string['s2'], $allowedTags);
94
                }
95
                if (isset($string['p2'])) {
96
                    $string['p2'] = wp_kses($string['p2'], $allowedTags);
97
                }
98
            });
99
        }
100
        return $options;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    protected function hasMultilingualIntegration($integration)
107
    {
108
        if (!in_array($integration, ['polylang', 'wpml'])) {
109
            return false;
110
        }
111
        $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration);
112
        if (!glsr($integrationClass)->isActive()) {
113
            glsr(Notice::class)->addError(sprintf(
114
                __('Please install/activate the %s plugin to enable integration.', 'site-reviews'),
115
                constant($integrationClass.'::PLUGIN_NAME')
116
            ));
117
            return false;
118
        } elseif (!glsr($integrationClass)->isSupported()) {
119
            glsr(Notice::class)->addError(sprintf(
120
                __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'),
121
                constant($integrationClass.'::PLUGIN_NAME'),
122
                constant($integrationClass.'::SUPPORTED_VERSION')
123
            ));
124
            return false;
125
        }
126
        return true;
127
    }
128
}
129