1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* ACF Post Object Field Class |
5
|
|
|
* |
6
|
|
|
* All the logic for this field type |
7
|
|
|
* |
8
|
|
|
* @class acf_field_post_object |
9
|
|
|
* @extends acf_field |
10
|
|
|
* @package ACF |
11
|
|
|
* @subpackage Fields |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if( ! class_exists('acf_field_post_object') ) : |
15
|
|
|
|
16
|
|
|
class acf_field_post_object 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
|
|
|
function __construct() { |
|
|
|
|
33
|
|
|
|
34
|
|
|
// vars |
35
|
|
|
$this->name = 'post_object'; |
36
|
|
|
$this->label = __("Post Object",'acf'); |
|
|
|
|
37
|
|
|
$this->category = 'relational'; |
38
|
|
|
$this->defaults = array( |
39
|
|
|
'post_type' => array(), |
40
|
|
|
'taxonomy' => array(), |
41
|
|
|
'allow_null' => 0, |
42
|
|
|
'multiple' => 0, |
43
|
|
|
'return_format' => 'object', |
44
|
|
|
'ui' => 1, |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
// extra |
49
|
|
|
add_action('wp_ajax_acf/fields/post_object/query', array($this, 'ajax_query')); |
50
|
|
|
add_action('wp_ajax_nopriv_acf/fields/post_object/query', array($this, 'ajax_query')); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
// do not delete! |
54
|
|
|
parent::__construct(); |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/* |
60
|
|
|
* get_choices |
61
|
|
|
* |
62
|
|
|
* This function will return an array of data formatted for use in a select2 AJAX response |
63
|
|
|
* |
64
|
|
|
* @type function |
65
|
|
|
* @date 15/10/2014 |
66
|
|
|
* @since 5.0.9 |
67
|
|
|
* |
68
|
|
|
* @param $options (array) |
69
|
|
|
* @return (array) |
70
|
|
|
*/ |
71
|
|
|
|
72
|
|
|
function get_choices( $options = array() ) { |
|
|
|
|
73
|
|
|
|
74
|
|
|
// defaults |
75
|
|
|
$options = acf_parse_args($options, array( |
76
|
|
|
'post_id' => 0, |
77
|
|
|
's' => '', |
78
|
|
|
'field_key' => '', |
79
|
|
|
'paged' => 1 |
80
|
|
|
)); |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
// vars |
84
|
|
|
$r = array(); |
85
|
|
|
$args = array(); |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
// paged |
89
|
|
|
$args['posts_per_page'] = 20; |
90
|
|
|
$args['paged'] = $options['paged']; |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
// load field |
94
|
|
|
$field = acf_get_field( $options['field_key'] ); |
95
|
|
|
|
96
|
|
|
if( !$field ) { |
97
|
|
|
|
98
|
|
|
return false; |
|
|
|
|
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
// update $args |
104
|
|
View Code Duplication |
if( !empty($field['post_type']) ) { |
|
|
|
|
105
|
|
|
|
106
|
|
|
$args['post_type'] = acf_get_array( $field['post_type'] ); |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
|
110
|
|
|
$args['post_type'] = acf_get_post_types(); |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
// create tax queries |
116
|
|
View Code Duplication |
if( !empty($field['taxonomy']) ) { |
|
|
|
|
117
|
|
|
|
118
|
|
|
// append to $args |
119
|
|
|
$args['tax_query'] = array(); |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
// decode terms |
123
|
|
|
$taxonomies = acf_decode_taxonomy_terms( $field['taxonomy'] ); |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// now create the tax queries |
127
|
|
|
foreach( $taxonomies as $taxonomy => $terms ) { |
128
|
|
|
|
129
|
|
|
$args['tax_query'][] = array( |
130
|
|
|
'taxonomy' => $taxonomy, |
131
|
|
|
'field' => 'slug', |
132
|
|
|
'terms' => $terms, |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
// search |
141
|
|
|
if( $options['s'] ) { |
142
|
|
|
|
143
|
|
|
$args['s'] = $options['s']; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
// filters |
149
|
|
|
$args = apply_filters('acf/fields/post_object/query', $args, $field, $options['post_id']); |
150
|
|
|
$args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id'] ); |
151
|
|
|
$args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id'] ); |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
// get posts grouped by post type |
155
|
|
|
$groups = acf_get_grouped_posts( $args ); |
156
|
|
|
|
157
|
|
View Code Duplication |
if( !empty($groups) ) { |
|
|
|
|
158
|
|
|
|
159
|
|
|
foreach( array_keys($groups) as $group_title ) { |
160
|
|
|
|
161
|
|
|
// vars |
162
|
|
|
$posts = acf_extract_var( $groups, $group_title ); |
163
|
|
|
$titles = array(); |
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
// data |
167
|
|
|
$data = array( |
168
|
|
|
'text' => $group_title, |
169
|
|
|
'children' => array() |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
foreach( array_keys($posts) as $post_id ) { |
174
|
|
|
|
175
|
|
|
// override data |
176
|
|
|
$posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'] ); |
177
|
|
|
|
178
|
|
|
}; |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
// order by search |
182
|
|
|
if( !empty($args['s']) ) { |
183
|
|
|
|
184
|
|
|
$posts = acf_order_by_search( $posts, $args['s'] ); |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
// append to $data |
190
|
|
|
foreach( array_keys($posts) as $post_id ) { |
191
|
|
|
|
192
|
|
|
$data['children'][] = array( |
193
|
|
|
'id' => $post_id, |
194
|
|
|
'text' => $posts[ $post_id ] |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
// append to $r |
201
|
|
|
$r[] = $data; |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
// optgroup or single |
207
|
|
|
if( count($args['post_type']) == 1 ) { |
208
|
|
|
|
209
|
|
|
$r = $r[0]['children']; |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
// return |
217
|
|
|
return $r; |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/* |
223
|
|
|
* ajax_query |
224
|
|
|
* |
225
|
|
|
* description |
226
|
|
|
* |
227
|
|
|
* @type function |
228
|
|
|
* @date 24/10/13 |
229
|
|
|
* @since 5.0.0 |
230
|
|
|
* |
231
|
|
|
* @param $post_id (int) |
232
|
|
|
* @return $post_id (int) |
233
|
|
|
*/ |
|
|
|
|
234
|
|
|
|
235
|
|
View Code Duplication |
function ajax_query() { |
|
|
|
|
236
|
|
|
|
237
|
|
|
// validate |
238
|
|
|
if( !acf_verify_ajax() ) { |
239
|
|
|
|
240
|
|
|
die(); |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
// get choices |
246
|
|
|
$choices = $this->get_choices( $_POST ); |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
// validate |
250
|
|
|
if( !$choices ) { |
|
|
|
|
251
|
|
|
|
252
|
|
|
die(); |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
// return JSON |
258
|
|
|
echo json_encode( $choices ); |
259
|
|
|
die(); |
260
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/* |
265
|
|
|
* get_post_title |
266
|
|
|
* |
267
|
|
|
* This function returns the HTML for a result |
268
|
|
|
* |
269
|
|
|
* @type function |
270
|
|
|
* @date 1/11/2013 |
271
|
|
|
* @since 5.0.0 |
272
|
|
|
* |
273
|
|
|
* @param $post (object) |
274
|
|
|
* @param $field (array) |
275
|
|
|
* @param $post_id (int) the post_id to which this value is saved to |
276
|
|
|
* @return (string) |
277
|
|
|
*/ |
278
|
|
|
|
279
|
|
View Code Duplication |
function get_post_title( $post, $field, $post_id = 0 ) { |
|
|
|
|
280
|
|
|
|
281
|
|
|
// get post_id |
282
|
|
|
if( !$post_id ) { |
283
|
|
|
|
284
|
|
|
$form_data = acf_get_setting('form_data'); |
285
|
|
|
|
286
|
|
|
if( !empty($form_data['post_id']) ) { |
287
|
|
|
|
288
|
|
|
$post_id = $form_data['post_id']; |
289
|
|
|
|
290
|
|
|
} else { |
291
|
|
|
|
292
|
|
|
$post_id = get_the_ID(); |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
// vars |
300
|
|
|
$title = acf_get_post_title( $post ); |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
// filters |
304
|
|
|
$title = apply_filters('acf/fields/post_object/result', $title, $post, $field, $post_id); |
305
|
|
|
$title = apply_filters('acf/fields/post_object/result/name=' . $field['_name'], $title, $post, $field, $post_id); |
306
|
|
|
$title = apply_filters('acf/fields/post_object/result/key=' . $field['key'], $title, $post, $field, $post_id); |
307
|
|
|
|
308
|
|
|
|
309
|
|
|
// return |
310
|
|
|
return $title; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/* |
315
|
|
|
* render_field() |
316
|
|
|
* |
317
|
|
|
* Create the HTML interface for your field |
318
|
|
|
* |
319
|
|
|
* @param $field - an array holding all the field's data |
320
|
|
|
* |
321
|
|
|
* @type action |
322
|
|
|
* @since 3.6 |
323
|
|
|
* @date 23/01/13 |
324
|
|
|
*/ |
325
|
|
|
|
326
|
|
|
function render_field( $field ) { |
|
|
|
|
327
|
|
|
|
328
|
|
|
// Change Field into a select |
329
|
|
|
$field['type'] = 'select'; |
330
|
|
|
$field['ui'] = 1; |
331
|
|
|
$field['ajax'] = 1; |
332
|
|
|
$field['choices'] = array(); |
333
|
|
|
|
334
|
|
|
|
335
|
|
|
// populate choices if value exists |
336
|
|
|
if( !empty($field['value']) ) { |
337
|
|
|
|
338
|
|
|
// get posts |
339
|
|
|
$posts = acf_get_posts(array( |
340
|
|
|
'post__in' => $field['value'], |
341
|
|
|
'post_type' => $field['post_type'] |
342
|
|
|
)); |
343
|
|
|
|
344
|
|
|
|
345
|
|
|
// set choices |
346
|
|
View Code Duplication |
if( !empty($posts) ) { |
|
|
|
|
347
|
|
|
|
348
|
|
|
foreach( array_keys($posts) as $i ) { |
349
|
|
|
|
350
|
|
|
// vars |
351
|
|
|
$post = acf_extract_var( $posts, $i ); |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
// append to choices |
355
|
|
|
$field['choices'][ $post->ID ] = $this->get_post_title( $post, $field ); |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
// render |
365
|
|
|
acf_render_field( $field ); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
|
369
|
|
|
/* |
370
|
|
|
* render_field_settings() |
371
|
|
|
* |
372
|
|
|
* Create extra options for your field. This is rendered when editing a field. |
373
|
|
|
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
374
|
|
|
* |
375
|
|
|
* @type action |
376
|
|
|
* @since 3.6 |
377
|
|
|
* @date 23/01/13 |
378
|
|
|
* |
379
|
|
|
* @param $field - an array holding all the field's data |
380
|
|
|
*/ |
381
|
|
|
|
382
|
|
|
function render_field_settings( $field ) { |
|
|
|
|
383
|
|
|
|
384
|
|
|
// default_value |
385
|
|
|
acf_render_field_setting( $field, array( |
386
|
|
|
'label' => __('Filter by Post Type','acf'), |
387
|
|
|
'instructions' => '', |
388
|
|
|
'type' => 'select', |
389
|
|
|
'name' => 'post_type', |
390
|
|
|
'choices' => acf_get_pretty_post_types(), |
391
|
|
|
'multiple' => 1, |
392
|
|
|
'ui' => 1, |
393
|
|
|
'allow_null' => 1, |
394
|
|
|
'placeholder' => __("All post types",'acf'), |
395
|
|
|
)); |
396
|
|
|
|
397
|
|
|
|
398
|
|
|
// default_value |
399
|
|
|
acf_render_field_setting( $field, array( |
400
|
|
|
'label' => __('Filter by Taxonomy','acf'), |
401
|
|
|
'instructions' => '', |
402
|
|
|
'type' => 'select', |
403
|
|
|
'name' => 'taxonomy', |
404
|
|
|
'choices' => acf_get_taxonomy_terms(), |
405
|
|
|
'multiple' => 1, |
406
|
|
|
'ui' => 1, |
407
|
|
|
'allow_null' => 1, |
408
|
|
|
'placeholder' => __("All taxonomies",'acf'), |
409
|
|
|
)); |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
// allow_null |
413
|
|
|
acf_render_field_setting( $field, array( |
414
|
|
|
'label' => __('Allow Null?','acf'), |
415
|
|
|
'instructions' => '', |
416
|
|
|
'type' => 'radio', |
417
|
|
|
'name' => 'allow_null', |
418
|
|
|
'choices' => array( |
419
|
|
|
1 => __("Yes",'acf'), |
420
|
|
|
0 => __("No",'acf'), |
421
|
|
|
), |
422
|
|
|
'layout' => 'horizontal', |
423
|
|
|
)); |
424
|
|
|
|
425
|
|
|
|
426
|
|
|
// multiple |
427
|
|
|
acf_render_field_setting( $field, array( |
428
|
|
|
'label' => __('Select multiple values?','acf'), |
429
|
|
|
'instructions' => '', |
430
|
|
|
'type' => 'radio', |
431
|
|
|
'name' => 'multiple', |
432
|
|
|
'choices' => array( |
433
|
|
|
1 => __("Yes",'acf'), |
434
|
|
|
0 => __("No",'acf'), |
435
|
|
|
), |
436
|
|
|
'layout' => 'horizontal', |
437
|
|
|
)); |
438
|
|
|
|
439
|
|
|
|
440
|
|
|
// return_format |
441
|
|
|
acf_render_field_setting( $field, array( |
442
|
|
|
'label' => __('Return Format','acf'), |
443
|
|
|
'instructions' => '', |
444
|
|
|
'type' => 'radio', |
445
|
|
|
'name' => 'return_format', |
446
|
|
|
'choices' => array( |
447
|
|
|
'object' => __("Post Object",'acf'), |
448
|
|
|
'id' => __("Post ID",'acf'), |
449
|
|
|
), |
450
|
|
|
'layout' => 'horizontal', |
451
|
|
|
)); |
452
|
|
|
|
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
|
456
|
|
|
/* |
457
|
|
|
* load_value() |
458
|
|
|
* |
459
|
|
|
* This filter is applied to the $value after it is loaded from the db |
460
|
|
|
* |
461
|
|
|
* @type filter |
462
|
|
|
* @since 3.6 |
463
|
|
|
* @date 23/01/13 |
464
|
|
|
* |
465
|
|
|
* @param $value (mixed) the value found in the database |
466
|
|
|
* @param $post_id (mixed) the $post_id from which the value was loaded |
467
|
|
|
* @param $field (array) the field array holding all the field options |
468
|
|
|
* @return $value |
469
|
|
|
*/ |
|
|
|
|
470
|
|
|
|
471
|
|
|
function load_value( $value, $post_id, $field ) { |
|
|
|
|
472
|
|
|
|
473
|
|
|
// ACF4 null |
474
|
|
|
if( $value === 'null' ) { |
475
|
|
|
|
476
|
|
|
return false; |
477
|
|
|
|
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
|
481
|
|
|
// return |
482
|
|
|
return $value; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
|
486
|
|
|
/* |
487
|
|
|
* format_value() |
488
|
|
|
* |
489
|
|
|
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
490
|
|
|
* |
491
|
|
|
* @type filter |
492
|
|
|
* @since 3.6 |
493
|
|
|
* @date 23/01/13 |
494
|
|
|
* |
495
|
|
|
* @param $value (mixed) the value which was loaded from the database |
496
|
|
|
* @param $post_id (mixed) the $post_id from which the value was loaded |
497
|
|
|
* @param $field (array) the field array holding all the field options |
498
|
|
|
* |
499
|
|
|
* @return $value (mixed) the modified value |
500
|
|
|
*/ |
|
|
|
|
501
|
|
|
|
502
|
|
|
function format_value( $value, $post_id, $field ) { |
|
|
|
|
503
|
|
|
|
504
|
|
|
// bail early if no value |
505
|
|
|
if( empty($value) ) { |
506
|
|
|
|
507
|
|
|
return $value; |
508
|
|
|
|
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
|
512
|
|
|
// force value to array |
513
|
|
|
$value = acf_get_array( $value ); |
514
|
|
|
|
515
|
|
|
|
516
|
|
|
// convert values to int |
517
|
|
|
$value = array_map('intval', $value); |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
// load posts if needed |
521
|
|
View Code Duplication |
if( $field['return_format'] == 'object' ) { |
|
|
|
|
522
|
|
|
|
523
|
|
|
// get posts |
524
|
|
|
$value = acf_get_posts(array( |
525
|
|
|
'post__in' => $value, |
526
|
|
|
'post_type' => $field['post_type'] |
527
|
|
|
)); |
528
|
|
|
|
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
|
532
|
|
|
// convert back from array if neccessary |
533
|
|
|
if( !$field['multiple'] ) { |
534
|
|
|
|
535
|
|
|
$value = array_shift($value); |
536
|
|
|
|
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
|
540
|
|
|
// return value |
541
|
|
|
return $value; |
542
|
|
|
|
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
|
546
|
|
|
/* |
547
|
|
|
* update_value() |
548
|
|
|
* |
549
|
|
|
* This filter is appied to the $value before it is updated in the db |
550
|
|
|
* |
551
|
|
|
* @type filter |
552
|
|
|
* @since 3.6 |
553
|
|
|
* @date 23/01/13 |
554
|
|
|
* |
555
|
|
|
* @param $value - the value which will be saved in the database |
556
|
|
|
* @param $post_id - the $post_id of which the value will be saved |
557
|
|
|
* @param $field - the field array holding all the field options |
558
|
|
|
* |
559
|
|
|
* @return $value - the modified value |
560
|
|
|
*/ |
|
|
|
|
561
|
|
|
|
562
|
|
View Code Duplication |
function update_value( $value, $post_id, $field ) { |
|
|
|
|
563
|
|
|
|
564
|
|
|
// validate |
565
|
|
|
if( empty($value) ) { |
566
|
|
|
|
567
|
|
|
return $value; |
568
|
|
|
|
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
|
572
|
|
|
// format |
573
|
|
|
if( is_array($value) ) { |
574
|
|
|
|
575
|
|
|
// array |
576
|
|
|
foreach( $value as $k => $v ){ |
577
|
|
|
|
578
|
|
|
// object? |
579
|
|
|
if( is_object($v) && isset($v->ID) ) { |
580
|
|
|
|
581
|
|
|
$value[ $k ] = $v->ID; |
582
|
|
|
|
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
|
588
|
|
|
// save value as strings, so we can clearly search for them in SQL LIKE statements |
589
|
|
|
$value = array_map('strval', $value); |
590
|
|
|
|
591
|
|
|
} elseif( is_object($value) && isset($value->ID) ) { |
592
|
|
|
|
593
|
|
|
// object |
594
|
|
|
$value = $value->ID; |
595
|
|
|
|
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
|
599
|
|
|
// return |
600
|
|
|
return $value; |
601
|
|
|
|
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
new acf_field_post_object(); |
607
|
|
|
|
608
|
|
|
endif; |
609
|
|
|
|
610
|
|
|
?> |
|
|
|
|
611
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.