| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Settings; |
| 4 |
|
| 5 |
use FluentForm\Framework\Request\Request; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
use FluentForm\App\Modules\ReCaptcha\ReCaptcha; |
| 8 |
use FluentForm\App\Modules\HCaptcha\HCaptcha; |
| 9 |
use FluentForm\App\Modules\Turnstile\Turnstile; |
| 10 |
use FluentForm\App\Services\Integrations\MailChimp\MailChimp; |
| 11 |
|
| 12 |
/** |
| 13 |
* Global Settings |
| 14 |
* |
| 15 |
* @package FluentForm\App\Modules\Settings |
| 16 |
*/ |
| 17 |
class Settings |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var \FluentForm\Framework\Request\Request |
| 21 |
*/ |
| 22 |
protected $request; |
| 23 |
|
| 24 |
/** |
| 25 |
* Settings constructor. |
| 26 |
* |
| 27 |
* @param \FluentForm\Framework\Request\Request $request |
| 28 |
*/ |
| 29 |
public function __construct(Request $request) |
| 30 |
{ |
| 31 |
$this->request = $request; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get a global settings for an specified key. |
| 36 |
*/ |
| 37 |
public function get() |
| 38 |
{ |
| 39 |
$values = []; |
| 40 |
$key = $this->request->get('key'); |
| 41 |
|
| 42 |
if (is_array($key)) { |
| 43 |
foreach ($key as $key_item) { |
| 44 |
$values[$key_item] = get_option($key_item); |
| 45 |
} |
| 46 |
} else { |
| 47 |
$values[$key] = get_option($key); |
| 48 |
} |
| 49 |
$values = apply_filters('fluentform_get_global_settings_values', $values, $key); |
| 50 |
wp_send_json_success($values, 200); |
| 51 |
} |
| 52 |
|
| 53 |
public function store() |
| 54 |
{ |
| 55 |
$key = $this->request->get('key'); |
| 56 |
$method = 'store' . ucwords($key); |
| 57 |
|
| 58 |
$allowedMethods = [ |
| 59 |
'storeReCaptcha', |
| 60 |
'storeHCaptcha', |
| 61 |
'storeTurnstile', |
| 62 |
'storeSaveGlobalLayoutSettings', |
| 63 |
'storeMailChimpSettings', |
| 64 |
'storeEmailSummarySettings' |
| 65 |
]; |
| 66 |
do_action('fluentform_saving_global_settings_with_key_method',$this->request); |
| 67 |
|
| 68 |
if (in_array($method, $allowedMethods)) { |
| 69 |
$this->{$method}(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function storeReCaptcha() |
| 74 |
{ |
| 75 |
$data = $this->request->get('reCaptcha'); |
| 76 |
|
| 77 |
if ($data == 'clear-settings') { |
| 78 |
delete_option('_fluentform_reCaptcha_details'); |
| 79 |
|
| 80 |
update_option('_fluentform_reCaptcha_keys_status', false, 'no'); |
| 81 |
|
| 82 |
wp_send_json_success([ |
| 83 |
'message' => __('Your reCAPTCHA settings are deleted.', 'fluentform'), |
| 84 |
'status' => false |
| 85 |
], 200); |
| 86 |
} |
| 87 |
|
| 88 |
$token = ArrayHelper::get($data, 'token'); |
| 89 |
$secretKey = ArrayHelper::get($data, 'secretKey'); |
| 90 |
|
| 91 |
// If token is not empty meaning user verified their captcha. |
| 92 |
if ($token) { |
| 93 |
// Validate the reCaptcha response. |
| 94 |
$version = ArrayHelper::get($data, 'api_version', 'v2_visible'); |
| 95 |
|
| 96 |
$status = ReCaptcha::validate($token, $secretKey, $version); |
| 97 |
|
| 98 |
// reCaptcha is valid. So proceed to store. |
| 99 |
if ($status) { |
| 100 |
// Prepare captcha data. |
| 101 |
$captchaData = [ |
| 102 |
'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')), |
| 103 |
'secretKey' => sanitize_text_field($secretKey), |
| 104 |
'api_version' => ArrayHelper::get($data, 'api_version') |
| 105 |
]; |
| 106 |
|
| 107 |
// Update the reCaptcha details with siteKey & secretKey. |
| 108 |
update_option('_fluentform_reCaptcha_details', $captchaData, 'no'); |
| 109 |
|
| 110 |
// Update the reCaptcha validation status. |
| 111 |
update_option('_fluentform_reCaptcha_keys_status', $status, 'no'); |
| 112 |
|
| 113 |
// Send success response letting the user know that |
| 114 |
// that the reCaptcha is valid and saved properly. |
| 115 |
wp_send_json_success([ |
| 116 |
'message' => __('Your reCAPTCHA is valid and saved.', 'fluentform'), |
| 117 |
'status' => $status |
| 118 |
], 200); |
| 119 |
} else { // reCaptcha is not valid. |
| 120 |
$message = __('Sorry, Your reCAPTCHA is not valid. Please try again', 'fluentform'); |
| 121 |
} |
| 122 |
} else { // The token is empty, so the user didn't verify their captcha. |
| 123 |
$message = __('Please validate your reCAPTCHA first and then hit save.', 'fluentform'); |
| 124 |
|
| 125 |
// Get the already stored reCaptcha status. |
| 126 |
$status = get_option('_fluentform_reCaptcha_keys_status'); |
| 127 |
|
| 128 |
if ($status) { |
| 129 |
$message = __('Your reCAPTCHA details are already valid. So no need to save again.', 'fluentform'); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
wp_send_json_error([ |
| 134 |
'message' => $message, |
| 135 |
'status' => $status |
| 136 |
], 400); |
| 137 |
} |
| 138 |
|
| 139 |
public function storeHCaptcha() |
| 140 |
{ |
| 141 |
$data = $this->request->get('hCaptcha'); |
| 142 |
|
| 143 |
if ($data == 'clear-settings') { |
| 144 |
delete_option('_fluentform_hCaptcha_details'); |
| 145 |
|
| 146 |
update_option('_fluentform_hCaptcha_keys_status', false, 'no'); |
| 147 |
|
| 148 |
wp_send_json_success([ |
| 149 |
'message' => __('Your hCaptcha settings are deleted.', 'fluentform'), |
| 150 |
'status' => false |
| 151 |
], 200); |
| 152 |
} |
| 153 |
|
| 154 |
$token = ArrayHelper::get($data, 'token'); |
| 155 |
$secretKey = ArrayHelper::get($data, 'secretKey'); |
| 156 |
|
| 157 |
// If token is not empty meaning user verified their captcha. |
| 158 |
if ($token) { |
| 159 |
// Validate the hCaptcha response. |
| 160 |
$status = HCaptcha::validate($token, $secretKey); |
| 161 |
|
| 162 |
// hCaptcha is valid. So proceed to store. |
| 163 |
if ($status) { |
| 164 |
// Prepare captcha data. |
| 165 |
$captchaData = [ |
| 166 |
'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')), |
| 167 |
'secretKey' => sanitize_text_field($secretKey), |
| 168 |
]; |
| 169 |
|
| 170 |
// Update the hCaptcha details with siteKey & secretKey. |
| 171 |
update_option('_fluentform_hCaptcha_details', $captchaData, 'no'); |
| 172 |
|
| 173 |
// Update the hCaptcha validation status. |
| 174 |
update_option('_fluentform_hCaptcha_keys_status', $status, 'no'); |
| 175 |
|
| 176 |
// Send success response letting the user know that |
| 177 |
// that the hCaptcha is valid and saved properly. |
| 178 |
wp_send_json_success([ |
| 179 |
'message' => __('Your hCaptcha is valid and saved.', 'fluentform'), |
| 180 |
'status' => $status |
| 181 |
], 200); |
| 182 |
} else { // hCaptcha is not valid. |
| 183 |
$message = __('Sorry, Your hCaptcha is not valid, Please try again', 'fluentform'); |
| 184 |
} |
| 185 |
} else { // The token is empty, so the user didn't verify their captcha. |
| 186 |
$message = __('Please validate your hCaptcha first and then hit save.', 'fluentform'); |
| 187 |
|
| 188 |
// Get the already stored hCaptcha status. |
| 189 |
$status = get_option('_fluentform_hCaptcha_keys_status'); |
| 190 |
|
| 191 |
if ($status) { |
| 192 |
$message = __('Your hCaptcha details are already valid, So no need to save again.', 'fluentform'); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
wp_send_json_error([ |
| 197 |
'message' => $message, |
| 198 |
'status' => $status |
| 199 |
], 400); |
| 200 |
} |
| 201 |
|
| 202 |
public function storeTurnstile() |
| 203 |
{ |
| 204 |
$data = $this->request->get('turnstile'); |
| 205 |
|
| 206 |
if ($data == 'clear-settings') { |
| 207 |
delete_option('_fluentform_turnstile_details'); |
| 208 |
|
| 209 |
update_option('_fluentform_turnstile_keys_status', false, 'no'); |
| 210 |
|
| 211 |
wp_send_json_success([ |
| 212 |
'message' => __('Your Turnstile settings are deleted.', 'fluentform'), |
| 213 |
'status' => false |
| 214 |
], 200); |
| 215 |
} |
| 216 |
|
| 217 |
$token = ArrayHelper::get($data, 'token'); |
| 218 |
$secretKey = ArrayHelper::get($data, 'secretKey'); |
| 219 |
|
| 220 |
// If token is not empty meaning user verified their captcha. |
| 221 |
if ($token) { |
| 222 |
// Validate the turnstile response. |
| 223 |
$status = Turnstile::validate($token, $secretKey); |
| 224 |
|
| 225 |
// turnstile is valid. So proceed to store. |
| 226 |
if ($status) { |
| 227 |
// Prepare captcha data. |
| 228 |
$captchaData = [ |
| 229 |
'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')), |
| 230 |
'secretKey' => sanitize_text_field($secretKey), |
| 231 |
'token' => $token |
| 232 |
]; |
| 233 |
|
| 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 |
wp_send_json_success([ |
| 243 |
'message' => __('Your Turnstile is valid and saved.', 'fluentform'), |
| 244 |
'status' => $status |
| 245 |
], 200); |
| 246 |
} else { |
| 247 |
// turnstile is not valid. |
| 248 |
$message = __('Sorry, Your Turnstile is 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 |
$message = __('Your Turnstile details are already valid. So no need to save again.', 'fluentform'); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
wp_send_json_error([ |
| 263 |
'message' => $message, |
| 264 |
'status' => $status |
| 265 |
], 400); |
| 266 |
} |
| 267 |
|
| 268 |
public function storeSaveGlobalLayoutSettings() |
| 269 |
{ |
| 270 |
$settings = $this->request->get('value'); |
| 271 |
$settings = json_decode($settings, true); |
| 272 |
$sanitizedSettings = fluentFormSanitizer($settings); |
| 273 |
|
| 274 |
if (ArrayHelper::get($settings, 'misc.email_footer_text')) { |
| 275 |
$sanitizedSettings['misc']['email_footer_text'] = wp_unslash($settings['misc']['email_footer_text']); |
| 276 |
} |
| 277 |
|
| 278 |
update_option('_fluentform_global_form_settings', $sanitizedSettings, 'no'); |
| 279 |
|
| 280 |
wp_send_json_success([ |
| 281 |
'message' => __('Global layout settings has been saved') |
| 282 |
], 200); |
| 283 |
} |
| 284 |
|
| 285 |
public function storeMailChimpSettings() |
| 286 |
{ |
| 287 |
$mailChimp = $this->request->get('mailchimp'); |
| 288 |
|
| 289 |
if (!$mailChimp['apiKey']) { |
| 290 |
$mailChimpSettings = [ |
| 291 |
'apiKey' => '', |
| 292 |
'status' => false |
| 293 |
]; |
| 294 |
// Update the reCaptcha details with siteKey & secretKey. |
| 295 |
|
| 296 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no'); |
| 297 |
|
| 298 |
wp_send_json_success([ |
| 299 |
'message' => __('Your settings has been updated', 'fluentform'), |
| 300 |
'status' => false |
| 301 |
], 200); |
| 302 |
} |
| 303 |
|
| 304 |
// Verify API key now |
| 305 |
try { |
| 306 |
$MailChimp = new MailChimp($mailChimp['apiKey']); |
| 307 |
$result = $MailChimp->get('lists'); |
| 308 |
if (!$MailChimp->success()) { |
| 309 |
throw new \Exception($MailChimp->getLastError()); |
| 310 |
} |
| 311 |
} catch (\Exception $exception) { |
| 312 |
wp_send_json_error([ |
| 313 |
'message' => $exception->getMessage() |
| 314 |
], 400); |
| 315 |
} |
| 316 |
|
| 317 |
// Mailchimp key is verified now, Proceed now |
| 318 |
|
| 319 |
$mailChimpSettings = [ |
| 320 |
'apiKey' => sanitize_text_field($mailChimp['apiKey']), |
| 321 |
'status' => true |
| 322 |
]; |
| 323 |
|
| 324 |
// Update the reCaptcha details with siteKey & secretKey. |
| 325 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no'); |
| 326 |
|
| 327 |
wp_send_json_success([ |
| 328 |
'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'), |
| 329 |
'status' => true |
| 330 |
], 200); |
| 331 |
} |
| 332 |
|
| 333 |
public function storeEmailSummarySettings() |
| 334 |
{ |
| 335 |
$defaults = [ |
| 336 |
'status' => 'yes', |
| 337 |
'send_to_type' => 'admin_email', |
| 338 |
'custom_recipients' => '', |
| 339 |
'sending_day' => 'Mon' |
| 340 |
]; |
| 341 |
$settings = $this->request->get('value'); |
| 342 |
$settings = json_decode($settings, true); |
| 343 |
|
| 344 |
$settings = wp_parse_args($settings, $defaults); |
| 345 |
|
| 346 |
update_option('_fluentform_email_report_summary', $settings); |
| 347 |
|
| 348 |
$emailReportHookName = 'fluentform_do_email_report_scheduled_tasks'; |
| 349 |
if (!wp_next_scheduled($emailReportHookName)) { |
| 350 |
wp_schedule_event(time(), 'daily', $emailReportHookName); |
| 351 |
} |
| 352 |
|
| 353 |
wp_send_json_success([ |
| 354 |
'message' => __('Email Summary Settings has been updated') |
| 355 |
], 200); |
| 356 |
|
| 357 |
} |
| 358 |
} |
| 359 |
|