$data
) );
if ( ! is_wp_error( $response ) )
return $response["body"];
}
/**
* Gets the challenge HTML wrapper for Turnstile.
*
* @param string $pubkey A public key for Turnstile
* @param string $form_name The name of the form
*
* @return string - The HTML to be embedded in the user's form.
*/
function wppb_turnstile_get_html ( $pubkey, $form_name='' ){
global $wppb_turnstile_forms; // is the counter for the number of forms that have turnstile so we always have unique ids on the element
if( is_null( $wppb_turnstile_forms ) )
$wppb_turnstile_forms = 0;
$wppb_turnstile_forms++;
if ( empty($pubkey) )
echo ''. esc_html__("To use Cloudflare Turnstile you must get a Site Key from", "profile-builder"). " https://dash.cloudflare.com/?to=/:account/turnstile
';
// We add a hidden field so we can easily check if Turnstile should be processed on this form
$output .= '';
if( $form_name == 'pb_login' ) {
add_filter( 'wppb_login_submit_button_extra_attributes', 'wppb_turnstile_login_submit_button_extra_attributes' );
}
return $output;
}
/**
* Add disabled attribute to login form submit button when Turnstile is used.
* Prevent form submission before the script is loaded and a token is received.
*
* @param string $attributes
* @return string
*/
function wppb_turnstile_login_submit_button_extra_attributes( $attributes ) {
return $attributes . ' disabled="disabled"';
}
/**
* Add Turnstile scripts to both front-end PB forms as well as Default WP forms
*/
function wppb_turnstile_script_footer(){
$field = wppb_get_turnstile_field();
/* if we do not have a turnstile field do nothing */
if( empty( $field ) )
return;
//do not add script if there is no shortcode
global $wppb_shortcode_on_front;
if( current_filter() == 'wp_footer' && ( !isset( $wppb_shortcode_on_front ) || $wppb_shortcode_on_front === false ) )
return;
//do not add script if the html for the field has not been added
global $wppb_turnstile_present;
if( !isset( $wppb_turnstile_present ) || $wppb_turnstile_present === false )
return;
//we don't have jquery on the backend
if( current_filter() != 'wp_footer' ) {
wp_print_scripts('jquery');
}else if(!wp_script_is('jquery')){
wp_print_scripts('jquery');
}
//get site key
$pubkey = '';
if( isset( $field['turnstile-site-key'] ) ) {
$pubkey = sanitize_text_field( $field['turnstile-site-key'] );
}
$theme = isset( $field['theme'] ) ? sanitize_text_field( $field['theme'] ) : 'auto';
// phpcs:disable
echo '
';
// phpcs:enable
echo '';
echo '';
}
add_action('wp_footer', 'wppb_turnstile_script_footer', 9999);
add_action('login_footer', 'wppb_turnstile_script_footer');
add_action('register_form', 'wppb_turnstile_script_footer');
add_action('lost_password', 'wppb_turnstile_script_footer');
/**
* A wppb_TurnstileResponse is returned from wppb_turnstile_check_answer()
*/
class wppb_TurnstileResponse {
var $is_valid;
}
/**
* Calls an HTTP POST function to verify if the user\'s answer was correct
* @param string $privkey
* @param string $remoteip
* @param string $response
* @return wppb_TurnstileResponse
*/
function wppb_turnstile_check_answer ( $privkey, $remoteip, $response ) {
if ( $remoteip == null || $remoteip == '' )
echo ''. esc_html__("For security reasons, you must pass the remote ip to Turnstile!", "profile-builder") .'
';
// Discard empty solution submissions
if ($response == null || strlen($response) == 0) {
$turnstileResponse = new wppb_TurnstileResponse();
$turnstileResponse->is_valid = false;
return $turnstileResponse;
}
$getResponse = _wppb_turnstile_submitHTTPPost(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
array (
'secret' => $privkey,
'remoteip' => $remoteip,
'response' => $response
)
);
$answers = json_decode($getResponse, true);
$turnstileResponse = new wppb_TurnstileResponse();
if (trim($answers ['success']) == true) {
$turnstileResponse->is_valid = true;
} else {
$turnstileResponse->is_valid = false;
}
return $turnstileResponse;
}
/* the function to validate the Turnstile response with the API */
function wppb_validate_turnstile_response( $publickey, $privatekey ){
if (isset($_POST['cf-turnstile-response'])){
$turnstile_response_field = sanitize_textarea_field( $_POST['cf-turnstile-response'] );
} else {
$turnstile_response_field = '';
}
$already_validated = false;
$saved = get_option( 'wppb_turnstile_validations', array() );
if( isset( $saved[ $turnstile_response_field ] ) && $saved[ $turnstile_response_field ] == true ){
$already_validated = true;
if( !wp_doing_ajax() ){
unset( $saved[ $turnstile_response_field ] );
update_option( 'wppb_turnstile_validations', $saved, false );
}
}
if( !$already_validated ){
if( isset( $_SERVER["REMOTE_ADDR"] ) ){
$resp = wppb_turnstile_check_answer($privatekey, sanitize_text_field( $_SERVER["REMOTE_ADDR"] ), $turnstile_response_field );
if( isset( $resp ) ){
$already_validated = ( ( !$resp->is_valid ) ? false : true );
}
}
}
// Save valid results when they are being triggered from an ajax request
if( wp_doing_ajax() && isset( $_POST['action'] ) && $_POST['action'] == 'pms_validate_checkout' ){
$saved = get_option( 'wppb_turnstile_validations', array() );
if( $already_validated === true )
$saved[ $turnstile_response_field ] = true;
update_option( 'wppb_turnstile_validations', $saved, false );
}
return $already_validated;
}
/* the function to add Turnstile to the registration form of PB */
function wppb_turnstile_handler ( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
if ( $field['field'] == 'Turnstile' ){
$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 ) );
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
wppb_turnstile_set_default_values();
if ( ($form_location == 'register') && ( isset($field['turnstile-pb-forms']) ) && ( strpos($field['turnstile-pb-forms'],'pb_register') !== false ) ) {
$error_mark = ( ( $field['required'] == 'Yes' ) ? '*' : '' );
global $wppb_turnstile_present;
$wppb_turnstile_present = true;
if ( array_key_exists( $field['id'], $field_check_errors ) )
$error_mark = '';
$publickey = trim( $field['turnstile-site-key'] );
$privatekey = trim( $field['turnstile-secret-key'] );
if ( empty( $publickey ) || empty( $privatekey ) )
return ''.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"). 'https://dash.cloudflare.com/?to=/:account/turnstile' ).'';
$output = '' . wppb_turnstile_get_html($publickey, 'pb_register');
if (!empty($item_description))
$output .= '' . $item_description . '';
return $output;
}
}
}
add_filter( 'wppb_output_form_field_turnstile', 'wppb_turnstile_handler', 10, 6 );
/* handle Turnstile field validation on PB Register form */
function wppb_check_turnstile_value( $message, $field, $request_data, $form_location ){
if( $field['field'] == 'Turnstile' ){
if ( ( $form_location == 'register' ) && ( isset($field['turnstile-pb-forms']) ) && ( strpos($field['turnstile-pb-forms'],'pb_register') !== false ) ) {
/* 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
so do not verify it again or it will fail */
global $wppb_turnstile_response;
if (!isset($wppb_turnstile_response)){
$wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
}
if ( ( $wppb_turnstile_response == false ) && ( $field['required'] == 'Yes' ) ){
return __('Cloudflare Turnstile could not be verified. Please try again.', 'profile-builder');
}
}
}
return $message;
}
add_filter( 'wppb_check_form_field_turnstile', 'wppb_check_turnstile_value', 10, 4 );
// Get the Turnstile field information
function wppb_get_turnstile_field(){
$wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
$field = array();
if ( $wppb_manage_fields != 'not_found' ) {
foreach ($wppb_manage_fields as $value) {
if ($value['field'] == 'Turnstile'){
$field = $value;
break;
}
}
}
return $field;
}
/* Display Turnstile on PB Recover Password form */
function wppb_display_turnstile_recover_password( $output ){
$field = wppb_get_turnstile_field();
if ( !empty($field) ) {
$publickey = trim($field['turnstile-site-key']);
$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));
$item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
// check where Turnstile should display and add Turnstile html
if ( isset($field['turnstile-pb-forms']) && ( strpos( $field['turnstile-pb-forms'],'pb_recover_password' ) !== false ) ) {
global $wppb_turnstile_present;
$wppb_turnstile_present = true;
$turnstile_output = '' . wppb_turnstile_get_html($publickey, 'pb_recover_password');
if (!empty($item_description))
$turnstile_output .= '' . $item_description . '';
$output = str_replace('', '
' . $turnstile_output . '
' . '', $output);
}
}
return $output;
}
add_filter('wppb_recover_password_generate_password_input','wppb_display_turnstile_recover_password');
/* Function that changes the messageNo from the Recover Password form */
function wppb_turnstile_change_recover_password_message_no($messageNo) {
if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'recover_password') {
$field = wppb_get_turnstile_field();
if (!empty($field)) {
global $wppb_turnstile_response;
if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
if ( isset($field['turnstile-pb-forms']) && (strpos($field['turnstile-pb-forms'], 'pb_recover_password') !== false) ) {
if ( $wppb_turnstile_response == false )
$messageNo = '';
}
}
}
return $messageNo;
}
add_filter('wppb_recover_password_message_no', 'wppb_turnstile_change_recover_password_message_no');
/* Function that adds the Turnstile error message on the Recover Password form */
function wppb_turnstile_recover_password_displayed_message1( $message ) {
$field = wppb_get_turnstile_field();
if ( !empty($field) ){
global $wppb_turnstile_response;
if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
if ( isset($field['turnstile-pb-forms']) && ( strpos( $field['turnstile-pb-forms'],'pb_recover_password' ) !== false ) && ( $wppb_turnstile_response == false )) {
$turnstile_error_message = __('Cloudflare Turnstile could not be verified. Please try again.', 'profile-builder');
if (($message == '
wppb_turnstile_error
') || ($message == '
wppb_captcha_error
'))
$message = '
' . $turnstile_error_message . '
';
else
$message = $message . '
' . $turnstile_error_message . '
';
}
}
return $message;
}
add_filter('wppb_recover_password_displayed_message1', 'wppb_turnstile_recover_password_displayed_message1');
/* Function that changes the default success message to wppb_turnstile_error if it doesn't validate */
function wppb_turnstile_recover_password_sent_message_1($message) {
if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'recover_password') {
$field = wppb_get_turnstile_field();
if (!empty($field)) {
global $wppb_turnstile_response;
if (!isset($wppb_turnstile_response)) $wppb_turnstile_response = wppb_validate_turnstile_response( trim( $field['turnstile-site-key'] ), trim( $field['turnstile-secret-key'] ) );
if ( isset($field['turnstile-pb-forms']) && ( strpos($field['turnstile-pb-forms'], 'pb_recover_password') !== false ) && ( $wppb_turnstile_response == false ) ){
$message = 'wppb_turnstile_error';
}
}
}
return $message;
}
add_filter('wppb_recover_password_sent_message1', 'wppb_turnstile_recover_password_sent_message_1');
/* Display Turnstile html on PB Login form */
function wppb_display_turnstile_login_form($form_part, $args) {
if( !isset( $args['form_id'] ) || $args['form_id'] != 'wppb-loginform' )
return $form_part;
$field = wppb_get_turnstile_field();
if ( !empty($field) ) {
$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));
$item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
if ( isset($field['turnstile-pb-forms']) && ( strpos( $field['turnstile-pb-forms'],'pb_login' ) !== false ) ) { // check where Turnstile should display
global $wppb_turnstile_present;
$wppb_turnstile_present = true;
$turnstile_output = '' . wppb_turnstile_get_html(trim($field['turnstile-site-key']), 'pb_login');
if (!empty($item_description))
$turnstile_output .= '' . $item_description . '';
$form_part .= '