| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class ContactForm extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/contact-form'; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
|
| 11 |
parent::__construct( self::$blockName ); |
| 12 |
|
| 13 |
add_action( 'wp_ajax_getwid_recaptcha_api_key_manage', [ $this, 'recaptcha_api_key_manage' ] ); |
| 14 |
|
| 15 |
add_action( 'wp_ajax_getwid_send_mail' , [ $this, 'send' ] ); |
| 16 |
add_action( 'wp_ajax_nopriv_getwid_send_mail', [ $this, 'send' ] ); |
| 17 |
|
| 18 |
$this->register_contact_form_blocks(); |
| 19 |
} |
| 20 |
|
| 21 |
public function getLabel() { |
| 22 |
return __('Contact Form', 'getwid'); |
| 23 |
} |
| 24 |
|
| 25 |
private function register_contact_form_blocks() { |
| 26 |
|
| 27 |
/* #region register all blocks */ |
| 28 |
register_block_type( |
| 29 |
'getwid/contact-form', |
| 30 |
array( |
| 31 |
'render_callback' => [ $this, 'render_callback' ] |
| 32 |
) |
| 33 |
); |
| 34 |
|
| 35 |
$field_name = 'getwid/field-name'; |
| 36 |
register_block_type( |
| 37 |
$field_name, |
| 38 |
array( |
| 39 |
'render_callback' => [ $this, 'render_field_name_block' ] |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
$field_email = 'getwid/field-email'; |
| 44 |
register_block_type( |
| 45 |
$field_email, |
| 46 |
array( |
| 47 |
'render_callback' => [ $this, 'render_field_email_block' ] |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
$field_textarea = 'getwid/field-textarea'; |
| 52 |
register_block_type( |
| 53 |
$field_textarea, |
| 54 |
array( |
| 55 |
'render_callback' => [ $this, 'render_field_textarea_block' ] |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
$field_captcha = 'getwid/captcha'; |
| 60 |
register_block_type( |
| 61 |
$field_captcha, |
| 62 |
array( |
| 63 |
'render_callback' => [ $this, 'render_captcha_block' ] |
| 64 |
) |
| 65 |
); |
| 66 |
/* #endregion */ |
| 67 |
} |
| 68 |
|
| 69 |
/* #region render inner blocks methods */ |
| 70 |
public function render_field_name_block( $attributes ) { |
| 71 |
ob_start();?> |
| 72 |
<?php getwid_get_template_part( 'contact-form/field-name', $attributes, false ); ?><?php |
| 73 |
|
| 74 |
$result = ob_get_clean(); |
| 75 |
return $result; |
| 76 |
} |
| 77 |
|
| 78 |
public function render_field_email_block( $attributes ) { |
| 79 |
ob_start();?> |
| 80 |
<?php getwid_get_template_part( 'contact-form/field-email', $attributes, false ); ?><?php |
| 81 |
|
| 82 |
$result = ob_get_clean(); |
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
public function render_field_textarea_block( $attributes ) { |
| 87 |
ob_start();?> |
| 88 |
<?php getwid_get_template_part( 'contact-form/field-textarea', $attributes, false ); ?><?php |
| 89 |
|
| 90 |
$result = ob_get_clean(); |
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
public function render_captcha_block( $attributes ) { |
| 95 |
|
| 96 |
$site_key = get_option( 'getwid_recaptcha_v2_site_key', '' ); |
| 97 |
|
| 98 |
$extra_attr = array( |
| 99 |
'site_key' => $site_key |
| 100 |
); |
| 101 |
|
| 102 |
$result = ''; |
| 103 |
if ( $site_key ) { |
| 104 |
|
| 105 |
wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js?render=explicit' ); |
| 106 |
|
| 107 |
ob_start();?> |
| 108 |
<?php getwid_get_template_part( 'contact-form/captcha', $attributes, false, $extra_attr ); ?><?php |
| 109 |
|
| 110 |
$result = ob_get_clean(); |
| 111 |
} |
| 112 |
|
| 113 |
return $result; |
| 114 |
} |
| 115 |
/* #endregion */ |
| 116 |
|
| 117 |
public function block_frontend_assets() { |
| 118 |
|
| 119 |
if ( is_admin() ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 128 |
|
| 129 |
wp_enqueue_style( |
| 130 |
self::$blockName, |
| 131 |
getwid_get_plugin_url( 'assets/blocks/contact-form/style' . $rtl . '.css' ), |
| 132 |
[], |
| 133 |
getwid()->settings()->getVersion() |
| 134 |
); |
| 135 |
|
| 136 |
wp_enqueue_script( |
| 137 |
self::$blockName, |
| 138 |
getwid_get_plugin_url( 'assets/blocks/contact-form/frontend.js' ), |
| 139 |
[ 'jquery' ], |
| 140 |
getwid()->settings()->getVersion(), |
| 141 |
true |
| 142 |
); |
| 143 |
|
| 144 |
/* |
| 145 |
* var Getwid = {"ajax_url":"https:\/\/getwid.loc\/wp-admin\/admin-ajax.php","nonces":{"recaptcha_v2_contact_form":"6fea8c6c3e"}}; |
| 146 |
*/ |
| 147 |
$inline_script = |
| 148 |
'var Getwid = Getwid || {};' . |
| 149 |
'Getwid["ajax_url"] = ' . json_encode( admin_url( 'admin-ajax.php' ) ) . ';' . |
| 150 |
'Getwid["nonces"] = ' . json_encode( |
| 151 |
array( 'recaptcha_v2_contact_form' => wp_create_nonce( 'getwid_nonce_contact_form' ) ) |
| 152 |
) . ';' |
| 153 |
; |
| 154 |
|
| 155 |
wp_add_inline_script( |
| 156 |
self::$blockName, |
| 157 |
$inline_script, |
| 158 |
'before' |
| 159 |
); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
public function render_callback( $attributes, $content ) { |
| 164 |
|
| 165 |
$class = 'wp-block-getwid-contact-form'; |
| 166 |
$block_name = $class; |
| 167 |
|
| 168 |
if ( isset( $attributes[ 'className' ] ) ) { |
| 169 |
$class .= ' ' . $attributes[ 'className' ]; |
| 170 |
} |
| 171 |
|
| 172 |
if ( isset( $attributes[ 'align' ] ) ) { |
| 173 |
$class .= ' align' . $attributes[ 'align' ]; |
| 174 |
} |
| 175 |
|
| 176 |
$button_style = ''; |
| 177 |
$button_class = ''; |
| 178 |
|
| 179 |
getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'color' ); |
| 180 |
getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'background' ); |
| 181 |
|
| 182 |
$extra_attr = array( |
| 183 |
'class' => $class, |
| 184 |
'block_name' => $block_name, |
| 185 |
'content' => $content, |
| 186 |
|
| 187 |
'button_style' => $button_style, |
| 188 |
'button_class' => $button_class |
| 189 |
); |
| 190 |
|
| 191 |
ob_start();?> |
| 192 |
<div class='<?php echo esc_attr( $class ); ?>'> |
| 193 |
<?php getwid_get_template_part( 'contact-form/contact-form', $attributes, false, $extra_attr ); ?> |
| 194 |
</div><?php |
| 195 |
|
| 196 |
$result = ob_get_clean(); |
| 197 |
|
| 198 |
$this->block_frontend_assets(); |
| 199 |
|
| 200 |
return $result; |
| 201 |
} |
| 202 |
|
| 203 |
public function send() { |
| 204 |
|
| 205 |
check_ajax_referer( 'getwid_nonce_contact_form', 'security' ); |
| 206 |
|
| 207 |
if ( !isset( $_POST['data']['g-recaptcha-response'] ) ) { |
| 208 |
$this->send_mail( $_POST['data'] ); |
| 209 |
} else { |
| 210 |
$recaptcha_challenge = sanitize_text_field( wp_unslash( $_POST['data']['g-recaptcha-response'] ) ); |
| 211 |
$recaptcha_secret_key = get_option('getwid_recaptcha_v2_secret_key'); |
| 212 |
|
| 213 |
$request = wp_remote_get( |
| 214 |
'https://google.com/recaptcha/api/siteverify?secret=' . $recaptcha_secret_key . '&response=' . $recaptcha_challenge, |
| 215 |
array( 'timeout' => 15 ) |
| 216 |
); |
| 217 |
|
| 218 |
$response = json_decode( wp_remote_retrieve_body( $request ), false ); |
| 219 |
|
| 220 |
$errors = ''; |
| 221 |
if ( ! $response->{ 'success' } ) { |
| 222 |
foreach ( $response->{ 'error-codes' } as $index => $value ) { |
| 223 |
$errors .= $this->get_error( $value ); |
| 224 |
} |
| 225 |
wp_send_json_error( $errors ); |
| 226 |
} else { |
| 227 |
$this->send_mail( $_POST['data'] ); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private function send_mail( $data ) { |
| 233 |
|
| 234 |
$to = get_option( 'admin_email' ); |
| 235 |
|
| 236 |
$subject = sprintf( |
| 237 |
//translators: %s is a blogname |
| 238 |
__( 'This e-mail was sent from a contact form on %s', 'getwid' ), |
| 239 |
get_option( 'blogname' ) |
| 240 |
); |
| 241 |
|
| 242 |
if ( ! empty( $data['subject'] ) ) { |
| 243 |
$subject = sanitize_text_field( wp_unslash( $data[ 'subject' ] ) ); |
| 244 |
} |
| 245 |
|
| 246 |
$email = sanitize_email( wp_unslash( $data[ 'email' ] ) ); |
| 247 |
$name = sanitize_text_field( wp_unslash( $data[ 'name' ] ) ); |
| 248 |
$message = sanitize_textarea_field( wp_unslash( $data[ 'message' ] ) ); |
| 249 |
$body = $message; |
| 250 |
|
| 251 |
if ( $email ) { |
| 252 |
$headers = array( 'Reply-To: ' . $name . ' <' . $email . '>' ); |
| 253 |
} |
| 254 |
|
| 255 |
$response = getwid()->mailer()->send( $to, $subject, $body, $headers ); |
| 256 |
|
| 257 |
if ( $response ) { |
| 258 |
wp_send_json_success( |
| 259 |
__( 'Thank you for your message. It has been sent.', |
| 260 |
'getwid' |
| 261 |
) ); |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
wp_send_json_error( |
| 266 |
__('There was an error trying to send your message. Please try again later.', 'getwid') |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
public function recaptcha_api_key_manage() { |
| 271 |
$nonce = sanitize_key( $_POST[ 'nonce' ] ); |
| 272 |
|
| 273 |
if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_contact_form' ) ) { |
| 274 |
wp_send_json_error(); |
| 275 |
} |
| 276 |
|
| 277 |
$option = sanitize_text_field( wp_unslash( $_POST['option'] ) ); |
| 278 |
|
| 279 |
$site_api_key = sanitize_text_field( wp_unslash( $_POST['data']['site_api_key'] ) ); |
| 280 |
$secret_api_key = sanitize_text_field( wp_unslash( $_POST['data']['secret_api_key'] ) ); |
| 281 |
|
| 282 |
$response = false; |
| 283 |
if ( $option == 'set' ) { |
| 284 |
if ( ! empty( $site_api_key ) ) { |
| 285 |
$response = update_option( 'getwid_recaptcha_v2_site_key', $site_api_key ); |
| 286 |
} |
| 287 |
if ( ! empty( $secret_api_key ) ) { |
| 288 |
$response = update_option( 'getwid_recaptcha_v2_secret_key', $secret_api_key ); |
| 289 |
} |
| 290 |
} elseif ( $option == 'delete' ) { |
| 291 |
$response = delete_option( 'getwid_recaptcha_v2_site_key' ); |
| 292 |
$response = delete_option( 'getwid_recaptcha_v2_secret_key' ); |
| 293 |
} |
| 294 |
|
| 295 |
wp_send_json_success( $response ); |
| 296 |
} |
| 297 |
|
| 298 |
private function get_error( $error_code ) { |
| 299 |
switch ( $error_code ) { |
| 300 |
case 'bad-request': |
| 301 |
return __( 'The request is invalid or malformed.', |
| 302 |
'getwid' |
| 303 |
); |
| 304 |
break; |
| 305 |
|
| 306 |
case 'missing-input-secret': |
| 307 |
return __( 'The secret parameter is missing.', |
| 308 |
'getwid' |
| 309 |
); |
| 310 |
break; |
| 311 |
|
| 312 |
case 'missing-input-response': |
| 313 |
return __( 'Please check the captcha.', |
| 314 |
'getwid' |
| 315 |
); |
| 316 |
break; |
| 317 |
|
| 318 |
case 'invalid-input-secret': |
| 319 |
return __( 'The secret parameter is invalid or malformed.', |
| 320 |
'getwid' |
| 321 |
); |
| 322 |
break; |
| 323 |
|
| 324 |
case 'invalid-input-response': |
| 325 |
return __( 'The response parameter is invalid or malformed.', |
| 326 |
'getwid' |
| 327 |
); |
| 328 |
break; |
| 329 |
|
| 330 |
case 'timeout-or-duplicate': |
| 331 |
return __( 'The response is no longer valid: either is too old or has been used previously.', |
| 332 |
'getwid' |
| 333 |
); |
| 334 |
break; |
| 335 |
default: |
| 336 |
return; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
getwid()->blocksManager()->addBlock( |
| 342 |
new \Getwid\Blocks\ContactForm() |
| 343 |
); |
| 344 |
|