PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.5
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.5
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/fields/FrmFieldCaptcha.php +36 -154 6.45.5 View file →
@@ -21,33 +21,16 @@
21 21 return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-captcha.php';
22 22 }
23 23
24 24 /**
25 - * Returns the image name for a captcha.
26 - *
27 - * @return string
28 - */
29 - public static function get_captcha_image_name() {
30 - $frm_settings = FrmAppHelper::get_settings();
31 - $active_captcha = $frm_settings->active_captcha;
32 - if ( $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ) {
33 - $image_name = 'recaptcha_v3';
34 - } else {
35 - $image_name = $active_captcha;
36 - }
37 -
38 - return $image_name;
39 - }
40 -
41 - /**
42 25 * @return array
43 26 */
44 27 protected function field_settings_for_type() {
45 28 return array(
46 - 'required' => false,
47 - 'invalid' => true,
48 - 'captcha_size' => true,
49 - 'default' => false,
29 + 'required' => false,
30 + 'invalid' => true,
31 + 'captcha_size' => true,
32 + 'default' => false,
50 33 );
51 34 }
52 35
53 36 /**
@@ -80,39 +63,23 @@
80 63 *
81 64 * @return string
82 65 */
83 66 protected function before_replace_html_shortcodes( $args, $html ) {
84 - $frm_settings = FrmAppHelper::get_settings();
85 - $replace_response = $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha-response' : 'h-captcha-response';
86 - $replaced_for = str_replace( ' for="field_[key]"', ' for="' . $replace_response . '"', $html );
87 -
88 - return $replaced_for;
67 + return str_replace( ' for="field_[key]"', ' for="g-recaptcha-response"', $html );
89 68 }
90 69
91 - /**
92 - * @return string
93 - */
94 70 public function front_field_input( $args, $shortcode_atts ) {
95 71 $frm_settings = FrmAppHelper::get_settings();
96 - if ( ! self::should_show_captcha() ) {
72 + if ( empty( $frm_settings->pubkey ) ) {
97 73 return '';
98 74 }
99 75
100 - $class_prefix = $this->class_prefix( $frm_settings );
101 - $captcha_class = $this->captcha_class( $frm_settings );
102 - $captcha_size = $this->captcha_size( $frm_settings );
76 + $class_prefix = $this->class_prefix();
77 + $captcha_size = $this->captcha_size();
103 78 $allow_mutiple = $frm_settings->re_multi;
104 79
105 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
106 - $site_key = $frm_settings->pubkey;
107 - $recaptcha_options = ' data-size="' . esc_attr( $captcha_size ) . '" data-theme="' . esc_attr( $this->field['captcha_theme'] ) . '"';
108 - } else {
109 - $site_key = $frm_settings->hcaptcha_pubkey;
110 - }
111 -
112 - $html = '<div id="' . esc_attr( $args['html_id'] ) . '" class="' . esc_attr( $class_prefix ) . $captcha_class . '" data-sitekey="' . esc_attr( $site_key ) . '"';
113 - $html .= ! empty( $recaptcha_options ) ? $recaptcha_options : '';
114 - if ( $captcha_size === 'invisible' && ! $allow_mutiple ) {
80 + $html = '<div id="' . esc_attr( $args['html_id'] ) . '" class="' . esc_attr( $class_prefix ) . 'g-recaptcha" data-sitekey="' . esc_attr( $frm_settings->pubkey ) . '" data-size="' . esc_attr( $captcha_size ) . '" data-theme="' . esc_attr( $this->field['captcha_theme'] ) . '"';
81 + if ( $captcha_size == 'invisible' && ! $allow_mutiple ) {
115 82 $html .= ' data-callback="frmAfterRecaptcha"';
116 83 }
117 84 $html .= '></div>';
118 85
@@ -118,33 +85,19 @@
118 85
119 86 return $html;
120 87 }
121 88
122 - /**
123 - * @return void
124 - */
125 89 protected function load_field_scripts( $args ) {
126 90 $api_js_url = $this->api_url();
127 91
128 - wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
129 - wp_enqueue_script( 'captcha-api' );
92 + wp_register_script( 'recaptcha-api', $api_js_url, array( 'formidable' ), '3', true );
93 + wp_enqueue_script( 'recaptcha-api' );
130 94 }
131 95
132 96 protected function api_url() {
133 - $frm_settings = FrmAppHelper::get_settings();
134 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
135 - return $this->recaptcha_api_url( $frm_settings );
136 - }
137 -
138 - return $this->hcaptcha_api_url();
139 - }
140 -
141 - /**
142 - * @param FrmSettings $frm_settings
143 - */
144 - protected function recaptcha_api_url( $frm_settings ) {
145 97 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
146 98
99 + $frm_settings = FrmAppHelper::get_settings();
147 100 $allow_mutiple = $frm_settings->re_multi;
148 101 if ( $allow_mutiple ) {
149 102 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
150 103 }
@@ -153,37 +106,13 @@
153 106 if ( ! empty( $lang ) ) {
154 107 $api_js_url .= '&hl=' . $lang;
155 108 }
156 109
157 - $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
158 -
159 - return $api_js_url;
110 + return apply_filters( 'frm_recaptcha_js_url', $api_js_url );
160 111 }
161 112
162 - protected function hcaptcha_api_url() {
163 - $api_js_url = 'https://js.hcaptcha.com/1/api.js';
164 -
165 - /**
166 - * Allows updating hcaptcha js api url.
167 - *
168 - * @since 6.0
169 - *
170 - * @param string $api_js_url
171 - */
172 - $api_js_url = apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
173 -
174 - return $api_js_url;
175 - }
176 -
177 - /**
178 - * @param FrmSettings $frm_settings
179 - *
180 - * @return string
181 - *
182 - * @psalm-return ''|'frm-'
183 - */
184 - protected function class_prefix( $frm_settings ) {
185 - if ( $this->allow_multiple( $frm_settings ) && $frm_settings->active_captcha === 'recaptcha' ) {
113 + protected function class_prefix() {
114 + if ( $this->allow_multiple() ) {
186 115 $class_prefix = 'frm-';
187 116 } else {
188 117 $class_prefix = '';
189 118 }
@@ -190,29 +119,19 @@
190 119
191 120 return $class_prefix;
192 121 }
193 122
194 - /**
195 - * @param FrmSettings $frm_settings
196 - *
197 - * @return string
198 - *
199 - * @psalm-return 'g-recaptcha'|'h-captcha'
200 - */
201 - protected function captcha_class( $frm_settings ) {
202 - return $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha' : 'h-captcha';
203 - }
123 + protected function allow_multiple() {
124 + $frm_settings = FrmAppHelper::get_settings();
204 125
205 - protected function allow_multiple( $frm_settings ) {
206 126 return $frm_settings->re_multi;
207 127 }
208 128
209 129 /**
210 130 * @return string
211 - *
212 - * @param FrmSettings $frm_settings
213 131 */
214 - protected function captcha_size( $frm_settings ) {
132 + protected function captcha_size() {
133 + $frm_settings = FrmAppHelper::get_settings();
215 134 if ( in_array( $frm_settings->re_type, array( 'invisible', 'v3' ), true ) ) {
216 135 return 'invisible';
217 136 }
218 137 // for reverse compatibility
@@ -231,9 +150,9 @@
231 150 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
232 151
233 152 if ( is_wp_error( $resp ) ) {
234 153 $error_string = $resp->get_error_message();
235 - $errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your captcha', 'formidable' );
154 + $errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' );
236 155 $errors[ 'field' . $args['id'] ] .= ' ' . $error_string;
237 156 return $errors;
238 157 }
239 158
@@ -240,28 +159,23 @@
240 159 if ( ! is_array( $response ) ) {
241 160 return $errors;
242 161 }
243 162
244 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
245 - if ( 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
246 - $threshold = floatval( $frm_settings->re_threshold );
247 - $score = floatval( $response['score'] );
163 + if ( 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
164 + $threshold = floatval( $frm_settings->re_threshold );
165 + $score = floatval( $response['score'] );
248 166
249 - $this->set_score( $score );
167 + $this->set_score( $score );
250 168
251 - if ( $score < $threshold ) {
252 - $response['success'] = false;
253 - }
169 + if ( $score < $threshold ) {
170 + $response['success'] = false;
254 171 }
255 172 }
256 173
257 174 if ( isset( $response['success'] ) && ! $response['success'] ) {
258 175 // What happens when the CAPTCHA was entered incorrectly
259 - $invalid_message = FrmField::get_option( $this->field, 'invalid' );
260 - if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
261 - $invalid_message = '';
262 - }
263 - $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
176 + $invalid_message = FrmField::get_option( $this->field, 'invalid' );
177 + $errors[ 'field' . $args['id'] ] = ( $invalid_message == '' ? $frm_settings->re_msg : $invalid_message );
264 178 }
265 179
266 180 return $errors;
267 181 }
@@ -290,9 +204,9 @@
290 204 return array();
291 205 }
292 206
293 207 // phpcs:ignore WordPress.Security.NonceVerification.Missing
294 - if ( ! isset( $_POST['g-recaptcha-response'] ) && ! isset( $_POST['h-captcha-response'] ) ) {
208 + if ( ! isset( $_POST['g-recaptcha-response'] ) ) {
295 209 // There was no captcha submitted.
296 210 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
297 211 }
298 212
@@ -302,20 +216,13 @@
302 216 /**
303 217 * @since 4.07
304 218 * @return bool
305 219 */
306 - public static function should_show_captcha() {
220 + private function should_show_captcha() {
307 221 $frm_settings = FrmAppHelper::get_settings();
308 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
309 - return ! empty( $frm_settings->pubkey );
310 - }
311 -
312 - return ! empty( $frm_settings->hcaptcha_pubkey );
222 + return ! empty( $frm_settings->pubkey );
313 223 }
314 224
315 - /**
316 - * @return bool
317 - */
318 225 protected function should_validate() {
319 226 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
320 227 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
321 228 return false;
@@ -321,44 +228,19 @@
321 228 return false;
322 229 }
323 230
324 231 // don't require the captcha if it shouldn't be shown
325 - return self::should_show_captcha();
232 + return $this->should_show_captcha();
326 233 }
327 234
328 - /**
329 - * @param FrmSettings $frm_settings
330 - */
331 235 protected function send_api_check( $frm_settings ) {
332 - $captcha_settings = new FrmFieldCaptchaSettings( $frm_settings );
333 -
334 236 $arg_array = array(
335 237 'body' => array(
336 - 'secret' => $captcha_settings->secret,
337 - 'response' => FrmAppHelper::get_param( $captcha_settings->token_field, '', 'post', 'sanitize_text_field' ),
238 + 'secret' => $frm_settings->privkey,
239 + 'response' => FrmAppHelper::get_param( 'g-recaptcha-response', '', 'post', 'sanitize_text_field' ),
338 240 'remoteip' => FrmAppHelper::get_ip_address(),
339 241 ),
340 242 );
341 243
342 - return wp_remote_post( $captcha_settings->endpoint, $arg_array );
343 - }
344 -
345 - /**
346 - * Updates field name in page builder to the currently activated captcha if it is set to the default.
347 - *
348 - * @since 6.0
349 - *
350 - * @param array $values
351 - *
352 - * @return array $values
353 - */
354 - public static function update_field_name( $values ) {
355 - if ( $values['type'] === 'captcha' ) {
356 - $name = $values['name'];
357 - if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
358 - $values['name'] = __( 'Captcha', 'formidable' );
359 - }
360 - }
361 -
362 - return $values;
244 + return wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
363 245 }
364 246 }