PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0
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 +37 -193 6.45.0 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,34 +119,20 @@
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 - /**
210 - * @return string
211 - *
212 - * @param FrmSettings $frm_settings
213 - */
214 - protected function captcha_size( $frm_settings ) {
215 - if ( in_array( $frm_settings->re_type, array( 'invisible', 'v3' ), true ) ) {
216 - return 'invisible';
217 - }
129 + protected function captcha_size() {
218 130 // for reverse compatibility
219 - return $this->field['captcha_size'] === 'default' ? 'normal' : $this->field['captcha_size'];
131 + $frm_settings = FrmAppHelper::get_settings();
132 + $captcha_size = ( $this->field['captcha_size'] == 'default' ) ? 'normal' : $this->field['captcha_size'];
133 +
134 + return ( $frm_settings->re_type == 'invisible' ) ? 'invisible' : $captcha_size;
220 135 }
221 136
222 137 /**
223 138 * @since 4.07
@@ -229,60 +144,22 @@
229 144 $frm_settings = FrmAppHelper::get_settings();
230 145 $resp = $this->send_api_check( $frm_settings );
231 146 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
232 147
233 - if ( is_wp_error( $resp ) ) {
148 + if ( isset( $response['success'] ) && ! $response['success'] ) {
149 + // What happens when the CAPTCHA was entered incorrectly
150 + $invalid_message = FrmField::get_option( $this->field, 'invalid' );
151 + $errors[ 'field' . $args['id'] ] = ( $invalid_message == '' ? $frm_settings->re_msg : $invalid_message );
152 + } elseif ( 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 - return $errors;
238 156 }
239 157
240 - if ( ! is_array( $response ) ) {
241 - return $errors;
242 - }
243 -
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'] );
248 -
249 - $this->set_score( $score );
250 -
251 - if ( $score < $threshold ) {
252 - $response['success'] = false;
253 - }
254 - }
255 - }
256 -
257 - if ( isset( $response['success'] ) && ! $response['success'] ) {
258 - // 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 );
264 - }
265 -
266 158 return $errors;
267 159 }
268 160
269 161 /**
270 - * @param float $score
271 - * @return void
272 - */
273 - private function set_score( $score ) {
274 - global $frm_vars;
275 - if ( ! isset( $frm_vars['captcha_scores'] ) ) {
276 - $frm_vars['captcha_scores'] = array();
277 - }
278 - $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id'];
279 - if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) {
280 - $frm_vars['captcha_scores'][ $form_id ] = $score;
281 - }
282 - }
283 -
284 - /**
285 162 * @param array $args
286 163 * @return array
287 164 */
288 165 public function validate( $args ) {
@@ -289,10 +166,9 @@
289 166 if ( ! $this->should_validate() ) {
290 167 return array();
291 168 }
292 169
293 - // phpcs:ignore WordPress.Security.NonceVerification.Missing
294 - if ( ! isset( $_POST['g-recaptcha-response'] ) && ! isset( $_POST['h-captcha-response'] ) ) {
170 + if ( ! isset( $_POST['g-recaptcha-response'] ) ) {
295 171 // There was no captcha submitted.
296 172 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
297 173 }
298 174
@@ -302,63 +178,31 @@
302 178 /**
303 179 * @since 4.07
304 180 * @return bool
305 181 */
306 - public static function should_show_captcha() {
182 + private function should_show_captcha() {
307 183 $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 );
184 + return ! empty( $frm_settings->pubkey );
313 185 }
314 186
315 - /**
316 - * @return bool
317 - */
318 187 protected function should_validate() {
319 - $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
188 + $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // WPCS: CSRF ok.
320 189 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
321 190 return false;
322 191 }
323 192
324 193 // don't require the captcha if it shouldn't be shown
325 - return self::should_show_captcha();
194 + return $this->should_show_captcha();
326 195 }
327 196
328 - /**
329 - * @param FrmSettings $frm_settings
330 - */
331 197 protected function send_api_check( $frm_settings ) {
332 - $captcha_settings = new FrmFieldCaptchaSettings( $frm_settings );
333 -
334 198 $arg_array = array(
335 199 'body' => array(
336 - 'secret' => $captcha_settings->secret,
337 - 'response' => FrmAppHelper::get_param( $captcha_settings->token_field, '', 'post', 'sanitize_text_field' ),
200 + 'secret' => $frm_settings->privkey,
201 + 'response' => FrmAppHelper::get_param( 'g-recaptcha-response', '', 'post', 'sanitize_text_field' ),
338 202 'remoteip' => FrmAppHelper::get_ip_address(),
339 203 ),
340 204 );
341 205
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;
206 + return wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
363 207 }
364 208 }