| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Settings; |
| 4 |
|
| 5 |
use FluentForm\App\Services\Integrations\MailChimp; |
| 6 |
use FluentForm\Framework\Request\Request; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
use FluentForm\App\Modules\ReCaptcha\ReCaptcha; |
| 9 |
|
| 10 |
/** |
| 11 |
* Global Settings |
| 12 |
* |
| 13 |
* @package FluentForm\App\Modules\Settings |
| 14 |
*/ |
| 15 |
class Settings |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var \FluentForm\Framework\Request\Request |
| 19 |
*/ |
| 20 |
protected $request; |
| 21 |
|
| 22 |
/** |
| 23 |
* Settings constructor. |
| 24 |
* |
| 25 |
* @param \FluentForm\Framework\Request\Request $request |
| 26 |
*/ |
| 27 |
public function __construct(Request $request) |
| 28 |
{ |
| 29 |
$this->request = $request; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get a global settings for an specified key. |
| 34 |
*/ |
| 35 |
public function get() |
| 36 |
{ |
| 37 |
$key = $this->request->get('key'); |
| 38 |
$values = array(); |
| 39 |
if (is_array($key)) { |
| 40 |
foreach ($key as $key_item) { |
| 41 |
$values[$key_item] = get_option($key_item); |
| 42 |
} |
| 43 |
} else { |
| 44 |
$values[$key] = get_option($key); |
| 45 |
} |
| 46 |
wp_send_json_success($values, 200); |
| 47 |
} |
| 48 |
|
| 49 |
public function store() |
| 50 |
{ |
| 51 |
$key = $this->request->get('key'); |
| 52 |
$method = 'store'.ucwords($key); |
| 53 |
|
| 54 |
$allowedMethods = array( |
| 55 |
'storeReCaptcha', |
| 56 |
'storeSaveGlobalLayoutSettings', |
| 57 |
'storeMailChimpSettings' |
| 58 |
); |
| 59 |
|
| 60 |
if (in_array($method, $allowedMethods)) { |
| 61 |
$this->{$method}(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function storeReCaptcha() |
| 66 |
{ |
| 67 |
$data = $this->request->get('reCaptcha'); |
| 68 |
|
| 69 |
$token = ArrayHelper::get($data, 'token'); |
| 70 |
|
| 71 |
$secretKey = ArrayHelper::get($data, 'secretKey'); |
| 72 |
|
| 73 |
// If token is not empty meaning user verified their captcha. |
| 74 |
if ($token) { |
| 75 |
// Validate the reCaptcha response. |
| 76 |
$status = ReCaptcha::validate($token, $secretKey); |
| 77 |
|
| 78 |
// reCaptcha is valid. So proceed to store. |
| 79 |
if ($status) { |
| 80 |
// Prepare captcha data. |
| 81 |
$captchaData = [ |
| 82 |
'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')), |
| 83 |
'secretKey' => sanitize_text_field($secretKey) |
| 84 |
]; |
| 85 |
|
| 86 |
// Update the reCaptcha details with siteKey & secretKey. |
| 87 |
update_option('_fluentform_reCaptcha_details', $captchaData); |
| 88 |
|
| 89 |
// Update the reCaptcha validation status. |
| 90 |
update_option('_fluentform_reCaptcha_keys_status', $status); |
| 91 |
|
| 92 |
// Send success response letting the user know that |
| 93 |
// that the reCaptcha is valid and saved properly. |
| 94 |
wp_send_json_success(array( |
| 95 |
'message' => __('Your reCaptcha is valid and saved.', 'fluentform'), |
| 96 |
'status' => $status |
| 97 |
), 200); |
| 98 |
} else { // reCatcha is not valid. |
| 99 |
$message = __('Sorry, Your reCaptcha is not valid, Please try again', 'fluentform'); |
| 100 |
} |
| 101 |
} else { // The token is empty, so the user didn't verify their captcha. |
| 102 |
$message = __('Please validate your reCaptcha first and then hit save.', 'fluentform'); |
| 103 |
|
| 104 |
// Get the already stored reCaptcha status. |
| 105 |
$status = get_option('_fluentform_reCaptcha_keys_status'); |
| 106 |
|
| 107 |
if ($status) { |
| 108 |
$message = __('Your reCaptcha details are already valid, So no need to save again.', 'fluentform'); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
wp_send_json_error(array( |
| 113 |
'message' => $message, |
| 114 |
'status' => $status |
| 115 |
), 400); |
| 116 |
} |
| 117 |
|
| 118 |
public function storeSaveGlobalLayoutSettings() |
| 119 |
{ |
| 120 |
$settings = $this->request->get('value'); |
| 121 |
$settings = json_decode($settings, true); |
| 122 |
$settings = fluentFormSanitizer($settings); |
| 123 |
update_option('_fluentform_global_form_settings', $settings); |
| 124 |
wp_send_json_success(array( |
| 125 |
'message' => __('Global layout settings is saved') |
| 126 |
), 200); |
| 127 |
} |
| 128 |
|
| 129 |
public function storeMailChimpSettings() |
| 130 |
{ |
| 131 |
$mailChimp =$this->request->get('mailchimp'); |
| 132 |
|
| 133 |
if(!$mailChimp['apiKey']) { |
| 134 |
$mailChimpSettings = array( |
| 135 |
'apiKey' => '', |
| 136 |
'status' => false |
| 137 |
); |
| 138 |
// Update the reCaptcha details with siteKey & secretKey. |
| 139 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings); |
| 140 |
wp_send_json_success(array( |
| 141 |
'message' => __('Your settings has been updated', 'fluentform'), |
| 142 |
'status' => false |
| 143 |
), 200); |
| 144 |
} |
| 145 |
|
| 146 |
// Verify API key now |
| 147 |
try { |
| 148 |
$MailChimp = new MailChimp($mailChimp['apiKey']); |
| 149 |
$result = $MailChimp->get('lists'); |
| 150 |
if(!$MailChimp->success()) { |
| 151 |
throw new \Exception($MailChimp->getLastError()); |
| 152 |
} |
| 153 |
} catch (\Exception $exception) { |
| 154 |
wp_send_json_error(array( |
| 155 |
'message' => $exception->getMessage() |
| 156 |
), 400); |
| 157 |
} |
| 158 |
|
| 159 |
// MailChimp key is verified now, Proceed now |
| 160 |
|
| 161 |
$mailChimpSettings = array( |
| 162 |
'apiKey' => sanitize_text_field($mailChimp['apiKey']), |
| 163 |
'status' => true |
| 164 |
); |
| 165 |
|
| 166 |
// Update the reCaptcha details with siteKey & secretKey. |
| 167 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings); |
| 168 |
|
| 169 |
wp_send_json_success(array( |
| 170 |
'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'), |
| 171 |
'status' => true |
| 172 |
), 200); |
| 173 |
|
| 174 |
} |
| 175 |
} |
| 176 |
|