| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 6.0 |
| 8 |
*/ |
| 9 |
class FrmFieldCaptchaSettings { |
| 10 |
|
| 11 |
/** |
| 12 |
* The global settings object. |
| 13 |
* |
| 14 |
* @var FrmSettings |
| 15 |
*/ |
| 16 |
public $frm_settings; |
| 17 |
|
| 18 |
/** |
| 19 |
* The private key used for validating the token. |
| 20 |
* |
| 21 |
* @since 6.0 |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
public $secret; |
| 26 |
|
| 27 |
/** |
| 28 |
* The key value to check in $_POST data with the token for validation. |
| 29 |
* |
| 30 |
* @since 6.0 |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $token_field; |
| 35 |
|
| 36 |
/** |
| 37 |
* The URL that is called when validating the token. |
| 38 |
* |
| 39 |
* @since 6.0 |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public $endpoint; |
| 44 |
|
| 45 |
/** |
| 46 |
* @param FrmSettings $frm_settings |
| 47 |
*/ |
| 48 |
public function __construct( $frm_settings ) { |
| 49 |
$this->frm_settings = $frm_settings; |
| 50 |
$this->set_secret(); |
| 51 |
$this->set_token_field(); |
| 52 |
$this->set_endpoint(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set the private key to $this->secret. |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
protected function set_secret() { |
| 61 |
$key = $this->get_key_for_privkey(); |
| 62 |
$this->secret = ! empty( $this->frm_settings->$key ) ? $this->frm_settings->$key : ''; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Set the string to use for $this->token_field. |
| 67 |
* This is consistent between the different CAPTCHA services. |
| 68 |
* The element class name is always the same as the key before "-response" in the $_POST value key. |
| 69 |
* For example (h-captcha-response, or cf-turnstile-response). |
| 70 |
* |
| 71 |
* @since 6.8.4 |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
protected function set_token_field() { |
| 76 |
$this->token_field = $this->get_element_class_name() . '-response'; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @since 6.8.4 |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
protected function set_endpoint() { |
| 85 |
$this->endpoint = ''; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the name of the CAPTCHA service. |
| 90 |
* |
| 91 |
* @since 6.8.4 |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function get_name() { |
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the class name used for the CAPTCHA element on the front end. |
| 101 |
* |
| 102 |
* @since 6.8.4 |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function get_element_class_name() { |
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the URL of the page to link to in global settings. This page should have |
| 112 |
* instructions on how to get site and secret keys. |
| 113 |
* |
| 114 |
* @since 6.8.4 |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function get_documentation_url() { |
| 119 |
return ''; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get the prefix for the global setting. |
| 124 |
* reCAPTCHA fields just use pubkey/privkey. |
| 125 |
* But other captcha integrations use a prefix like hcaptcha_public/turnstile_privkey. |
| 126 |
* |
| 127 |
* @since 6.8.4 |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function get_settings_prefix() { |
| 132 |
return ''; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the string to use for the tooltip on the Global settings page when hovering over the Site Key label. |
| 137 |
* |
| 138 |
* @since 6.8.4 |
| 139 |
* |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
public function get_site_key_tooltip() { |
| 143 |
return ''; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get the key used in FrmSettings for the site secret used for validating a Captcha. |
| 148 |
* |
| 149 |
* @since 6.8.4 |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
protected function get_key_for_privkey() { |
| 154 |
return $this->get_settings_prefix() . 'privkey'; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get the key used in FrmSettings for the site key used to initialize a Captcha on the front end. |
| 159 |
* |
| 160 |
* @since 6.8.4 |
| 161 |
* |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
protected function get_key_for_pubkey() { |
| 165 |
return $this->get_settings_prefix() . 'pubkey'; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the site key to use for a Captcha on the front end. |
| 170 |
* |
| 171 |
* @since 6.8.4 |
| 172 |
* |
| 173 |
* @return string |
| 174 |
*/ |
| 175 |
public function get_pubkey() { |
| 176 |
$key = $this->get_key_for_pubkey(); |
| 177 |
return isset( $this->frm_settings->$key ) ? $this->frm_settings->$key : ''; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Check if the public key is not empty. |
| 182 |
* |
| 183 |
* @since 6.8.4 |
| 184 |
* |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
public function has_pubkey() { |
| 188 |
return ! empty( $this->get_pubkey() ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* This can be used to add additional attributes to a front end Captcha element. |
| 193 |
* |
| 194 |
* @since 6.8.4 |
| 195 |
* |
| 196 |
* @param array $attributes |
| 197 |
* @param array $field |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
public function add_front_end_element_attributes( $attributes, $field ) { |
| 201 |
$attributes['data-size'] = $this->get_captcha_size( $field ); |
| 202 |
|
| 203 |
if ( ! empty( $field['captcha_theme'] ) ) { |
| 204 |
$attributes['data-theme'] = $field['captcha_theme']; |
| 205 |
} |
| 206 |
|
| 207 |
return $attributes; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @since 6.8.4 |
| 212 |
* |
| 213 |
* @param array $field |
| 214 |
* @return string Either 'normal' or 'compact'. |
| 215 |
*/ |
| 216 |
protected function get_captcha_size( $field ) { |
| 217 |
return $field['captcha_size'] === 'default' ? 'normal' : $field['captcha_size']; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Determine if the Captcha Size setting should be shown in field settings. |
| 222 |
* This has options for "Normal" and "Compact". |
| 223 |
* This is supported by all CAPTCHA types except for invisible reCAPTCHAs. |
| 224 |
* |
| 225 |
* @since 6.8.4 |
| 226 |
* |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
public function should_show_captcha_size() { |
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Determine if we should show a theme dropdown for our Captcha field. |
| 235 |
* This is applicable for all Captcha types except for invisible reCAPTCHA. |
| 236 |
* |
| 237 |
* @since 6.8.4 |
| 238 |
* |
| 239 |
* @return bool |
| 240 |
*/ |
| 241 |
public function should_show_captcha_theme() { |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Determine if the "auto" option should be shown for a specific captcha type. |
| 247 |
* This is only applicable for Turnstile. |
| 248 |
* |
| 249 |
* @since 6.8.4 |
| 250 |
* |
| 251 |
* @return bool |
| 252 |
*/ |
| 253 |
public function should_show_captcha_theme_auto_option() { |
| 254 |
return false; |
| 255 |
} |
| 256 |
} |
| 257 |
|