| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class ContactForm extends AbstractBlock { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
|
| 9 |
parent::__construct( 'getwid/contact-form' ); |
| 10 |
|
| 11 |
add_action( 'wp_ajax_getwid_update_recaptcha_credentials', array( $this, 'update_recaptcha_credentials' ) ); |
| 12 |
|
| 13 |
add_action( 'wp_ajax_getwid_send_mail', array( $this, 'send' ) ); |
| 14 |
add_action( 'wp_ajax_nopriv_getwid_send_mail', array( $this, 'send' ) ); |
| 15 |
|
| 16 |
$this->register_contact_form_blocks(); |
| 17 |
|
| 18 |
add_action( 'enqueue_block_assets', array( $this, 'block_frontend_assets' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
public function get_label() { |
| 22 |
return __( 'Contact Form', 'getwid' ); |
| 23 |
} |
| 24 |
|
| 25 |
private function register_contact_form_blocks() { |
| 26 |
|
| 27 |
register_block_type( |
| 28 |
getwid_get_plugin_path( 'assets/blocks/contact-form/contact-form' ), |
| 29 |
array( |
| 30 |
'render_callback' => array( $this, 'render_callback' ), |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
register_block_type( |
| 35 |
getwid_get_plugin_path( 'assets/blocks/contact-form/field-name' ), |
| 36 |
array( |
| 37 |
'render_callback' => array( $this, 'render_field_name_block' ), |
| 38 |
) |
| 39 |
); |
| 40 |
|
| 41 |
register_block_type( |
| 42 |
getwid_get_plugin_path( 'assets/blocks/contact-form/field-email' ), |
| 43 |
array( |
| 44 |
'render_callback' => array( $this, 'render_field_email_block' ), |
| 45 |
) |
| 46 |
); |
| 47 |
|
| 48 |
register_block_type( |
| 49 |
getwid_get_plugin_path( 'assets/blocks/contact-form/field-textarea' ), |
| 50 |
array( |
| 51 |
'render_callback' => array( $this, 'render_field_textarea_block' ), |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function render_field_name_block( $attributes ) { |
| 57 |
|
| 58 |
ob_start(); |
| 59 |
getwid_get_template_part( 'contact-form/field-name', $attributes, false ); |
| 60 |
|
| 61 |
return ob_get_clean(); |
| 62 |
} |
| 63 |
|
| 64 |
public function render_field_email_block( $attributes ) { |
| 65 |
|
| 66 |
ob_start(); |
| 67 |
getwid_get_template_part( 'contact-form/field-email', $attributes, false ); |
| 68 |
|
| 69 |
return ob_get_clean(); |
| 70 |
} |
| 71 |
|
| 72 |
public function render_field_textarea_block( $attributes ) { |
| 73 |
|
| 74 |
ob_start(); |
| 75 |
getwid_get_template_part( 'contact-form/field-textarea', $attributes, false ); |
| 76 |
|
| 77 |
return ob_get_clean(); |
| 78 |
} |
| 79 |
|
| 80 |
public function render_captcha_block( $attributes ) { |
| 81 |
|
| 82 |
$site_key = get_option( 'getwid_recaptcha_v2_site_key', '' ); |
| 83 |
|
| 84 |
$extra_attr = array( |
| 85 |
'site_key' => $site_key, |
| 86 |
); |
| 87 |
|
| 88 |
$result = ''; |
| 89 |
if ( $site_key ) { |
| 90 |
|
| 91 |
wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js?render=explicit' ); |
| 92 |
|
| 93 |
ob_start(); |
| 94 |
getwid_get_template_part( 'contact-form/captcha', $attributes, false, $extra_attr ); |
| 95 |
|
| 96 |
$result = ob_get_clean(); |
| 97 |
} |
| 98 |
|
| 99 |
return $result; |
| 100 |
} |
| 101 |
|
| 102 |
public function block_frontend_assets() { |
| 103 |
|
| 104 |
if ( is_admin() ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$inline_script = 'var Getwid = Getwid || {};'; |
| 109 |
$inline_script .= 'Getwid["ajax_url"] = ' . wp_json_encode( admin_url( 'admin-ajax.php' ) ) . ';'; |
| 110 |
$inline_script .= 'Getwid["nonces"] = ' . wp_json_encode( |
| 111 |
array( |
| 112 |
'contact_form' => wp_create_nonce( 'getwid_nonce_send_contact_form' ), |
| 113 |
) |
| 114 |
) . ';'; |
| 115 |
|
| 116 |
wp_add_inline_script( |
| 117 |
'getwid-contact-form-view-script', |
| 118 |
$inline_script, |
| 119 |
'before' |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
public function render_callback( $attributes, $content ) { |
| 124 |
|
| 125 |
$class = 'wp-block-getwid-contact-form'; |
| 126 |
$block_name = $class; |
| 127 |
|
| 128 |
$button_style = ''; |
| 129 |
$button_class = ''; |
| 130 |
|
| 131 |
getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'color' ); |
| 132 |
getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'background' ); |
| 133 |
|
| 134 |
$recaptcha_theme = isset( $attributes['recaptchaTheme'] ) ? $attributes['recaptchaTheme'] : ''; |
| 135 |
$captcha = $this->render_captcha_block( array( 'theme' => $recaptcha_theme ) ); |
| 136 |
|
| 137 |
$extra_attr = array( |
| 138 |
'class' => $class, |
| 139 |
'block_name' => $block_name, |
| 140 |
'content' => $content . $captcha, |
| 141 |
'button_style' => $button_style, |
| 142 |
'button_class' => $button_class, |
| 143 |
); |
| 144 |
|
| 145 |
$wrapper_attributes = get_block_wrapper_attributes(); |
| 146 |
|
| 147 |
ob_start(); |
| 148 |
?> |
| 149 |
<div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 150 |
<?php getwid_get_template_part( 'contact-form/contact-form', $attributes, false, $extra_attr ); ?> |
| 151 |
</div> |
| 152 |
<?php |
| 153 |
|
| 154 |
return ob_get_clean(); |
| 155 |
} |
| 156 |
|
| 157 |
public function send() { |
| 158 |
|
| 159 |
check_ajax_referer( 'getwid_nonce_send_contact_form', 'nonce' ); |
| 160 |
|
| 161 |
$recaptcha_secret_key = get_option( 'getwid_recaptcha_v2_secret_key', false ); |
| 162 |
|
| 163 |
if ( $recaptcha_secret_key ) { |
| 164 |
|
| 165 |
if ( empty( $_POST['data']['g-recaptcha-response'] ) ) { |
| 166 |
wp_send_json_error( $this->get_error( 'bad-request' ) ); |
| 167 |
} |
| 168 |
|
| 169 |
$recaptcha_challenge = sanitize_text_field( wp_unslash( $_POST['data']['g-recaptcha-response'] ) ); |
| 170 |
|
| 171 |
$request = wp_remote_get( |
| 172 |
'https://google.com/recaptcha/api/siteverify?secret=' . $recaptcha_secret_key . '&response=' . $recaptcha_challenge, |
| 173 |
array( 'timeout' => 15 ) |
| 174 |
); |
| 175 |
|
| 176 |
$response = json_decode( wp_remote_retrieve_body( $request ), false ); |
| 177 |
|
| 178 |
$errors = ''; |
| 179 |
if ( ! $response->{'success'} ) { |
| 180 |
foreach ( $response->{'error-codes'} as $value ) { |
| 181 |
$errors .= $this->get_error( $value ); |
| 182 |
} |
| 183 |
wp_send_json_error( $errors ); |
| 184 |
} else { |
| 185 |
$this->send_mail( $_POST['data'] ); |
| 186 |
} |
| 187 |
} else { |
| 188 |
$this->send_mail( $_POST['data'] ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
private function send_mail( $data ) { |
| 193 |
|
| 194 |
$default_recipient = get_option( 'admin_email' ); |
| 195 |
$recipient = get_option( 'getwid_contact_form_recipient_email', '' ); |
| 196 |
$to = '' !== $recipient ? $recipient : $default_recipient; |
| 197 |
|
| 198 |
$subject = esc_html__( 'Contact Form', 'getwid' ); |
| 199 |
|
| 200 |
if ( ! empty( $data['subject'] ) ) { |
| 201 |
$subject = sprintf( |
| 202 |
// translators: %s is email subject. |
| 203 |
esc_html__( 'Contact Form: %s', 'getwid' ), |
| 204 |
sanitize_text_field( wp_unslash( $data['subject'] ) ) |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
$email = sanitize_email( wp_unslash( $data['email'] ) ); |
| 209 |
$name = sanitize_text_field( wp_unslash( $data['name'] ) ); |
| 210 |
$message = array(); |
| 211 |
$message[] = sanitize_textarea_field( wp_unslash( $data['message'] ) ); |
| 212 |
$message[] = '<br/><br/>'; |
| 213 |
$message[] = '<hr/>'; |
| 214 |
$message[] = sprintf( |
| 215 |
// translators: %s is a blogname. |
| 216 |
__( 'This e-mail was sent from a contact form on %s', 'getwid' ), |
| 217 |
get_option( 'blogname' ) |
| 218 |
); |
| 219 |
|
| 220 |
$body = implode( '', $message ); |
| 221 |
|
| 222 |
if ( $email ) { |
| 223 |
$headers = array( 'Reply-To: ' . $name . ' <' . $email . '>' ); |
| 224 |
} |
| 225 |
|
| 226 |
$response = getwid()->mailer()->send( $to, $subject, $body, $headers ); |
| 227 |
|
| 228 |
if ( $response ) { |
| 229 |
wp_send_json_success( |
| 230 |
__( 'Thank you for your message. It has been sent.', 'getwid' ) |
| 231 |
); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
wp_send_json_error( |
| 236 |
__( 'There was an error trying to send your message. Please try again later.', 'getwid' ) |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
public function update_recaptcha_credentials() { |
| 241 |
|
| 242 |
check_ajax_referer( 'getwid_nonce_recaptcha_v2', 'nonce' ); |
| 243 |
|
| 244 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 245 |
wp_send_json_error(); |
| 246 |
} |
| 247 |
|
| 248 |
$site_key = sanitize_text_field( wp_unslash( $_POST['data']['site_key'] ) ); |
| 249 |
$secret_key = sanitize_text_field( wp_unslash( $_POST['data']['secret_key'] ) ); |
| 250 |
|
| 251 |
if ( ! empty( $site_key ) ) { |
| 252 |
update_option( 'getwid_recaptcha_v2_site_key', $site_key ); |
| 253 |
} else { |
| 254 |
delete_option( 'getwid_recaptcha_v2_site_key' ); |
| 255 |
} |
| 256 |
|
| 257 |
if ( ! empty( $secret_key ) ) { |
| 258 |
update_option( 'getwid_recaptcha_v2_secret_key', $secret_key ); |
| 259 |
} else { |
| 260 |
delete_option( 'getwid_recaptcha_v2_secret_key' ); |
| 261 |
} |
| 262 |
|
| 263 |
wp_send_json_success(); |
| 264 |
} |
| 265 |
|
| 266 |
private function get_error( $error_code ) { |
| 267 |
|
| 268 |
switch ( $error_code ) { |
| 269 |
case 'bad-request': |
| 270 |
return __( 'The request is invalid or malformed.', 'getwid' ); |
| 271 |
|
| 272 |
case 'missing-input-secret': |
| 273 |
return __( 'The secret parameter is missing.', 'getwid' ); |
| 274 |
|
| 275 |
case 'missing-input-response': |
| 276 |
return __( 'Please check the captcha.', 'getwid' ); |
| 277 |
|
| 278 |
case 'invalid-input-secret': |
| 279 |
return __( 'The secret parameter is invalid or malformed.', 'getwid' ); |
| 280 |
|
| 281 |
case 'invalid-input-response': |
| 282 |
return __( 'The response parameter is invalid or malformed.', 'getwid' ); |
| 283 |
|
| 284 |
case 'timeout-or-duplicate': |
| 285 |
return __( 'The response is no longer valid: either is too old or has been used previously.', 'getwid' ); |
| 286 |
|
| 287 |
default: |
| 288 |
return null; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
getwid()->blocksManager()->addBlock( |
| 294 |
new ContactForm() |
| 295 |
); |
| 296 |
|