PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.0
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / front-end / default-fields / turnstile / turnstile.php

turnstile.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.0, at front-end/default-fields/turnstile/turnstile.php

626 lines 29.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /**
5 * Submits an HTTP POST to the Turnstile siteverify server.
6 * Turnstile requires POST, unlike reCAPTCHA which historically allowed GET.
7 *
8 * @param string $path
9 * @param array $data
10 */
11 function _wppb_turnstile_submitHTTPPost($path, $data)
12 {
13 $response = wp_remote_post( $path, array(
14 'body' => $data
15 ) );
16
17 if ( ! is_wp_error( $response ) )
18 return $response["body"];
19 }
20
21 /**
22 * Gets the challenge HTML wrapper for Turnstile.
23 *
24 * @param string $pubkey A public key for Turnstile
25 * @param string $form_name The name of the form
26 *
27 * @return string - The HTML to be embedded in the user's form.
28 */
29 function wppb_turnstile_get_html ( $pubkey, $form_name='' ){
30 global $wppb_turnstile_forms; // is the counter for the number of forms that have turnstile so we always have unique ids on the element
31 if( is_null( $wppb_turnstile_forms ) )
32 $wppb_turnstile_forms = 0;
33 $wppb_turnstile_forms++;
34
35 if ( empty($pubkey) )
36 echo '<span class="error">'. esc_html__("To use Cloudflare Turnstile you must get a Site Key from", "profile-builder"). " <a href='https://dash.cloudflare.com/?to=/:account/turnstile'>https://dash.cloudflare.com/?to=/:account/turnstile</a></span><br/><br/>";
37
38 $field = wppb_get_turnstile_field();
39 $theme = isset( $field['theme'] ) ? esc_attr( sanitize_text_field( $field['theme'] ) ) : 'auto';
40
41 $output = '<div id="wppb-turnstile-element-'.$form_name.$wppb_turnstile_forms.'" class="wppb-turnstile-element cf-turnstile" data-wppb-sitekey="'.esc_attr( $pubkey ).'" data-wppb-theme="'.$theme.'"></div>';
42
43 // We add a hidden field so we can easily check if Turnstile should be processed on this form
44 $output .= '<input type="hidden" name="wppb-turnstile-present" value="1">';
45
46 if( $form_name == 'pb_login' ) {
47 add_filter( 'wppb_login_submit_button_extra_attributes', 'wppb_turnstile_login_submit_button_extra_attributes' );
48 }
49
50 return $output;
51 }
52
53 /**
54 * Add disabled attribute to login form submit button when Turnstile is used.
55 * Prevent form submission before the script is loaded and a token is received.
56 *
57 * @param string $attributes
58 * @return string
59 */
60 function wppb_turnstile_login_submit_button_extra_attributes( $attributes ) {
61 return $attributes . ' disabled="disabled"';
62 }
63
64 /**
65 * Add Turnstile scripts to both front-end PB forms as well as Default WP forms
66 */
67 function wppb_turnstile_script_footer(){
68 $field = wppb_get_turnstile_field();
69 /* if we do not have a turnstile field do nothing */
70 if( empty( $field ) )
71 return;
72
73 //do not add script if there is no shortcode
74 global $wppb_shortcode_on_front;
75 if( current_filter() == 'wp_footer' && ( !isset( $wppb_shortcode_on_front ) || $wppb_shortcode_on_front === false ) )
76 return;
77
78 //do not add script if the html for the field has not been added
79 global $wppb_turnstile_present;
80 if( !isset( $wppb_turnstile_present ) || $wppb_turnstile_present === false )
81 return;
82
83 //we don't have jquery on the backend
84 if( current_filter() != 'wp_footer' ) {
85 wp_print_scripts('jquery');
86 }else if(!wp_script_is('jquery')){
87 wp_print_scripts('jquery');
88 }
89
90 //get site key
91 $pubkey = '';
92 if( isset( $field['turnstile-site-key'] ) ) {
93 $pubkey = sanitize_text_field( $field['turnstile-site-key'] );
94 }
95
96 $theme = isset( $field['theme'] ) ? sanitize_text_field( $field['theme'] ) : 'auto';
97
98 // phpcs:disable
99 echo '
100 <script>
101 window.wppbTurnstileCallbackExecuted = false;
102
103 var wppbTurnstileCallback = function() {
104 if( !window.wppbTurnstileCallbackExecuted ){
105 let $elements = jQuery(".wppb-turnstile-element");
106
107 $elements.each(function(){
108 let $turnstileElement = jQuery(this);
109
110 if ( typeof $turnstileElement.data("wppb-turnstile-id") !== "undefined" ) {
111 turnstile.reset( $turnstileElement.data("wppb-turnstile-id") );
112 return;
113 }
114
115 let widgetId = turnstile.render(
116 "#" + $turnstileElement.attr("id"),
117 {
118 "sitekey" : "' . $pubkey . '",
119 "theme": "' . $theme . '"
120 }
121 )
122
123 $turnstileElement.data("wppb-turnstile-id", widgetId);
124 });
125
126 window.wppbTurnstileCallbackExecuted = true;
127
128 // Enable login form submit button initially as Turnstile handles its own disabled state or we wait for callback
129 if( jQuery("#wppb-loginform input[type=submit]").length > 0 ) {
130 jQuery("#wppb-loginform input[type=submit]").attr("disabled", false);
131 }
132 }
133 };
134 </script>';
135 // phpcs:enable
136
137 echo '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=wppbTurnstileCallback&render=explicit" async defer></script>';
138 echo '<script>
139 /* compatibility with other plugins that may include Turnstile with an onload callback. if their script loads first then our callback will not execute so call it explicitly */
140 jQuery( window ).on( "load", function() {
141 wppbTurnstileCallback();
142 });
143 </script>';
144
145 }
146 add_action('wp_footer', 'wppb_turnstile_script_footer', 9999);
147 add_action('login_footer', 'wppb_turnstile_script_footer');
148 add_action('register_form', 'wppb_turnstile_script_footer');
149 add_action('lost_password', 'wppb_turnstile_script_footer');
150
151 /**
152 * A wppb_TurnstileResponse is returned from wppb_turnstile_check_answer()
153 */
154 class wppb_TurnstileResponse {
155 var $is_valid;
156 }
157
158 /**
159 * Calls an HTTP POST function to verify if the user\'s answer was correct
160 * @param string $privkey
161 * @param string $remoteip
162 * @param string $response
163 * @return wppb_TurnstileResponse
164 */
165 function wppb_turnstile_check_answer ( $privkey, $remoteip, $response ) {
166
167 if ( $remoteip == null || $remoteip == '' )
168 echo '<span class="error">'. esc_html__("For security reasons, you must pass the remote ip to Turnstile!", "profile-builder") .'</span><br/><br/>';
169
170 // Discard empty solution submissions
171 if ($response == null || strlen($response) == 0) {
172 $turnstileResponse = new wppb_TurnstileResponse();
173 $turnstileResponse->is_valid = false;
174
175 return $turnstileResponse;
176 }
177
178 $getResponse = _wppb_turnstile_submitHTTPPost(
179 "https://challenges.cloudflare.com/turnstile/v0/siteverify",
180 array (
181 'secret' => $privkey,
182 'remoteip' => $remoteip,
183 'response' => $response
184 )
185 );
186
187 $answers = json_decode($getResponse, true);
188 $turnstileResponse = new wppb_TurnstileResponse();
189
190 if (trim($answers ['success']) == true) {
191 $turnstileResponse->is_valid = true;
192 } else {
193 $turnstileResponse->is_valid = false;
194 }
195
196 return $turnstileResponse;
197
198 }
199
200 /* the function to validate the Turnstile response with the API */
201 function wppb_validate_turnstile_response( $publickey, $privatekey ){
202 if (isset($_POST['cf-turnstile-response'])){
203 $turnstile_response_field = sanitize_textarea_field( $_POST['cf-turnstile-response'] );
204 } else {
205 $turnstile_response_field = '';
206 }
207
208 $already_validated = false;
209 $saved = get_option( 'wppb_turnstile_validations', array() );
210
211 if( isset( $saved[ $turnstile_response_field ] ) && $saved[ $turnstile_response_field ] == true ){
212 $already_validated = true;
213
214 if( !wp_doing_ajax() ){
215 unset( $saved[ $turnstile_response_field ] );
216 update_option( 'wppb_turnstile_validations', $saved, false );
217 }
218 }
219
220 if( !$already_validated ){
221
222 if( isset( $_SERVER["REMOTE_ADDR"] ) ){
223 $resp = wppb_turnstile_check_answer($privatekey, sanitize_text_field( $_SERVER["REMOTE_ADDR"] ), $turnstile_response_field );
224
225 if( isset( $resp ) ){
226 $already_validated = ( ( !$resp->is_valid ) ? false : true );
227 }
228 }
229
230 }
231
232 // Save valid results when they are being triggered from an ajax request
233 if( wp_doing_ajax() && isset( $_POST['action'] ) && $_POST['action'] == 'pms_validate_checkout' ){
234
235 $saved = get_option( 'wppb_turnstile_validations', array() );
236
237 if( $already_validated === true )
238 $saved[ $turnstile_response_field ] = true;
239
240 update_option( 'wppb_turnstile_validations', $saved, false );
241
242 }
243
244 return $already_validated;
245
246 }
247
248 /* the function to add Turnstile to the registration form of PB */
249 function wppb_turnstile_handler ( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
250 if ( $field['field'] == 'Turnstile' ){
251 $item_title = apply_filters( 'wppb_'.$form_location.'_turnstile_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) );
252 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
253
254 wppb_turnstile_set_default_values();
255
256 if ( ($form_location == 'register') && ( isset($field['turnstile-pb-forms']) ) && ( strpos($field['turnstile-pb-forms'],'pb_register') !== false ) ) {
257 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
258
259 global $wppb_turnstile_present;
260 $wppb_turnstile_present = true;
261
262 if ( array_key_exists( $field['id'], $field_check_errors ) )
263 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
264
265 $publickey = trim( $field['turnstile-site-key'] );
266 $privatekey = trim( $field['turnstile-secret-key'] );
267
268 if ( empty( $publickey ) || empty( $privatekey ) )
269 return '<span class="custom_field_turnstile_error_message" id="'.$field['meta-name'].'_error_message">'.apply_filters( 'wppb_'.$form_location.'_turnstile_custom_field_'.$field['id'].'_error_message', __("To use Cloudflare Turnstile you must get a Site Key and Secret Key from:", "profile-builder"). '<a href="https://dash.cloudflare.com/?to=/:account/turnstile">https://dash.cloudflare.com/?to=/:account/turnstile</a>' ).'</span>';
270
271 $output = '<label for="turnstile_response_field">' . $item_title . $error_mark . '</label>' . wppb_turnstile_get_html($publickey, 'pb_register');
272 if (!empty($item_description))
273 $output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
274
275 return $output;
276
277 }
278 }
279 }
280 add_filter( 'wppb_output_form_field_turnstile', 'wppb_turnstile_handler', 10, 6 );
281
282 /* handle Turnstile field validation on PB Register form */
283 function wppb_check_turnstile_value( $message, $field, $request_data, $form_location ){
284 if( $field['field'] == 'Turnstile' ){
285 if ( ( $form_location == 'register' ) && ( isset($field['turnstile-pb-forms']) ) && ( strpos($field['turnstile-pb-forms'],'pb_register') !== false ) ) {
286 /* theme my login plugin executes the register_errors hook on the frontend on all pages so on our register forms we might have already a turnstile response
287 so do not verify it again or it will fail */
288 global $wppb_turnstile_response;
289 if (!isset($wppb_turnstile_response)){
290 $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
291 }
292 if ( ( $wppb_turnstile_response == false ) && ( $field['required'] == 'Yes' ) ){
293 return __('Cloudflare Turnstile could not be verified. Please try again.', 'profile-builder');
294 }
295 }
296 }
297 return $message;
298 }
299 add_filter( 'wppb_check_form_field_turnstile', 'wppb_check_turnstile_value', 10, 4 );
300
301 // Get the Turnstile field information
302 function wppb_get_turnstile_field(){
303 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
304 $field = array();
305 if ( $wppb_manage_fields != 'not_found' ) {
306 foreach ($wppb_manage_fields as $value) {
307 if ($value['field'] == 'Turnstile'){
308 $field = $value;
309 break;
310 }
311 }
312 }
313 return $field;
314 }
315
316 /* Display Turnstile on PB Recover Password form */
317 function wppb_display_turnstile_recover_password( $output ){
318 $field = wppb_get_turnstile_field();
319
320 if ( !empty($field) ) {
321 $publickey = trim($field['turnstile-site-key']);
322 $item_title = apply_filters('wppb_recover_password_turnstile_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
323 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
324
325 // check where Turnstile should display and add Turnstile html
326 if ( isset($field['turnstile-pb-forms']) && ( strpos( $field['turnstile-pb-forms'],'pb_recover_password' ) !== false ) ) {
327
328 global $wppb_turnstile_present;
329 $wppb_turnstile_present = true;
330
331 $turnstile_output = '<label for="turnstile_response_field">' . $item_title . '</label>' . wppb_turnstile_get_html($publickey, 'pb_recover_password');
332 if (!empty($item_description))
333 $turnstile_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
334
335 $output = str_replace('</ul>', '<li class="wppb-form-field wppb-turnstile">' . $turnstile_output . '</li>' . '</ul>', $output);
336 }
337 }
338 return $output;
339 }
340 add_filter('wppb_recover_password_generate_password_input','wppb_display_turnstile_recover_password');
341
342 /* Function that changes the messageNo from the Recover Password form */
343 function wppb_turnstile_change_recover_password_message_no($messageNo) {
344
345 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'recover_password') {
346 $field = wppb_get_turnstile_field();
347 if (!empty($field)) {
348
349 global $wppb_turnstile_response;
350 if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
351
352 if ( isset($field['turnstile-pb-forms']) && (strpos($field['turnstile-pb-forms'], 'pb_recover_password') !== false) ) {
353
354 if ( $wppb_turnstile_response == false )
355 $messageNo = '';
356 }
357 }
358 }
359
360 return $messageNo;
361 }
362 add_filter('wppb_recover_password_message_no', 'wppb_turnstile_change_recover_password_message_no');
363
364 /* Function that adds the Turnstile error message on the Recover Password form */
365 function wppb_turnstile_recover_password_displayed_message1( $message ) {
366 $field = wppb_get_turnstile_field();
367
368 if ( !empty($field) ){
369 global $wppb_turnstile_response;
370 if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
371
372 if ( isset($field['turnstile-pb-forms']) && ( strpos( $field['turnstile-pb-forms'],'pb_recover_password' ) !== false ) && ( $wppb_turnstile_response == false )) {
373
374 $turnstile_error_message = __('Cloudflare Turnstile could not be verified. Please try again.', 'profile-builder');
375
376 if (($message == '<p class="wppb-warning">wppb_turnstile_error</p>') || ($message == '<p class="wppb-warning">wppb_captcha_error</p>'))
377 $message = '<p class="wppb-warning">' . $turnstile_error_message . '</p>';
378 else
379 $message = $message . '<p class="wppb-warning">' . $turnstile_error_message . '</p>';
380
381 }
382 }
383
384 return $message;
385 }
386 add_filter('wppb_recover_password_displayed_message1', 'wppb_turnstile_recover_password_displayed_message1');
387
388 /* Function that changes the default success message to wppb_turnstile_error if it doesn't validate */
389 function wppb_turnstile_recover_password_sent_message_1($message) {
390
391 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'recover_password') {
392 $field = wppb_get_turnstile_field();
393
394 if (!empty($field)) {
395 global $wppb_turnstile_response;
396 if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
397
398 if ( isset($field['turnstile-pb-forms']) && ( strpos($field['turnstile-pb-forms'], 'pb_recover_password') !== false ) && ( $wppb_turnstile_response == false ) ){
399 $message = 'wppb_turnstile_error';
400 }
401 }
402
403 }
404
405 return $message;
406 }
407 add_filter('wppb_recover_password_sent_message1', 'wppb_turnstile_recover_password_sent_message_1');
408
409 /* Display Turnstile html on PB Login form */
410 function wppb_display_turnstile_login_form($form_part, $args) {
411
412 if( !isset( $args['form_id'] ) || $args['form_id'] != 'wppb-loginform' )
413 return $form_part;
414
415 $field = wppb_get_turnstile_field();
416
417 if ( !empty($field) ) {
418 $item_title = apply_filters('wppb_login_turnstile_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
419 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
420
421 if ( isset($field['turnstile-pb-forms']) && ( strpos( $field['turnstile-pb-forms'],'pb_login' ) !== false ) ) { // check where Turnstile should display
422
423 global $wppb_turnstile_present;
424 $wppb_turnstile_present = true;
425
426 $turnstile_output = '<label for="turnstile_response_field">' . $item_title . '</label>' . wppb_turnstile_get_html(trim($field['turnstile-site-key']), 'pb_login');
427 if (!empty($item_description))
428 $turnstile_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
429
430 $form_part .= '<div class="wppb-form-field wppb-turnstile">' . $turnstile_output . '</div>';
431
432 }
433 }
434
435 return $form_part;
436 }
437 add_filter('login_form_middle', 'wppb_display_turnstile_login_form', 10, 2);
438
439 /* Display Turnstile html on default WP Login form */
440 function wppb_display_turnstile_wp_login_form(){
441 $field = wppb_get_turnstile_field();
442
443 if ( !empty($field) ) {
444 $item_title = apply_filters('wppb_login_turnstile_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
445 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
446
447 if ( isset($field['turnstile-wp-forms']) && (strpos( $field['turnstile-wp-forms'],'default_wp_login' ) !== false) ) {
448
449 global $wppb_turnstile_present;
450 $wppb_turnstile_present = true;
451
452 $turnstile_output = '<label for="turnstile_response_field" style="padding-left:15px; padding-bottom:7px;">' . $item_title . '</label>' . wppb_turnstile_get_html(trim($field['turnstile-site-key']));
453 if (!empty($item_description))
454 $turnstile_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
455
456 echo '<div class="wppb-form-field wppb-turnstile" style="margin-left:-14px; margin-bottom: 15px;">' . $turnstile_output . '</div>'; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
457
458 }
459 }
460 }
461 add_action( 'login_form', 'wppb_display_turnstile_wp_login_form' );
462
463 //Show Turnstile error on Login form (both default and PB one)
464 function wppb_turnstile_login_wp_error_message($user){
465 //make sure you\'re on a Login form (WP or PB)
466 if ( isset( $_POST['log'] ) && !is_wp_error($user) && !isset( $_POST['pms_login'] ) ) {
467
468 $field = wppb_get_turnstile_field();
469 if ( !empty($field) ){
470 global $wppb_turnstile_response;
471
472 if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
473
474 $turnstile_error_message = __('Cloudflare Turnstile could not be verified. Please try again.','profile-builder');
475
476 //Turnstile error for displaying on the PB login form
477 if ( isset($_POST['wppb_login']) && ($_POST['wppb_login'] == true) ) {
478
479 // it\'s a PB login form, check if we have Turnstile on it and display error if not valid
480 if ((isset($field['turnstile-pb-forms'])) && (strpos($field['turnstile-pb-forms'], 'pb_login') !== false) && ($wppb_turnstile_response == false)) {
481 $user = new WP_Error('wppb_turnstile_error', $turnstile_error_message);
482 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
483 remove_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
484 }
485
486 }
487 else {
488 //Turnstile error for displaying on the default WP login form
489 if (isset($field['turnstile-wp-forms']) && (strpos($field['turnstile-wp-forms'], 'default_wp_login') !== false) && ($wppb_turnstile_response == false)) {
490 $user = new WP_Error('wppb_turnstile_error', $turnstile_error_message);
491 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
492 remove_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
493 }
494
495 }
496 }
497 }
498 return $user;
499 }
500 add_filter('authenticate','wppb_turnstile_login_wp_error_message', 9);
501
502 /**
503 * Add a Turnstile CSS class to the Register form field
504 *
505 * @param $classes - existing field classes
506 * @param $field - field data
507 * @return mixed|string
508 */
509 function wppb_register_form_turnstile_type_class( $classes, $field ){
510
511 if ( isset( $field['field'] ) && $field['field'] == 'Turnstile' )
512 $classes .= ' wppb-turnstile';
513
514 return $classes;
515 }
516 add_filter( 'wppb_field_css_class', 'wppb_register_form_turnstile_type_class', 20, 2);
517
518 // Display Turnstile html on default WP Recover Password form
519 function wppb_display_turnstile_default_wp_recover_password() {
520 $field = wppb_get_turnstile_field();
521
522 if (!empty($field)) {
523 $publickey = trim($field['turnstile-site-key']);
524 $item_title = apply_filters('wppb_recover_password_turnstile_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
525 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
526
527 if ( isset($field['turnstile-wp-forms']) && (strpos( $field['turnstile-wp-forms'], 'default_wp_recover_password') !== false) ) {
528
529 global $wppb_turnstile_present;
530 $wppb_turnstile_present = true;
531
532 $turnstile_output = '<label for="turnstile_response_field" style="padding-left:15px; padding-bottom:7px;">' . $item_title . '</label>' . wppb_turnstile_get_html($publickey);
533 if (!empty($item_description))
534 $turnstile_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
535
536 echo '<div class="wppb-form-field wppb-turnstile" style="margin-left:-14px; margin-bottom: 15px;">' . $turnstile_output . '</div>'; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
537
538 }
539 }
540 }
541 add_action('lostpassword_form','wppb_display_turnstile_default_wp_recover_password');
542
543 // Verify and show Turnstile errors for default WP Recover Password
544 function wppb_verify_turnstile_default_wp_recover_password(){
545
546 // If field \'username or email\' is empty - return
547 if( isset( $_REQUEST['user_login'] ) && "" === $_REQUEST['user_login'] )
548 return;
549
550 $field = wppb_get_turnstile_field();
551 if ( !empty($field) ){
552 global $wppb_turnstile_response;
553 if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
554
555 $turnstile_error_message = esc_html__('Cloudflare Turnstile could not be verified. Please try again.','profile-builder');
556
557 // If Turnstile not entered or incorrect Turnstile answer
558 if ( isset( $_REQUEST['cf-turnstile-response'] ) && ( ( "" === $_REQUEST['cf-turnstile-response'] ) || ( $wppb_turnstile_response == false ) ) ) {
559 wp_die( esc_html( $turnstile_error_message ) . '<br />' . esc_html__( "Click the BACK button on your browser, and try again.", 'profile-builder' ) ) ;
560 }
561 }
562 }
563 add_action('lostpassword_post','wppb_verify_turnstile_default_wp_recover_password');
564
565 /* Display Turnstile html on default WP Register form */
566 function wppb_display_turnstile_default_wp_register(){
567 $field = wppb_get_turnstile_field();
568
569 if (!empty($field)) {
570
571 $publickey = trim($field['turnstile-site-key']);
572 $item_title = apply_filters('wppb_register_turnstile_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
573 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
574
575 wppb_turnstile_set_default_values();
576 if (isset($field['turnstile-wp-forms']) && (strpos($field['turnstile-wp-forms'], 'default_wp_register') !== false)) {
577
578 global $wppb_turnstile_present;
579 $wppb_turnstile_present = true;
580
581 $turnstile_output = '<label for="turnstile_response_field" style="padding-left:15px; padding-bottom:7px;">' . $item_title . '</label>' . wppb_turnstile_get_html($publickey);
582 if (!empty($item_description))
583 $turnstile_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
584
585 echo '<div class="wppb-form-field wppb-turnstile" style="margin-left:-14px; margin-bottom: 15px;">' . $turnstile_output . '</div>'; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
586
587 }
588 }
589 }
590 add_action( 'register_form', 'wppb_display_turnstile_default_wp_register' );
591
592 // Verify and show Turnstile errors for default WP Register form
593 function wppb_verify_turnstile_default_wp_register( $errors ){
594
595 $field = wppb_get_turnstile_field();
596 if ( !empty($field) ){
597 global $wppb_turnstile_response;
598 if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
599
600 $turnstile_error_message = esc_html__('Cloudflare Turnstile could not be verified. Please try again.','profile-builder');
601
602 // If Turnstile not entered or incorrect Turnstile answer
603 if ( isset( $_REQUEST['cf-turnstile-response'] ) && ( ( "" === $_REQUEST['cf-turnstile-response'] ) || ( $wppb_turnstile_response == false ) ) ) {
604 $errors->add( 'wppb_turnstile_error', $turnstile_error_message );
605 }
606 }
607
608 return $errors;
609 }
610 add_filter('registration_errors','wppb_verify_turnstile_default_wp_register');
611
612 // set default values in case there's already an existing Turnstile field in Manage fields (when upgrading)
613 function wppb_turnstile_set_default_values() {
614 $manage_fields = get_option('wppb_manage_fields', 'not_set');
615 if ($manage_fields != 'not_set') {
616 foreach ($manage_fields as $key => $value) {
617 if ($value['field'] == 'Turnstile') {
618 if ( !isset($value['turnstile-pb-forms']) ) $manage_fields[$key]['turnstile-pb-forms'] = 'pb_register';
619 if ( !isset($value['turnstile-wp-forms']) ) $manage_fields[$key]['turnstile-wp-forms'] = 'default_wp_register';
620 if ( !isset($value['theme']) ) $manage_fields[$key]['theme'] = 'auto';
621 }
622 }
623 update_option('wppb_manage_fields', $manage_fields);
624 }
625 }
626