forked from irvinlim/acf-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-recaptcha-v5.php
253 lines (193 loc) · 6.57 KB
/
acf-recaptcha-v5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
require("lib/autoload.php");
class acf_field_recaptcha extends acf_field {
/*
* __construct
*
* This function will setup the field type data
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
/*
* name (string) Single word, no spaces. Underscores allowed
*/
$this->name = 'recaptcha';
/*
* label (string) Multiple words, can include spaces, visible when selecting a field type
*/
$this->label = __('reCAPTCHA', 'acf-recaptcha');
/*
* category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
*/
$this->category = 'Custom';
/*
* defaults (array) Array of default settings which are merged into the field object. These are used later in settings
*/
$this->defaults = array(
'site_key' => '',
'secret_key' => '',
're_theme' => 'light',
're_type' => 'image',
're_size' => 'normal',
);
$this->l10n = array(
'error' => __('Please click the checkbox.', 'acf-recaptcha'),
);
// do not delete!
parent::__construct();
}
/*
* render_field_settings()
*
* Create extra settings for your field. These are visible when editing a field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field_settings( $field ) {
/*
* acf_render_field_setting
*
* This function will create a setting for your field. Simply pass the $field parameter and an array of field settings.
* The array of settings does not require a `value` or `prefix`; These settings are found from the $field array.
*
* More than one setting can be added by copy/paste the above code.
* Please note that you must also have a matching $defaults value for the field name (font_size)
*/
acf_render_field_setting( $field, array(
'label' => __('Site Key','acf-recaptcha'),
'instructions' => __('Enter your site key from Google reCAPTCHA.','acf-recaptcha'),
'name' => 'site_key',
'required' => true,
));
acf_render_field_setting( $field, array(
'label' => __('Secret Key','acf-recaptcha'),
'instructions' => __('Enter your secret key from Google reCAPTCHA.','acf-recaptcha'),
'name' => 'secret_key',
'required' => true,
));
acf_render_field_setting( $field, array(
'label' => __('Theme','acf-recaptcha'),
'type' => 'radio',
'name' => 're_theme',
'layout' => 'horizontal',
'choices' => array(
'light' => __( 'light' ),
'dark' => __( 'dark' ),
),
));
acf_render_field_setting( $field, array(
'label' => __('Type','acf-recaptcha'),
'type' => 'radio',
'name' => 're_type',
'layout' => 'horizontal',
'choices' => array(
'image' => __( 'image' ),
'audio' => __( 'audio' ),
),
));
acf_render_field_setting( $field, array(
'label' => __('Size','acf-recaptcha'),
'type' => 'radio',
'name' => 're_size',
'layout' => 'horizontal',
'choices' => array(
'normal' => __( 'normal' ),
'compact' => __( 'compact' ),
),
));
}
/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field (array) the $field being rendered
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field( $field ) {
if (is_admin()) return;
if ($field['site_key'] && $field['secret_key']): ?>
<div class="g-recaptcha" data-sitekey="<?php echo $field['site_key'];?>" data-theme="<?php echo $field['re_theme'];?>" data-type="<?php echo $field['re_type'];?>" data-size="<?php echo $field['re_size'];?>" data-callback="acf_captcha_called"></div>
<input type="hidden" name="<?php echo $field['name']?>">
<?php else :
echo "Please enter your site key and secret key first.";
endif;
}
/*
* input_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is created.
* Use this action to add CSS + JavaScript to assist your render_field() action.
*
* @type action (admin_enqueue_scripts)
* @since 3.6
* @date 23/01/13
*
* @param n/a
* @return n/a
*/
function input_admin_enqueue_scripts() {
$dir = plugin_dir_url( __FILE__ );
wp_register_script( 'acf-input-recaptcha', "{$dir}js/input.js" );
wp_enqueue_script('acf-input-recaptcha');
if (!is_admin()) wp_enqueue_script('recaptcha-api', 'https://www.google.com/recaptcha/api.js');
}
/*
* input_admin_head()
*
* This action is called in the admin_head action on the edit screen where your field is created.
* Use this action to add CSS and JavaScript to assist your render_field() action.
*
* @type action (admin_head)
* @since 3.6
* @date 23/01/13
*
* @param n/a
* @return n/a
*/
function input_admin_head() {
}
/*
* validate_value()
*
* This filter is used to perform validation on the value prior to saving.
* All values are validated regardless of the field's required setting. This allows you to validate and return
* messages to the user if the value is not correct
*
* @type filter
* @date 11/02/2014
* @since 5.0.0
*
* @param $valid (boolean) validation status based on the value and the field's required setting
* @param $value (mixed) the $_POST value
* @param $field (array) the field array holding all the field options
* @param $input (string) the corresponding input name for $_POST value
* @return $valid
*/
function validate_value( $valid, $value, $field, $input ){
if (!strlen($value)) return false;
$api = new \ReCaptcha\ReCaptcha($field['secret_key']);
$response = $api->verify($value, $_SERVER['REMOTE_ADDR']);
return $response->isSuccess();
}
}
// create field
new acf_field_recaptcha();
?>