PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.5.1
3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 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 / re-captcha-v3 / re-captcha-v3.php
jetformbuilder / modules / captcha / re-captcha-v3 Last commit date
re-captcha-v3.php 1 week ago verify-token-action.php 1 week ago
re-captcha-v3.php
196 lines
1 <?php
2
3
4 namespace JFB_Modules\Captcha\Re_Captcha_V3;
5
6 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
7 use Jet_Form_Builder\Exceptions\Gateway_Exception;
8 use Jet_Form_Builder\Exceptions\Repository_Exception;
9 use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha;
10 use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha_From_Options;
11 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script;
12 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script;
13 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Settings_From_Options;
14 use JFB_Modules\Captcha\Module;
15 use JFB_Modules\Security\Exceptions\Spam_Exception;
16
17 // If this file is called directly, abort.
18 if ( ! defined( 'WPINC' ) ) {
19 die;
20 }
21
22 class Re_Captcha_V3 extends Base_Captcha_From_Options implements
23 Captcha_Separate_Frontend_Script,
24 Captcha_Separate_Editor_Script {
25
26 const OPTIONS = array(
27 'secret' => '',
28 'key' => '',
29 'threshold' => 0.5,
30 );
31
32 public function get_id(): string {
33 return 'google';
34 }
35
36 public function get_title(): string {
37 return __( 'reCAPTCHA v3', 'jet-form-builder' );
38 }
39
40 public function verify( array $request ) {
41 $action = ( new Verify_Token_Action() )
42 ->set_secret( $this->options['secret'] ?? '' )
43 ->set_token( $request[ self::FIELD ] ?? '' )
44 ->set_threshold( self::sanitize_threshold( $this->options['threshold'] ?? null ) );
45
46 try {
47 $action->send_request();
48 } catch ( Gateway_Exception $exception ) {
49 throw new Spam_Exception(
50 Module::SPAM_EXCEPTION, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
51 $exception->getMessage(), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
52 ...$exception->get_additional() // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
53 );
54 }
55 }
56
57 /**
58 * @return string
59 */
60 public function render(): string {
61 $key = esc_attr( $this->options['key'] ?? '' );
62 $url = esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) );
63
64 if ( ! $key || ! $url ) {
65 return '';
66 }
67
68 $handle = $this->get_handle();
69
70 wp_enqueue_script(
71 $handle . '-api',
72 $url,
73 array(),
74 '1.0.0',
75 true
76 );
77
78 $handle = $this->get_handle();
79 wp_enqueue_script( $handle );
80
81 /**
82 * In some themes, the "the_content" filter may be executed before the "wp_enqueue_scripts" action.
83 * Therefore, we should make sure that our script is registered before adding an inline script.
84 */
85 $this->register_frontend_scripts();
86 $this->module()->add_inline_config( array( 'key' => $key ), $handle );
87
88 return sprintf(
89 '<input type="hidden" class="%1$s" name="%2$s" value=""/>',
90 self::FIELD_CLASS,
91 self::FIELD
92 );
93 }
94
95 public function sanitize_options( array $options ): Base_Captcha {
96 if ( isset( $options[ $this->get_id() ] ) ) {
97 parent::sanitize_options( $options );
98
99 return $this;
100 }
101
102 /**
103 * For backward compatibility
104 */
105 $this->options = wp_array_slice_assoc( $options, array_keys( self::OPTIONS ) );
106
107 if ( empty( $options['use_global'] ) ) {
108 return $this;
109 }
110
111 $this->options = array_merge(
112 $this->options,
113 $this->on_load_options()
114 );
115
116 return $this;
117 }
118
119 public function on_save_options( array $post_request ): array {
120 // phpcs:disable WordPress.Security.NonceVerification.Missing
121 $secret = sanitize_text_field( $post_request['secret'] ?? '' );
122 $key = sanitize_text_field( $post_request['key'] ?? '' );
123 $threshold = self::sanitize_threshold( $post_request['threshold'] ?? null );
124 // phpcs:enable WordPress.Security.NonceVerification.Missing
125
126 return array(
127 'secret' => $secret,
128 'key' => $key,
129 'threshold' => $threshold,
130 );
131 }
132
133 /**
134 * @return array
135 * @see Captcha_Settings_From_Options
136 */
137 public function on_load_options(): array {
138 $parent = parent::on_load_options();
139
140 if ( ! empty( $parent ) ) {
141 return $parent;
142 }
143
144 /**
145 * For backward compatibility
146 */
147 $options = Tab_Handler_Manager::get_options( 'captcha-tab', self::OPTIONS );
148
149 return wp_array_slice_assoc( $options, array_keys( self::OPTIONS ) );
150 }
151
152 private static function sanitize_threshold( $threshold ): float {
153 $threshold = filter_var( $threshold, FILTER_VALIDATE_FLOAT );
154
155 if ( false === $threshold || 0 > $threshold || 1 < $threshold ) {
156 return self::OPTIONS['threshold'];
157 }
158
159 return (float) $threshold;
160 }
161
162 public function enqueue_editor_script() {
163 $script_asset = require_once $this->module()->get_dir( 'assets/build/re-captcha-v3/editor.asset.php' );
164
165 wp_enqueue_script(
166 $this->get_handle(),
167 $this->module()->get_url( 'assets/build/re-captcha-v3/editor.js' ),
168 $script_asset['dependencies'],
169 $script_asset['version'],
170 true
171 );
172 }
173
174 public function register_frontend_scripts() {
175 $script_asset = require_once $this->module()->get_dir( 'assets/build/re-captcha-v3/frontend.asset.php' );
176
177 // scripts have already registered
178 if ( true === $script_asset ) {
179 return;
180 }
181
182 array_push(
183 $script_asset['dependencies'],
184 'jet-plugins'
185 );
186
187 wp_register_script(
188 $this->get_handle(),
189 $this->module()->get_url( 'assets/build/re-captcha-v3/frontend.js' ),
190 $script_asset['dependencies'],
191 $script_asset['version'],
192 true
193 );
194 }
195 }
196