| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | namespace ElementPack\Modules\ContactForm; |
|---|
| 4 | |
|---|
| 5 | use ElementPack\Base\Element_Pack_Module_Base; |
|---|
| 6 | use ElementPack\Utils; |
|---|
| 7 | |
|---|
| 8 | if (!defined('ABSPATH')) exit; // Exit if accessed directly |
|---|
| 9 | |
|---|
| 10 | class Module extends Element_Pack_Module_Base { |
|---|
| 11 | |
|---|
| 12 | public function get_name() { |
|---|
| 13 | return 'contact-form'; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | public function get_widgets() { |
|---|
| 17 | |
|---|
| 18 | $widgets = ['Contact_Form']; |
|---|
| 19 | |
|---|
| 20 | return $widgets; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | public function is_valid_captcha() { |
|---|
| 24 | |
|---|
| 25 | $ep_api_settings = get_option('element_pack_api_settings'); |
|---|
| 26 | |
|---|
| 27 | if (isset($_POST['g-recaptcha-response']) and !empty($ep_api_settings['recaptcha_secret_key'])) { |
|---|
| 28 | $request = wp_remote_get('https://www.google.com/recaptcha/api/siteverify?secret=' . $ep_api_settings['recaptcha_secret_key'] . '&response=' . esc_textarea($_POST["g-recaptcha-response"]) . '&remoteip=' . $_SERVER["REMOTE_ADDR"]); |
|---|
| 29 | $response = wp_remote_retrieve_body($request); |
|---|
| 30 | |
|---|
| 31 | $result = json_decode($response, TRUE); |
|---|
| 32 | |
|---|
| 33 | if (isset($result['success']) && $result['success'] == 1) { |
|---|
| 34 | // Captcha ok |
|---|
| 35 | return true; |
|---|
| 36 | } else { |
|---|
| 37 | // Captcha failed; |
|---|
| 38 | return false; |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | return false; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | public function normalize_email( $email ) { |
|---|
| 46 | /** |
|---|
| 47 | * Split the email into local part and domain |
|---|
| 48 | */ |
|---|
| 49 | list( $local, $domain ) = explode( '@', $email ); |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Remove any text after the plus sign in the local part |
|---|
| 53 | */ |
|---|
| 54 | if ( ( $plusPos = strpos( $local, '+' ) ) !== false ) { |
|---|
| 55 | $local = substr( $local, 0, $plusPos ); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Return the normalized email |
|---|
| 60 | */ |
|---|
| 61 | return $local . '@' . $domain; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public function are_emails_same( $email1, $email2 ) { |
|---|
| 65 | return $this->normalize_email( $email1 ) === $this->normalize_email( $email2 ); |
|---|
| 66 | } |
|---|
| 67 | public function contact_form() { |
|---|
| 68 | |
|---|
| 69 | $email = get_bloginfo('admin_email'); |
|---|
| 70 | $error_empty = esc_html__('Please fill in all the required fields.', 'bdthemes-element-pack'); |
|---|
| 71 | $error_noemail = esc_html__('Please enter a valid e-mail.', 'bdthemes-element-pack'); |
|---|
| 72 | $error_same_as_admin = esc_html__('You can not use this e-mail due to security issues.', 'bdthemes-element-pack'); |
|---|
| 73 | $error_spam_email = esc_html__('You are trying to send e-mail by banned e-mail. Multiple tries can ban you permanently!', 'bdthemes-element-pack'); |
|---|
| 74 | $result = esc_html__('Unknown error! Please check your settings.', 'bdthemes-element-pack'); |
|---|
| 75 | $ep_api_settings = get_option('element_pack_api_settings'); |
|---|
| 76 | $api_settings = get_option('element_pack_api_settings');; |
|---|
| 77 | |
|---|
| 78 | if (!empty($ep_api_settings['contact_form_email'])) { |
|---|
| 79 | $email = $ep_api_settings['contact_form_email']; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|---|
| 83 | |
|---|
| 84 | if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'simpleContactForm')) { |
|---|
| 85 | $result = esc_html__('Security check failed!', 'bdthemes-element-pack'); |
|---|
| 86 | echo '<span class="bdt-text-warning">' . esc_html($result) . '</span>'; |
|---|
| 87 | wp_die(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | $post_id = sanitize_text_field($_REQUEST['page_id']); |
|---|
| 91 | $widget_id = sanitize_text_field($_REQUEST['widget_id']); |
|---|
| 92 | |
|---|
| 93 | $error = false; |
|---|
| 94 | |
|---|
| 95 | // this part fetches everything that has been POSTed, sanitizes them and lets us use them as $form_data['subject'] |
|---|
| 96 | foreach ($_POST as $field => $value) { |
|---|
| 97 | if (is_email($value)) { |
|---|
| 98 | $value = sanitize_email($value); |
|---|
| 99 | } else { |
|---|
| 100 | $value = sanitize_textarea_field($value); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | $form_data[$field] = strip_tags($value); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | foreach ($form_data as $key => $value) { |
|---|
| 107 | $value = trim($value); |
|---|
| 108 | if (empty($value)) { |
|---|
| 109 | $error = true; |
|---|
| 110 | $result = $error_empty; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | // Success message part |
|---|
| 115 | if (!empty($_POST['custom_success_message'])) { |
|---|
| 116 | $custom_message = sanitize_text_field($_POST['custom_success_message']); |
|---|
| 117 | |
|---|
| 118 | $placeholders = ['[name]', '[email]']; |
|---|
| 119 | $replacements = [esc_html($form_data['name']), esc_html($form_data['email'])]; |
|---|
| 120 | $custom_message = str_replace($placeholders, $replacements, $custom_message); |
|---|
| 121 | |
|---|
| 122 | $success = (strlen($custom_message) <= 255) |
|---|
| 123 | ? $custom_message |
|---|
| 124 | : esc_html__('Invalid length of custom success message.', 'bdthemes-element-pack'); |
|---|
| 125 | } else { |
|---|
| 126 | $success = sprintf( |
|---|
| 127 | esc_html__('Hi, %s. We got your e-mail. We\'ll reply to you very soon. Thanks for being with us...', 'bdthemes-element-pack'), |
|---|
| 128 | esc_html($form_data['name']) |
|---|
| 129 | ); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | // and if the e-mail is not valid, switch $error to TRUE and set the result text to the shortcode attribute named 'error_noemail' |
|---|
| 134 | if (!is_email($form_data['email'])) { |
|---|
| 135 | $error = true; |
|---|
| 136 | $result = $error_noemail; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Stop spamming |
|---|
| 141 | */ |
|---|
| 142 | if (!$error) { |
|---|
| 143 | $admin_email = get_option('admin_email'); |
|---|
| 144 | if ( $this->are_emails_same( wp_kses_post( trim( $form_data['email'] ) ), $admin_email ) || $admin_email == wp_kses_post(trim($form_data['email'])) || $email == wp_kses_post(trim($form_data['email']))) { |
|---|
| 145 | $error = true; |
|---|
| 146 | $result = $error_same_as_admin; |
|---|
| 147 | } else { |
|---|
| 148 | if (isset($api_settings['contact_form_spam_email'])) { |
|---|
| 149 | $spam_email_list = $api_settings['contact_form_spam_email']; |
|---|
| 150 | $final_spam_list = explode(',', $spam_email_list); |
|---|
| 151 | foreach ($final_spam_list as $spam_email) { |
|---|
| 152 | if (trim($form_data['email']) == trim($spam_email)) { |
|---|
| 153 | $error = true; |
|---|
| 154 | $result = $error_spam_email; |
|---|
| 155 | break; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** Recaptcha*/ |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | $result_recaptcha = $this->get_widget_settings($post_id, $widget_id); |
|---|
| 166 | |
|---|
| 167 | if (isset($result_recaptcha['show_recaptcha']) && $result_recaptcha['show_recaptcha'] == 'yes') { |
|---|
| 168 | if (!empty($ep_api_settings['recaptcha_site_key']) and !empty($ep_api_settings['recaptcha_secret_key'])) { |
|---|
| 169 | if (!$this->is_valid_captcha()) { |
|---|
| 170 | $error = true; |
|---|
| 171 | $result = esc_html__("reCAPTCHA is invalid!", "bdthemes-element-pack"); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | $contact_number = isset($form_data['contact']) ? esc_attr($form_data['contact']) : ''; |
|---|
| 178 | $contact_subject = isset($form_data['subject']) ? esc_attr($form_data['subject']) : ''; |
|---|
| 179 | |
|---|
| 180 | // but if $error is still FALSE, put together the POSTed variables and send the e-mail! |
|---|
| 181 | if ($error == false) { |
|---|
| 182 | // get the website's name and puts it in front of the subject |
|---|
| 183 | $email_subject = "[" . get_bloginfo('name') . "] " . $contact_subject; |
|---|
| 184 | // get the message from the form and add the IP address of the user below it |
|---|
| 185 | $email_message = $this->message_html($form_data['message'], $form_data['name'], $form_data['email'], $contact_number); |
|---|
| 186 | // set the e-mail headers with the user's name, e-mail address and character encoding |
|---|
| 187 | $headers = "Reply-To: " . $form_data['name'] . " <" . $form_data['email'] . ">\n"; |
|---|
| 188 | $headers .= "Content-Type: text/html; charset=UTF-8\n"; |
|---|
| 189 | $headers .= "Content-Transfer-Encoding: 8bit\n"; |
|---|
| 190 | // send the e-mail with the shortcode attribute named 'email' and the POSTed data |
|---|
| 191 | wp_mail($email, html_entity_decode($email_subject), $email_message, $headers); |
|---|
| 192 | // and set the result text to the shortcode attribute named 'success' |
|---|
| 193 | $result = $success; |
|---|
| 194 | // ...and switch the $sent variable to TRUE |
|---|
| 195 | $sent = true; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | $redirect_url = (isset($form_data['redirect-url']) && !empty($form_data['redirect-url'])) ? esc_url($form_data['redirect-url']) : 'no'; |
|---|
| 200 | $is_external = (isset($form_data['is-external']) && !empty($form_data['is-external'])) ? esc_attr($form_data['is-external']) : 'no'; |
|---|
| 201 | |
|---|
| 202 | $reset_status = (isset($form_data['reset-after-submit']) && ($form_data['reset-after-submit'] == 'yes')) ? 'yes' : 'no'; |
|---|
| 203 | |
|---|
| 204 | if ($error == false) { |
|---|
| 205 | echo '<span class="bdt-text-success" data-resetstatus="' . esc_html($reset_status) . '" data-redirect="' . wp_kses_post($redirect_url) . '" data-external="' . esc_attr($is_external) . '">' . esc_html($result) . '</span>'; |
|---|
| 206 | // wp_redirect( $form_data['redirect_url'] ); |
|---|
| 207 | } else { |
|---|
| 208 | echo '<span class="bdt-text-warning">' . esc_html($result) . '</span>'; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | die; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | public function message_html($message, $name, $email, $number = '') { |
|---|
| 216 | |
|---|
| 217 | $fullmsg = "<html lang='en-US'><body style='background-color: #f5f5f5; padding: 35px;'>"; |
|---|
| 218 | $fullmsg .= "<div style='max-width: 768px; margin: 0 auto; background-color: #fff; padding: 50px 35px;'>"; |
|---|
| 219 | $fullmsg .= nl2br($message); |
|---|
| 220 | $fullmsg .= "<br><br>"; |
|---|
| 221 | $fullmsg .= "<b>" . esc_html($name) . "<b><br>"; |
|---|
| 222 | $fullmsg .= esc_html($email) . "<br>"; |
|---|
| 223 | $fullmsg .= ($number) ? esc_html($number) . "<br>" : ""; |
|---|
| 224 | $fullmsg .= "<em>IP: " . Utils::get_client_ip() . "</em>"; |
|---|
| 225 | $fullmsg .= "</div>"; |
|---|
| 226 | $fullmsg .= "</body></html>"; |
|---|
| 227 | |
|---|
| 228 | return $fullmsg; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | public function __construct() { |
|---|
| 232 | parent::__construct(); |
|---|
| 233 | |
|---|
| 234 | add_action('wp_ajax_element_pack_contact_form', [$this, 'contact_form']); |
|---|
| 235 | add_action('wp_ajax_nopriv_element_pack_contact_form', [$this, 'contact_form']); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|