first commit

This commit is contained in:
Roman Pyrih
2025-08-26 10:50:15 +02:00
commit 97352dcdc9
6905 changed files with 2717203 additions and 0 deletions

View File

@@ -0,0 +1,382 @@
<?php
/**
* Turnstile service main file
*/
if ( ! class_exists( 'WPCF7_Service' ) ) {
return;
}
class WPCF7_Turnstile extends WPCF7_Service {
private static $instance;
private $sitekeys;
/**
* Returns the singleton instance of the class.
*/
public static function get_instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* The constructor.
*/
private function __construct() {
$this->sitekeys = WPCF7::get_option( 'turnstile' );
}
/**
* Returns the service title.
*/
public function get_title() {
return __( 'Turnstile', 'contact-form-7' );
}
/**
* Returns true if the service is active.
*/
public function is_active() {
$sitekey = $this->get_sitekey();
$secret = $this->get_secret( $sitekey );
return $sitekey && $secret;
}
/**
* Returns an array of categories to which the service belongs to.
*/
public function get_categories() {
return array( 'spam_protection' );
}
/**
* Returns the icon that represents the service.
*/
public function icon() {
}
/**
* Returns a link to the service provider.
*/
public function link() {
echo wp_kses_data( wpcf7_link(
'https://www.cloudflare.com/application-services/products/turnstile/',
'cloudflare.com'
) );
}
/**
* Returns a sitekey.
*/
public function get_sitekey() {
$sitekeys = (array) $this->sitekeys;
$sitekey = array_key_first( $sitekeys ) ?? '';
return apply_filters( 'wpcf7_turnstile_sitekey', $sitekey );
}
/**
* Returns the secret key that is paired with the given sitekey.
*/
public function get_secret( $sitekey ) {
$sitekeys = (array) $this->sitekeys;
$secret = $sitekeys[$sitekey] ?? '';
return apply_filters( 'wpcf7_turnstile_secret', $secret );
}
/**
* Logs an API response.
*/
protected function log( $url, $request, $response ) {
wpcf7_log_remote_request( $url, $request, $response );
}
/**
* Verifies a response token.
*/
public function verify( $token ) {
$is_human = false;
if ( empty( $token ) or ! $this->is_active() ) {
return $is_human;
}
$endpoint = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$sitekey = $this->get_sitekey();
$secret = $this->get_secret( $sitekey );
$request = array(
'body' => array(
'secret' => $secret,
'response' => $token,
),
);
$response = wp_remote_post( sanitize_url( $endpoint ), $request );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
if ( WP_DEBUG ) {
$this->log( $endpoint, $request, $response );
}
return $is_human;
}
$response_body = wp_remote_retrieve_body( $response );
$response_body = json_decode( $response_body, true );
if ( $response_body['success'] ) {
$is_human = true;
}
if ( $submission = WPCF7_Submission::get_instance() ) {
$submission->push( 'turnstile', array(
'response' => $response_body,
) );
}
return $is_human;
}
/**
* Returns the menu page URL for the service configuration.
*/
protected function menu_page_url( $args = '' ) {
$args = wp_parse_args( $args, array() );
$url = menu_page_url( 'wpcf7-integration', false );
$url = add_query_arg( array( 'service' => 'turnstile' ), $url );
if ( ! empty( $args ) ) {
$url = add_query_arg( $args, $url );
}
return $url;
}
/**
* Saves the service configuration data.
*/
protected function save_data() {
WPCF7::update_option( 'turnstile', $this->sitekeys );
}
/**
* Resets the service configuration data.
*/
protected function reset_data() {
$this->sitekeys = null;
$this->save_data();
}
/**
* The loading process of the service configuration page.
*/
public function load( $action = '' ) {
if (
'setup' === $action and
'POST' === wpcf7_superglobal_server( 'REQUEST_METHOD' )
) {
check_admin_referer( 'wpcf7-turnstile-setup' );
if ( wpcf7_superglobal_post( 'reset' ) ) {
$this->reset_data();
$redirect_to = $this->menu_page_url( 'action=setup' );
} else {
$sitekey = wpcf7_superglobal_post( 'sitekey' );
$secret = wpcf7_superglobal_post( 'secret' );
if ( $sitekey and $secret ) {
$this->sitekeys = array( $sitekey => $secret );
$this->save_data();
$redirect_to = $this->menu_page_url( array(
'message' => 'success',
) );
} else {
$redirect_to = $this->menu_page_url( array(
'action' => 'setup',
'message' => 'invalid',
) );
}
}
wp_safe_redirect( $redirect_to );
exit();
}
}
/**
* Displays a notice on the integration page.
*/
public function admin_notice( $message = '' ) {
if ( 'invalid' === $message ) {
wp_admin_notice(
__( '<strong>Error:</strong> Invalid key values.', 'contact-form-7' ),
array( 'type' => 'error' )
);
}
if ( 'success' === $message ) {
wp_admin_notice(
__( 'Settings saved.', 'contact-form-7' ),
array( 'type' => 'success' )
);
}
}
/**
* Displays the service configuration box.
*/
public function display( $action = '' ) {
$formatter = new WPCF7_HTMLFormatter( array(
'allowed_html' => array_merge( wpcf7_kses_allowed_html(), array(
'form' => array(
'action' => true,
'method' => true,
),
) ),
) );
$formatter->append_start_tag( 'p' );
$formatter->append_preformatted(
esc_html( __( 'Turnstile is Cloudflare&#8217;s smart CAPTCHA alternative, which confirms web visitors are real and blocks unwanted bots without slowing down web experiences for real users.', 'contact-form-7' ) )
);
$formatter->end_tag( 'p' );
$formatter->append_start_tag( 'p' );
$formatter->append_start_tag( 'strong' );
$formatter->append_preformatted(
wpcf7_link(
__( 'https://contactform7.com/turnstile-integration/', 'contact-form-7' ),
__( 'Cloudflare Turnstile integration', 'contact-form-7' )
)
);
$formatter->end_tag( 'p' );
if ( $this->is_active() ) {
$formatter->append_start_tag( 'p', array(
'class' => 'dashicons-before dashicons-yes',
) );
$formatter->append_preformatted(
esc_html( __( 'Turnstile is active on this site.', 'contact-form-7' ) )
);
$formatter->end_tag( 'p' );
}
if ( 'setup' === $action ) {
$formatter->call_user_func( function () {
$this->display_setup();
} );
} else {
$formatter->append_start_tag( 'p' );
$formatter->append_start_tag( 'a', array(
'href' => esc_url( $this->menu_page_url( 'action=setup' ) ),
'class' => 'button',
) );
$formatter->append_preformatted(
esc_html( __( 'Setup integration', 'contact-form-7' ) )
);
$formatter->end_tag( 'p' );
}
$formatter->print();
}
/**
* Displays the service setup form.
*/
private function display_setup() {
$sitekey = $this->is_active() ? $this->get_sitekey() : '';
$secret = $this->is_active() ? $this->get_secret( $sitekey ) : '';
?>
<form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>">
<?php wp_nonce_field( 'wpcf7-turnstile-setup' ); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><label for="sitekey"><?php echo esc_html( __( 'Site Key', 'contact-form-7' ) ); ?></label></th>
<td><?php
if ( $this->is_active() ) {
echo esc_html( $sitekey );
echo sprintf(
'<input type="hidden" value="%1$s" id="sitekey" name="sitekey" />',
esc_attr( $sitekey )
);
} else {
echo sprintf(
'<input type="text" aria-required="true" value="%1$s" id="sitekey" name="sitekey" class="regular-text code" />',
esc_attr( $sitekey )
);
}
?></td>
</tr>
<tr>
<th scope="row"><label for="secret"><?php echo esc_html( __( 'Secret Key', 'contact-form-7' ) ); ?></label></th>
<td><?php
if ( $this->is_active() ) {
echo esc_html( wpcf7_mask_password( $secret, 4, 4 ) );
echo sprintf(
'<input type="hidden" value="%1$s" id="secret" name="secret" />',
esc_attr( $secret )
);
} else {
echo sprintf(
'<input type="text" aria-required="true" value="%1$s" id="secret" name="secret" class="regular-text code" />',
esc_attr( $secret )
);
}
?></td>
</tr>
</tbody>
</table>
<?php
if ( $this->is_active() ) {
submit_button(
_x( 'Remove Keys', 'API keys', 'contact-form-7' ),
'small', 'reset'
);
} else {
submit_button( __( 'Save Changes', 'contact-form-7' ) );
}
?>
</form>
<?php
}
}

View File

@@ -0,0 +1,204 @@
<?php
/**
* Turnstile module main file
*/
include_once path_join( __DIR__, 'service.php' );
add_action( 'wpcf7_init', 'wpcf7_turnstile_register_service', 35, 0 );
/**
* Registers the Turnstile service.
*/
function wpcf7_turnstile_register_service() {
$integration = WPCF7_Integration::get_instance();
$integration->add_service( 'turnstile',
WPCF7_Turnstile::get_instance()
);
}
add_action( 'wp_enqueue_scripts', 'wpcf7_turnstile_enqueue_scripts', 10, 0 );
/**
* Enqueues the Turnstile script.
*/
function wpcf7_turnstile_enqueue_scripts() {
$service = WPCF7_Turnstile::get_instance();
if ( ! $service->is_active() ) {
return;
}
wp_enqueue_script(
'cloudflare-turnstile',
'https://challenges.cloudflare.com/turnstile/v0/api.js',
array(),
null,
array(
'strategy' => 'async',
)
);
wp_add_inline_script(
'cloudflare-turnstile',
"document.addEventListener( 'wpcf7submit', e => turnstile.reset() );"
);
}
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_turnstile', 10, 0 );
/**
* Registers the Turnstile form-tag type.
*/
function wpcf7_add_form_tag_turnstile() {
$service = WPCF7_Turnstile::get_instance();
if ( ! $service->is_active() ) {
wpcf7_add_form_tag(
'turnstile',
'__return_empty_string',
array(
'display-block' => true,
)
);
return;
}
wpcf7_add_form_tag(
'turnstile',
'wpcf7_turnstile_form_tag_handler',
array(
'display-block' => true,
'singular' => true,
)
);
}
/**
* The Turnstile form-tag handler.
*/
function wpcf7_turnstile_form_tag_handler( $tag ) {
$service = WPCF7_Turnstile::get_instance();
if ( ! $service->is_active() ) {
return;
}
return sprintf(
'<div %s></div>',
wpcf7_format_atts( array(
'class' => 'wpcf7-turnstile cf-turnstile',
'data-sitekey' => $service->get_sitekey(),
'data-response-field-name' => '_wpcf7_turnstile_response',
'data-action' =>
$tag->get_option( 'action', '[-0-9a-zA-Z_]{1,32}', true ),
'data-size' =>
$tag->get_option( 'size', '(normal|flexible|compact)', true ),
'data-theme' => $tag->get_option( 'theme', '(light|dark|auto)', true ),
'data-language' => $tag->get_option( 'language', '[a-z-]{2,5}', true ),
'data-tabindex' => $tag->get_option( 'tabindex', 'signed_int', true ),
) )
);
}
add_filter( 'wpcf7_form_elements', 'wpcf7_turnstile_prepend_widget', 10, 1 );
/**
* Prepends a Turnstile widget to the form content if the form template
* does not include a Turnstile form-tag.
*/
function wpcf7_turnstile_prepend_widget( $content ) {
$service = WPCF7_Turnstile::get_instance();
if ( ! $service->is_active() ) {
return $content;
}
$contact_form = WPCF7_ContactForm::get_current();
$manager = WPCF7_FormTagsManager::get_instance();
$tags = $contact_form->scan_form_tags( array(
'type' => 'turnstile',
) );
if ( empty( $tags ) ) {
$content = $manager->replace_all( '[turnstile]' ) . "\n\n" . $content;
}
return $content;
}
add_filter( 'wpcf7_spam', 'wpcf7_turnstile_verify_response', 9, 2 );
/**
* Verifies the Turnstile response token.
*
* @param bool $spam The spam/ham status inherited from preceding callbacks.
* @param WPCF7_Submission $submission The submission object.
* @return bool True if the submitter is a bot, false if a human.
*/
function wpcf7_turnstile_verify_response( $spam, $submission ) {
if ( $spam ) {
return $spam;
}
$service = WPCF7_Turnstile::get_instance();
if ( ! $service->is_active() ) {
return $spam;
}
$token = wpcf7_superglobal_post( '_wpcf7_turnstile_response' );
if ( $service->verify( $token ) ) { // Human
$spam = false;
} else { // Bot
$spam = true;
if ( '' === $token ) {
$submission->add_spam_log( array(
'agent' => 'turnstile',
'reason' => __( 'Turnstile token is empty.', 'contact-form-7' ),
) );
} else {
$submission->add_spam_log( array(
'agent' => 'turnstile',
'reason' => __( 'Turnstile validation failed.', 'contact-form-7' ),
) );
}
}
return $spam;
}
add_filter(
'wpcf7_flamingo_inbound_message_parameters',
'wpcf7_flamingo_inbound_message_parameters_turnstile',
10, 1
);
/**
* Passes response data from Turnstile siteverify API to Flamingo.
*/
function wpcf7_flamingo_inbound_message_parameters_turnstile( $params ) {
$meta = null;
if ( $submission = WPCF7_Submission::get_instance() ) {
$meta = $submission->pull( 'turnstile' );
}
if ( isset( $meta ) ) {
$params['meta']['turnstile'] = wp_json_encode( $meta );
}
return $params;
}