Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmFieldCaptcha::allow_multiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
	protected $is_tall = true;
21
22
	/**
23
	 * @return string
24
	 */
25
	protected function include_form_builder_file() {
26
		return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-captcha.php';
27
	}
28
29
	/**
30
	 * @return array
31
	 */
32
	protected function field_settings_for_type() {
33
		return array(
34
			'required'      => false,
35
			'invalid'       => true,
36
			'default_blank' => false,
37
			'captcha_size'  => true,
38
		);
39
	}
40
41
	/**
42
	 * @return array
43
	 */
44
	protected function new_field_settings() {
45
		$frm_settings = FrmAppHelper::get_settings();
46
		return array(
47
			'invalid' => $frm_settings->re_msg,
48
		);
49
	}
50
51
	/**
52
	 * @return array
53
	 */
54
	protected function extra_field_opts() {
55
		return array(
56
			'label'         => 'none',
57
			'captcha_size'  => 'normal',
58
			'captcha_theme' => 'light',
59
		);
60
	}
61
62
	/**
63
	 * Remove the "for" attribute for captcha
64
	 *
65
	 * @param array $args
66
	 * @param string $html
67
	 *
68
	 * @return string
69
	 */
70
	protected function before_replace_html_shortcodes( $args, $html ) {
71
		return str_replace( ' for="field_[key]"', '', $html );
72
	}
73
74
	public function front_field_input( $args, $shortcode_atts ) {
75
		$frm_settings = FrmAppHelper::get_settings();
76
		if ( empty( $frm_settings->pubkey ) ) {
77
			return '';
78
		}
79
80
		$class_prefix = $this->class_prefix();
81
		$captcha_size = $this->captcha_size();
82
		$allow_mutiple = $frm_settings->re_multi;
83
84
		$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'] ) . '"';
85
		if ( $captcha_size == 'invisible' && ! $allow_mutiple ) {
86
			$html .= ' data-callback="frmAfterRecaptcha"';
87
		}
88
		$html .= '></div>';
89
90
		return $html;
91
	}
92
93
	protected function load_field_scripts( $args ) {
94
		$api_js_url = $this->api_url();
95
96
		wp_register_script( 'recaptcha-api', $api_js_url, array( 'formidable' ), '', true );
97
		wp_enqueue_script( 'recaptcha-api' );
98
	}
99
100
	protected function api_url() {
101
		$api_js_url = 'https://www.google.com/recaptcha/api.js?';
102
103
		$frm_settings = FrmAppHelper::get_settings();
104
		$allow_mutiple = $frm_settings->re_multi;
105
		if ( $allow_mutiple ) {
106
			$api_js_url .= '&onload=frmRecaptcha&render=explicit';
107
		}
108
109
		$lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
110
		if ( ! empty( $lang ) ) {
111
			$api_js_url .= '&hl=' . $lang;
112
		}
113
114
		return apply_filters( 'frm_recaptcha_js_url', $api_js_url );
115
	}
116
117
	protected function class_prefix() {
118
		if ( $this->allow_multiple() ) {
119
			$class_prefix = 'frm-';
120
		} else {
121
			$class_prefix = '';
122
		}
123
		return $class_prefix;
124
	}
125
126
	protected function allow_multiple() {
127
		$frm_settings = FrmAppHelper::get_settings();
128
		return $frm_settings->re_multi;
129
	}
130
131
	protected function captcha_size() {
132
		// for reverse compatibility
133
		$frm_settings = FrmAppHelper::get_settings();
134
		$captcha_size = ( $this->field['captcha_size'] == 'default' ) ? 'normal' : $this->field['captcha_size'];
135
		return ( $frm_settings->re_type == 'invisible' ) ? 'invisible' : $captcha_size;
136
	}
137
138
	public function validate( $args ) {
139
		$errors = array();
140
141
		if ( ! $this->should_validate() ) {
142
			return $errors;
143
		}
144
145 View Code Duplication
		if ( ! isset( $_POST['g-recaptcha-response']) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
			// If captcha is missing, check if it was already verified
147
			if ( ! isset( $_POST['recaptcha_checked'] ) || ! wp_verify_nonce( $_POST['recaptcha_checked'], 'frm_ajax' ) ) {
148
				// There was no captcha submitted
149
				$errors[ 'field' . $args['id'] ] = __( 'The captcha is missing from this form', 'formidable' );
150
			}
151
			return $errors;
152
		}
153
154
		$frm_settings = FrmAppHelper::get_settings();
155
156
		$resp = $this->send_api_check( $frm_settings );
157
		$response = json_decode( wp_remote_retrieve_body( $resp ), true );
158
159
		if ( isset( $response['success'] ) && ! $response['success'] ) {
160
			// What happens when the CAPTCHA was entered incorrectly
161
			$invalid_message = FrmField::get_option( $this->field, 'invalid' );
162
			$errors[ 'field' . $args['id'] ] = ( $invalid_message == '' ? $frm_settings->re_msg : $invalid_message );
163 View Code Duplication
		} elseif ( is_wp_error( $resp ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
			$error_string = $resp->get_error_message();
165
			$errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' );
166
			$errors[ 'field' . $args['id'] ] .= ' ' . $error_string;
167
		}
168
169
		return $errors;
170
	}
171
172
	protected function should_validate() {
173
		$is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, stripslashes_deep( $_POST ) );
174
		if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
175
			return false;
176
		}
177
178
		$frm_settings = FrmAppHelper::get_settings();
179
		if ( empty( $frm_settings->pubkey ) ) {
180
			// don't require the captcha if it shouldn't be shown
181
			return false;
182
		}
183
184
		return true;
185
	}
186
187
	protected function send_api_check( $frm_settings ) {
188
		$arg_array = array(
189
			'body'      => array(
190
				'secret'   => $frm_settings->privkey,
191
				'response' => $_POST['g-recaptcha-response'],
192
				'remoteip' => FrmAppHelper::get_ip_address(),
193
			),
194
		);
195
196
		return wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
197
	}
198
}
199