| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\GlobalSettings; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Form\CleanTalkHandler; |
| 6 |
use FluentForm\App\Modules\HCaptcha\HCaptcha; |
| 7 |
use FluentForm\App\Modules\ReCaptcha\ReCaptcha; |
| 8 |
use FluentForm\App\Modules\Turnstile\Turnstile; |
| 9 |
use FluentForm\App\Services\Integrations\MailChimp\MailChimp; |
| 10 |
use FluentForm\Framework\Support\Arr; |
| 11 |
|
| 12 |
class GlobalSettingsHelper |
| 13 |
{ |
| 14 |
public function storeReCaptcha($attributes) |
| 15 |
{ |
| 16 |
$data = Arr::get($attributes, 'reCaptcha'); |
| 17 |
|
| 18 |
if ('clear-settings' == $data) { |
| 19 |
delete_option('_fluentform_reCaptcha_details'); |
| 20 |
|
| 21 |
update_option('_fluentform_reCaptcha_keys_status', false, 'no'); |
| 22 |
|
| 23 |
return([ |
| 24 |
'message' => __('Your reCAPTCHA settings are deleted.', 'fluentform'), |
| 25 |
'status' => true, |
| 26 |
]); |
| 27 |
} |
| 28 |
|
| 29 |
$token = Arr::get($data, 'token'); |
| 30 |
$secretKey = Arr::get($data, 'secretKey'); |
| 31 |
|
| 32 |
// If token is not empty meaning user verified their captcha. |
| 33 |
if ($token) { |
| 34 |
// Validate the reCaptcha response. |
| 35 |
$version = Arr::get($data, 'api_version', 'v2_visible'); |
| 36 |
|
| 37 |
$status = ReCaptcha::validate($token, $secretKey, $version); |
| 38 |
|
| 39 |
// reCaptcha is valid. So proceed to store. |
| 40 |
if ($status) { |
| 41 |
// Prepare captcha data. |
| 42 |
$captchaData = [ |
| 43 |
'siteKey' => sanitize_text_field(Arr::get($data, 'siteKey')), |
| 44 |
'secretKey' => sanitize_text_field($secretKey), |
| 45 |
'api_version' => Arr::get($data, 'api_version'), |
| 46 |
]; |
| 47 |
|
| 48 |
// Update the reCaptcha details with siteKey & secretKey. |
| 49 |
update_option('_fluentform_reCaptcha_details', $captchaData, 'no'); |
| 50 |
|
| 51 |
// Update the reCaptcha validation status. |
| 52 |
update_option('_fluentform_reCaptcha_keys_status', $status, 'no'); |
| 53 |
|
| 54 |
// Send success response letting the user know that |
| 55 |
// that the reCaptcha is valid and saved properly. |
| 56 |
return ([ |
| 57 |
'message' => __('Your reCAPTCHA is valid and saved.', 'fluentform'), |
| 58 |
'status' => $status, |
| 59 |
]); |
| 60 |
} else { // reCaptcha is not valid. |
| 61 |
$message = __('Sorry, Your reCAPTCHA is not valid. Please try again', 'fluentform'); |
| 62 |
} |
| 63 |
} else { // The token is empty, so the user didn't verify their captcha. |
| 64 |
$message = __('Please validate your reCAPTCHA first and then hit save.', 'fluentform'); |
| 65 |
|
| 66 |
// Get the already stored reCaptcha status. |
| 67 |
$status = get_option('_fluentform_reCaptcha_keys_status'); |
| 68 |
|
| 69 |
if ($status) { |
| 70 |
$message = __('Your reCAPTCHA details are already valid. So no need to save again.', 'fluentform'); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return([ |
| 75 |
'message' => $message, |
| 76 |
'status' => $status, |
| 77 |
]); |
| 78 |
} |
| 79 |
|
| 80 |
public function storeHCaptcha($attributes) |
| 81 |
{ |
| 82 |
$data = Arr::get($attributes, 'hCaptcha'); |
| 83 |
|
| 84 |
if ('clear-settings' == $data) { |
| 85 |
delete_option('_fluentform_hCaptcha_details'); |
| 86 |
|
| 87 |
update_option('_fluentform_hCaptcha_keys_status', false, 'no'); |
| 88 |
|
| 89 |
return([ |
| 90 |
'message' => __('Your hCaptcha settings are deleted.', 'fluentform'), |
| 91 |
'status' => true, |
| 92 |
]); |
| 93 |
} |
| 94 |
|
| 95 |
$token = Arr::get($data, 'token'); |
| 96 |
$secretKey = Arr::get($data, 'secretKey'); |
| 97 |
|
| 98 |
// If token is not empty meaning user verified their captcha. |
| 99 |
if ($token) { |
| 100 |
// Validate the hCaptcha response. |
| 101 |
$status = HCaptcha::validate($token, $secretKey); |
| 102 |
|
| 103 |
// hCaptcha is valid. So proceed to store. |
| 104 |
if ($status) { |
| 105 |
// Prepare captcha data. |
| 106 |
$captchaData = [ |
| 107 |
'siteKey' => sanitize_text_field(Arr::get($data, 'siteKey')), |
| 108 |
'secretKey' => sanitize_text_field($secretKey), |
| 109 |
]; |
| 110 |
|
| 111 |
// Update the hCaptcha details with siteKey & secretKey. |
| 112 |
update_option('_fluentform_hCaptcha_details', $captchaData, 'no'); |
| 113 |
|
| 114 |
// Update the hCaptcha validation status. |
| 115 |
update_option('_fluentform_hCaptcha_keys_status', $status, 'no'); |
| 116 |
|
| 117 |
// Send success response letting the user know that |
| 118 |
// that the hCaptcha is valid and saved properly. |
| 119 |
return([ |
| 120 |
'message' => __('Your hCaptcha is valid and saved.', 'fluentform'), |
| 121 |
'status' => $status, |
| 122 |
]); |
| 123 |
} else { // hCaptcha is not valid. |
| 124 |
$message = __('Sorry, Your hCaptcha is not valid, Please try again', 'fluentform'); |
| 125 |
} |
| 126 |
} else { // The token is empty, so the user didn't verify their captcha. |
| 127 |
$message = __('Please validate your hCaptcha first and then hit save.', 'fluentform'); |
| 128 |
|
| 129 |
// Get the already stored hCaptcha status. |
| 130 |
$status = get_option('_fluentform_hCaptcha_keys_status'); |
| 131 |
|
| 132 |
if ($status) { |
| 133 |
$message = __('Your hCaptcha details are already valid, So no need to save again.', 'fluentform'); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return([ |
| 138 |
'message' => $message, |
| 139 |
'status' => $status, |
| 140 |
]); |
| 141 |
} |
| 142 |
|
| 143 |
public function storeCleantalk($attributes) |
| 144 |
{ |
| 145 |
$data = Arr::get($attributes, 'cleantalk'); |
| 146 |
|
| 147 |
if ('clear-settings' == $data) { |
| 148 |
delete_option('_fluentform_cleantalk_details'); |
| 149 |
|
| 150 |
return([ |
| 151 |
'message' => __('Your CleanTalk settings are deleted.', 'fluentform'), |
| 152 |
'status' => true, |
| 153 |
]); |
| 154 |
} |
| 155 |
|
| 156 |
$accessKey = Arr::get($data, 'accessKey'); |
| 157 |
$validation = Arr::get($data, 'validation'); |
| 158 |
$status = false; |
| 159 |
|
| 160 |
if ($accessKey) { |
| 161 |
// Validate the cleantalk response. |
| 162 |
$status = CleanTalkHandler::validate($accessKey); |
| 163 |
|
| 164 |
// cleantalk is valid. So proceed to store. |
| 165 |
if ($status) { |
| 166 |
// Prepare data. |
| 167 |
$captchaData = [ |
| 168 |
'accessKey' => sanitize_text_field($accessKey), |
| 169 |
'status' => true, |
| 170 |
'validation' => $validation |
| 171 |
]; |
| 172 |
|
| 173 |
// Update the cleantalk details |
| 174 |
update_option('_fluentform_cleantalk_details', $captchaData, 'no'); |
| 175 |
|
| 176 |
// Send success response letting the user know the cleantalk is valid and saved properly. |
| 177 |
return([ |
| 178 |
'message' => __('Your CleanTalk is valid and saved.', 'fluentform'), |
| 179 |
'status' => $status, |
| 180 |
]); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$message = __('Sorry, Your CleanTalk is not valid, Please try again', 'fluentform'); |
| 185 |
|
| 186 |
$captchaData = [ |
| 187 |
'accessKey' => '', |
| 188 |
'status' => $status, |
| 189 |
'validation' => '' |
| 190 |
]; |
| 191 |
|
| 192 |
update_option('_fluentform_cleantalk_details', $captchaData, 'no'); |
| 193 |
|
| 194 |
return([ |
| 195 |
'message' => $message, |
| 196 |
'status' => $status, |
| 197 |
]); |
| 198 |
} |
| 199 |
|
| 200 |
public function storeTurnstile($attributes) |
| 201 |
{ |
| 202 |
$data = Arr::get($attributes, 'turnstile'); |
| 203 |
|
| 204 |
if ('clear-settings' == $data) { |
| 205 |
delete_option('_fluentform_turnstile_details'); |
| 206 |
|
| 207 |
update_option('_fluentform_turnstile_keys_status', false, 'no'); |
| 208 |
|
| 209 |
return([ |
| 210 |
'message' => __('Your Turnstile settings are deleted.', 'fluentform'), |
| 211 |
'status' => true, |
| 212 |
]); |
| 213 |
} |
| 214 |
|
| 215 |
$token = Arr::get($data, 'token'); |
| 216 |
$secretKey = sanitize_text_field(Arr::get($data, 'secretKey')); |
| 217 |
|
| 218 |
// Prepare captcha data. |
| 219 |
$captchaData = [ |
| 220 |
'siteKey' => Arr::get($data, 'siteKey'), |
| 221 |
'secretKey' => $secretKey, |
| 222 |
'invisible' => 'no', |
| 223 |
'appearance' => Arr::get($data, 'appearance', 'always'), |
| 224 |
'theme' => Arr::get($data, 'theme', 'auto') |
| 225 |
]; |
| 226 |
|
| 227 |
// If token is not empty meaning user verified their captcha. |
| 228 |
if ($token) { |
| 229 |
// Validate the turnstile response. |
| 230 |
$status = Turnstile::validate($token, $secretKey); |
| 231 |
|
| 232 |
// turnstile is valid. So proceed to store. |
| 233 |
if ($status) { |
| 234 |
// Update the turnstile details with siteKey & secretKey. |
| 235 |
update_option('_fluentform_turnstile_details', $captchaData, 'no'); |
| 236 |
|
| 237 |
// Update the turnstile validation status. |
| 238 |
update_option('_fluentform_turnstile_keys_status', $status, 'no'); |
| 239 |
|
| 240 |
// Send success response letting the user know that |
| 241 |
// that the turnstile is valid and saved properly. |
| 242 |
return([ |
| 243 |
'message' => __('Your Turnstile Keys are valid.', 'fluentform'), |
| 244 |
'status' => $status, |
| 245 |
]); |
| 246 |
} else { |
| 247 |
// turnstile is not valid. |
| 248 |
$message = __('Sorry, Your Turnstile Keys are not valid. Please try again!', 'fluentform'); |
| 249 |
} |
| 250 |
} else { |
| 251 |
// The token is empty, so the user didn't verify their captcha. |
| 252 |
$message = __('Please validate your Turnstile first and then hit save.', 'fluentform'); |
| 253 |
|
| 254 |
// Get the already stored reCaptcha status. |
| 255 |
$status = get_option('_fluentform_turnstile_keys_status'); |
| 256 |
|
| 257 |
if ($status) { |
| 258 |
update_option('_fluentform_turnstile_details', $captchaData, 'no'); |
| 259 |
$message = __('Your Turnstile settings is saved.', 'fluentform'); |
| 260 |
|
| 261 |
return([ |
| 262 |
'message' => $message, |
| 263 |
'status' => $status, |
| 264 |
]); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return([ |
| 269 |
'message' => $message, |
| 270 |
'status' => $status, |
| 271 |
]); |
| 272 |
} |
| 273 |
|
| 274 |
public function storeSaveGlobalLayoutSettings($attributes) |
| 275 |
{ |
| 276 |
$settings = Arr::get($attributes, 'form_settings'); |
| 277 |
$settings = json_decode($settings, true); |
| 278 |
$sanitizedSettings = fluentFormSanitizer($settings); |
| 279 |
|
| 280 |
if (Arr::get($settings, 'misc.email_footer_text')) { |
| 281 |
$sanitizedSettings['misc']['email_footer_text'] = wp_unslash($settings['misc']['email_footer_text']); |
| 282 |
} |
| 283 |
|
| 284 |
update_option('_fluentform_global_form_settings', $sanitizedSettings, 'no'); |
| 285 |
|
| 286 |
return ([ |
| 287 |
'message' => __('Global settings has been saved', 'fluentform') |
| 288 |
]); |
| 289 |
} |
| 290 |
|
| 291 |
public function storeMailChimpSettings($attributes) |
| 292 |
{ |
| 293 |
$mailChimp = Arr::get($attributes, 'mailchimp'); |
| 294 |
|
| 295 |
if (!$mailChimp['apiKey']) { |
| 296 |
$mailChimpSettings = [ |
| 297 |
'apiKey' => '', |
| 298 |
'status' => false, |
| 299 |
]; |
| 300 |
// Update the reCaptcha details with siteKey & secretKey. |
| 301 |
|
| 302 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no'); |
| 303 |
|
| 304 |
return([ |
| 305 |
'message' => __('Your settings has been updated', 'fluentform'), |
| 306 |
'status' => false, |
| 307 |
]); |
| 308 |
} |
| 309 |
|
| 310 |
// Verify API key now |
| 311 |
try { |
| 312 |
$MailChimp = new MailChimp($mailChimp['apiKey']); |
| 313 |
$result = $MailChimp->get('lists'); |
| 314 |
if (!$MailChimp->success()) { |
| 315 |
throw new \Exception($MailChimp->getLastError()); |
| 316 |
} |
| 317 |
} catch (\Exception $exception) { |
| 318 |
return([ |
| 319 |
'message' => $exception->getMessage(), |
| 320 |
]); |
| 321 |
} |
| 322 |
|
| 323 |
// Mailchimp key is verified now, Proceed now |
| 324 |
|
| 325 |
$mailChimpSettings = [ |
| 326 |
'apiKey' => sanitize_text_field($mailChimp['apiKey']), |
| 327 |
'status' => true, |
| 328 |
]; |
| 329 |
|
| 330 |
// Update the reCaptcha details with siteKey & secretKey. |
| 331 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no'); |
| 332 |
|
| 333 |
return([ |
| 334 |
'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'), |
| 335 |
'status' => true, |
| 336 |
]); |
| 337 |
} |
| 338 |
|
| 339 |
public function storeEmailSummarySettings($attributes) |
| 340 |
{ |
| 341 |
$settings = Arr::get($attributes, 'email_report'); |
| 342 |
$settings = json_decode($settings, true); |
| 343 |
|
| 344 |
$defaults = [ |
| 345 |
'status' => 'yes', |
| 346 |
'send_to_type' => 'admin_email', |
| 347 |
'custom_recipients' => '', |
| 348 |
'sending_day' => 'Mon', |
| 349 |
]; |
| 350 |
|
| 351 |
$settings = wp_parse_args($settings, $defaults); |
| 352 |
|
| 353 |
update_option('_fluentform_email_report_summary', $settings); |
| 354 |
|
| 355 |
$emailReportHookName = 'fluentform_do_email_report_scheduled_tasks'; |
| 356 |
if (!wp_next_scheduled($emailReportHookName)) { |
| 357 |
wp_schedule_event(time(), 'daily', $emailReportHookName); |
| 358 |
} |
| 359 |
|
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
public function storeAutosaveSettings($attributes) |
| 364 |
{ |
| 365 |
$enabled = Arr::get($attributes, 'autosave_enabled', 'no'); |
| 366 |
|
| 367 |
$settings = get_option('_fluentform_global_form_settings', []); |
| 368 |
if (!isset($settings['misc'])) { |
| 369 |
$settings['misc'] = []; |
| 370 |
} |
| 371 |
$settings['misc']['autosave_enabled'] = $enabled; |
| 372 |
|
| 373 |
update_option('_fluentform_global_form_settings', $settings, 'no'); |
| 374 |
|
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
public function storeDefaultStyleTemplate($attributes) |
| 379 |
{ |
| 380 |
$data = Arr::get($attributes, 'settings'); |
| 381 |
|
| 382 |
if (is_string($data) && $data === 'clear-settings') { |
| 383 |
delete_option('_fluentform_default_style_template'); |
| 384 |
|
| 385 |
return [ |
| 386 |
'message' => __('Default style template has been cleared.', 'fluentform'), |
| 387 |
'status' => true, |
| 388 |
]; |
| 389 |
} |
| 390 |
|
| 391 |
if (is_string($data)) { |
| 392 |
$data = json_decode($data, true); |
| 393 |
} |
| 394 |
|
| 395 |
$sanitizedData = [ |
| 396 |
'enabled' => Arr::get($data, 'enabled', 'no'), |
| 397 |
'custom_css' => fluentformSanitizeCSS(Arr::get($data, 'custom_css', '')), |
| 398 |
'styler_enabled' => Arr::get($data, 'styler_enabled', 'no'), |
| 399 |
'styler_theme' => sanitize_text_field(Arr::get($data, 'styler_theme', '')), |
| 400 |
'styler_styles' => [], |
| 401 |
'source_form_id' => absint(Arr::get($data, 'source_form_id', 0)), |
| 402 |
]; |
| 403 |
|
| 404 |
$stylerStyles = Arr::get($data, 'styler_styles'); |
| 405 |
if ($stylerStyles && is_array($stylerStyles)) { |
| 406 |
$sanitizedData['styler_styles'] = $stylerStyles; |
| 407 |
} |
| 408 |
|
| 409 |
update_option('_fluentform_default_style_template', $sanitizedData, 'no'); |
| 410 |
|
| 411 |
return [ |
| 412 |
'message' => __('Default style template has been saved successfully.', 'fluentform'), |
| 413 |
'status' => true, |
| 414 |
]; |
| 415 |
} |
| 416 |
} |
| 417 |
|