| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Captcha\Turnstile; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 12 |
use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha_From_Options; |
| 13 |
use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script; |
| 14 |
use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script; |
| 15 |
use JFB_Modules\Captcha\Module; |
| 16 |
use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 17 |
|
| 18 |
class Turnstile extends Base_Captcha_From_Options implements |
| 19 |
Captcha_Separate_Frontend_Script, |
| 20 |
Captcha_Separate_Editor_Script { |
| 21 |
|
| 22 |
public function get_id(): string { |
| 23 |
return 'turnstile'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_title(): string { |
| 27 |
return __( 'Turnstile', 'jet-form-builder' ); |
| 28 |
} |
| 29 |
|
| 30 |
public function verify( array $request ) { |
| 31 |
$action = ( new Verify_Token_Action() ) |
| 32 |
->set_secret( $this->options['secret'] ?? '' ) |
| 33 |
->set_challenge( $request[ self::FIELD ] ?? '' ); |
| 34 |
|
| 35 |
try { |
| 36 |
$action->send_request(); |
| 37 |
} catch ( Gateway_Exception $exception ) { |
| 38 |
throw new Spam_Exception( |
| 39 |
Module::SPAM_EXCEPTION, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 40 |
$exception->getMessage(), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 41 |
...$exception->get_additional() // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 42 |
); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function render(): string { |
| 50 |
$captcha_args = apply_filters( |
| 51 |
'jet-form-builder/turnstile/options', |
| 52 |
array( |
| 53 |
'sitekey' => $this->options['key'] ?? '', |
| 54 |
'action' => Module::PREFIX_TURNSTILE . jet_fb_live()->form_id, |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
if ( empty( $captcha_args['sitekey'] ) ) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
$handle = $this->get_handle( '-api' ); |
| 63 |
wp_enqueue_script( $handle ); |
| 64 |
|
| 65 |
/** |
| 66 |
* In some themes, the "the_content" filter may be executed before the "wp_enqueue_scripts" action. |
| 67 |
* Therefore, we should make sure that our script is registered before adding an inline script. |
| 68 |
*/ |
| 69 |
$this->register_frontend_scripts(); |
| 70 |
$this->module()->add_inline_config( $captcha_args, $handle ); |
| 71 |
|
| 72 |
return sprintf( |
| 73 |
'<div class="jet-form-builder-row captcha-token-container" data-validation-type="inherit"> |
| 74 |
<input type="hidden" class="%1$s" name="%2$s" value="" data-jfb-sync required="required"> |
| 75 |
</div>', |
| 76 |
self::FIELD_CLASS, |
| 77 |
self::FIELD |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public function on_save_options( array $post_request ): array { |
| 82 |
// phpcs:disable WordPress.Security.NonceVerification.Missing |
| 83 |
$secret = sanitize_text_field( $post_request['secret'] ?? '' ); |
| 84 |
$key = sanitize_text_field( $post_request['key'] ?? '' ); |
| 85 |
|
| 86 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 87 |
|
| 88 |
return array( |
| 89 |
'secret' => $secret, |
| 90 |
'key' => $key, |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
public function enqueue_editor_script() { |
| 95 |
$script_asset = require_once $this->module()->get_dir( 'assets/build/turnstile/editor.asset.php' ); |
| 96 |
|
| 97 |
wp_enqueue_script( |
| 98 |
$this->module()->get_handle( $this->get_id() ), |
| 99 |
$this->module()->get_url( 'assets/build/turnstile/editor.js' ), |
| 100 |
$script_asset['dependencies'], |
| 101 |
$script_asset['version'], |
| 102 |
true |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
public function register_frontend_scripts() { |
| 107 |
$handle = $this->get_handle(); |
| 108 |
$script_asset = require_once $this->module()->get_dir( 'assets/build/turnstile/frontend.asset.php' ); |
| 109 |
|
| 110 |
// scripts have already registered |
| 111 |
if ( true === $script_asset ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$captcha_url = esc_url_raw( |
| 116 |
apply_filters( |
| 117 |
'jet-form-builder/turnstile/url', |
| 118 |
'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=jfbTurnstileOnLoad&render=explicit' |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
array_push( |
| 123 |
$script_asset['dependencies'], |
| 124 |
\Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE |
| 125 |
); |
| 126 |
|
| 127 |
wp_register_script( |
| 128 |
$handle, |
| 129 |
$this->module()->get_url( 'assets/build/turnstile/frontend.js' ), |
| 130 |
$script_asset['dependencies'], |
| 131 |
$script_asset['version'], |
| 132 |
true |
| 133 |
); |
| 134 |
|
| 135 |
wp_register_script( |
| 136 |
$handle . '-api', |
| 137 |
$captcha_url, |
| 138 |
array( $handle ), |
| 139 |
'1.0.0', |
| 140 |
true |
| 141 |
); |
| 142 |
} |
| 143 |
} |
| 144 |
|