PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.4.7
JetFormBuilder — Dynamic Blocks Form Builder v3.4.7
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / captcha / turnstile / turnstile.php
jetformbuilder / modules / captcha / turnstile Last commit date
turnstile.php 1 year ago verify-token-action.php 1 year ago
turnstile.php
144 lines
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 . 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