request = $request; } /** * Get a global settings for an specified key. */ public function get() { $key = $this->request->get('key'); $values = array(); if (is_array($key)) { foreach ($key as $key_item) { $values[$key_item] = get_option($key_item); } } else { $values[$key] = get_option($key); } wp_send_json_success($values, 200); } public function store() { $key = $this->request->get('key'); $method = 'store'.ucwords($key); $allowedMethods = array( 'storeReCaptcha', 'storeSaveGlobalLayoutSettings', 'storeMailChimpSettings' ); if (in_array($method, $allowedMethods)) { $this->{$method}(); } } public function storeReCaptcha() { $data = $this->request->get('reCaptcha'); $token = ArrayHelper::get($data, 'token'); $secretKey = ArrayHelper::get($data, 'secretKey'); // If token is not empty meaning user verified their captcha. if ($token) { // Validate the reCaptcha response. $status = ReCaptcha::validate($token, $secretKey); // reCaptcha is valid. So proceed to store. if ($status) { // Prepare captcha data. $captchaData = [ 'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')), 'secretKey' => sanitize_text_field($secretKey) ]; // Update the reCaptcha details with siteKey & secretKey. update_option('_fluentform_reCaptcha_details', $captchaData); // Update the reCaptcha validation status. update_option('_fluentform_reCaptcha_keys_status', $status); // Send success response letting the user know that // that the reCaptcha is valid and saved properly. wp_send_json_success(array( 'message' => __('Your reCaptcha is valid and saved.', 'fluentform'), 'status' => $status ), 200); } else { // reCatcha is not valid. $message = __('Sorry, Your reCaptcha is not valid, Please try again', 'fluentform'); } } else { // The token is empty, so the user didn't verify their captcha. $message = __('Please validate your reCaptcha first and then hit save.', 'fluentform'); // Get the already stored reCaptcha status. $status = get_option('_fluentform_reCaptcha_keys_status'); if ($status) { $message = __('Your reCaptcha details are already valid, So no need to save again.', 'fluentform'); } } wp_send_json_error(array( 'message' => $message, 'status' => $status ), 400); } public function storeSaveGlobalLayoutSettings() { $settings = $this->request->get('value'); $settings = json_decode($settings, true); $settings = fluentFormSanitizer($settings); update_option('_fluentform_global_form_settings', $settings); wp_send_json_success(array( 'message' => __('Global layout settings is saved') ), 200); } public function storeMailChimpSettings() { $mailChimp =$this->request->get('mailchimp'); if(!$mailChimp['apiKey']) { $mailChimpSettings = array( 'apiKey' => '', 'status' => false ); // Update the reCaptcha details with siteKey & secretKey. update_option('_fluentform_mailchimp_details', $mailChimpSettings); wp_send_json_success(array( 'message' => __('Your settings has been updated', 'fluentform'), 'status' => false ), 200); } // Verify API key now try { $MailChimp = new MailChimp($mailChimp['apiKey']); $result = $MailChimp->get('lists'); if(!$MailChimp->success()) { throw new \Exception($MailChimp->getLastError()); } } catch (\Exception $exception) { wp_send_json_error(array( 'message' => $exception->getMessage() ), 400); } // MailChimp key is verified now, Proceed now $mailChimpSettings = array( 'apiKey' => sanitize_text_field($mailChimp['apiKey']), 'status' => true ); // Update the reCaptcha details with siteKey & secretKey. update_option('_fluentform_mailchimp_details', $mailChimpSettings); wp_send_json_success(array( 'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'), 'status' => true ), 200); } }