PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
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 / recaptcha / recaptcha.php

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

664 lines 32.2 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 * Encodes the given data into a query string format
6 * @param $data - array of string elements to be encoded
7 * @return string - encoded request
8 */
9 function _wppb_encodeQS($data)
10 {
11 $req = "";
12 foreach ($data as $key => $value) {
13 $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
14 }
15 // Cut the last '&'
16 $req=substr($req, 0, strlen($req)-1);
17 return $req;
18 }
19
20
21
22 /**
23 * Submits an HTTP GET to a reCAPTCHA server
24 * @param string $path
25 * @param array $data
26 */
27 function _wppb_submitHTTPGet($path, $data)
28 {
29 $req = _wppb_encodeQS($data);
30 $response = wp_remote_get($path . $req);
31
32 if ( ! is_wp_error( $response ))
33 return $response["body"];
34 }
35
36 /**
37 * Gets the challenge HTML (javascript and non-javascript version).
38 * This is called from the browser, and the resulting reCAPTCHA HTML widget
39 * is embedded within the HTML form it was called from.
40 * @param string $pubkey A public key for reCAPTCHA
41 * @param string $error The error given by reCAPTCHA (optional, default is null)
42 * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
43
44 * @return string - The HTML to be embedded in the user's form.
45 */
46 function wppb_recaptcha_get_html ( $pubkey, $form_name='' ){
47 global $wppb_recaptcha_forms; // is the counter for the number of forms that have recaptcha so we always have unique ids on the element
48 if( is_null( $wppb_recaptcha_forms ) )
49 $wppb_recaptcha_forms = 0;
50 $wppb_recaptcha_forms++;
51
52 $field = wppb_get_recaptcha_field();
53
54 if ( empty($pubkey) )
55 echo '<span class="error">'. esc_html__("To use reCAPTCHA you must get an API key from", "profile-builder"). " <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a></span><br/><br/>";
56
57 // extra class needed for Invisible reCAPTCHA html
58 $invisible_class = '';
59 if ( isset($field['recaptcha-type']) && ($field['recaptcha-type'] == 'invisible') ) {
60 $invisible_class = 'wppb-invisible-recaptcha';
61 }
62
63 // reCAPTCHA html for all forms and we make sure we have a unique id for v2
64 return '<div id="wppb-recaptcha-element-'.$form_name.$wppb_recaptcha_forms.'" class="wppb-recaptcha-element '.$invisible_class.'"></div>';
65 }
66
67
68
69 /**
70 * Add reCAPTCHA scripts to both front-end PB forms (with support for multiple forms) as well as Default WP forms
71 */
72 function wppb_recaptcha_script_footer(){
73 $field = wppb_get_recaptcha_field();
74 /* if we do not have a recaptcha field don't do nothing */
75 if( empty( $field ) )
76 return;
77
78 //do not add script if there is no shortcode
79 global $wppb_shortcode_on_front;
80 if( current_filter() == 'wp_footer' && ( !isset( $wppb_shortcode_on_front ) || $wppb_shortcode_on_front === false ) )
81 return;
82
83 //do not add script if the html for the field has not been added
84 global $wppb_recaptcha_present;
85 if( !isset( $wppb_recaptcha_present ) || $wppb_recaptcha_present === false )
86 return;
87
88 //we don't have jquery on the backend
89 if( current_filter() != 'wp_footer' ) {
90 wp_print_scripts('jquery');
91 }else if(!wp_script_is('jquery')){
92 wp_print_scripts('jquery');
93 }
94
95 //get site key
96 $pubkey = '';
97 if( isset( $field['public-key'] ) ) {
98 $pubkey = sanitize_text_field( $field['public-key'] );
99 }
100
101 // Check if we have a reCAPTCHA type
102 if ( !isset($field['recaptcha-type']) )
103 $field['recaptcha-type'] = 'v2' ;
104
105 /*for invisible recaptcha we have extra parameters and the selector is different. v2 is initialized on the id of the div
106 that must be unique and invisible is on the submit button of the forms that have the div */
107 if( $field['recaptcha-type'] === 'invisible' ) {
108 $callback_conditions = 'jQuery("input[type=\'submit\']", jQuery( ".wppb-recaptcha-element" ).closest("form") )';
109 $invisible_parameters = '"callback" : wppbInvisibleRecaptchaOnSubmit,"size": "invisible"';
110 }else {
111 $callback_conditions = 'jQuery(".wppb-recaptcha-element")';
112 $invisible_parameters = '';
113 }
114 //the section below is properly escaped or the variables contain static strings
115 // phpcs:disable
116 echo '
117 <script>
118 var wppbRecaptchaCallback = function() {
119 if( typeof window.wppbRecaptchaCallbackExecuted == "undefined" ){//see if we executed this before
120 '.$callback_conditions.'.each(function(){
121 recID = grecaptcha.render( jQuery(this).attr("id"), {
122 "sitekey" : "' . $pubkey . '",
123 "error-callback": wppbRecaptchaInitializationError,
124 '.$invisible_parameters.'
125 });
126 });
127 window.wppbRecaptchaCallbackExecuted = true;//we use this to make sure we only run the callback once
128 }
129 };
130
131 /* the callback function for when the captcha does not load propperly, maybe network problem or wrong keys */
132 function wppbRecaptchaInitializationError(){
133 window.wppbRecaptchaInitError = true;
134 ';
135
136 if( $field['recaptcha-type'] === 'invisible' ) {
137 echo '
138 /* make sure that if the invisible recaptcha did not load properly ( network error or wrong keys ) we can still submit the form */
139 jQuery("input[type=\'submit\']", jQuery( ".wppb-recaptcha-element" ).closest("form") ).on("click", function(e){
140 jQuery(this).closest("form").submit();
141 });
142 ';
143 }
144
145 echo '
146 //add a captcha field so we do not just let the form submit if we do not have a captcha response
147 jQuery( ".wppb-recaptcha-element" ).after(\''. wp_nonce_field( 'wppb_recaptcha_init_error', 'wppb_recaptcha_load_error', false, false ) .'\');
148 }
149
150 /* compatibility with other plugins that may include recaptcha with an onload callback. if their script loads first then our callback will not execute so call it explicitly */
151 jQuery( window ).on( "load", function() {
152 wppbRecaptchaCallback();
153 });
154 </script>';
155 // phpcs:enable
156 if( $field['recaptcha-type'] === 'invisible' ) {
157 echo '<script>
158 /* success callback for invisible recaptcha. it submits the form that contains the right token response */
159 function wppbInvisibleRecaptchaOnSubmit(token){
160
161 var elem = jQuery(".g-recaptcha-response").filter(function(){
162 return jQuery(this).val() === token;
163 });
164
165 var submitForm = true
166
167 /* dont submit form if PMS gateway is Stripe */
168 if( jQuery(".pms_pay_gate[type=radio]").length > 0 ){
169 jQuery(".pms_pay_gate").each( function(){
170 if( jQuery(this).is(":checked") && !jQuery(this).is(":disabled") && ( jQuery(this).val() == "stripe_intents" || jQuery(this).val() == "stripe" ) )
171 submitForm = false
172 })
173 } else if( jQuery(".pms_pay_gate[type=hidden]").length > 0 ) {
174
175 if( !jQuery(".pms_pay_gate[type=hidden]").is(":disabled") && ( jQuery(".pms_pay_gate[type=hidden]").val() == "stripe_intents" || jQuery(".pms_pay_gate[type=hidden]").val() == "stripe" ) )
176 submitForm = false
177
178 }
179
180 if( submitForm ){
181 var form = elem.closest("form");
182 form.submit();
183 } else {
184 jQuery(document).trigger( "wppb_invisible_recaptcha_success", jQuery( ".form-submit input[type=\'submit\']", elem.closest("form") ) )
185 }
186 }
187 </script>';
188 }
189
190 $lang = '&hl=en';
191 $locale = get_locale();
192 if(!empty($locale)) {
193 $locale_parts = explode('_',$locale);
194 $lang = '&hl='.urlencode($locale_parts[0]);
195 }
196
197 $source = apply_filters( 'wppb_recaptcha_custom_field_source', 'www.google.com' );
198
199 echo '<script src="https://'. esc_attr( $source ) .'/recaptcha/api.js?onload=wppbRecaptchaCallback&render=explicit'.esc_attr( $lang ).'" async defer></script>';
200 }
201 add_action('wp_footer', 'wppb_recaptcha_script_footer', 9999);
202 add_action('login_footer', 'wppb_recaptcha_script_footer');
203 add_action('register_form', 'wppb_recaptcha_script_footer');
204 add_action('lost_password', 'wppb_recaptcha_script_footer');
205
206
207 /**
208 * A wppb_ReCaptchaResponse is returned from wppb_recaptcha_check_answer()
209 */
210 class wppb_ReCaptchaResponse {
211 var $is_valid;
212 }
213
214
215 /**
216 * Calls an HTTP POST function to verify if the user's answer was correct
217 * @param string $privkey
218 * @param string $remoteip
219 * @param string $response
220 * @return wppb_ReCaptchaResponse
221 */
222 function wppb_recaptcha_check_answer ( $privkey, $remoteip, $response ){
223
224 if ( $remoteip == null || $remoteip == '' )
225 echo '<span class="error">'. esc_html__("For security reasons, you must pass the remote ip to reCAPTCHA!", "profile-builder") .'</span><br/><br/>';
226
227 // Discard empty solution submissions
228 if ($response == null || strlen($response) == 0) {
229 $recaptchaResponse = new wppb_ReCaptchaResponse();
230
231 if( isset( $_POST['wppb_recaptcha_load_error'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_recaptcha_load_error'] ), 'wppb_recaptcha_init_error' ) )
232 $recaptchaResponse->is_valid = true;
233 else
234 $recaptchaResponse->is_valid = false;
235
236 return $recaptchaResponse;
237 }
238
239 $source = apply_filters( 'wppb_recaptcha_custom_field_source', 'www.google.com' );
240
241 $getResponse = _wppb_submitHTTPGet(
242 "https://".$source."/recaptcha/api/siteverify?",
243 array (
244 'secret' => $privkey,
245 'remoteip' => $remoteip,
246 'response' => $response
247 )
248 );
249
250 $answers = json_decode($getResponse, true);
251 $recaptchaResponse = new wppb_ReCaptchaResponse();
252 if (trim($answers ['success']) == true) {
253 $recaptchaResponse->is_valid = true;
254 } else {
255 $recaptchaResponse->is_valid = false;
256 }
257 return $recaptchaResponse;
258
259 }
260
261 /* the function to display error message on the registration page */
262 function wppb_validate_captcha_response( $publickey, $privatekey ){
263 if (isset($_POST['g-recaptcha-response'])){
264 $recaptcha_response_field = sanitize_textarea_field( $_POST['g-recaptcha-response'] );
265 }
266 else {
267 $recaptcha_response_field = '';
268 }
269 if( isset( $_SERVER["REMOTE_ADDR"] ) )
270 $resp = wppb_recaptcha_check_answer($privatekey, sanitize_text_field( $_SERVER["REMOTE_ADDR"] ), $recaptcha_response_field );
271
272 if ( !empty( $_POST ) && isset( $resp ) )
273 return ( ( !$resp->is_valid ) ? false : true );
274 }
275
276 /* the function to add reCAPTCHA to the registration form of PB */
277 function wppb_recaptcha_handler ( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
278 if ( $field['field'] == 'reCAPTCHA' ){
279 $item_title = apply_filters( 'wppb_'.$form_location.'_recaptcha_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) );
280 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
281
282 wppb_recaptcha_set_default_values();
283 if ( ($form_location == 'register') && ( isset($field['captcha-pb-forms']) ) && (strpos($field['captcha-pb-forms'],'pb_register') !== false) ) {
284 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
285
286 global $wppb_recaptcha_present;
287 $wppb_recaptcha_present = true;
288
289 if ( array_key_exists( $field['id'], $field_check_errors ) )
290 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
291
292 $publickey = trim( $field['public-key'] );
293 $privatekey = trim( $field['private-key'] );
294
295 if ( empty( $publickey ) || empty( $privatekey ) )
296 return '<span class="custom_field_recaptcha_error_message" id="'.$field['meta-name'].'_error_message">'.apply_filters( 'wppb_'.$form_location.'_recaptcha_custom_field_'.$field['id'].'_error_message', __("To use reCAPTCHA you must get an API public key from:", "profile-builder"). '<a href="https://www.google.com/recaptcha/admin/create">https://www.google.com/recaptcha/admin/create</a>' ).'</span>';
297
298 if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
299 $output = '<label for="recaptcha_response_field">' . $item_title . $error_mark . '</label>' . wppb_recaptcha_get_html($publickey, 'pb_register');
300 if (!empty($item_description))
301 $output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
302 }
303 else {
304 // html for Invisible reCAPTCHA
305 $output = wppb_recaptcha_get_html($publickey, 'pb_register');
306 }
307
308
309 return $output;
310
311 }
312 }
313 }
314 add_filter( 'wppb_output_form_field_recaptcha', 'wppb_recaptcha_handler', 10, 6 );
315
316
317 /* handle reCAPTCHA field validation on PB Register form */
318 function wppb_check_recaptcha_value( $message, $field, $request_data, $form_location ){
319 if( $field['field'] == 'reCAPTCHA' ){
320 if ( ( $form_location == 'register' ) && ( isset($field['captcha-pb-forms']) ) && (strpos($field['captcha-pb-forms'],'pb_register') !== false) ) {
321
322 /* 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 recaptcha response
323 so do not verify it again or it will fail */
324 global $wppb_recaptcha_response;
325 if (!isset($wppb_recaptcha_response)){
326 $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
327 }
328 if ( ( $wppb_recaptcha_response == false ) && ( $field['required'] == 'Yes' ) ){
329 return wppb_required_field_error($field["field-title"]);
330 }
331 }
332 }
333 return $message;
334 }
335 add_filter( 'wppb_check_form_field_recaptcha', 'wppb_check_recaptcha_value', 10, 4 );
336
337 // Get the reCAPTCHA field information
338 function wppb_get_recaptcha_field(){
339 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
340 $field = '';
341 if ( $wppb_manage_fields != 'not_found' ) {
342 foreach ($wppb_manage_fields as $value) {
343 if ($value['field'] == 'reCAPTCHA')
344 $field = $value;
345 }
346 }
347 return $field;
348 }
349
350 /* Display reCAPTCHA on PB Recover Password form */
351 function wppb_display_recaptcha_recover_password( $output ){
352 $field = wppb_get_recaptcha_field();
353
354 if ( !empty($field) ) {
355 $publickey = trim($field['public-key']);
356 $item_title = apply_filters('wppb_recover_password_recaptcha_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
357 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
358
359 // check where reCAPTCHA should display and add reCAPTCHA html
360 if ( isset($field['captcha-pb-forms']) && ( strpos( $field['captcha-pb-forms'],'pb_recover_password' ) !== false ) ) {
361
362 global $wppb_recaptcha_present;
363 $wppb_recaptcha_present = true;
364
365 if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
366 $recaptcha_output = '<label for="recaptcha_response_field">' . $item_title . '</label>' . wppb_recaptcha_get_html($publickey, 'pb_recover_password');
367 if (!empty($item_description))
368 $recaptcha_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
369
370 $output = str_replace('</ul>', '<li class="wppb-form-field wppb-recaptcha">' . $recaptcha_output . '</li>' . '</ul>', $output);
371 }
372 else {
373 // output Invisible reCAPTCHA html
374 $output = str_replace('</ul>', '<li class="wppb-form-field wppb-recaptcha">' . wppb_recaptcha_get_html($publickey, 'pb_recover_password') . '</li>' . '</ul>', $output);
375 }
376 }
377 }
378 return $output;
379 }
380 add_filter('wppb_recover_password_generate_password_input','wppb_display_recaptcha_recover_password');
381
382 /* Function that changes the messageNo from the Recover Password form */
383 function wppb_recaptcha_change_recover_password_message_no($messageNo) {
384
385 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'recover_password') {
386 $field = wppb_get_recaptcha_field();
387 if (!empty($field)) {
388
389 global $wppb_recaptcha_response;
390 if (!isset($wppb_recaptcha_response)) $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
391
392 if ( isset($field['captcha-pb-forms']) && (strpos($field['captcha-pb-forms'], 'pb_recover_password') !== false) ) {
393
394 if ( ($wppb_recaptcha_response == false ) && ( $field['required'] == 'Yes' ) )
395 $messageNo = '';
396 }
397 }
398 }
399
400 return $messageNo;
401 }
402 add_filter('wppb_recover_password_message_no', 'wppb_recaptcha_change_recover_password_message_no');
403
404 /* Function that adds the reCAPTCHA error message on the Recover Password form */
405 function wppb_recaptcha_recover_password_displayed_message1( $message ) {
406 $field = wppb_get_recaptcha_field();
407
408 if ( !empty($field) ){
409 global $wppb_recaptcha_response;
410 if (!isset($wppb_recaptcha_response)) $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
411
412 if ( isset($field['captcha-pb-forms']) && ( strpos( $field['captcha-pb-forms'],'pb_recover_password' ) !== false ) && ( $wppb_recaptcha_response == false )) {
413
414 // This message is also altered by the plugin-compatibilities.php file, in regards to Captcha plugin ( function wppb_captcha_recover_password_displayed_message1 )
415 if (($message == '<p class="wppb-warning">wppb_recaptcha_error</p>') || ($message == '<p class="wppb-warning">wppb_captcha_error</p>'))
416 $message = '<p class="wppb-warning">' . wppb_recaptcha_field_error($field["field-title"]) . '</p>';
417 else
418 $message = $message . '<p class="wppb-warning">' . wppb_recaptcha_field_error($field["field-title"]) . '</p>';
419
420 }
421 }
422
423 return $message;
424 }
425 add_filter('wppb_recover_password_displayed_message1', 'wppb_recaptcha_recover_password_displayed_message1');
426
427 /* Function that changes the default success message to wppb_recaptcha_error if the reCAPTCHA doesn't validate
428 so that we can change the message displayed with the wppb_recover_password_displayed_message1 filter */
429 function wppb_recaptcha_recover_password_sent_message_1($message) {
430
431 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'recover_password') {
432 $field = wppb_get_recaptcha_field();
433
434 if (!empty($field)) {
435 global $wppb_recaptcha_response;
436 if (!isset($wppb_recaptcha_response)) $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
437
438 if ( isset($field['captcha-pb-forms']) && ( strpos($field['captcha-pb-forms'], 'pb_recover_password') !== false ) && ( $wppb_recaptcha_response == false ) ){
439 $message = 'wppb_recaptcha_error';
440 }
441 }
442
443 }
444
445 return $message;
446 }
447 add_filter('wppb_recover_password_sent_message1', 'wppb_recaptcha_recover_password_sent_message_1');
448
449 /* Display reCAPTCHA html on PB Login form */
450 function wppb_display_recaptcha_login_form($form_part, $args) {
451
452 if( !isset( $args['form_id'] ) || $args['form_id'] != 'wppb-loginform' )
453 return $form_part;
454
455 $field = wppb_get_recaptcha_field();
456
457 if ( !empty($field) ) {
458 $item_title = apply_filters('wppb_login_recaptcha_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
459 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
460
461 if ( isset($field['captcha-pb-forms']) && ( strpos( $field['captcha-pb-forms'],'pb_login' ) !== false ) ) { // check where reCAPTCHA should display and add reCAPTCHA html
462
463 global $wppb_recaptcha_present;
464 $wppb_recaptcha_present = true;
465
466 if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
467 $recaptcha_output = '<label for="recaptcha_response_field">' . $item_title . '</label>' . wppb_recaptcha_get_html(trim($field['public-key']), 'pb_login');
468 if (!empty($item_description))
469 $recaptcha_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
470
471 $form_part .= '<div class="wppb-form-field wppb-recaptcha">' . $recaptcha_output . '</div>';
472 }
473 else {
474 //output Invisible reCAPTCHA html
475 $form_part .= wppb_recaptcha_get_html(trim($field['public-key']), 'pb_login');
476 }
477 }
478 }
479
480 return $form_part;
481 }
482 add_filter('login_form_middle', 'wppb_display_recaptcha_login_form', 10, 2);
483
484 /* Display reCAPTCHA html on default WP Login form */
485 function wppb_display_recaptcha_wp_login_form(){
486 $field = wppb_get_recaptcha_field();
487
488 if ( !empty($field) ) {
489 $item_title = apply_filters('wppb_login_recaptcha_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
490 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
491
492 if ( isset($field['captcha-wp-forms']) && (strpos( $field['captcha-wp-forms'],'default_wp_login' ) !== false) ) { // check where reCAPTCHA should display and add reCAPTCHA html
493
494 global $wppb_recaptcha_present;
495 $wppb_recaptcha_present = true;
496
497 if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
498 $recaptcha_output = '<label for="recaptcha_response_field" style="padding-left:15px; padding-bottom:7px;">' . $item_title . '</label>' . wppb_recaptcha_get_html(trim($field['public-key']));
499 if (!empty($item_description))
500 $recaptcha_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
501
502 echo '<div class="wppb-form-field wppb-recaptcha" style="margin-left:-14px; margin-bottom: 15px;">' . $recaptcha_output . '</div>'; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
503 }
504 else {
505 // output Invisible reCAPTCHA html
506 echo wppb_recaptcha_get_html( trim($field['public-key'])); /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
507 }
508 }
509 }
510 }
511 add_action( 'login_form', 'wppb_display_recaptcha_wp_login_form' );
512
513 //Show reCAPTCHA error on Login form (both default and PB one)
514 function wppb_recaptcha_login_wp_error_message($user){
515 //make sure you're on a Login form (WP or PB)
516 if ( isset( $_POST['wp-submit'] ) && !is_wp_error($user) && !isset( $_POST['pms_login'] ) ) {
517
518 $field = wppb_get_recaptcha_field();
519 if ( !empty($field) ){
520 global $wppb_recaptcha_response;
521
522 if (!isset($wppb_recaptcha_response)) $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
523
524 //reCAPTCHA error for displaying on the PB login form
525 if ( isset($_POST['wppb_login']) && ($_POST['wppb_login'] == true) ) {
526
527 // it's a PB login form, check if we have a reCAPTCHA on it and display error if not valid
528 if ((isset($field['captcha-pb-forms'])) && (strpos($field['captcha-pb-forms'], 'pb_login') !== false) && ($wppb_recaptcha_response == false)) {
529 $user = new WP_Error('wppb_recaptcha_error', __('Please enter a (valid) reCAPTCHA value', 'profile-builder'));
530 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
531 remove_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
532 }
533
534 }
535 else {
536 //reCAPTCHA error for displaying on the default WP login form
537 if (isset($field['captcha-wp-forms']) && (strpos($field['captcha-wp-forms'], 'default_wp_login') !== false) && ($wppb_recaptcha_response == false)) {
538 $user = new WP_Error('wppb_recaptcha_error', __('Please enter a (valid) reCAPTCHA value', 'profile-builder'));
539 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
540 remove_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
541 }
542
543 }
544 }
545 }
546 return $user;
547 }
548 add_filter('authenticate','wppb_recaptcha_login_wp_error_message', 9);
549
550 // Display reCAPTCHA html on default WP Recover Password form
551 function wppb_display_recaptcha_default_wp_recover_password() {
552 $field = wppb_get_recaptcha_field();
553
554 if (!empty($field)) {
555 $publickey = trim($field['public-key']);
556 $item_title = apply_filters('wppb_recover_password_recaptcha_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
557 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
558
559 if ( isset($field['captcha-wp-forms']) && (strpos( $field['captcha-wp-forms'], 'default_wp_recover_password') !== false) ) { // check where reCAPTCHA should display and add reCAPTCHA html
560
561 global $wppb_recaptcha_present;
562 $wppb_recaptcha_present = true;
563
564 if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ){
565 $recaptcha_output = '<label for="recaptcha_response_field" style="padding-left:15px; padding-bottom:7px;">' . $item_title . '</label>' . wppb_recaptcha_get_html($publickey);
566 if (!empty($item_description))
567 $recaptcha_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
568
569 echo '<div class="wppb-form-field wppb-recaptcha" style="margin-left:-14px; margin-bottom: 15px;">' . $recaptcha_output . '</div>'; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
570 }
571 else {
572 // output Invisible reCAPTCHA html
573 echo wppb_recaptcha_get_html($publickey); /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
574 }
575 }
576 }
577 }
578 add_action('lostpassword_form','wppb_display_recaptcha_default_wp_recover_password');
579
580 // Verify and show reCAPTCHA errors for default WP Recover Password
581 function wppb_verify_recaptcha_default_wp_recover_password(){
582
583 // If field 'username or email' is empty - return
584 if( isset( $_REQUEST['user_login'] ) && "" === $_REQUEST['user_login'] )
585 return;
586
587 $field = wppb_get_recaptcha_field();
588 if ( !empty($field) ){
589 global $wppb_recaptcha_response;
590 if (!isset($wppb_recaptcha_response)) $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
591
592 // If reCAPTCHA not entered or incorrect reCAPTCHA answer
593 if ( isset( $_REQUEST['g-recaptcha-response'] ) && ( ( "" === $_REQUEST['g-recaptcha-response'] ) || ( $wppb_recaptcha_response == false ) ) ) {
594 wp_die( esc_html__('Please enter a (valid) reCAPTCHA value','profile-builder') . '<br />' . esc_html__( "Click the BACK button on your browser, and try again.", 'profile-builder' ) ) ;
595 }
596 }
597 }
598 add_action('lostpassword_post','wppb_verify_recaptcha_default_wp_recover_password');
599
600 /* Display reCAPTCHA html on default WP Register form */
601 function wppb_display_recaptcha_default_wp_register(){
602 $field = wppb_get_recaptcha_field();
603
604 if (!empty($field)) {
605
606 $publickey = trim($field['public-key']);
607 $item_title = apply_filters('wppb_register_recaptcha_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
608 $item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
609
610 wppb_recaptcha_set_default_values();
611 if (isset($field['captcha-wp-forms']) && (strpos($field['captcha-wp-forms'], 'default_wp_register') !== false)) { // check where reCAPTCHA should display and add reCAPTCHA html
612
613 global $wppb_recaptcha_present;
614 $wppb_recaptcha_present = true;
615
616 if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
617 $recaptcha_output = '<label for="recaptcha_response_field" style="padding-left:15px; padding-bottom:7px;">' . $item_title . '</label>' . wppb_recaptcha_get_html($publickey);
618 if (!empty($item_description))
619 $recaptcha_output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>';
620
621 echo '<div class="wppb-form-field wppb-recaptcha" style="margin-left:-14px; margin-bottom: 15px;">' . $recaptcha_output . '</div>'; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
622 }
623 else {
624 // output reCAPTCHA html
625 echo wppb_recaptcha_get_html($publickey); /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* properly escaped when constructing the var */
626 }
627 }
628 }
629 }
630 add_action( 'register_form', 'wppb_display_recaptcha_default_wp_register' );
631
632 // Verify and show reCAPTCHA errors for default WP Register form
633 function wppb_verify_recaptcha_default_wp_register( $errors ){
634
635 $field = wppb_get_recaptcha_field();
636 if ( !empty($field) ){
637 global $wppb_recaptcha_response;
638 if (!isset($wppb_recaptcha_response)) $wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
639
640 // If reCAPTCHA not entered or incorrect reCAPTCHA answer
641 if ( isset( $_REQUEST['g-recaptcha-response'] ) && ( ( "" === $_REQUEST['g-recaptcha-response'] ) || ( $wppb_recaptcha_response == false ) ) ) {
642 $errors->add( 'wppb_recaptcha_error', __('Please enter a (valid) reCAPTCHA value','profile-builder') );
643 }
644 }
645
646 return $errors;
647 }
648 add_filter('registration_errors','wppb_verify_recaptcha_default_wp_register');
649
650 // set default values in case there's already an existing reCAPTCHA field in Manage fields (when upgrading)
651 function wppb_recaptcha_set_default_values() {
652 $manage_fields = get_option('wppb_manage_fields', 'not_set');
653 if ($manage_fields != 'not_set') {
654 foreach ($manage_fields as $key => $value) {
655 if ($value['field'] == 'reCAPTCHA') {
656 if ( !isset($value['captcha-pb-forms']) ) $manage_fields[$key]['captcha-pb-forms'] = 'pb_register';
657 if ( !isset($value['captcha-wp-forms']) ) $manage_fields[$key]['captcha-wp-forms'] = 'default_wp_register';
658 if ( !isset($value['recaptcha-type']) ) $manage_fields[$key]['recaptcha-type'] = 'v2';
659 }
660 }
661 update_option('wppb_manage_fields', $manage_fields);
662 }
663 }
664