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