1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* ACF Taxonomy Field Class |
5
|
|
|
* |
6
|
|
|
* All the logic for this field type |
7
|
|
|
* |
8
|
|
|
* @class acf_field_tab |
9
|
|
|
* @extends acf_field |
10
|
|
|
* @package ACF |
11
|
|
|
* @subpackage Fields |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if( ! class_exists('acf_field_taxonomy') ) : |
15
|
|
|
|
16
|
|
|
class acf_field_taxonomy extends acf_field { |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/* |
20
|
|
|
* __construct |
21
|
|
|
* |
22
|
|
|
* This function will setup the field type data |
23
|
|
|
* |
24
|
|
|
* @type function |
25
|
|
|
* @date 5/03/2014 |
26
|
|
|
* @since 5.0.0 |
27
|
|
|
* |
28
|
|
|
* @param n/a |
29
|
|
|
* @return n/a |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
|
32
|
|
View Code Duplication |
function __construct() { |
|
|
|
|
33
|
|
|
|
34
|
|
|
// vars |
35
|
|
|
$this->name = 'taxonomy'; |
|
|
|
|
36
|
|
|
$this->label = __("Taxonomy",'acf'); |
|
|
|
|
37
|
|
|
$this->category = 'relational'; |
|
|
|
|
38
|
|
|
$this->defaults = array( |
|
|
|
|
39
|
|
|
'taxonomy' => 'category', |
40
|
|
|
'field_type' => 'checkbox', |
41
|
|
|
'multiple' => 0, |
42
|
|
|
'allow_null' => 0, |
43
|
|
|
//'load_save_terms' => 0, // removed in 5.2.7 |
|
|
|
|
44
|
|
|
'return_format' => 'id', |
45
|
|
|
'add_term' => 1, // 5.2.3 |
46
|
|
|
'load_terms' => 0, // 5.2.7 |
47
|
|
|
'save_terms' => 0 // 5.2.7 |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
// extra |
52
|
|
|
add_action('wp_ajax_acf/fields/taxonomy/query', array($this, 'ajax_query')); |
53
|
|
|
add_action('wp_ajax_nopriv_acf/fields/taxonomy/query', array($this, 'ajax_query')); |
54
|
|
|
add_action('wp_ajax_acf/fields/taxonomy/add_term', array($this, 'ajax_add_term')); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
// do not delete! |
58
|
|
|
parent::__construct(); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/* |
64
|
|
|
* get_choices |
65
|
|
|
* |
66
|
|
|
* This function will return an array of data formatted for use in a select2 AJAX response |
67
|
|
|
* |
68
|
|
|
* @type function |
69
|
|
|
* @date 15/10/2014 |
70
|
|
|
* @since 5.0.9 |
71
|
|
|
* |
72
|
|
|
* @param $options (array) |
73
|
|
|
* @return (array) |
74
|
|
|
*/ |
75
|
|
|
|
76
|
|
|
function get_choices( $options = array() ) { |
|
|
|
|
77
|
|
|
|
78
|
|
|
// defaults |
79
|
|
|
$options = acf_parse_args($options, array( |
80
|
|
|
'post_id' => 0, |
81
|
|
|
's' => '', |
82
|
|
|
'field_key' => '', |
83
|
|
|
'paged' => 0 |
84
|
|
|
)); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
// load field |
88
|
|
|
$field = acf_get_field( $options['field_key'] ); |
89
|
|
|
|
90
|
|
|
if( !$field ) { |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
// vars |
98
|
|
|
$r = array(); |
99
|
|
|
$args = array(); |
100
|
|
|
$is_hierarchical = is_taxonomy_hierarchical( $field['taxonomy'] ); |
101
|
|
|
$is_pagination = ($options['paged'] > 0); |
102
|
|
|
$limit = 20; |
103
|
|
|
$offset = 20 * ($options['paged'] - 1); |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
// hide empty |
107
|
|
|
$args['hide_empty'] = false; |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
// pagination |
111
|
|
|
// - don't bother for hierarchial terms, we will need to load all terms anyway |
112
|
|
|
if( !$is_hierarchical && $is_pagination ) { |
113
|
|
|
|
114
|
|
|
$args['offset'] = $offset; |
115
|
|
|
$args['number'] = $limit; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
// search |
121
|
|
|
if( $options['s'] ) { |
122
|
|
|
|
123
|
|
|
$args['search'] = $options['s']; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
// filters |
129
|
|
|
$args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']); |
130
|
|
|
$args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id'] ); |
131
|
|
|
$args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id'] ); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
// get terms |
135
|
|
|
$terms = get_terms( $field['taxonomy'], $args ); |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
// sort into hierachial order! |
139
|
|
|
if( $is_hierarchical ) { |
140
|
|
|
|
141
|
|
|
// get parent |
142
|
|
|
$parent = acf_maybe_get( $args, 'parent', 0 ); |
143
|
|
|
$parent = acf_maybe_get( $args, 'child_of', $parent ); |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
// this will fail if a search has taken place because parents wont exist |
147
|
|
|
if( empty($args['search']) ) { |
148
|
|
|
|
149
|
|
|
$terms = _get_term_children( $parent, $terms, $field['taxonomy'] ); |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
// fake pagination |
155
|
|
|
if( $is_pagination ) { |
156
|
|
|
|
157
|
|
|
$terms = array_slice($terms, $offset, $limit); |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/// append to r |
165
|
|
|
foreach( $terms as $term ) { |
166
|
|
|
|
167
|
|
|
// add to json |
168
|
|
|
$r[] = array( |
169
|
|
|
'id' => $term->term_id, |
170
|
|
|
'text' => $this->get_term_title( $term, $field, $options['post_id'] ) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
// return |
177
|
|
|
return $r; |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/* |
183
|
|
|
* ajax_query |
184
|
|
|
* |
185
|
|
|
* description |
186
|
|
|
* |
187
|
|
|
* @type function |
188
|
|
|
* @date 24/10/13 |
189
|
|
|
* @since 5.0.0 |
190
|
|
|
* |
191
|
|
|
* @param $post_id (int) |
192
|
|
|
* @return $post_id (int) |
193
|
|
|
*/ |
|
|
|
|
194
|
|
|
|
195
|
|
View Code Duplication |
function ajax_query() { |
|
|
|
|
196
|
|
|
|
197
|
|
|
// validate |
198
|
|
|
if( !acf_verify_ajax() ) { |
199
|
|
|
|
200
|
|
|
die(); |
|
|
|
|
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
// get choices |
206
|
|
|
$choices = $this->get_choices( $_POST ); |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
// validate |
210
|
|
|
if( !$choices ) { |
211
|
|
|
|
212
|
|
|
die(); |
|
|
|
|
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
// return JSON |
218
|
|
|
echo json_encode( $choices ); |
219
|
|
|
die(); |
|
|
|
|
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/* |
225
|
|
|
* get_term_title |
226
|
|
|
* |
227
|
|
|
* This function returns the HTML for a result |
228
|
|
|
* |
229
|
|
|
* @type function |
230
|
|
|
* @date 1/11/2013 |
231
|
|
|
* @since 5.0.0 |
232
|
|
|
* |
233
|
|
|
* @param $post (object) |
234
|
|
|
* @param $field (array) |
235
|
|
|
* @param $post_id (int) the post_id to which this value is saved to |
236
|
|
|
* @return (string) |
237
|
|
|
*/ |
238
|
|
|
|
239
|
|
|
function get_term_title( $term, $field, $post_id = 0 ) { |
|
|
|
|
240
|
|
|
|
241
|
|
|
// get post_id |
242
|
|
|
if( !$post_id ) { |
243
|
|
|
|
244
|
|
|
$form_data = acf_get_setting('form_data'); |
245
|
|
|
|
246
|
|
|
if( !empty($form_data['post_id']) ) { |
247
|
|
|
|
248
|
|
|
$post_id = $form_data['post_id']; |
249
|
|
|
|
250
|
|
|
} else { |
251
|
|
|
|
252
|
|
|
$post_id = get_the_ID(); |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
// vars |
259
|
|
|
$title = ''; |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
// ancestors |
263
|
|
|
$ancestors = get_ancestors( $term->term_id, $field['taxonomy'] ); |
264
|
|
|
|
265
|
|
|
if( !empty($ancestors) ) { |
266
|
|
|
|
267
|
|
|
$title .= str_repeat('- ', count($ancestors)); |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
// title |
273
|
|
|
$title .= $term->name; |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
// filters |
277
|
|
|
$title = apply_filters('acf/fields/taxonomy/result', $title, $term, $field, $post_id); |
278
|
|
|
$title = apply_filters('acf/fields/taxonomy/result/name=' . $field['_name'] , $title, $term, $field, $post_id); |
279
|
|
|
$title = apply_filters('acf/fields/taxonomy/result/key=' . $field['key'], $title, $term, $field, $post_id); |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
// return |
283
|
|
|
return $title; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/* |
288
|
|
|
* get_terms |
289
|
|
|
* |
290
|
|
|
* This function will return an array of terms for a given field value |
291
|
|
|
* |
292
|
|
|
* @type function |
293
|
|
|
* @date 13/06/2014 |
294
|
|
|
* @since 5.0.0 |
295
|
|
|
* |
296
|
|
|
* @param $value (array) |
297
|
|
|
* @return $value |
298
|
|
|
*/ |
|
|
|
|
299
|
|
|
|
300
|
|
|
function get_terms( $value, $taxonomy = 'category' ) { |
|
|
|
|
301
|
|
|
|
302
|
|
|
// load terms in 1 query to save multiple DB calls from following code |
303
|
|
|
if( count($value) > 1 ) { |
304
|
|
|
|
305
|
|
|
$terms = get_terms($taxonomy, array( |
|
|
|
|
306
|
|
|
'hide_empty' => false, |
307
|
|
|
'include' => $value, |
308
|
|
|
)); |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
// update value to include $post |
314
|
|
|
foreach( array_keys($value) as $i ) { |
315
|
|
|
|
316
|
|
|
$value[ $i ] = get_term( $value[ $i ], $taxonomy ); |
317
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
// filter out null values |
322
|
|
|
$value = array_filter($value); |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
// return |
326
|
|
|
return $value; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
/* |
331
|
|
|
* load_value() |
332
|
|
|
* |
333
|
|
|
* This filter is appied to the $value after it is loaded from the db |
334
|
|
|
* |
335
|
|
|
* @type filter |
336
|
|
|
* @since 3.6 |
337
|
|
|
* @date 23/01/13 |
338
|
|
|
* |
339
|
|
|
* @param $value - the value found in the database |
340
|
|
|
* @param $post_id - the $post_id from which the value was loaded from |
341
|
|
|
* @param $field - the field array holding all the field options |
342
|
|
|
* |
343
|
|
|
* @return $value - the value to be saved in te database |
344
|
|
|
*/ |
|
|
|
|
345
|
|
|
|
346
|
|
|
function load_value( $value, $post_id, $field ) { |
|
|
|
|
347
|
|
|
|
348
|
|
|
// get valid terms |
349
|
|
|
$value = acf_get_valid_terms($value, $field['taxonomy']); |
350
|
|
|
|
351
|
|
|
|
352
|
|
|
// load/save |
353
|
|
|
if( $field['load_terms'] ) { |
354
|
|
|
|
355
|
|
|
// get terms |
356
|
|
|
$term_ids = wp_get_object_terms($post_id, $field['taxonomy'], array('fields' => 'ids', 'orderby' => 'none')); |
357
|
|
|
|
358
|
|
|
|
359
|
|
|
// error |
360
|
|
|
if( is_wp_error($term_ids) ) { |
361
|
|
|
|
362
|
|
|
return false; |
363
|
|
|
|
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
// return |
368
|
|
|
return $term_ids; |
369
|
|
|
|
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
// return |
374
|
|
|
return $value; |
375
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
/* |
380
|
|
|
* update_value() |
381
|
|
|
* |
382
|
|
|
* This filter is appied to the $value before it is updated in the db |
383
|
|
|
* |
384
|
|
|
* @type filter |
385
|
|
|
* @since 3.6 |
386
|
|
|
* @date 23/01/13 |
387
|
|
|
* |
388
|
|
|
* @param $value - the value which will be saved in the database |
389
|
|
|
* @param $field - the field array holding all the field options |
390
|
|
|
* @param $post_id - the $post_id of which the value will be saved |
391
|
|
|
* |
392
|
|
|
* @return $value - the modified value |
393
|
|
|
*/ |
|
|
|
|
394
|
|
|
|
395
|
|
|
function update_value( $value, $post_id, $field ) { |
|
|
|
|
396
|
|
|
|
397
|
|
|
// vars |
398
|
|
|
if( is_array($value) ) { |
399
|
|
|
|
400
|
|
|
$value = array_filter($value); |
401
|
|
|
|
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
|
405
|
|
|
// save_terms |
406
|
|
|
if( $field['save_terms'] ) { |
407
|
|
|
|
408
|
|
|
// vars |
409
|
|
|
$taxonomy = $field['taxonomy']; |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
// force value to array |
413
|
|
|
$term_ids = acf_get_array( $value ); |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
// convert to int |
417
|
|
|
$term_ids = array_map('intval', $term_ids); |
418
|
|
|
|
419
|
|
|
|
420
|
|
|
// bypass $this->set_terms if called directly from update_field |
421
|
|
|
if( !did_action('acf/save_post') ) { |
422
|
|
|
|
423
|
|
|
wp_set_object_terms( $post_id, $term_ids, $taxonomy, false ); |
424
|
|
|
|
425
|
|
|
return $value; |
426
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
// initialize |
431
|
|
|
if( empty($this->set_terms) ) { |
432
|
|
|
|
433
|
|
|
// create holder |
434
|
|
|
$this->set_terms = array(); |
|
|
|
|
435
|
|
|
|
436
|
|
|
|
437
|
|
|
// add action |
438
|
|
|
add_action('acf/save_post', array($this, 'set_terms'), 15, 1); |
439
|
|
|
|
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
|
443
|
|
|
// append |
444
|
|
|
if( empty($this->set_terms[ $taxonomy ]) ) { |
445
|
|
|
|
446
|
|
|
$this->set_terms[ $taxonomy ] = array(); |
447
|
|
|
|
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
$this->set_terms[ $taxonomy ] = array_merge($this->set_terms[ $taxonomy ], $term_ids); |
451
|
|
|
|
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
|
455
|
|
|
// return |
456
|
|
|
return $value; |
457
|
|
|
|
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
|
461
|
|
|
/* |
462
|
|
|
* set_terms |
463
|
|
|
* |
464
|
|
|
* description |
465
|
|
|
* |
466
|
|
|
* @type function |
467
|
|
|
* @date 26/11/2014 |
468
|
|
|
* @since 5.0.9 |
469
|
|
|
* |
470
|
|
|
* @param $post_id (int) |
471
|
|
|
* @return $post_id (int) |
472
|
|
|
*/ |
|
|
|
|
473
|
|
|
|
474
|
|
|
function set_terms( $post_id ) { |
|
|
|
|
475
|
|
|
|
476
|
|
|
// bail ealry if no terms |
477
|
|
|
if( empty($this->set_terms) ) { |
478
|
|
|
|
479
|
|
|
return; |
480
|
|
|
|
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
|
484
|
|
|
// loop over terms |
485
|
|
|
foreach( $this->set_terms as $taxonomy => $term_ids ){ |
486
|
|
|
|
487
|
|
|
wp_set_object_terms( $post_id, $term_ids, $taxonomy, false ); |
488
|
|
|
|
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
|
492
|
|
|
// reset array ( WP saves twice ) |
493
|
|
|
$this->set_terms = array(); |
494
|
|
|
|
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
/* |
499
|
|
|
* format_value() |
500
|
|
|
* |
501
|
|
|
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
502
|
|
|
* |
503
|
|
|
* @type filter |
504
|
|
|
* @since 3.6 |
505
|
|
|
* @date 23/01/13 |
506
|
|
|
* |
507
|
|
|
* @param $value (mixed) the value which was loaded from the database |
508
|
|
|
* @param $post_id (mixed) the $post_id from which the value was loaded |
509
|
|
|
* @param $field (array) the field array holding all the field options |
510
|
|
|
* |
511
|
|
|
* @return $value (mixed) the modified value |
512
|
|
|
*/ |
|
|
|
|
513
|
|
|
|
514
|
|
|
function format_value( $value, $post_id, $field ) { |
|
|
|
|
515
|
|
|
|
516
|
|
|
// bail early if no value |
517
|
|
|
if( empty($value) ) { |
518
|
|
|
|
519
|
|
|
return $value; |
520
|
|
|
|
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
|
524
|
|
|
// force value to array |
525
|
|
|
$value = acf_get_array( $value ); |
526
|
|
|
|
527
|
|
|
|
528
|
|
|
// convert values to int |
529
|
|
|
$value = array_map('intval', $value); |
530
|
|
|
|
531
|
|
|
|
532
|
|
|
// load posts if needed |
533
|
|
|
if( $field['return_format'] == 'object' ) { |
534
|
|
|
|
535
|
|
|
// get posts |
536
|
|
|
$value = $this->get_terms( $value, $field["taxonomy"] ); |
537
|
|
|
|
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
|
541
|
|
|
// convert back from array if neccessary |
542
|
|
|
if( $field['field_type'] == 'select' || $field['field_type'] == 'radio' ) { |
543
|
|
|
|
544
|
|
|
$value = array_shift($value); |
545
|
|
|
|
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
|
549
|
|
|
// return |
550
|
|
|
return $value; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
|
554
|
|
|
/* |
555
|
|
|
* render_field() |
556
|
|
|
* |
557
|
|
|
* Create the HTML interface for your field |
558
|
|
|
* |
559
|
|
|
* @type action |
560
|
|
|
* @since 3.6 |
561
|
|
|
* @date 23/01/13 |
562
|
|
|
* |
563
|
|
|
* @param $field - an array holding all the field's data |
564
|
|
|
*/ |
565
|
|
|
|
566
|
|
|
function render_field( $field ) { |
|
|
|
|
567
|
|
|
|
568
|
|
|
// force value to array |
569
|
|
|
$field['value'] = acf_get_array( $field['value'] ); |
570
|
|
|
|
571
|
|
|
|
572
|
|
|
// convert values to int |
573
|
|
|
$field['value'] = array_map('intval', $field['value']); |
574
|
|
|
|
575
|
|
|
|
576
|
|
|
// vars |
577
|
|
|
$div = array( |
578
|
|
|
'class' => 'acf-taxonomy-field acf-soh', |
579
|
|
|
'data-save' => $field['save_terms'], |
580
|
|
|
'data-type' => $field['field_type'], |
581
|
|
|
'data-taxonomy' => $field['taxonomy'] |
582
|
|
|
); |
583
|
|
|
|
584
|
|
|
|
585
|
|
|
// get taxonomy |
586
|
|
|
$taxonomy = get_taxonomy( $field['taxonomy'] ); |
587
|
|
|
|
588
|
|
|
?> |
589
|
|
|
<div <?php acf_esc_attr_e($div); ?>> |
590
|
|
|
<?php if( $field['add_term'] && current_user_can( $taxonomy->cap->manage_terms) ): ?> |
591
|
|
|
<a href="#" class="acf-icon -plus acf-js-tooltip small acf-soh-target" data-name="add" title="<?php echo esc_attr($taxonomy->labels->add_new_item); ?>"></a> |
592
|
|
|
<?php endif; |
593
|
|
|
|
594
|
|
|
if( $field['field_type'] == 'select' ) { |
595
|
|
|
|
596
|
|
|
$field['multiple'] = 0; |
597
|
|
|
|
598
|
|
|
$this->render_field_select( $field ); |
599
|
|
|
|
600
|
|
|
} elseif( $field['field_type'] == 'multi_select' ) { |
601
|
|
|
|
602
|
|
|
$field['multiple'] = 1; |
603
|
|
|
|
604
|
|
|
$this->render_field_select( $field ); |
605
|
|
|
|
606
|
|
|
} elseif( $field['field_type'] == 'radio' ) { |
607
|
|
|
|
608
|
|
|
$this->render_field_checkbox( $field ); |
609
|
|
|
|
610
|
|
|
} elseif( $field['field_type'] == 'checkbox' ) { |
611
|
|
|
|
612
|
|
|
$this->render_field_checkbox( $field ); |
613
|
|
|
|
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
?> |
617
|
|
|
</div><?php |
618
|
|
|
|
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
|
622
|
|
|
/* |
623
|
|
|
* render_field_select() |
624
|
|
|
* |
625
|
|
|
* Create the HTML interface for your field |
626
|
|
|
* |
627
|
|
|
* @type action |
628
|
|
|
* @since 3.6 |
629
|
|
|
* @date 23/01/13 |
630
|
|
|
* |
631
|
|
|
* @param $field - an array holding all the field's data |
632
|
|
|
*/ |
633
|
|
|
|
634
|
|
|
function render_field_select( $field ) { |
|
|
|
|
635
|
|
|
|
636
|
|
|
// Change Field into a select |
637
|
|
|
$field['type'] = 'select'; |
638
|
|
|
$field['ui'] = 1; |
639
|
|
|
$field['ajax'] = 1; |
640
|
|
|
$field['choices'] = array(); |
641
|
|
|
|
642
|
|
|
|
643
|
|
|
// value |
644
|
|
|
if( !empty($field['value']) ) { |
645
|
|
|
|
646
|
|
|
// get terms |
647
|
|
|
$terms = $this->get_terms( $field['value'], $field['taxonomy'] ); |
648
|
|
|
|
649
|
|
|
|
650
|
|
|
// set choices |
651
|
|
|
if( !empty($terms) ) { |
652
|
|
|
|
653
|
|
|
foreach( array_keys($terms) as $i ) { |
654
|
|
|
|
655
|
|
|
// vars |
656
|
|
|
$term = acf_extract_var( $terms, $i ); |
657
|
|
|
|
658
|
|
|
|
659
|
|
|
// append to choices |
660
|
|
|
$field['choices'][ $term->term_id ] = $this->get_term_title( $term, $field ); |
661
|
|
|
|
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
|
669
|
|
|
// render select |
670
|
|
|
acf_render_field( $field ); |
|
|
|
|
671
|
|
|
|
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
|
675
|
|
|
/* |
676
|
|
|
* render_field_checkbox() |
677
|
|
|
* |
678
|
|
|
* Create the HTML interface for your field |
679
|
|
|
* |
680
|
|
|
* @type action |
681
|
|
|
* @since 3.6 |
682
|
|
|
* @date 23/01/13 |
683
|
|
|
* |
684
|
|
|
* @param $field - an array holding all the field's data |
685
|
|
|
*/ |
686
|
|
|
|
687
|
|
|
function render_field_checkbox( $field ) { |
|
|
|
|
688
|
|
|
|
689
|
|
|
// hidden input |
690
|
|
|
acf_hidden_input(array( |
691
|
|
|
'type' => 'hidden', |
692
|
|
|
'name' => $field['name'], |
693
|
|
|
)); |
694
|
|
|
|
695
|
|
|
|
696
|
|
|
// checkbox saves an array |
697
|
|
|
if( $field['field_type'] == 'checkbox' ) { |
698
|
|
|
|
699
|
|
|
$field['name'] .= '[]'; |
700
|
|
|
|
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
|
704
|
|
|
// taxonomy |
705
|
|
|
$taxonomy_obj = get_taxonomy($field['taxonomy']); |
706
|
|
|
|
707
|
|
|
|
708
|
|
|
// vars |
709
|
|
|
$args = array( |
710
|
|
|
'taxonomy' => $field['taxonomy'], |
711
|
|
|
'show_option_none' => __('No', 'acf') . ' ' . $taxonomy_obj->labels->name, |
712
|
|
|
'hide_empty' => false, |
713
|
|
|
'style' => 'none', |
714
|
|
|
'walker' => new acf_taxonomy_field_walker( $field ), |
715
|
|
|
); |
716
|
|
|
|
717
|
|
|
|
718
|
|
|
// filter for 3rd party customization |
719
|
|
|
$args = apply_filters('acf/fields/taxonomy/wp_list_categories', $args, $field); |
720
|
|
|
$args = apply_filters('acf/fields/taxonomy/wp_list_categories/name=' . $field['_name'], $args, $field); |
721
|
|
|
$args = apply_filters('acf/fields/taxonomy/wp_list_categories/key=' . $field['key'], $args, $field); |
722
|
|
|
|
723
|
|
|
?><div class="categorychecklist-holder"> |
724
|
|
|
|
725
|
|
|
<ul class="acf-checkbox-list acf-bl"> |
726
|
|
|
|
727
|
|
|
<?php if( $field['field_type'] == 'radio' && $field['allow_null'] ): ?> |
728
|
|
|
<li> |
729
|
|
|
<label class="selectit"> |
730
|
|
|
<input type="radio" name="<?php echo $field['name']; ?>" value="" /> <?php _e("None", 'acf'); ?> |
731
|
|
|
</label> |
732
|
|
|
</li> |
733
|
|
|
<?php endif; ?> |
734
|
|
|
|
735
|
|
|
<?php wp_list_categories( $args ); ?> |
736
|
|
|
|
737
|
|
|
</ul> |
738
|
|
|
|
739
|
|
|
</div><?php |
740
|
|
|
|
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
|
744
|
|
|
/* |
745
|
|
|
* render_field_settings() |
746
|
|
|
* |
747
|
|
|
* Create extra options for your field. This is rendered when editing a field. |
748
|
|
|
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
749
|
|
|
* |
750
|
|
|
* @type action |
751
|
|
|
* @since 3.6 |
752
|
|
|
* @date 23/01/13 |
753
|
|
|
* |
754
|
|
|
* @param $field - an array holding all the field's data |
755
|
|
|
*/ |
756
|
|
|
|
757
|
|
|
function render_field_settings( $field ) { |
|
|
|
|
758
|
|
|
|
759
|
|
|
// default_value |
760
|
|
|
acf_render_field_setting( $field, array( |
761
|
|
|
'label' => __('Taxonomy','acf'), |
762
|
|
|
'instructions' => __('Select the taxonomy to be displayed','acf'), |
763
|
|
|
'type' => 'select', |
764
|
|
|
'name' => 'taxonomy', |
765
|
|
|
'choices' => acf_get_taxonomies(), |
766
|
|
|
)); |
767
|
|
|
|
768
|
|
|
|
769
|
|
|
// field_type |
770
|
|
|
acf_render_field_setting( $field, array( |
771
|
|
|
'label' => __('Appearance','acf'), |
772
|
|
|
'instructions' => __('Select the appearance of this field','acf'), |
773
|
|
|
'type' => 'select', |
774
|
|
|
'name' => 'field_type', |
775
|
|
|
'optgroup' => true, |
776
|
|
|
'choices' => array( |
777
|
|
|
__("Multiple Values",'acf') => array( |
778
|
|
|
'checkbox' => __('Checkbox', 'acf'), |
779
|
|
|
'multi_select' => __('Multi Select', 'acf') |
780
|
|
|
), |
781
|
|
|
__("Single Value",'acf') => array( |
782
|
|
|
'radio' => __('Radio Buttons', 'acf'), |
783
|
|
|
'select' => __('Select', 'acf') |
784
|
|
|
) |
785
|
|
|
) |
786
|
|
|
)); |
787
|
|
|
|
788
|
|
|
|
789
|
|
|
// allow_null |
790
|
|
|
acf_render_field_setting( $field, array( |
791
|
|
|
'label' => __('Allow Null?','acf'), |
792
|
|
|
'instructions' => '', |
793
|
|
|
'type' => 'radio', |
794
|
|
|
'name' => 'allow_null', |
795
|
|
|
'choices' => array( |
796
|
|
|
1 => __("Yes",'acf'), |
797
|
|
|
0 => __("No",'acf'), |
798
|
|
|
), |
799
|
|
|
'layout' => 'horizontal', |
800
|
|
|
)); |
801
|
|
|
|
802
|
|
|
|
803
|
|
|
// add_term |
804
|
|
|
acf_render_field_setting( $field, array( |
805
|
|
|
'label' => __('Create Terms','acf'), |
806
|
|
|
'instructions' => __('Allow new terms to be created whilst editing','acf'), |
807
|
|
|
'type' => 'radio', |
808
|
|
|
'name' => 'add_term', |
809
|
|
|
'choices' => array( |
810
|
|
|
1 => __("Yes",'acf'), |
811
|
|
|
0 => __("No",'acf'), |
812
|
|
|
), |
813
|
|
|
'layout' => 'horizontal', |
814
|
|
|
)); |
815
|
|
|
|
816
|
|
|
|
817
|
|
|
// save_terms |
818
|
|
|
acf_render_field_setting( $field, array( |
819
|
|
|
'label' => __('Save Terms','acf'), |
820
|
|
|
'instructions' => __('Connect selected terms to the post','acf'), |
821
|
|
|
'type' => 'radio', |
822
|
|
|
'name' => 'save_terms', |
823
|
|
|
'choices' => array( |
824
|
|
|
1 => __("Yes",'acf'), |
825
|
|
|
0 => __("No",'acf'), |
826
|
|
|
), |
827
|
|
|
'layout' => 'horizontal', |
828
|
|
|
)); |
829
|
|
|
|
830
|
|
|
|
831
|
|
|
// load_terms |
832
|
|
|
acf_render_field_setting( $field, array( |
833
|
|
|
'label' => __('Load Terms','acf'), |
834
|
|
|
'instructions' => __('Load value from posts terms','acf'), |
835
|
|
|
'type' => 'radio', |
836
|
|
|
'name' => 'load_terms', |
837
|
|
|
'choices' => array( |
838
|
|
|
1 => __("Yes",'acf'), |
839
|
|
|
0 => __("No",'acf'), |
840
|
|
|
), |
841
|
|
|
'layout' => 'horizontal', |
842
|
|
|
)); |
843
|
|
|
|
844
|
|
|
|
845
|
|
|
// return_format |
846
|
|
|
acf_render_field_setting( $field, array( |
847
|
|
|
'label' => __('Return Value','acf'), |
848
|
|
|
'instructions' => '', |
849
|
|
|
'type' => 'radio', |
850
|
|
|
'name' => 'return_format', |
851
|
|
|
'choices' => array( |
852
|
|
|
'object' => __("Term Object",'acf'), |
853
|
|
|
'id' => __("Term ID",'acf') |
854
|
|
|
), |
855
|
|
|
'layout' => 'horizontal', |
856
|
|
|
)); |
857
|
|
|
|
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
|
861
|
|
|
/* |
862
|
|
|
* ajax_add_term |
863
|
|
|
* |
864
|
|
|
* description |
865
|
|
|
* |
866
|
|
|
* @type function |
867
|
|
|
* @date 17/04/2015 |
868
|
|
|
* @since 5.2.3 |
869
|
|
|
* |
870
|
|
|
* @param $post_id (int) |
871
|
|
|
* @return $post_id (int) |
872
|
|
|
*/ |
|
|
|
|
873
|
|
|
|
874
|
|
|
function ajax_add_term() { |
|
|
|
|
875
|
|
|
|
876
|
|
|
// vars |
877
|
|
|
$args = acf_parse_args($_POST, array( |
878
|
|
|
'nonce' => '', |
879
|
|
|
'field_key' => '', |
880
|
|
|
'term_name' => '', |
881
|
|
|
'term_parent' => '' |
882
|
|
|
)); |
883
|
|
|
|
884
|
|
|
|
885
|
|
|
// verify nonce |
886
|
|
|
if( ! wp_verify_nonce($args['nonce'], 'acf_nonce') ) { |
887
|
|
|
|
888
|
|
|
die(); |
|
|
|
|
889
|
|
|
|
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
|
893
|
|
|
// load field |
894
|
|
|
$field = acf_get_field( $args['field_key'] ); |
895
|
|
|
|
896
|
|
|
if( !$field ) { |
897
|
|
|
|
898
|
|
|
die(); |
|
|
|
|
899
|
|
|
|
900
|
|
|
} |
901
|
|
|
|
902
|
|
|
|
903
|
|
|
// vars |
904
|
|
|
$taxonomy_obj = get_taxonomy($field['taxonomy']); |
905
|
|
|
$taxonomy_label = $taxonomy_obj->labels->singular_name; |
906
|
|
|
|
907
|
|
|
|
908
|
|
|
// validate cap |
909
|
|
|
// note: this situation should never occur due to condition of the add new button |
910
|
|
|
if( !current_user_can( $taxonomy_obj->cap->manage_terms) ) { |
911
|
|
|
|
912
|
|
|
echo '<p><strong>' . __("Error", 'acf') . '.</strong> ' . sprintf( __('User unable to add new %s', 'acf'), $taxonomy_label ) . '</p>'; |
913
|
|
|
die; |
|
|
|
|
914
|
|
|
|
915
|
|
|
} |
916
|
|
|
|
917
|
|
|
|
918
|
|
|
// save? |
919
|
|
|
if( $args['term_name'] ) { |
920
|
|
|
|
921
|
|
|
// exists |
922
|
|
|
if( term_exists($args['term_name'], $field['taxonomy']) ) { |
923
|
|
|
|
924
|
|
|
wp_send_json_error(array( |
925
|
|
|
'error' => sprintf( __('%s already exists', 'acf'), $taxonomy_label ) |
926
|
|
|
)); |
927
|
|
|
|
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
|
931
|
|
|
// insert |
932
|
|
|
$extra = array(); |
933
|
|
|
|
934
|
|
|
if( $args['term_parent'] ) { |
935
|
|
|
|
936
|
|
|
$extra['parent'] = $args['term_parent']; |
937
|
|
|
|
938
|
|
|
} |
939
|
|
|
|
940
|
|
|
$data = wp_insert_term( $args['term_name'], $field['taxonomy'], $extra ); |
941
|
|
|
|
942
|
|
|
|
943
|
|
|
// error? |
944
|
|
|
if( is_wp_error($data) ) { |
945
|
|
|
|
946
|
|
|
wp_send_json_error(array( |
947
|
|
|
'error' => $data->get_error_message() |
948
|
|
|
)); |
949
|
|
|
|
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
|
953
|
|
|
// ancestors |
954
|
|
|
$prefix = ''; |
955
|
|
|
$ancestors = get_ancestors( $data['term_id'], $field['taxonomy'] ); |
956
|
|
|
|
957
|
|
|
if( !empty($ancestors) ) { |
958
|
|
|
|
959
|
|
|
$prefix = str_repeat('- ', count($ancestors)); |
960
|
|
|
|
961
|
|
|
} |
962
|
|
|
|
963
|
|
|
|
964
|
|
|
// success |
965
|
|
|
wp_send_json_success(array( |
966
|
|
|
'message' => sprintf( __('%s added', 'acf'), $taxonomy_label ), |
967
|
|
|
'term_id' => $data['term_id'], |
968
|
|
|
'term_name' => $args['term_name'], |
969
|
|
|
'term_label' => $prefix . $args['term_name'], |
970
|
|
|
'term_parent' => $args['term_parent'] |
971
|
|
|
)); |
972
|
|
|
|
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
?><form method="post"><?php |
976
|
|
|
|
977
|
|
|
acf_render_field_wrap(array( |
978
|
|
|
'label' => 'Name', |
979
|
|
|
'name' => 'term_name', |
980
|
|
|
'type' => 'text' |
981
|
|
|
)); |
982
|
|
|
|
983
|
|
|
|
984
|
|
|
if( is_taxonomy_hierarchical( $field['taxonomy'] ) ) { |
985
|
|
|
|
986
|
|
|
$choices = array(); |
987
|
|
|
$choices2 = $this->get_choices(array( 'field_key' => $field['key'] )); |
988
|
|
|
|
989
|
|
|
if( $choices2 ) { |
990
|
|
|
|
991
|
|
|
foreach( $choices2 as $v) { |
992
|
|
|
|
993
|
|
|
$choices[ $v['id'] ] = $v['text']; |
994
|
|
|
|
995
|
|
|
} |
996
|
|
|
|
997
|
|
|
} |
998
|
|
|
|
999
|
|
|
acf_render_field_wrap(array( |
1000
|
|
|
'label' => 'Parent', |
1001
|
|
|
'name' => 'term_parent', |
1002
|
|
|
'type' => 'select', |
1003
|
|
|
'allow_null' => 1, |
1004
|
|
|
'ui' => 0, |
1005
|
|
|
'choices' => $choices |
1006
|
|
|
)); |
1007
|
|
|
|
1008
|
|
|
} |
1009
|
|
|
|
1010
|
|
|
|
1011
|
|
|
?><p class="acf-submit"><button class="acf-button blue" type="submit"><?php _e("Add", 'acf'); ?></button><i class="acf-spinner"></i><span></span></p></form><?php |
1012
|
|
|
|
1013
|
|
|
|
1014
|
|
|
// die |
1015
|
|
|
die; |
|
|
|
|
1016
|
|
|
|
1017
|
|
|
} |
1018
|
|
|
|
1019
|
|
|
|
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
new acf_field_taxonomy(); |
1023
|
|
|
|
1024
|
|
|
endif; |
1025
|
|
|
|
1026
|
|
|
if( ! class_exists('acf_taxonomy_field_walker') ) : |
1027
|
|
|
|
1028
|
|
|
class acf_taxonomy_field_walker extends Walker { |
|
|
|
|
1029
|
|
|
|
1030
|
|
|
var $field = null, |
|
|
|
|
1031
|
|
|
$tree_type = 'category', |
1032
|
|
|
$db_fields = array ( 'parent' => 'parent', 'id' => 'term_id' ); |
1033
|
|
|
|
1034
|
|
|
function __construct( $field ) { |
|
|
|
|
1035
|
|
|
|
1036
|
|
|
$this->field = $field; |
1037
|
|
|
|
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
function start_el( &$output, $term, $depth = 0, $args = array(), $current_object_id = 0) { |
|
|
|
|
1041
|
|
|
|
1042
|
|
|
// vars |
1043
|
|
|
$selected = in_array( $term->term_id, $this->field['value'] ); |
1044
|
|
|
|
1045
|
|
|
|
1046
|
|
|
// append |
1047
|
|
|
$output .= '<li data-id="' . $term->term_id . '"><label><input type="' . $this->field['field_type'] . '" name="' . $this->field['name'] . '" value="' . $term->term_id . '" ' . ($selected ? 'checked="checked"' : '') . ' /> <span>' . $term->name . '</span></label>'; |
1048
|
|
|
|
1049
|
|
|
} |
1050
|
|
|
|
1051
|
|
|
function end_el( &$output, $term, $depth = 0, $args = array() ) { |
|
|
|
|
1052
|
|
|
|
1053
|
|
|
// append |
1054
|
|
|
$output .= '</li>' . "\n"; |
1055
|
|
|
|
1056
|
|
|
} |
1057
|
|
|
|
1058
|
|
|
function start_lvl( &$output, $depth = 0, $args = array() ) { |
|
|
|
|
1059
|
|
|
|
1060
|
|
|
// append |
1061
|
|
|
$output .= '<ul class="children acf-bl">' . "\n"; |
1062
|
|
|
|
1063
|
|
|
} |
1064
|
|
|
|
1065
|
|
|
function end_lvl( &$output, $depth = 0, $args = array() ) { |
|
|
|
|
1066
|
|
|
|
1067
|
|
|
// append |
1068
|
|
|
$output .= '</ul>' . "\n"; |
1069
|
|
|
|
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
} |
1073
|
|
|
|
1074
|
|
|
endif; |
1075
|
|
|
|
1076
|
|
|
?> |
1077
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.