PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 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
← All changes | includes/blocks/contact-form.php +186 -241 2.1.23.0.2 View file →
@@ -1,292 +1,253 @@
1 1 <?php
2 2
3 3 namespace Getwid\Blocks;
4 4
5 -class ContactForm extends \Getwid\Blocks\AbstractBlock {
5 +class ContactForm extends AbstractBlock {
6 6
7 - protected static $blockName = 'getwid/contact-form';
7 + public function __construct() {
8 8
9 - public function __construct() {
9 + parent::__construct( 'getwid/contact-form' );
10 10
11 - parent::__construct( self::$blockName );
11 + add_action( 'wp_ajax_getwid_update_recaptcha_credentials', array( $this, 'update_recaptcha_credentials' ) );
12 12
13 - add_action( 'wp_ajax_getwid_update_recaptcha_credentials', [ $this, 'update_recaptcha_credentials' ] );
13 + add_action( 'wp_ajax_getwid_send_mail', array( $this, 'send' ) );
14 + add_action( 'wp_ajax_nopriv_getwid_send_mail', array( $this, 'send' ) );
14 15
15 - add_action( 'wp_ajax_getwid_send_mail', [ $this, 'send' ] );
16 - add_action( 'wp_ajax_nopriv_getwid_send_mail', [ $this, 'send' ] );
16 + $this->register_contact_form_blocks();
17 17
18 - $this->register_contact_form_blocks();
19 - }
18 + add_action( 'enqueue_block_assets', array( $this, 'block_frontend_assets' ) );
19 + }
20 20
21 - public function getLabel() {
22 - return __('Contact Form', 'getwid');
21 + public function get_label() {
22 + return __( 'Contact Form', 'getwid' );
23 23 }
24 24
25 - private function register_contact_form_blocks() {
25 + private function register_contact_form_blocks() {
26 26
27 - /* #region register all blocks */
28 - register_block_type(
29 - 'getwid/contact-form',
30 - array(
31 - 'render_callback' => [ $this, 'render_callback' ]
32 - )
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 + )
33 32 );
34 33
35 - $field_name = 'getwid/field-name';
36 - register_block_type(
37 - $field_name,
38 - array(
39 - 'render_callback' => [ $this, 'render_field_name_block' ]
40 - )
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 + )
41 39 );
42 40
43 - $field_email = 'getwid/field-email';
44 - register_block_type(
45 - $field_email,
46 - array(
47 - 'render_callback' => [ $this, 'render_field_email_block' ]
48 - )
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 + )
49 46 );
50 47
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 - }
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 + }
60 55
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
56 + public function render_field_name_block( $attributes ) {
65 57
66 - $result = ob_get_clean();
67 - return $result;
68 - }
58 + ob_start();
59 + getwid_get_template_part( 'contact-form/field-name', $attributes, false );
69 60
70 - public function render_field_email_block( $attributes ) {
71 - ob_start();?>
72 - <?php getwid_get_template_part( 'contact-form/field-email', $attributes, false ); ?><?php
61 + return ob_get_clean();
62 + }
73 63
74 - $result = ob_get_clean();
75 - return $result;
76 - }
64 + public function render_field_email_block( $attributes ) {
77 65
78 - public function render_field_textarea_block( $attributes ) {
79 - ob_start();?>
80 - <?php getwid_get_template_part( 'contact-form/field-textarea', $attributes, false ); ?><?php
66 + ob_start();
67 + getwid_get_template_part( 'contact-form/field-email', $attributes, false );
81 68
82 - $result = ob_get_clean();
83 - return $result;
84 - }
69 + return ob_get_clean();
70 + }
85 71
86 - public function render_captcha_block( $attributes ) {
72 + public function render_field_textarea_block( $attributes ) {
87 73
88 - $site_key = get_option( 'getwid_recaptcha_v2_site_key', '' );
74 + ob_start();
75 + getwid_get_template_part( 'contact-form/field-textarea', $attributes, false );
89 76
90 - $extra_attr = array(
91 - 'site_key' => $site_key
92 - );
77 + return ob_get_clean();
78 + }
93 79
94 - $result = '';
95 - if ( $site_key ) {
80 + public function render_captcha_block( $attributes ) {
96 81
97 - wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js?render=explicit' );
82 + $site_key = get_option( 'getwid_recaptcha_v2_site_key', '' );
98 83
99 - ob_start();?>
100 - <?php getwid_get_template_part( 'contact-form/captcha', $attributes, false, $extra_attr ); ?><?php
84 + $extra_attr = array(
85 + 'site_key' => $site_key,
86 + );
101 87
102 - $result = ob_get_clean();
103 - }
88 + $result = '';
89 + if ( $site_key ) {
104 90
105 - return $result;
106 - }
107 - /* #endregion */
91 + wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js?render=explicit' );
108 92
109 - public function block_frontend_assets() {
93 + ob_start();
94 + getwid_get_template_part( 'contact-form/captcha', $attributes, false, $extra_attr );
110 95
111 - if ( is_admin() ) {
112 - return;
96 + $result = ob_get_clean();
113 97 }
114 98
115 - if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
99 + return $result;
100 + }
101 +
102 + public function block_frontend_assets() {
103 +
104 + if ( is_admin() ) {
116 105 return;
117 106 }
118 107
119 - $rtl = is_rtl() ? '.rtl' : '';
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 + ) . ';';
120 115
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 116 wp_add_inline_script(
145 - self::$blockName,
117 + 'getwid-contact-form-view-script',
146 118 $inline_script,
147 119 'before'
148 120 );
121 + }
149 122
150 - }
123 + public function render_callback( $attributes, $content ) {
151 124
152 - public function render_callback( $attributes, $content ) {
125 + $class = 'wp-block-getwid-contact-form';
126 + $block_name = $class;
153 127
154 - $class = 'wp-block-getwid-contact-form';
155 - $block_name = $class;
128 + $button_style = '';
129 + $button_class = '';
156 130
157 - if ( isset( $attributes[ 'className' ] ) ) {
158 - $class .= ' ' . $attributes[ 'className' ];
159 - }
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' );
160 133
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 134 $recaptcha_theme = isset( $attributes['recaptchaTheme'] ) ? $attributes['recaptchaTheme'] : '';
172 - $captcha = $this->render_captcha_block( [ 'theme' => $recaptcha_theme ] );
135 + $captcha = $this->render_captcha_block( array( 'theme' => $recaptcha_theme ) );
173 136
174 - $extra_attr = array(
175 - 'class' => $class,
176 - 'block_name' => $block_name,
177 - 'content' => $content . $captcha,
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 + );
178 144
179 - 'button_style' => $button_style,
180 - 'button_class' => $button_class
181 - );
145 + $wrapper_attributes = get_block_wrapper_attributes();
182 146
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
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
187 153
188 - $result = ob_get_clean();
154 + return ob_get_clean();
155 + }
189 156
190 - $this->block_frontend_assets();
157 + public function send() {
191 158
192 - return $result;
193 - }
159 + check_ajax_referer( 'getwid_nonce_send_contact_form', 'nonce' );
194 160
195 - public function send() {
196 -
197 - check_ajax_referer( 'getwid_nonce_send_contact_form', 'nonce' );
198 -
199 161 $recaptcha_secret_key = get_option( 'getwid_recaptcha_v2_secret_key', false );
200 162
201 - if ( $recaptcha_secret_key ) {
163 + if ( $recaptcha_secret_key ) {
202 164
203 165 if ( empty( $_POST['data']['g-recaptcha-response'] ) ) {
204 166 wp_send_json_error( $this->get_error( 'bad-request' ) );
205 167 }
206 168
207 - $recaptcha_challenge = sanitize_text_field( wp_unslash( $_POST['data']['g-recaptcha-response'] ) );
169 + $recaptcha_challenge = sanitize_text_field( wp_unslash( $_POST['data']['g-recaptcha-response'] ) );
208 170
209 - $request = wp_remote_get(
210 - 'https://google.com/recaptcha/api/siteverify?secret=' . $recaptcha_secret_key . '&response=' . $recaptcha_challenge,
211 - array( 'timeout' => 15 )
212 - );
171 + $request = wp_remote_get(
172 + 'https://google.com/recaptcha/api/siteverify?secret=' . $recaptcha_secret_key . '&response=' . $recaptcha_challenge,
173 + array( 'timeout' => 15 )
174 + );
213 175
214 - $response = json_decode( wp_remote_retrieve_body( $request ), false );
176 + $response = json_decode( wp_remote_retrieve_body( $request ), false );
215 177
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 {
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 {
227 188 $this->send_mail( $_POST['data'] );
228 189 }
229 - }
190 + }
230 191
231 - private function send_mail( $data ) {
192 + private function send_mail( $data ) {
232 193
233 - $default_recipient = get_option( 'admin_email' );
234 - $recipient = get_option( 'getwid_contact_form_recipient_email', '' );
235 - $to = $recipient != '' ? $recipient : $default_recipient;
194 + $default_recipient = get_option( 'admin_email' );
195 + $recipient = get_option( 'getwid_contact_form_recipient_email', '' );
196 + $to = '' !== $recipient ? $recipient : $default_recipient;
236 197
237 - $subject = esc_html__( 'Contact Form', 'getwid' );
198 + $subject = esc_html__( 'Contact Form', 'getwid' );
238 199
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 - }
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 + }
246 207
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 - );
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 + );
257 219
258 - $body = implode( '', $message );
220 + $body = implode( '', $message );
259 221
260 - if ( $email ) {
261 - $headers = array( 'Reply-To: ' . $name . ' <' . $email . '>' );
262 - }
222 + if ( $email ) {
223 + $headers = array( 'Reply-To: ' . $name . ' <' . $email . '>' );
224 + }
263 225
264 - $response = getwid()->mailer()->send( $to, $subject, $body, $headers );
226 + $response = getwid()->mailer()->send( $to, $subject, $body, $headers );
265 227
266 - if ( $response ) {
267 - wp_send_json_success(
268 - __( 'Thank you for your message. It has been sent.',
269 - 'getwid'
270 - ) );
271 - return;
272 - }
228 + if ( $response ) {
229 + wp_send_json_success(
230 + __( 'Thank you for your message. It has been sent.', 'getwid' )
231 + );
232 + return;
233 + }
273 234
274 - wp_send_json_error(
275 - __('There was an error trying to send your message. Please try again later.', 'getwid')
276 - );
277 - }
235 + wp_send_json_error(
236 + __( 'There was an error trying to send your message. Please try again later.', 'getwid' )
237 + );
238 + }
278 239
279 - public function update_recaptcha_credentials() {
240 + public function update_recaptcha_credentials() {
280 241
281 242 check_ajax_referer( 'getwid_nonce_recaptcha_v2', 'nonce' );
282 243
283 - if ( ! current_user_can( 'manage_options' ) ) {
284 - wp_send_json_error();
285 - }
244 + if ( ! current_user_can( 'manage_options' ) ) {
245 + wp_send_json_error();
246 + }
286 247
287 - $site_key = sanitize_text_field( wp_unslash( $_POST['data']['site_key'] ) );
288 - $secret_key = sanitize_text_field( wp_unslash( $_POST['data']['secret_key'] ) );
248 + $site_key = sanitize_text_field( wp_unslash( $_POST['data']['site_key'] ) );
249 + $secret_key = sanitize_text_field( wp_unslash( $_POST['data']['secret_key'] ) );
289 250
290 251 if ( ! empty( $site_key ) ) {
291 252 update_option( 'getwid_recaptcha_v2_site_key', $site_key );
292 253 } else {
@@ -298,53 +259,37 @@
298 259 } else {
299 260 delete_option( 'getwid_recaptcha_v2_secret_key' );
300 261 }
301 262
302 - wp_send_json_success();
303 - }
263 + wp_send_json_success();
264 + }
304 265
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;
266 + private function get_error( $error_code ) {
312 267
313 - case 'missing-input-secret':
314 - return __( 'The secret parameter is missing.',
315 - 'getwid'
316 - );
317 - break;
268 + switch ( $error_code ) {
269 + case 'bad-request':
270 + return __( 'The request is invalid or malformed.', 'getwid' );
318 271
319 - case 'missing-input-response':
320 - return __( 'Please check the captcha.',
321 - 'getwid'
322 - );
323 - break;
272 + case 'missing-input-secret':
273 + return __( 'The secret parameter is missing.', 'getwid' );
324 274
325 - case 'invalid-input-secret':
326 - return __( 'The secret parameter is invalid or malformed.',
327 - 'getwid'
328 - );
329 - break;
275 + case 'missing-input-response':
276 + return __( 'Please check the captcha.', 'getwid' );
330 277
331 - case 'invalid-input-response':
332 - return __( 'The response parameter is invalid or malformed.',
333 - 'getwid'
334 - );
335 - break;
278 + case 'invalid-input-secret':
279 + return __( 'The secret parameter is invalid or malformed.', 'getwid' );
336 280
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 - }
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 + }
346 291 }
347 292
348 293 getwid()->blocksManager()->addBlock(
349 - new \Getwid\Blocks\ContactForm()
294 + new ContactForm()
350 295 );