field-advanced-link.php
3 months ago
field-button-group.php
1 week ago
field-button.php
7 months ago
field-checkbox.php
3 months ago
field-clone.php
2 years ago
field-code-editor.php
3 months ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
3 months ago
field-flexible-content-actions-title.php
3 months ago
field-flexible-content-actions-toggle.php
3 months ago
field-flexible-content-actions.php
3 months ago
field-flexible-content-async.php
4 months ago
field-flexible-content-compatibility.php
3 months ago
field-flexible-content-controls.php
9 months ago
field-flexible-content-hide.php
3 months ago
field-flexible-content-hooks.php
9 months ago
field-flexible-content-modal-edit.php
9 months ago
field-flexible-content-modal-select.php
3 months ago
field-flexible-content-modal-settings.php
3 months ago
field-flexible-content-popup.php
3 months ago
field-flexible-content-preview.php
3 months ago
field-flexible-content-state.php
9 months ago
field-flexible-content-thumbnail.php
9 months ago
field-flexible-content-wysiwyg.php
9 months ago
field-flexible-content.php
3 months ago
field-forms.php
3 months ago
field-gallery.php
3 months ago
field-google-map.php
3 months ago
field-group.php
3 months ago
field-hidden.php
3 years ago
field-image.php
3 months ago
field-post-object.php
3 months ago
field-post-statuses.php
3 months ago
field-post-types.php
3 months ago
field-radio.php
3 months ago
field-recaptcha.php
3 months ago
field-relationship.php
3 months ago
field-repeater.php
3 months ago
field-select.php
3 months ago
field-slug.php
1 year ago
field-taxonomies.php
3 months ago
field-taxonomy-terms.php
3 months ago
field-taxonomy.php
3 months ago
field-textarea.php
3 months ago
field-user-roles.php
3 months ago
field-user.php
3 months ago
field-wysiwyg.php
3 months ago
field-recaptcha.php
325 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_recaptcha')): |
| 8 | |
| 9 | class acfe_field_recaptcha extends acf_field{ |
| 10 | |
| 11 | public $hide_logo = false; |
| 12 | |
| 13 | /** |
| 14 | * initialize |
| 15 | */ |
| 16 | function initialize(){ |
| 17 | |
| 18 | $category = acfe_is_acf('6.1') ? 'advanced' : 'jquery'; |
| 19 | |
| 20 | $this->name = 'acfe_recaptcha'; |
| 21 | $this->label = __('Google reCaptcha', 'acfe'); |
| 22 | $this->category = $category; |
| 23 | $this->defaults = array( |
| 24 | 'required' => 0, |
| 25 | 'disabled' => 0, |
| 26 | 'readonly' => 0, |
| 27 | 'version' => 'v2', |
| 28 | 'v2_theme' => 'light', |
| 29 | 'v2_size' => 'normal', |
| 30 | 'v3_hide_logo' => false, |
| 31 | 'site_key' => '', |
| 32 | 'secret_key' => '', |
| 33 | ); |
| 34 | |
| 35 | $this->add_action('acf/input/admin_print_footer_scripts', array($this, 'admin_print_footer_scripts')); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * render_field_settings |
| 42 | * |
| 43 | * @param $field |
| 44 | */ |
| 45 | function render_field_settings($field){ |
| 46 | |
| 47 | // Version |
| 48 | acf_render_field_setting($field, array( |
| 49 | 'label' => __('Version', 'acfe'), |
| 50 | 'instructions' => __('Select the reCaptcha version', 'acfe'), |
| 51 | 'type' => 'select', |
| 52 | 'name' => 'version', |
| 53 | 'choices' => array( |
| 54 | 'v2' => __('reCaptcha V2', 'acfe'), |
| 55 | 'v3' => __('reCaptcha V3', 'acfe'), |
| 56 | ) |
| 57 | )); |
| 58 | |
| 59 | // V2 Theme |
| 60 | acf_render_field_setting($field, array( |
| 61 | 'label' => __('Theme', 'acfe'), |
| 62 | 'instructions' => __('Select the reCaptcha theme', 'acfe'), |
| 63 | 'type' => 'select', |
| 64 | 'name' => 'v2_theme', |
| 65 | 'choices' => array( |
| 66 | 'light' => __('Light', 'acfe'), |
| 67 | 'dark' => __('Dark', 'acfe'), |
| 68 | ), |
| 69 | 'conditional_logic' => array( |
| 70 | array( |
| 71 | array( |
| 72 | 'field' => 'version', |
| 73 | 'operator' => '==', |
| 74 | 'value' => 'v2', |
| 75 | ) |
| 76 | ) |
| 77 | ) |
| 78 | )); |
| 79 | |
| 80 | // V2 Size |
| 81 | acf_render_field_setting($field, array( |
| 82 | 'label' => __('Size', 'acfe'), |
| 83 | 'instructions' => __('Select the reCaptcha size', 'acfe'), |
| 84 | 'type' => 'select', |
| 85 | 'name' => 'v2_size', |
| 86 | 'choices' => array( |
| 87 | 'normal' => __('Normal', 'acfe'), |
| 88 | 'compact' => __('Compact', 'acfe'), |
| 89 | ), |
| 90 | 'conditional_logic' => array( |
| 91 | array( |
| 92 | array( |
| 93 | 'field' => 'version', |
| 94 | 'operator' => '==', |
| 95 | 'value' => 'v2', |
| 96 | ) |
| 97 | ) |
| 98 | ) |
| 99 | )); |
| 100 | |
| 101 | // V3 Hide Logo |
| 102 | acf_render_field_setting($field, array( |
| 103 | 'label' => __('Hide logo', 'acfe'), |
| 104 | 'instructions' => __('Hide the reCaptcha logo', 'acfe'), |
| 105 | 'type' => 'true_false', |
| 106 | 'name' => 'v3_hide_logo', |
| 107 | 'ui' => true, |
| 108 | 'conditional_logic' => array( |
| 109 | array( |
| 110 | array( |
| 111 | 'field' => 'version', |
| 112 | 'operator' => '==', |
| 113 | 'value' => 'v3', |
| 114 | ) |
| 115 | ) |
| 116 | ) |
| 117 | )); |
| 118 | |
| 119 | // Site Key |
| 120 | acf_render_field_setting($field, array( |
| 121 | 'label' => __('Site key', 'acfe'), |
| 122 | 'instructions' => __('Enter the site key. <a href="https://www.google.com/recaptcha/admin" target="_blank">reCaptcha API Admin</a>', 'acfe'), |
| 123 | 'type' => 'text', |
| 124 | 'name' => 'site_key', |
| 125 | )); |
| 126 | |
| 127 | // Site Secret |
| 128 | acf_render_field_setting($field, array( |
| 129 | 'label' => __('Secret key', 'acfe'), |
| 130 | 'instructions' => __('Enter the secret key. <a href="https://www.google.com/recaptcha/admin" target="_blank">reCaptcha API Admin</a>', 'acfe'), |
| 131 | 'type' => 'text', |
| 132 | 'name' => 'secret_key', |
| 133 | )); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * prepare_field |
| 140 | * |
| 141 | * @param $field |
| 142 | * |
| 143 | * @return array |
| 144 | */ |
| 145 | function prepare_field($field){ |
| 146 | |
| 147 | if($field['version'] === 'v3'){ |
| 148 | $field['wrapper']['class'] = 'acf-hidden'; |
| 149 | } |
| 150 | |
| 151 | return $field; |
| 152 | |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /** |
| 157 | * render_field |
| 158 | * |
| 159 | * @param $field |
| 160 | */ |
| 161 | function render_field($field){ |
| 162 | |
| 163 | // vars |
| 164 | $site_key = acf_get_setting('acfe/field/recaptcha/site_key') ? acf_get_setting('acfe/field/recaptcha/site_key') : $field['site_key']; |
| 165 | $version = acf_get_setting('acfe/field/recaptcha/version') ? acf_get_setting('acfe/field/recaptcha/version') : $field['version']; |
| 166 | |
| 167 | // wrapper attributes |
| 168 | $wrapper = array( |
| 169 | 'class' => 'acf-input-wrap', |
| 170 | 'data-site-key' => $site_key, |
| 171 | 'data-version' => $version, |
| 172 | ); |
| 173 | |
| 174 | // v2 |
| 175 | if($version === 'v2'){ |
| 176 | |
| 177 | // vars |
| 178 | $field['v2_theme'] = acf_get_setting('acfe/field/recaptcha/v2/theme') ? acf_get_setting('acfe/field/recaptcha/v2/theme') : $field['v2_theme']; |
| 179 | $field['v2_size'] = acf_get_setting('acfe/field/recaptcha/v2/size') ? acf_get_setting('acfe/field/recaptcha/v2/size') : $field['v2_size']; |
| 180 | |
| 181 | // wrapper attributes |
| 182 | $wrapper['data-size'] = $field['v2_size']; |
| 183 | $wrapper['data-theme'] = $field['v2_theme']; |
| 184 | |
| 185 | } |
| 186 | |
| 187 | // v3 |
| 188 | if($version === 'v3'){ |
| 189 | |
| 190 | $field['v3_hide_logo'] = acf_get_setting('acfe/field/recaptcha/v3/hide_logo') ? acf_get_setting('acfe/field/recaptcha/v3/hide_logo') : $field['v3_hide_logo']; |
| 191 | |
| 192 | if($field['v3_hide_logo']){ |
| 193 | $this->hide_logo = true; |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | // hidden input |
| 199 | $hidden_input = array( |
| 200 | 'id' => $field['id'], |
| 201 | 'name' => $field['name'], |
| 202 | ); |
| 203 | |
| 204 | ?> |
| 205 | <div <?php echo acf_esc_atts($wrapper); ?>> |
| 206 | |
| 207 | <?php if($version === 'v2'){ ?> |
| 208 | <div></div> |
| 209 | <?php } ?> |
| 210 | <?php acf_hidden_input($hidden_input); ?> |
| 211 | |
| 212 | </div> |
| 213 | <?php |
| 214 | |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * validate_value |
| 220 | * |
| 221 | * @param $valid |
| 222 | * @param $value |
| 223 | * @param $field |
| 224 | * @param $input |
| 225 | * |
| 226 | * @return bool|mixed|string|null |
| 227 | */ |
| 228 | function validate_value($valid, $value, $field, $input){ |
| 229 | |
| 230 | // bail early if not required |
| 231 | if(!$field['required']){ |
| 232 | return $valid; |
| 233 | } |
| 234 | |
| 235 | // bail early in ajax validation |
| 236 | // token can only be verified once then becomes invalid |
| 237 | $should_validate = apply_filters('acfe/field/recpatcha/should_validate_value', !acf_is_ajax(), $value, $field, $input); |
| 238 | if(!$should_validate){ |
| 239 | return $valid; |
| 240 | } |
| 241 | |
| 242 | // vars |
| 243 | $secret_key = acf_get_setting('acfe/field/recaptcha/secret_key') ? acf_get_setting('acfe/field/recaptcha/secret_key') : $field['secret_key']; |
| 244 | |
| 245 | // post request |
| 246 | $response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array( |
| 247 | 'body' => array( |
| 248 | 'secret' => $secret_key, |
| 249 | 'response' => $value, |
| 250 | ), |
| 251 | )); |
| 252 | |
| 253 | // validate request response |
| 254 | if(is_wp_error($response)){ |
| 255 | return __('Invalid reCaptcha, please try again', 'acfe'); |
| 256 | } |
| 257 | |
| 258 | // get response as json |
| 259 | $data = json_decode(wp_remote_retrieve_body($response)); |
| 260 | |
| 261 | // success is not true|false |
| 262 | // something went wrong |
| 263 | if(!isset($data->success)){ |
| 264 | return __('Invalid reCaptcha, please try again', 'acfe'); |
| 265 | } |
| 266 | |
| 267 | // error |
| 268 | if($data->success === false){ |
| 269 | return __('Invalid reCaptcha, please try again', 'acfe'); |
| 270 | } |
| 271 | |
| 272 | // success |
| 273 | return true; |
| 274 | |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * update_value |
| 280 | * |
| 281 | * @param $value |
| 282 | * @param $post_id |
| 283 | * @param $field |
| 284 | * |
| 285 | * @return null |
| 286 | */ |
| 287 | function update_value($value, $post_id, $field){ |
| 288 | |
| 289 | // front-end form: set flag to indicate recaptcha is present |
| 290 | if(acfe_is_local_post_id('acfe/form/validation')){ |
| 291 | acf_set_form_data('acfe/form/recaptcha', true); |
| 292 | } |
| 293 | |
| 294 | // do not save value |
| 295 | return null; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * admin_print_footer_scripts |
| 302 | * |
| 303 | * @return void |
| 304 | */ |
| 305 | function admin_print_footer_scripts(){ |
| 306 | |
| 307 | if($this->hide_logo){ |
| 308 | ?> |
| 309 | <style> |
| 310 | .grecaptcha-badge{ |
| 311 | display: none; |
| 312 | visibility: hidden; |
| 313 | } |
| 314 | </style> |
| 315 | <?php |
| 316 | } |
| 317 | |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | |
| 322 | // initialize |
| 323 | acf_register_field_type('acfe_field_recaptcha'); |
| 324 | |
| 325 | endif; |