';
echo cfturnstile_field_show('.wpcf7-submit', 'turnstileCF7Callback', 'contact-form-7', '-cf7-' . $id);
?>
';
$thecontent = ob_get_contents();
ob_end_clean();
wp_reset_postdata();
$thecontent = trim(preg_replace('/\s+/', ' ', $thecontent));
return $thecontent;
}
// Add Turnstile to all CF7 forms at once.
if ((!empty(get_option('cfturnstile_cf7_all')) && get_option('cfturnstile_cf7_all'))) {
add_action('wpcf7_form_elements', 'cfturnstile_field_cf7', 10, 1);
function cfturnstile_field_cf7($content) {
$cfturnstile_key = sanitize_text_field(get_option('cfturnstile_key'));
if (false === strpos($content, $cfturnstile_key)) {
return preg_replace('/(]*type="submit")/i', cfturnstile_cf7_shortcode() . '
$1', $content);
} else {
return $content;
}
}
}
// Validate form submission
add_filter('wpcf7_validate', 'cfturnstile_cf7_verify_recaptcha', 20, 2);
function cfturnstile_cf7_verify_recaptcha($result) {
if (!class_exists('WPCF7_Submission')) {
return $result;
}
// Prevent duplicate execution within a single request.
static $cfturnstile_cf7_ran = false;
if ( $cfturnstile_cf7_ran ) {
return $result;
}
$post = WPCF7_Submission::get_instance();
$_wpcf7 = !empty($_POST['_wpcf7']) ? absint($_POST['_wpcf7']) : 0;
if (!empty($post)) {
$data = $post->get_posted_data();
// Check if "Enable on all CF7 Forms" option is enabled
$cf7_all_enabled = !empty(get_option('cfturnstile_cf7_all')) && get_option('cfturnstile_cf7_all');
// Check if the form contains our shortcode [cf7-simple-turnstile]
$form_has_shortcode = false;
if ($_wpcf7 && class_exists('WPCF7_ContactForm')) {
$contact_form = WPCF7_ContactForm::get_instance($_wpcf7);
if ($contact_form) {
$form_content = $contact_form->prop('form');
$form_has_shortcode = (false !== strpos($form_content, '[cf7-simple-turnstile]'));
}
}
// Only validate if SCT is enabled for this form (either via "all forms" option or shortcode)
if (!$cf7_all_enabled && !$form_has_shortcode) {
return $result;
}
if(cfturnstile_whitelisted()) {
return $result;
}
$message = cfturnstile_failed_message();
$token = isset($data['cf-turnstile-response']) ? $data['cf-turnstile-response'] : '';
$check = cfturnstile_check($token);
$success = $check['success'];
$cfturnstile_cf7_ran = true;
if ($success != true) {
$result->invalidate(array('type' => 'captcha', 'name' => 'cf-turnstile'), $message);
return $result;
}
}
return $result;
}
// Add form tag
add_action('wpcf7_init', 'cfturnstile_cf7_add_form_tag_button', 10, 0);
function cfturnstile_cf7_add_form_tag_button() {
wpcf7_add_form_tag('cf7-simple-turnstile', 'cfturnstile_cf7_shortcode');
}
// Form tag generator
add_action('wpcf7_admin_init', 'cfturnstile_cf7_add_tag_generator_button', 55, 0);
function cfturnstile_cf7_add_tag_generator_button() {
$tag_generator = WPCF7_TagGenerator::get_instance();
$tag_generator->add('cf7-simple-turnstile', esc_html__('cloudflare turnstile', 'contact-form-7'), 'cfturnstile_cf7_tag_generator_button', array( 'version' => '2' ) );
}
// Insert tag form
function cfturnstile_cf7_tag_generator_button($contact_form, $args = '') {
$args = wp_parse_args($args, array());
?>