field-advanced-link.php
6 years ago
field-button.php
6 years ago
field-clone.php
6 years ago
field-column.php
6 years ago
field-dynamic-message.php
6 years ago
field-file.php
6 years ago
field-flexible-content.php
6 years ago
field-forms.php
6 years ago
field-group.php
6 years ago
field-hidden.php
6 years ago
field-image.php
6 years ago
field-post-statuses.php
6 years ago
field-post-types.php
6 years ago
field-recaptcha.php
6 years ago
field-repeater.php
6 years ago
field-select.php
6 years ago
field-slug.php
6 years ago
field-taxonomies.php
6 years ago
field-taxonomy-terms.php
6 years ago
field-textarea.php
6 years ago
field-user-roles.php
6 years ago
field-recaptcha.php
318 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | class acfe_field_recaptcha extends acf_field{ |
| 7 | |
| 8 | public function __construct(){ |
| 9 | |
| 10 | $this->name = 'acfe_recaptcha'; |
| 11 | $this->label = __('reCAPTCHA', 'acf'); |
| 12 | $this->category = 'jquery'; |
| 13 | $this->defaults = array( |
| 14 | 'required' => 0, |
| 15 | 'disabled' => 0, |
| 16 | 'readonly' => 0, |
| 17 | 'version' => 'v2', |
| 18 | 'theme' => 'light', |
| 19 | 'size' => 'normal', |
| 20 | ); |
| 21 | |
| 22 | parent::__construct(); |
| 23 | |
| 24 | } |
| 25 | |
| 26 | public function render_field_settings($field){ |
| 27 | |
| 28 | // Version |
| 29 | acf_render_field_setting($field, array( |
| 30 | 'label' => __('Version', 'acf'), |
| 31 | 'instructions' => __('Select the reCaptcha version', 'acf'), |
| 32 | 'type' => 'select', |
| 33 | 'name' => 'version', |
| 34 | 'optgroup' => false, |
| 35 | 'choices' => array( |
| 36 | 'v2' => __('reCaptcha V2', 'acf'), |
| 37 | 'v3' => __('reCaptcha V3', 'acf'), |
| 38 | ) |
| 39 | )); |
| 40 | |
| 41 | // V2 Theme |
| 42 | acf_render_field_setting($field, array( |
| 43 | 'label' => __('Theme', 'acf'), |
| 44 | 'instructions' => __('Select the reCaptcha theme', 'acf'), |
| 45 | 'type' => 'select', |
| 46 | 'name' => 'v2_theme', |
| 47 | 'optgroup' => false, |
| 48 | 'choices' => array( |
| 49 | 'light' => __('Light', 'acf'), |
| 50 | 'dark' => __('Dark', 'acf'), |
| 51 | ), |
| 52 | 'conditional_logic' => array( |
| 53 | array( |
| 54 | array( |
| 55 | 'field' => 'version', |
| 56 | 'operator' => '==', |
| 57 | 'value' => 'v2', |
| 58 | ) |
| 59 | ) |
| 60 | ) |
| 61 | )); |
| 62 | |
| 63 | // V2 Size |
| 64 | acf_render_field_setting($field, array( |
| 65 | 'label' => __('Size', 'acf'), |
| 66 | 'instructions' => __('Select the reCaptcha size', 'acf'), |
| 67 | 'type' => 'select', |
| 68 | 'name' => 'v2_size', |
| 69 | 'optgroup' => false, |
| 70 | 'choices' => array( |
| 71 | 'normal' => __('Normal', 'acf'), |
| 72 | 'compact' => __('Compact', 'acf'), |
| 73 | ), |
| 74 | 'conditional_logic' => array( |
| 75 | array( |
| 76 | array( |
| 77 | 'field' => 'version', |
| 78 | 'operator' => '==', |
| 79 | 'value' => 'v2', |
| 80 | ) |
| 81 | ) |
| 82 | ) |
| 83 | )); |
| 84 | |
| 85 | // V3 Hide Logo |
| 86 | acf_render_field_setting($field, array( |
| 87 | 'label' => __('Hide logo', 'acf'), |
| 88 | 'instructions' => __('Hide the reCaptcha logo', 'acf'), |
| 89 | 'type' => 'true_false', |
| 90 | 'name' => 'v3_hide_logo', |
| 91 | 'ui' => true, |
| 92 | 'conditional_logic' => array( |
| 93 | array( |
| 94 | array( |
| 95 | 'field' => 'version', |
| 96 | 'operator' => '==', |
| 97 | 'value' => 'v3', |
| 98 | ) |
| 99 | ) |
| 100 | ) |
| 101 | )); |
| 102 | |
| 103 | // Site Key |
| 104 | acf_render_field_setting($field, array( |
| 105 | 'label' => __('Site key', 'acf'), |
| 106 | 'instructions' => __('Enter the site key. <a href="https://www.google.com/recaptcha/admin" target="_blank">reCaptcha API Admin</a>', 'acf'), |
| 107 | 'type' => 'text', |
| 108 | 'name' => 'site_key', |
| 109 | )); |
| 110 | |
| 111 | // Site Secret |
| 112 | acf_render_field_setting($field, array( |
| 113 | 'label' => __('Secret key', 'acf'), |
| 114 | 'instructions' => __('Enter the secret key. <a href="https://www.google.com/recaptcha/admin" target="_blank">reCaptcha API Admin</a>', 'acf'), |
| 115 | 'type' => 'text', |
| 116 | 'name' => 'secret_key', |
| 117 | )); |
| 118 | |
| 119 | } |
| 120 | |
| 121 | public function prepare_field($field){ |
| 122 | |
| 123 | if($field['version'] === 'v3') |
| 124 | $field['wrapper']['class'] = 'acf-hidden'; |
| 125 | |
| 126 | return $field; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | public function render_field($field){ |
| 131 | |
| 132 | // Site key |
| 133 | $site_key = acf_get_setting('acfe/field/recaptcha/site_key', $field['site_key']); |
| 134 | |
| 135 | // Version |
| 136 | $field['version'] = acf_get_setting('acfe/field/recaptcha/version', $field['version']); |
| 137 | |
| 138 | // V2 |
| 139 | if($field['version'] === 'v2'){ ?> |
| 140 | |
| 141 | <?php |
| 142 | // Theme & Size |
| 143 | $field['v2_theme'] = acf_get_setting('acfe/field/recaptcha/v2/theme', $field['v2_theme']); |
| 144 | $field['v2_size'] = acf_get_setting('acfe/field/recaptcha/v2/size', $field['v2_size']); |
| 145 | ?> |
| 146 | |
| 147 | <script type="text/javascript"> |
| 148 | acf.addAction('validation_failure', function($form){ |
| 149 | |
| 150 | if($form.find('.acfe-recaptcha').length) |
| 151 | grecaptcha.reset(acfe_recaptcha_<?php echo $field['key']; ?>); |
| 152 | |
| 153 | }); |
| 154 | |
| 155 | var acfe_recaptcha_<?php echo $field['key']; ?>; |
| 156 | |
| 157 | var acfe_recaptcha_error_<?php echo $field['key']; ?> = function(){ |
| 158 | |
| 159 | jQuery('input#<?php echo $field['key']; ?>').val('error').change(); |
| 160 | |
| 161 | }; |
| 162 | |
| 163 | var acfe_recaptcha_callback_<?php echo $field['key']; ?> = function(response){ |
| 164 | |
| 165 | jQuery('.g-recaptcha-response').change(); |
| 166 | jQuery('input#<?php echo $field['key']; ?>').val(response).change(); |
| 167 | jQuery('input#<?php echo $field['key']; ?>').closest('.acf-input').find('> .acf-notice.-error').hide(); |
| 168 | |
| 169 | }; |
| 170 | |
| 171 | var acfe_recaptcha_expired_<?php echo $field['key']; ?> = function(){ |
| 172 | |
| 173 | jQuery('input#<?php echo $field['key']; ?>').val('expired').change(); |
| 174 | |
| 175 | }; |
| 176 | |
| 177 | var acfe_recaptcha_onload_<?php echo $field['key']; ?> = function(){ |
| 178 | |
| 179 | acfe_recaptcha_<?php echo $field['key']; ?> = grecaptcha.render('acfe_recaptcha_<?php echo $field['key']; ?>', { |
| 180 | 'sitekey': '<?php echo $site_key; ?>', |
| 181 | 'error-callback': 'acfe_recaptcha_error_<?php echo $field['key']; ?>', |
| 182 | 'callback': 'acfe_recaptcha_callback_<?php echo $field['key']; ?>', |
| 183 | 'expired-callback': 'acfe_recaptcha_expired_<?php echo $field['key']; ?>', |
| 184 | 'theme': '<?php echo $field['v2_theme']; ?>', |
| 185 | 'size': '<?php echo $field['v2_size']; ?>' |
| 186 | }); |
| 187 | |
| 188 | }; |
| 189 | </script> |
| 190 | |
| 191 | <script src="https://www.google.com/recaptcha/api.js?onload=acfe_recaptcha_onload_<?php echo $field['key']; ?>&render=explicit" async defer></script> |
| 192 | |
| 193 | <div id="acfe_recaptcha_<?php echo $field['key']; ?>" class="acfe-recaptcha"></div> |
| 194 | |
| 195 | <div class="acf-input-wrap"> |
| 196 | <?php |
| 197 | acf_hidden_input(array( |
| 198 | 'id' => $field['key'], |
| 199 | 'name' => $field['name'] |
| 200 | )); |
| 201 | ?> |
| 202 | </div> |
| 203 | |
| 204 | <?php |
| 205 | return; |
| 206 | |
| 207 | } |
| 208 | |
| 209 | // V3 |
| 210 | elseif($field['version'] === 'v3'){ |
| 211 | |
| 212 | // Hide logo |
| 213 | $field['v3_hide_logo'] = acf_get_setting('acfe/field/recaptcha/v3/hide_logo', $field['v3_hide_logo']); |
| 214 | |
| 215 | if($field['v3_hide_logo']){ ?> |
| 216 | <style> |
| 217 | .grecaptcha-badge{ |
| 218 | display: none; |
| 219 | visibility: hidden; |
| 220 | } |
| 221 | </style> |
| 222 | <?php } ?> |
| 223 | |
| 224 | <script type="text/javascript"> |
| 225 | var acfe_recaptcha_onload_<?php echo $field['key']; ?> = function(){ |
| 226 | |
| 227 | grecaptcha.ready(function() { |
| 228 | grecaptcha.execute('<?php echo $site_key; ?>', {action: 'homepage'}).then(function(response){ |
| 229 | |
| 230 | jQuery('.g-recaptcha-response').change(); |
| 231 | jQuery('input#<?php echo $field['key']; ?>').val(response).change(); |
| 232 | jQuery('input#<?php echo $field['key']; ?>').closest('.acf-input').find('> .acf-notice.-error').hide(); |
| 233 | |
| 234 | }); |
| 235 | }); |
| 236 | |
| 237 | }; |
| 238 | </script> |
| 239 | |
| 240 | <script src="https://www.google.com/recaptcha/api.js?render=<?php echo $site_key; ?>&onload=acfe_recaptcha_onload_<?php echo $field['key']; ?>" async defer></script> |
| 241 | |
| 242 | <div id="acfe_recaptcha_<?php echo $field['key']; ?>" class="acfe-recaptcha"></div> |
| 243 | |
| 244 | <div class="acf-input-wrap"> |
| 245 | <?php |
| 246 | acf_hidden_input(array( |
| 247 | 'id' => $field['key'], |
| 248 | 'name' => $field['name'] |
| 249 | )); |
| 250 | ?> |
| 251 | </div> |
| 252 | |
| 253 | <?php |
| 254 | return; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | public function validate_value($valid, $value, $field, $input){ |
| 261 | |
| 262 | // No value |
| 263 | if(empty($value)){ |
| 264 | |
| 265 | return __('This field is required.'); |
| 266 | |
| 267 | } |
| 268 | |
| 269 | // Expired |
| 270 | elseif($value === 'expired'){ |
| 271 | |
| 272 | return __('reCaptcha has expired.'); |
| 273 | |
| 274 | } |
| 275 | |
| 276 | // Error |
| 277 | elseif($value === 'error'){ |
| 278 | |
| 279 | return __('An error has occured.'); |
| 280 | |
| 281 | } |
| 282 | |
| 283 | // Success |
| 284 | else{ |
| 285 | |
| 286 | // Secret key |
| 287 | $secret_key = acf_get_setting('acfe/field/recaptcha/secret_key', $field['secret_key']); |
| 288 | |
| 289 | // API Call |
| 290 | $api = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret_key}&response={$value}"); |
| 291 | |
| 292 | // No response |
| 293 | if(empty($api)) |
| 294 | return false; |
| 295 | |
| 296 | $response = json_decode($api); |
| 297 | |
| 298 | if($response->success === false){ |
| 299 | |
| 300 | $valid = false; |
| 301 | |
| 302 | } |
| 303 | |
| 304 | elseif($response->success === true){ |
| 305 | |
| 306 | $valid = true; |
| 307 | |
| 308 | } |
| 309 | |
| 310 | } |
| 311 | |
| 312 | return $valid; |
| 313 | |
| 314 | } |
| 315 | |
| 316 | } |
| 317 | |
| 318 | new acfe_field_recaptcha(); |