Passed
Push — feature/rebusify ( fe0687...495106 )
by Paul
05:25 queued 15s
created

SettingsController::isPolylangActiveAndSupported()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 12
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\Modules\Notice;
8
9
class SettingsController extends Controller
10
{
11
    /**
12
     * @param mixed $input
13
     * @return array
14
     * @callback register_setting
15
     */
16 1
    public function callbackRegisterSettings($input)
17
    {
18 1
        if (!is_array($input)) {
19
            $input = ['settings' => []];
20
        }
21 1
        if ('settings' == key($input)) {
22
            $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input);
23
            $options = $this->sanitizeGeneral($input, $options);
24
            $options = $this->sanitizeSubmissions($input, $options);
25
            $options = $this->sanitizeTranslations($input, $options);
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
        $inputForm = $input['settings']['general'];
51
        if (!$this->hasMultilingualIntegration($inputForm['support']['multilingual'])) {
52
            $options['settings']['general']['support']['multilingual'] = '';
53
        }
54
        if ('' == trim($inputForm['notification_message'])) {
55
            $options['settings']['general']['notification_message'] = glsr()->defaults['settings']['general']['notification_message'];
56
        }
57
        $options['settings']['general']['notifications'] = glsr_get($inputForm, 'notifications', []);
58
        return $options;
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    protected function sanitizeSubmissions(array $input, array $options)
65
    {
66
        $inputForm = $input['settings']['submissions'];
67
        $options['settings']['submissions']['required'] = isset($inputForm['required'])
68
            ? $inputForm['required']
69
            : [];
70
        return $options;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    protected function sanitizeTranslations(array $input, array $options)
77
    {
78
        if (isset($input['settings']['strings'])) {
79
            $options['settings']['strings'] = array_values(array_filter($input['settings']['strings']));
80
            $allowedTags = [
81
                'a' => ['class' => [], 'href' => [], 'target' => []],
82
                'span' => ['class' => []],
83
            ];
84
            array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) {
85
                if (isset($string['s2'])) {
86
                    $string['s2'] = wp_kses($string['s2'], $allowedTags);
87
                }
88
                if (isset($string['p2'])) {
89
                    $string['p2'] = wp_kses($string['p2'], $allowedTags);
90
                }
91
            });
92
        }
93
        return $options;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    protected function hasMultilingualIntegration($integration)
100
    {
101
        if (!in_array($integration, ['polylang', 'wpml'])) {
102
            return false;
103
        }
104
        $integrationClass = 'GeminiLabs\SiteReviews\Modules\\'.ucfirst($integration);
105
        if (!glsr($integrationClass)->isActive()) {
106
            glsr(Notice::class)->addError(sprintf(
107
                __('Please install/activate the %s plugin to enable integration.', 'site-reviews'),
108
                constant($integrationClass.'::PLUGIN_NAME')
109
            ));
110
            return false;
111
        } elseif (!glsr($integrationClass)->isSupported()) {
112
            glsr(Notice::class)->addError(sprintf(
113
                __('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'),
114
                constant($integrationClass.'::PLUGIN_NAME'),
115
                constant($integrationClass.'::SUPPORTED_VERSION')
116
            ));
117
            return false;
118
        }
119
        return true;
120
    }
121
}
122