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