1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GeminiLabs\SiteReviews\Addons\Updater; |
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
|
|
|
|
12
|
|
|
class SettingsController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param mixed $input |
16
|
|
|
* @return array |
17
|
|
|
* @callback register_setting |
18
|
|
|
*/ |
19
|
3 |
|
public function callbackRegisterSettings($input) |
20
|
|
|
{ |
21
|
3 |
|
$settings = Arr::consolidate($input); |
22
|
3 |
|
if (1 === count($settings) && array_key_exists('settings', $settings)) { |
23
|
|
|
$options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
24
|
|
|
$options = $this->sanitizeGeneral($input, $options); |
25
|
|
|
$options = $this->sanitizeLicenses($input, $options); |
26
|
|
|
$options = $this->sanitizeSubmissions($input, $options); |
27
|
|
|
$options = $this->sanitizeTranslations($input, $options); |
28
|
|
|
$options = glsr()->filterArray('settings/callback', $options, $settings); |
29
|
|
|
if (filter_input(INPUT_POST, 'option_page') == glsr()->id.'-settings') { |
30
|
|
|
glsr(Notice::class)->addSuccess(_x('Settings updated.', 'admin-text', 'site-reviews')); |
31
|
|
|
} |
32
|
|
|
glsr(Notice::class)->store(); // store the notices before the page reloads |
33
|
|
|
return $options; |
34
|
|
|
} |
35
|
3 |
|
return $input; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return void |
40
|
|
|
* @action admin_init |
41
|
|
|
*/ |
42
|
7 |
|
public function registerSettings() |
43
|
|
|
{ |
44
|
7 |
|
register_setting(glsr()->id.'-settings', OptionManager::databaseKey(), [ |
45
|
7 |
|
'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
46
|
|
|
]); |
47
|
7 |
|
} |
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
|
|
|
$multiFields = ['limit_assignments', 'required']; |
93
|
|
|
foreach ($multiFields as $name) { |
94
|
|
|
$defaultValue = Arr::get($inputForm, $name, []); |
95
|
|
|
$options = Arr::set($options, $key.'.'.$name, $defaultValue); |
96
|
|
|
} |
97
|
|
|
return $options; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function sanitizeTranslations(array $input, array $options) |
104
|
|
|
{ |
105
|
|
|
$key = 'settings.strings'; |
106
|
|
|
$inputForm = Arr::consolidate(Arr::get($input, $key)); |
107
|
|
|
if (!empty($inputForm)) { |
108
|
|
|
$options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
109
|
|
|
$allowedTags = [ |
110
|
|
|
'a' => ['class' => [], 'href' => [], 'target' => []], |
111
|
|
|
'span' => ['class' => []], |
112
|
|
|
]; |
113
|
|
|
array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
114
|
|
|
if (isset($string['s2'])) { |
115
|
|
|
$string['s2'] = wp_kses($string['s2'], $allowedTags); |
116
|
|
|
} |
117
|
|
|
if (isset($string['p2'])) { |
118
|
|
|
$string['p2'] = wp_kses($string['p2'], $allowedTags); |
119
|
|
|
} |
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
return $options; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $integrationSlug |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
protected function hasMultilingualIntegration($integrationSlug) |
130
|
|
|
{ |
131
|
|
|
$integration = glsr(Multilingual::class)->getIntegration($integrationSlug); |
132
|
|
|
if (!$integration) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
if (!$integration->isActive()) { |
136
|
|
|
glsr(Notice::class)->addError(sprintf( |
137
|
|
|
_x('Please install/activate the %s plugin to enable integration.', 'admin-text', 'site-reviews'), |
138
|
|
|
$integration->pluginName |
139
|
|
|
)); |
140
|
|
|
return false; |
141
|
|
|
} elseif (!$integration->isSupported()) { |
142
|
|
|
glsr(Notice::class)->addError(sprintf( |
143
|
|
|
_x('Please update the %s plugin to v%s or greater to enable integration.', 'admin-text', 'site-reviews'), |
144
|
|
|
$integration->pluginName, |
145
|
|
|
$integration->supportedVersion |
146
|
|
|
)); |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $license |
154
|
|
|
* @param string $slug |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
protected function verifyLicense($license, $slug) |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
|
|
$addon = glsr($slug); // use addon alias to get the class |
161
|
|
|
$updater = new Updater($addon->update_url, $addon->file, [ |
162
|
|
|
'license' => $license, |
163
|
|
|
'testedTo' => $addon->testedTo, |
164
|
|
|
]); |
165
|
|
|
if (!$updater->isLicenseValid()) { |
166
|
|
|
throw new Exception('Invalid license: '.$license.' ('.$addon->id.')'); |
167
|
|
|
} |
168
|
|
|
} catch (Exception $e) { |
169
|
|
|
$license = ''; |
170
|
|
|
glsr_log()->error($e->getMessage()); |
171
|
|
|
$error = _x('The license you entered is either invalid or has not yet been activated.', 'admin-text', 'site-reviews'); |
172
|
|
|
$message = sprintf(_x('To activate your license, please visit the %s page on your Nifty Plugins account and click the "Manage Sites" button to activate it for your website.', 'admin-text', 'site-reviews'), |
173
|
|
|
sprintf('<a href="https://niftyplugins.com/account/license-keys/" target="_blank">%s</a>', _x('License Keys', 'admin-text', 'site-reviews')) |
174
|
|
|
); |
175
|
|
|
glsr(Notice::class)->addError(sprintf('<strong>%s</strong><br>%s', $error, $message)); |
176
|
|
|
} |
177
|
|
|
return $license; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|