PluginProbe
Getwid – Gutenberg Blocks / 2.1.3
Getwid – Gutenberg Blocks v2.1.3
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks / contact-form.php

contact-form.php in Getwid – Gutenberg Blocks 2.1.3, at includes/blocks/contact-form.php

351 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_update_recaptcha_credentials', [ $this, 'update_recaptcha_credentials' ] );
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 /* #endregion */
59 }
60
61 /* #region render inner blocks methods */
62 public function render_field_name_block( $attributes ) {
63 ob_start();?>
64 <?php getwid_get_template_part( 'contact-form/field-name', $attributes, false ); ?><?php
65
66 $result = ob_get_clean();
67 return $result;
68 }
69
70 public function render_field_email_block( $attributes ) {
71 ob_start();?>
72 <?php getwid_get_template_part( 'contact-form/field-email', $attributes, false ); ?><?php
73
74 $result = ob_get_clean();
75 return $result;
76 }
77
78 public function render_field_textarea_block( $attributes ) {
79 ob_start();?>
80 <?php getwid_get_template_part( 'contact-form/field-textarea', $attributes, false ); ?><?php
81
82 $result = ob_get_clean();
83 return $result;
84 }
85
86 public function render_captcha_block( $attributes ) {
87
88 $site_key = get_option( 'getwid_recaptcha_v2_site_key', '' );
89
90 $extra_attr = array(
91 'site_key' => $site_key
92 );
93
94 $result = '';
95 if ( $site_key ) {
96
97 wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js?render=explicit' );
98
99 ob_start();?>
100 <?php getwid_get_template_part( 'contact-form/captcha', $attributes, false, $extra_attr ); ?><?php
101
102 $result = ob_get_clean();
103 }
104
105 return $result;
106 }
107 /* #endregion */
108
109 public function block_frontend_assets() {
110
111 if ( is_admin() ) {
112 return;
113 }
114
115 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
116 return;
117 }
118
119 $rtl = is_rtl() ? '.rtl' : '';
120
121 wp_enqueue_style(
122 self::$blockName,
123 getwid_get_plugin_url( 'assets/blocks/contact-form/style' . $rtl . '.css' ),
124 [],
125 getwid()->settings()->getVersion()
126 );
127
128 wp_enqueue_script(
129 self::$blockName,
130 getwid_get_plugin_url( 'assets/blocks/contact-form/frontend.js' ),
131 [ 'jquery' ],
132 getwid()->settings()->getVersion(),
133 true
134 );
135
136 $inline_script =
137 'var Getwid = Getwid || {};' .
138 'Getwid["ajax_url"] = ' . json_encode( admin_url( 'admin-ajax.php' ) ) . ';' .
139 'Getwid["nonces"] = ' . json_encode(
140 array( 'contact_form' => wp_create_nonce( 'getwid_nonce_send_contact_form' ) )
141 ) . ';'
142 ;
143
144 wp_add_inline_script(
145 self::$blockName,
146 $inline_script,
147 'before'
148 );
149
150 }
151
152 public function render_callback( $attributes, $content ) {
153
154 $class = 'wp-block-getwid-contact-form';
155 $block_name = $class;
156
157 if ( isset( $attributes[ 'className' ] ) ) {
158 $class .= ' ' . $attributes[ 'className' ];
159 }
160
161 if ( isset( $attributes[ 'align' ] ) ) {
162 $class .= ' align' . $attributes[ 'align' ];
163 }
164
165 $button_style = '';
166 $button_class = '';
167
168 getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'color' );
169 getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'background' );
170
171 $recaptcha_theme = isset( $attributes['recaptchaTheme'] ) ? $attributes['recaptchaTheme'] : '';
172 $captcha = $this->render_captcha_block( [ 'theme' => $recaptcha_theme ] );
173
174 $extra_attr = array(
175 'class' => $class,
176 'block_name' => $block_name,
177 'content' => $content . $captcha,
178
179 'button_style' => $button_style,
180 'button_class' => $button_class
181 );
182
183 ob_start();?>
184 <div class="<?php echo esc_attr( $class ); ?>" >
185 <?php getwid_get_template_part( 'contact-form/contact-form', $attributes, false, $extra_attr ); ?>
186 </div><?php
187
188 $result = ob_get_clean();
189
190 $this->block_frontend_assets();
191
192 return $result;
193 }
194
195 public function send() {
196
197 check_ajax_referer( 'getwid_nonce_send_contact_form', 'nonce' );
198
199 $recaptcha_secret_key = get_option( 'getwid_recaptcha_v2_secret_key', false );
200
201 if ( $recaptcha_secret_key ) {
202
203 if ( empty( $_POST['data']['g-recaptcha-response'] ) ) {
204 wp_send_json_error( $this->get_error( 'bad-request' ) );
205 }
206
207 $recaptcha_challenge = sanitize_text_field( wp_unslash( $_POST['data']['g-recaptcha-response'] ) );
208
209 $request = wp_remote_get(
210 'https://google.com/recaptcha/api/siteverify?secret=' . $recaptcha_secret_key . '&response=' . $recaptcha_challenge,
211 array( 'timeout' => 15 )
212 );
213
214 $response = json_decode( wp_remote_retrieve_body( $request ), false );
215
216 $errors = '';
217 if ( ! $response->{ 'success' } ) {
218 foreach ( $response->{ 'error-codes' } as $index => $value ) {
219 $errors .= $this->get_error( $value );
220 }
221 wp_send_json_error( $errors );
222 } else {
223 $this->send_mail( $_POST['data'] );
224 }
225
226 } else {
227 $this->send_mail( $_POST['data'] );
228 }
229 }
230
231 private function send_mail( $data ) {
232
233 $default_recipient = get_option( 'admin_email' );
234 $recipient = get_option( 'getwid_contact_form_recipient_email', '' );
235 $to = $recipient != '' ? $recipient : $default_recipient;
236
237 $subject = esc_html__( 'Contact Form', 'getwid' );
238
239 if ( ! empty( $data['subject'] ) ) {
240 $subject = sprintf(
241 //translators: %s is email subject
242 esc_html__( 'Contact Form: %s', 'getwid' ),
243 sanitize_text_field( wp_unslash( $data[ 'subject' ] ) )
244 );
245 }
246
247 $email = sanitize_email( wp_unslash( $data[ 'email' ] ) );
248 $name = sanitize_text_field( wp_unslash( $data[ 'name' ] ) );
249 $message[] = sanitize_textarea_field( wp_unslash( $data[ 'message' ] ) );
250 $message[] = '<br/><br/>';
251 $message[] = '<hr/>';
252 $message[] = sprintf(
253 //translators: %s is a blogname
254 __( 'This e-mail was sent from a contact form on %s', 'getwid' ),
255 get_option( 'blogname' )
256 );
257
258 $body = implode( '', $message );
259
260 if ( $email ) {
261 $headers = array( 'Reply-To: ' . $name . ' <' . $email . '>' );
262 }
263
264 $response = getwid()->mailer()->send( $to, $subject, $body, $headers );
265
266 if ( $response ) {
267 wp_send_json_success(
268 __( 'Thank you for your message. It has been sent.',
269 'getwid'
270 ) );
271 return;
272 }
273
274 wp_send_json_error(
275 __('There was an error trying to send your message. Please try again later.', 'getwid')
276 );
277 }
278
279 public function update_recaptcha_credentials() {
280
281 check_ajax_referer( 'getwid_nonce_recaptcha_v2', 'nonce' );
282
283 if ( ! current_user_can( 'manage_options' ) ) {
284 wp_send_json_error();
285 }
286
287 $site_key = sanitize_text_field( wp_unslash( $_POST['data']['site_key'] ) );
288 $secret_key = sanitize_text_field( wp_unslash( $_POST['data']['secret_key'] ) );
289
290 if ( ! empty( $site_key ) ) {
291 update_option( 'getwid_recaptcha_v2_site_key', $site_key );
292 } else {
293 delete_option( 'getwid_recaptcha_v2_site_key' );
294 }
295
296 if ( ! empty( $secret_key ) ) {
297 update_option( 'getwid_recaptcha_v2_secret_key', $secret_key );
298 } else {
299 delete_option( 'getwid_recaptcha_v2_secret_key' );
300 }
301
302 wp_send_json_success();
303 }
304
305 private function get_error( $error_code ) {
306 switch ( $error_code ) {
307 case 'bad-request':
308 return __( 'The request is invalid or malformed.',
309 'getwid'
310 );
311 break;
312
313 case 'missing-input-secret':
314 return __( 'The secret parameter is missing.',
315 'getwid'
316 );
317 break;
318
319 case 'missing-input-response':
320 return __( 'Please check the captcha.',
321 'getwid'
322 );
323 break;
324
325 case 'invalid-input-secret':
326 return __( 'The secret parameter is invalid or malformed.',
327 'getwid'
328 );
329 break;
330
331 case 'invalid-input-response':
332 return __( 'The response parameter is invalid or malformed.',
333 'getwid'
334 );
335 break;
336
337 case 'timeout-or-duplicate':
338 return __( 'The response is no longer valid: either is too old or has been used previously.',
339 'getwid'
340 );
341 break;
342 default:
343 return;
344 }
345 }
346 }
347
348 getwid()->blocksManager()->addBlock(
349 new \Getwid\Blocks\ContactForm()
350 );
351