PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 3.06.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v3.06.06
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
formidable / classes / models / fields / FrmFieldCaptcha.php

FrmFieldCaptcha.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 3.06.06, at classes/models/fields/FrmFieldCaptcha.php

197 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @since 3.0
5 */
6 class FrmFieldCaptcha extends FrmFieldType {
7
8 /**
9 * @var string
10 * @since 3.0
11 */
12 protected $type = 'captcha';
13
14 /**
15 * @var bool
16 * @since 3.0
17 */
18 protected $has_for_label = false;
19
20 /**
21 * @return string
22 */
23 protected function include_form_builder_file() {
24 return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-captcha.php';
25 }
26
27 /**
28 * @return array
29 */
30 protected function field_settings_for_type() {
31 return array(
32 'required' => false,
33 'invalid' => true,
34 'default_blank' => false,
35 'captcha_size' => true,
36 );
37 }
38
39 /**
40 * @return array
41 */
42 protected function new_field_settings() {
43 $frm_settings = FrmAppHelper::get_settings();
44 return array(
45 'invalid' => $frm_settings->re_msg,
46 );
47 }
48
49 /**
50 * @return array
51 */
52 protected function extra_field_opts() {
53 return array(
54 'label' => 'none',
55 'captcha_size' => 'normal',
56 'captcha_theme' => 'light',
57 );
58 }
59
60 /**
61 * Remove the "for" attribute for captcha
62 *
63 * @param array $args
64 * @param string $html
65 *
66 * @return string
67 */
68 protected function before_replace_html_shortcodes( $args, $html ) {
69 return str_replace( ' for="field_[key]"', ' for="g-recaptcha-response"', $html );
70 }
71
72 public function front_field_input( $args, $shortcode_atts ) {
73 $frm_settings = FrmAppHelper::get_settings();
74 if ( empty( $frm_settings->pubkey ) ) {
75 return '';
76 }
77
78 $class_prefix = $this->class_prefix();
79 $captcha_size = $this->captcha_size();
80 $allow_mutiple = $frm_settings->re_multi;
81
82 $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'] ) . '"';
83 if ( $captcha_size == 'invisible' && ! $allow_mutiple ) {
84 $html .= ' data-callback="frmAfterRecaptcha"';
85 }
86 $html .= '></div>';
87
88 return $html;
89 }
90
91 protected function load_field_scripts( $args ) {
92 $api_js_url = $this->api_url();
93
94 wp_register_script( 'recaptcha-api', $api_js_url, array( 'formidable' ), '3', true );
95 wp_enqueue_script( 'recaptcha-api' );
96 }
97
98 protected function api_url() {
99 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
100
101 $frm_settings = FrmAppHelper::get_settings();
102 $allow_mutiple = $frm_settings->re_multi;
103 if ( $allow_mutiple ) {
104 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
105 }
106
107 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
108 if ( ! empty( $lang ) ) {
109 $api_js_url .= '&hl=' . $lang;
110 }
111
112 return apply_filters( 'frm_recaptcha_js_url', $api_js_url );
113 }
114
115 protected function class_prefix() {
116 if ( $this->allow_multiple() ) {
117 $class_prefix = 'frm-';
118 } else {
119 $class_prefix = '';
120 }
121 return $class_prefix;
122 }
123
124 protected function allow_multiple() {
125 $frm_settings = FrmAppHelper::get_settings();
126 return $frm_settings->re_multi;
127 }
128
129 protected function captcha_size() {
130 // for reverse compatibility
131 $frm_settings = FrmAppHelper::get_settings();
132 $captcha_size = ( $this->field['captcha_size'] == 'default' ) ? 'normal' : $this->field['captcha_size'];
133 return ( $frm_settings->re_type == 'invisible' ) ? 'invisible' : $captcha_size;
134 }
135
136 public function validate( $args ) {
137 $errors = array();
138
139 if ( ! $this->should_validate() ) {
140 return $errors;
141 }
142
143 if ( ! isset( $_POST['g-recaptcha-response'] ) ) {
144 // If captcha is missing, check if it was already verified
145 if ( ! isset( $_POST['recaptcha_checked'] ) || ! wp_verify_nonce( $_POST['recaptcha_checked'], 'frm_ajax' ) ) {
146 // There was no captcha submitted
147 $errors[ 'field' . $args['id'] ] = __( 'The captcha is missing from this form', 'formidable' );
148 }
149 return $errors;
150 }
151
152 $frm_settings = FrmAppHelper::get_settings();
153
154 $resp = $this->send_api_check( $frm_settings );
155 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
156
157 if ( isset( $response['success'] ) && ! $response['success'] ) {
158 // What happens when the CAPTCHA was entered incorrectly
159 $invalid_message = FrmField::get_option( $this->field, 'invalid' );
160 $errors[ 'field' . $args['id'] ] = ( $invalid_message == '' ? $frm_settings->re_msg : $invalid_message );
161 } elseif ( is_wp_error( $resp ) ) {
162 $error_string = $resp->get_error_message();
163 $errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' );
164 $errors[ 'field' . $args['id'] ] .= ' ' . $error_string;
165 }
166
167 return $errors;
168 }
169
170 protected function should_validate() {
171 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, stripslashes_deep( $_POST ) ); // WPCS: CSRF ok.
172 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
173 return false;
174 }
175
176 $frm_settings = FrmAppHelper::get_settings();
177 if ( empty( $frm_settings->pubkey ) ) {
178 // don't require the captcha if it shouldn't be shown
179 return false;
180 }
181
182 return true;
183 }
184
185 protected function send_api_check( $frm_settings ) {
186 $arg_array = array(
187 'body' => array(
188 'secret' => $frm_settings->privkey,
189 'response' => $_POST['g-recaptcha-response'], // WPCS: CSRF ok.
190 'remoteip' => FrmAppHelper::get_ip_address(),
191 ),
192 );
193
194 return wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
195 }
196 }
197