| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 3.0 |
| 8 |
*/ |
| 9 |
class FrmFieldCaptcha extends FrmFieldType { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
* @since 3.0 |
| 14 |
*/ |
| 15 |
protected $type = 'captcha'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
protected function include_form_builder_file() { |
| 21 |
return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-captcha.php'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
protected function field_settings_for_type() { |
| 28 |
return array( |
| 29 |
'required' => false, |
| 30 |
'invalid' => true, |
| 31 |
'captcha_size' => true, |
| 32 |
'default' => false, |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
protected function new_field_settings() { |
| 40 |
$frm_settings = FrmAppHelper::get_settings(); |
| 41 |
|
| 42 |
return array( |
| 43 |
'invalid' => $frm_settings->re_msg, |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
protected function extra_field_opts() { |
| 51 |
return array( |
| 52 |
'label' => 'none', |
| 53 |
'captcha_size' => 'normal', |
| 54 |
'captcha_theme' => 'light', |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Remove the "for" attribute for captcha |
| 60 |
* |
| 61 |
* @param array $args |
| 62 |
* @param string $html |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
protected function before_replace_html_shortcodes( $args, $html ) { |
| 67 |
return str_replace( ' for="field_[key]"', ' for="g-recaptcha-response"', $html ); |
| 68 |
} |
| 69 |
|
| 70 |
public function front_field_input( $args, $shortcode_atts ) { |
| 71 |
$frm_settings = FrmAppHelper::get_settings(); |
| 72 |
if ( empty( $frm_settings->pubkey ) ) { |
| 73 |
return ''; |
| 74 |
} |
| 75 |
|
| 76 |
$class_prefix = $this->class_prefix(); |
| 77 |
$captcha_size = $this->captcha_size(); |
| 78 |
$allow_mutiple = $frm_settings->re_multi; |
| 79 |
|
| 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 ) { |
| 82 |
$html .= ' data-callback="frmAfterRecaptcha"'; |
| 83 |
} |
| 84 |
$html .= '></div>'; |
| 85 |
|
| 86 |
return $html; |
| 87 |
} |
| 88 |
|
| 89 |
protected function load_field_scripts( $args ) { |
| 90 |
$api_js_url = $this->api_url(); |
| 91 |
|
| 92 |
wp_register_script( 'recaptcha-api', $api_js_url, array( 'formidable' ), '3', true ); |
| 93 |
wp_enqueue_script( 'recaptcha-api' ); |
| 94 |
} |
| 95 |
|
| 96 |
protected function api_url() { |
| 97 |
$api_js_url = 'https://www.google.com/recaptcha/api.js?'; |
| 98 |
|
| 99 |
$frm_settings = FrmAppHelper::get_settings(); |
| 100 |
$allow_mutiple = $frm_settings->re_multi; |
| 101 |
if ( $allow_mutiple ) { |
| 102 |
$api_js_url .= '&onload=frmRecaptcha&render=explicit'; |
| 103 |
} |
| 104 |
|
| 105 |
$lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field ); |
| 106 |
if ( ! empty( $lang ) ) { |
| 107 |
$api_js_url .= '&hl=' . $lang; |
| 108 |
} |
| 109 |
|
| 110 |
return apply_filters( 'frm_recaptcha_js_url', $api_js_url ); |
| 111 |
} |
| 112 |
|
| 113 |
protected function class_prefix() { |
| 114 |
if ( $this->allow_multiple() ) { |
| 115 |
$class_prefix = 'frm-'; |
| 116 |
} else { |
| 117 |
$class_prefix = ''; |
| 118 |
} |
| 119 |
|
| 120 |
return $class_prefix; |
| 121 |
} |
| 122 |
|
| 123 |
protected function allow_multiple() { |
| 124 |
$frm_settings = FrmAppHelper::get_settings(); |
| 125 |
|
| 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 |
|
| 134 |
return ( $frm_settings->re_type == 'invisible' ) ? 'invisible' : $captcha_size; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @since 4.07 |
| 139 |
* @param array $args |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
protected function validate_against_api( $args ) { |
| 143 |
$errors = array(); |
| 144 |
$frm_settings = FrmAppHelper::get_settings(); |
| 145 |
$resp = $this->send_api_check( $frm_settings ); |
| 146 |
$response = json_decode( wp_remote_retrieve_body( $resp ), true ); |
| 147 |
|
| 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 ) ) { |
| 153 |
$error_string = $resp->get_error_message(); |
| 154 |
$errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' ); |
| 155 |
$errors[ 'field' . $args['id'] ] .= ' ' . $error_string; |
| 156 |
} |
| 157 |
|
| 158 |
return $errors; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param array $args |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
public function validate( $args ) { |
| 166 |
if ( ! $this->should_validate() ) { |
| 167 |
return array(); |
| 168 |
} |
| 169 |
|
| 170 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 171 |
if ( ! isset( $_POST['g-recaptcha-response'] ) ) { |
| 172 |
// There was no captcha submitted. |
| 173 |
return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) ); |
| 174 |
} |
| 175 |
|
| 176 |
return $this->validate_against_api( $args ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @since 4.07 |
| 181 |
* @return bool |
| 182 |
*/ |
| 183 |
private function should_show_captcha() { |
| 184 |
$frm_settings = FrmAppHelper::get_settings(); |
| 185 |
return ! empty( $frm_settings->pubkey ); |
| 186 |
} |
| 187 |
|
| 188 |
protected function should_validate() { |
| 189 |
$is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 190 |
if ( FrmAppHelper::is_admin() || $is_hidden_field ) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
// don't require the captcha if it shouldn't be shown |
| 195 |
return $this->should_show_captcha(); |
| 196 |
} |
| 197 |
|
| 198 |
protected function send_api_check( $frm_settings ) { |
| 199 |
$arg_array = array( |
| 200 |
'body' => array( |
| 201 |
'secret' => $frm_settings->privkey, |
| 202 |
'response' => FrmAppHelper::get_param( 'g-recaptcha-response', '', 'post', 'sanitize_text_field' ), |
| 203 |
'remoteip' => FrmAppHelper::get_ip_address(), |
| 204 |
), |
| 205 |
); |
| 206 |
|
| 207 |
return wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array ); |
| 208 |
} |
| 209 |
} |
| 210 |
|