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