1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GeminiLabs\SiteReviews\Application; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Multilingual; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Updater; |
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::consolidateArray($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::consolidateArray(Arr::get($input, $key)); |
75
|
|
|
foreach ($licenses as $slug => &$license) { |
76
|
|
|
$license = $this->verifyLicense($license, $slug); |
77
|
|
|
} |
78
|
|
|
$options = Arr::set($options, $key, $licenses); |
79
|
|
|
return $options; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function sanitizeSubmissions(array $input, array $options) |
86
|
|
|
{ |
87
|
|
|
$key = 'settings.submissions'; |
88
|
|
|
$inputForm = Arr::get($input, $key); |
89
|
|
|
$defaultValue = isset($inputForm['required']) |
90
|
|
|
? $inputForm['required'] |
91
|
|
|
: []; |
92
|
|
|
$options = Arr::set($options, $key.'.required', $defaultValue); |
93
|
|
|
return $options; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
protected function sanitizeTranslations(array $input, array $options) |
100
|
|
|
{ |
101
|
|
|
$key = 'settings.strings'; |
102
|
|
|
$inputForm = Arr::consolidateArray(Arr::get($input, $key)); |
103
|
|
|
if (!empty($inputForm)) { |
104
|
|
|
$options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
105
|
|
|
$allowedTags = [ |
106
|
|
|
'a' => ['class' => [], 'href' => [], 'target' => []], |
107
|
|
|
'span' => ['class' => []], |
108
|
|
|
]; |
109
|
|
|
array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
110
|
|
|
if (isset($string['s2'])) { |
111
|
|
|
$string['s2'] = wp_kses($string['s2'], $allowedTags); |
112
|
|
|
} |
113
|
|
|
if (isset($string['p2'])) { |
114
|
|
|
$string['p2'] = wp_kses($string['p2'], $allowedTags); |
115
|
|
|
} |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
return $options; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $integrationSlug |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function hasMultilingualIntegration($integrationSlug) |
126
|
|
|
{ |
127
|
|
|
$integration = glsr(Multilingual::class)->getIntegration($integrationSlug); |
128
|
|
|
if (!$integration) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
if (!$integration->isActive()) { |
132
|
|
|
glsr(Notice::class)->addError(sprintf( |
133
|
|
|
__('Please install/activate the %s plugin to enable integration.', 'site-reviews'), |
134
|
|
|
$integration->pluginName |
135
|
|
|
)); |
136
|
|
|
return false; |
137
|
|
|
} elseif (!$integration->isSupported()) { |
138
|
|
|
glsr(Notice::class)->addError(sprintf( |
139
|
|
|
__('Please update the %s plugin to v%s or greater to enable integration.', 'site-reviews'), |
140
|
|
|
$integration->pluginName, |
141
|
|
|
$integration->supportedVersion |
142
|
|
|
)); |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $license |
150
|
|
|
* @param string $slug |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function verifyLicense($license, $slug) |
154
|
|
|
{ |
155
|
|
|
try { |
156
|
|
|
$addon = glsr($slug); |
157
|
|
|
$updater = new Updater($addon->update_url, $addon->file, [ |
158
|
|
|
'license' => $license, |
159
|
|
|
'testedTo' => $addon->testedTo, |
160
|
|
|
]); |
161
|
|
|
if (!$updater->isLicenseValid()) { |
162
|
|
|
throw new Exception('Invalid license: '.$license.' ('.$addon->id.')'); |
163
|
|
|
} |
164
|
|
|
} catch (Exception $e) { |
165
|
|
|
$license = ''; |
166
|
|
|
glsr_log()->debug($e->getMessage()); |
167
|
|
|
glsr(Notice::class)->addError(__('A license you entered was invalid.', 'site-reviews')); |
168
|
|
|
} |
169
|
|
|
return $license; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|