class-acf-field.php
9.11 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
if ( ! class_exists( 'acf_field' ) ) :
class acf_field {
// vars
var $name = '',
$label = '',
$category = 'basic',
$defaults = array(),
$l10n = array(),
$public = true;
public $show_in_rest = true;
/*
* __construct
*
* This function will initialize the field type
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// initialize
$this->initialize();
// register info
acf_register_field_type_info(
array(
'label' => $this->label,
'name' => $this->name,
'category' => $this->category,
'public' => $this->public,
)
);
// value
$this->add_field_filter( 'acf/load_value', array( $this, 'load_value' ), 10, 3 );
$this->add_field_filter( 'acf/update_value', array( $this, 'update_value' ), 10, 3 );
$this->add_field_filter( 'acf/format_value', array( $this, 'format_value' ), 10, 3 );
$this->add_field_filter( 'acf/validate_value', array( $this, 'validate_value' ), 10, 4 );
$this->add_field_action( 'acf/delete_value', array( $this, 'delete_value' ), 10, 3 );
// field
$this->add_field_filter( 'acf/validate_rest_value', array( $this, 'validate_rest_value' ), 10, 3 );
$this->add_field_filter( 'acf/validate_field', array( $this, 'validate_field' ), 10, 1 );
$this->add_field_filter( 'acf/load_field', array( $this, 'load_field' ), 10, 1 );
$this->add_field_filter( 'acf/update_field', array( $this, 'update_field' ), 10, 1 );
$this->add_field_filter( 'acf/duplicate_field', array( $this, 'duplicate_field' ), 10, 1 );
$this->add_field_action( 'acf/delete_field', array( $this, 'delete_field' ), 10, 1 );
$this->add_field_action( 'acf/render_field', array( $this, 'render_field' ), 9, 1 );
$this->add_field_action( 'acf/render_field_settings', array( $this, 'render_field_settings' ), 9, 1 );
$this->add_field_action( 'acf/render_field_general_settings', array( $this, 'render_field_general_settings' ), 9, 1 );
$this->add_field_action( 'acf/render_field_validation_settings', array( $this, 'render_field_validation_settings' ), 9, 1 );
$this->add_field_action( 'acf/render_field_presentation_settings', array( $this, 'render_field_presentation_settings' ), 9, 1 );
$this->add_field_action( 'acf/render_field_conditional_logic_settings', array( $this, 'render_field_conditional_logic_settings' ), 9, 1 );
$this->add_field_filter( 'acf/prepare_field', array( $this, 'prepare_field' ), 10, 1 );
$this->add_field_filter( 'acf/translate_field', array( $this, 'translate_field' ), 10, 1 );
// input actions
$this->add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'input_admin_enqueue_scripts' ), 10, 0 );
$this->add_action( 'acf/input/admin_head', array( $this, 'input_admin_head' ), 10, 0 );
$this->add_action( 'acf/input/form_data', array( $this, 'input_form_data' ), 10, 1 );
$this->add_filter( 'acf/input/admin_l10n', array( $this, 'input_admin_l10n' ), 10, 1 );
$this->add_action( 'acf/input/admin_footer', array( $this, 'input_admin_footer' ), 10, 1 );
// field group actions
$this->add_action( 'acf/field_group/admin_enqueue_scripts', array( $this, 'field_group_admin_enqueue_scripts' ), 10, 0 );
$this->add_action( 'acf/field_group/admin_head', array( $this, 'field_group_admin_head' ), 10, 0 );
$this->add_action( 'acf/field_group/admin_footer', array( $this, 'field_group_admin_footer' ), 10, 0 );
}
/*
* initialize
*
* This function will initialize the field type
*
* @type function
* @date 27/6/17
* @since 5.6.0
*
* @param n/a
* @return n/a
*/
function initialize() {
/* do nothing */
}
/*
* add_filter
*
* This function checks if the function is_callable before adding the filter
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param $tag (string)
* @param $function_to_add (string)
* @param $priority (int)
* @param $accepted_args (int)
* @return n/a
*/
function add_filter( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
// bail early if no callable
if ( ! is_callable( $function_to_add ) ) {
return;
}
// add
add_filter( $tag, $function_to_add, $priority, $accepted_args );
}
/*
* add_field_filter
*
* This function will add a field type specific filter
*
* @type function
* @date 29/09/2016
* @since 5.4.0
*
* @param $tag (string)
* @param $function_to_add (string)
* @param $priority (int)
* @param $accepted_args (int)
* @return n/a
*/
function add_field_filter( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
// append
$tag .= '/type=' . $this->name;
// add
$this->add_filter( $tag, $function_to_add, $priority, $accepted_args );
}
/*
* add_action
*
* This function checks if the function is_callable before adding the action
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param $tag (string)
* @param $function_to_add (string)
* @param $priority (int)
* @param $accepted_args (int)
* @return n/a
*/
function add_action( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
// bail early if no callable
if ( ! is_callable( $function_to_add ) ) {
return;
}
// add
add_action( $tag, $function_to_add, $priority, $accepted_args );
}
/*
* add_field_action
*
* This function will add a field type specific filter
*
* @type function
* @date 29/09/2016
* @since 5.4.0
*
* @param $tag (string)
* @param $function_to_add (string)
* @param $priority (int)
* @param $accepted_args (int)
* @return n/a
*/
function add_field_action( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
// append
$tag .= '/type=' . $this->name;
// add
$this->add_action( $tag, $function_to_add, $priority, $accepted_args );
}
/*
* validate_field
*
* This function will append default settings to a field
*
* @type filter ("acf/validate_field/type={$this->name}")
* @since 3.6
* @date 23/01/13
*
* @param $field (array)
* @return $field (array)
*/
function validate_field( $field ) {
// bail early if no defaults
if ( ! is_array( $this->defaults ) ) {
return $field;
}
// merge in defaults but keep order of $field keys
foreach ( $this->defaults as $k => $v ) {
if ( ! isset( $field[ $k ] ) ) {
$field[ $k ] = $v;
}
}
// return
return $field;
}
/*
* admin_l10n
*
* This function will append l10n text translations to an array which is later passed to JS
*
* @type filter ("acf/input/admin_l10n")
* @since 3.6
* @date 23/01/13
*
* @param $l10n (array)
* @return $l10n (array)
*/
function input_admin_l10n( $l10n ) {
// bail early if no defaults
if ( empty( $this->l10n ) ) {
return $l10n;
}
// append
$l10n[ $this->name ] = $this->l10n;
// return
return $l10n;
}
/**
* Add additional validation for fields being updated via the REST API.
*
* @param bool $valid
* @param mixed $value
* @param array $field
*
* @return bool|WP_Error
*/
public function validate_rest_value( $valid, $value, $field ) {
return $valid;
}
/**
* Return the schema array for the REST API.
*
* @param array $field
* @return array
*/
public function get_rest_schema( array $field ) {
$schema = array(
'type' => array( 'string', 'null' ),
'required' => ! empty( $field['required'] ),
);
if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
$schema['default'] = $field['default_value'];
}
return $schema;
}
/**
* Return an array of links for addition to the REST API response. Each link is an array and must have both `rel` and
* `href` keys. The `href` key must be a REST API resource URL. If a link is marked as `embeddable`, the `_embed` URL
* parameter will trigger WordPress to dispatch an internal sub request and load the object within the same request
* under the `_embedded` response property.
*
* e.g;
* [
* [
* 'rel' => 'acf:post',
* 'href' => 'https://example.com/wp-json/wp/v2/posts/497',
* 'embeddable' => true,
* ],
* [
* 'rel' => 'acf:user',
* 'href' => 'https://example.com/wp-json/wp/v2/users/2',
* 'embeddable' => true,
* ],
* ]
*
* @param mixed $value The raw (unformatted) field value.
* @param string|int $post_id
* @param array $field
* @return array
*/
public function get_rest_links( $value, $post_id, array $field ) {
return array();
}
/**
* Apply basic formatting to prepare the value for default REST output.
*
* @param mixed $value
* @param string|int $post_id
* @param array $field
* @return mixed
*/
public function format_value_for_rest( $value, $post_id, array $field ) {
return $value;
}
}
endif; // class_exists check