| 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\Services\Integrations\MailChimp\MailChimp; |
| 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 |
$values = []; |
| 38 |
$key = $this->request->get('key'); |
| 39 |
|
| 40 |
if (is_array($key)) { |
| 41 |
foreach ($key as $key_item) { |
| 42 |
$values[$key_item] = get_option($key_item); |
| 43 |
} |
| 44 |
} else { |
| 45 |
$values[$key] = get_option($key); |
| 46 |
} |
| 47 |
|
| 48 |
wp_send_json_success($values, 200); |
| 49 |
} |
| 50 |
|
| 51 |
public function store() |
| 52 |
{ |
| 53 |
$key = $this->request->get('key'); |
| 54 |
$method = 'store' . ucwords($key); |
| 55 |
|
| 56 |
$allowedMethods = [ |
| 57 |
'storeReCaptcha', |
| 58 |
'storeSaveGlobalLayoutSettings', |
| 59 |
'storeMailChimpSettings', |
| 60 |
'storeEmailSummarySettings' |
| 61 |
]; |
| 62 |
|
| 63 |
if (in_array($method, $allowedMethods)) { |
| 64 |
$this->{$method}(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function storeReCaptcha() |
| 69 |
{ |
| 70 |
$data = $this->request->get('reCaptcha'); |
| 71 |
|
| 72 |
if ($data == 'clear-settings') { |
| 73 |
delete_option('_fluentform_reCaptcha_details'); |
| 74 |
|
| 75 |
update_option('_fluentform_reCaptcha_keys_status', false, 'no'); |
| 76 |
|
| 77 |
wp_send_json_success([ |
| 78 |
'message' => __('Your reCaptcha settings are deleted.', 'fluentform'), |
| 79 |
'status' => false |
| 80 |
], 200); |
| 81 |
} |
| 82 |
|
| 83 |
$token = ArrayHelper::get($data, 'token'); |
| 84 |
|
| 85 |
$secretKey = ArrayHelper::get($data, 'secretKey'); |
| 86 |
|
| 87 |
// If token is not empty meaning user verified their captcha. |
| 88 |
if ($token) { |
| 89 |
// Validate the reCaptcha response. |
| 90 |
$status = ReCaptcha::validate($token, $secretKey); |
| 91 |
|
| 92 |
// reCaptcha is valid. So proceed to store. |
| 93 |
if ($status) { |
| 94 |
// Prepare captcha data. |
| 95 |
$captchaData = [ |
| 96 |
'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')), |
| 97 |
'secretKey' => sanitize_text_field($secretKey) |
| 98 |
]; |
| 99 |
|
| 100 |
// Update the reCaptcha details with siteKey & secretKey. |
| 101 |
update_option('_fluentform_reCaptcha_details', $captchaData, 'no'); |
| 102 |
|
| 103 |
// Update the reCaptcha validation status. |
| 104 |
update_option('_fluentform_reCaptcha_keys_status', $status, 'no'); |
| 105 |
|
| 106 |
// Send success response letting the user know that |
| 107 |
// that the reCaptcha is valid and saved properly. |
| 108 |
wp_send_json_success([ |
| 109 |
'message' => __('Your reCaptcha is valid and saved.', 'fluentform'), |
| 110 |
'status' => $status |
| 111 |
], 200); |
| 112 |
} else { // reCatcha is not valid. |
| 113 |
$message = __('Sorry, Your reCaptcha is not valid, Please try again', 'fluentform'); |
| 114 |
} |
| 115 |
} else { // The token is empty, so the user didn't verify their captcha. |
| 116 |
$message = __('Please validate your reCaptcha first and then hit save.', 'fluentform'); |
| 117 |
|
| 118 |
// Get the already stored reCaptcha status. |
| 119 |
$status = get_option('_fluentform_reCaptcha_keys_status'); |
| 120 |
|
| 121 |
if ($status) { |
| 122 |
$message = __('Your reCaptcha details are already valid, So no need to save again.', 'fluentform'); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
wp_send_json_error([ |
| 127 |
'message' => $message, |
| 128 |
'status' => $status |
| 129 |
], 400); |
| 130 |
} |
| 131 |
|
| 132 |
public function storeSaveGlobalLayoutSettings() |
| 133 |
{ |
| 134 |
$settings = $this->request->get('value'); |
| 135 |
$settings = json_decode($settings, true); |
| 136 |
$sanitizedSettings = fluentFormSanitizer($settings); |
| 137 |
|
| 138 |
if (ArrayHelper::get($settings, 'misc.email_footer_text')) { |
| 139 |
$sanitizedSettings['misc']['email_footer_text'] = wp_unslash($settings['misc']['email_footer_text']); |
| 140 |
} |
| 141 |
|
| 142 |
update_option('_fluentform_global_form_settings', $sanitizedSettings, 'no'); |
| 143 |
|
| 144 |
wp_send_json_success([ |
| 145 |
'message' => __('Global layout settings has been saved') |
| 146 |
], 200); |
| 147 |
} |
| 148 |
|
| 149 |
public function storeMailChimpSettings() |
| 150 |
{ |
| 151 |
$mailChimp = $this->request->get('mailchimp'); |
| 152 |
|
| 153 |
if (!$mailChimp['apiKey']) { |
| 154 |
$mailChimpSettings = [ |
| 155 |
'apiKey' => '', |
| 156 |
'status' => false |
| 157 |
]; |
| 158 |
// Update the reCaptcha details with siteKey & secretKey. |
| 159 |
|
| 160 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no'); |
| 161 |
|
| 162 |
wp_send_json_success([ |
| 163 |
'message' => __('Your settings has been updated', 'fluentform'), |
| 164 |
'status' => false |
| 165 |
], 200); |
| 166 |
} |
| 167 |
|
| 168 |
// Verify API key now |
| 169 |
try { |
| 170 |
$MailChimp = new MailChimp($mailChimp['apiKey']); |
| 171 |
$result = $MailChimp->get('lists'); |
| 172 |
if (!$MailChimp->success()) { |
| 173 |
throw new \Exception($MailChimp->getLastError()); |
| 174 |
} |
| 175 |
} catch (\Exception $exception) { |
| 176 |
wp_send_json_error([ |
| 177 |
'message' => $exception->getMessage() |
| 178 |
], 400); |
| 179 |
} |
| 180 |
|
| 181 |
// MailChimp key is verified now, Proceed now |
| 182 |
|
| 183 |
$mailChimpSettings = [ |
| 184 |
'apiKey' => sanitize_text_field($mailChimp['apiKey']), |
| 185 |
'status' => true |
| 186 |
]; |
| 187 |
|
| 188 |
// Update the reCaptcha details with siteKey & secretKey. |
| 189 |
update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no'); |
| 190 |
|
| 191 |
wp_send_json_success([ |
| 192 |
'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'), |
| 193 |
'status' => true |
| 194 |
], 200); |
| 195 |
} |
| 196 |
|
| 197 |
public function storeEmailSummarySettings() |
| 198 |
{ |
| 199 |
$defaults = [ |
| 200 |
'status' => 'yes', |
| 201 |
'send_to_type' => 'admin_email', |
| 202 |
'custom_recipients' => '', |
| 203 |
'sending_day' => 'Mon' |
| 204 |
]; |
| 205 |
$settings = $this->request->get('value'); |
| 206 |
$settings = json_decode($settings, true); |
| 207 |
|
| 208 |
$settings = wp_parse_args($settings, $defaults); |
| 209 |
|
| 210 |
update_option('_fluentform_email_report_summary', $settings); |
| 211 |
|
| 212 |
$emailReportHookName = 'fluentform_do_email_report_scheduled_tasks'; |
| 213 |
if (!wp_next_scheduled($emailReportHookName)) { |
| 214 |
wp_schedule_event(time(), 'daily', $emailReportHookName); |
| 215 |
} |
| 216 |
|
| 217 |
wp_send_json_success([ |
| 218 |
'message' => __('Email Summary Settings has been updated') |
| 219 |
], 200); |
| 220 |
|
| 221 |
} |
| 222 |
} |
| 223 |
|