PluginProbe
Advanced Custom Fields (ACF®) / 6.8.1
Advanced Custom Fields (ACF®) v6.8.1
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 / api / api-term.php
api-term.php
521 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package ACF
4 * @author WP Engine
5 *
6 * © 2026 Advanced Custom Fields (ACF®). All rights reserved.
7 * "ACF" is a trademark of WP Engine.
8 * Licensed under the GNU General Public License v2 or later.
9 * https://www.gnu.org/licenses/gpl-2.0.html
10 */
11
12 /**
13 * Returns an array of taxonomy names.
14 *
15 * @date 7/10/13
16 * @since 5.0.0
17 *
18 * @param array $args An array of args used in the get_taxonomies() function.
19 * @return array An array of taxonomy names.
20 */
21 function acf_get_taxonomies( $args = array() ) {
22
23 // vars
24 $taxonomies = array();
25
26 // get taxonomy objects
27 $objects = get_taxonomies( $args, 'objects' );
28
29 // loop
30 foreach ( $objects as $i => $object ) {
31
32 // bail early if is builtin (WP) private post type
33 // - nav_menu_item, revision, customize_changeset, etc
34 if ( $object->_builtin && ! $object->public ) {
35 continue;
36 }
37
38 // append
39 $taxonomies[] = $i;
40 }
41
42 // custom post_type arg which does not yet exist in core
43 if ( isset( $args['post_type'] ) ) {
44 $taxonomies = acf_get_taxonomies_for_post_type( $args['post_type'] );
45 }
46
47 // filter
48 $taxonomies = apply_filters( 'acf/get_taxonomies', $taxonomies, $args );
49
50 // return
51 return $taxonomies;
52 }
53
54 /**
55 * acf_get_taxonomies_for_post_type
56 *
57 * Returns an array of taxonomies for a given post type(s)
58 *
59 * @date 7/9/18
60 * @since 5.7.5
61 *
62 * @param string|array $post_types The post types to compare against.
63 * @return array
64 */
65 function acf_get_taxonomies_for_post_type( $post_types = 'post' ) {
66
67 // vars
68 $taxonomies = array();
69
70 // loop
71 foreach ( (array) $post_types as $post_type ) {
72 $object_taxonomies = get_object_taxonomies( $post_type );
73 foreach ( (array) $object_taxonomies as $taxonomy ) {
74 $taxonomies[] = $taxonomy;
75 }
76 }
77
78 // remove duplicates
79 $taxonomies = array_unique( $taxonomies );
80
81 // return
82 return $taxonomies;
83 }
84
85 /**
86 * Returns an array of taxonomies in the format "name => label" for use in a select field.
87 *
88 * @date 3/8/18
89 * @since 5.7.2
90 *
91 * @param array $taxonomies Optional. An array of specific taxonomies to return.
92 * @return array
93 */
94 function acf_get_taxonomy_labels( $taxonomies = array() ) {
95
96 // default
97 if ( empty( $taxonomies ) ) {
98 $taxonomies = acf_get_taxonomies();
99 }
100
101 // vars
102 $ref = array();
103 $data = array();
104
105 // loop
106 foreach ( $taxonomies as $taxonomy ) {
107
108 // vars
109 $object = get_taxonomy( $taxonomy );
110 $label = $object->labels->singular_name;
111
112 // append
113 $data[ $taxonomy ] = $label;
114
115 // increase counter
116 if ( ! isset( $ref[ $label ] ) ) {
117 $ref[ $label ] = 0;
118 }
119 ++$ref[ $label ];
120 }
121
122 // show taxonomy name next to label for shared labels
123 foreach ( $data as $taxonomy => $label ) {
124 if ( $ref[ $label ] > 1 ) {
125 $data[ $taxonomy ] .= ' (' . $taxonomy . ')';
126 }
127 }
128
129 // return
130 return $data;
131 }
132
133 /**
134 * acf_get_term_title
135 *
136 * Returns the title for this term object.
137 *
138 * @date 10/9/18
139 * @since 5.0.0
140 *
141 * @param object $term The WP_Term object.
142 * @return string
143 */
144 function acf_get_term_title( $term ) {
145 $title = $term->name;
146
147 // Allow for empty name.
148 if ( $title === '' ) {
149 $title = __( '(no title)', 'acf' );
150 }
151
152 // Prepend ancestors indentation.
153 if ( is_taxonomy_hierarchical( $term->taxonomy ) ) {
154 $ancestors = get_ancestors( $term->term_id, $term->taxonomy );
155 $title = str_repeat( '- ', count( $ancestors ) ) . $title;
156 }
157
158 return $title;
159 }
160
161 /**
162 * acf_get_grouped_terms
163 *
164 * Returns an array of terms for the given query $args and groups by taxonomy name.
165 *
166 * @date 2/8/18
167 * @since 5.7.2
168 *
169 * @param array $args An array of args used in the get_terms() function.
170 * @return array
171 */
172 function acf_get_grouped_terms( $args ) {
173
174 // vars
175 $data = array();
176
177 // defaults
178 $args = wp_parse_args(
179 $args,
180 array(
181 'taxonomy' => null,
182 'hide_empty' => false,
183 'update_term_meta_cache' => false,
184 )
185 );
186
187 // vars
188 $taxonomies = acf_get_taxonomy_labels( acf_get_array( $args['taxonomy'] ) );
189 $is_single = ( count( $taxonomies ) == 1 );
190
191 // specify exact taxonomies required for _acf_terms_clauses() to work.
192 $args['taxonomy'] = array_keys( $taxonomies );
193
194 // add filter to group results by taxonomy
195 if ( ! $is_single ) {
196 add_filter( 'terms_clauses', '_acf_terms_clauses', 10, 3 );
197 }
198
199 // get terms
200 $terms = get_terms( $args );
201
202 // remove this filter (only once)
203 if ( ! $is_single ) {
204 remove_filter( 'terms_clauses', '_acf_terms_clauses', 10, 3 );
205 }
206
207 // loop
208 foreach ( $taxonomies as $taxonomy => $label ) {
209
210 // vars
211 $this_terms = array();
212
213 // populate $this_terms
214 foreach ( $terms as $term ) {
215 if ( $term->taxonomy == $taxonomy ) {
216 $this_terms[] = $term;
217 }
218 }
219
220 // bail early if no $items
221 if ( empty( $this_terms ) ) {
222 continue;
223 }
224
225 // sort into hierachial order
226 // this will fail if a search has taken place because parents wont exist
227 if ( is_taxonomy_hierarchical( $taxonomy ) && empty( $args['s'] ) ) {
228
229 // get all terms from this taxonomy
230 $all_terms = get_terms(
231 array_merge(
232 $args,
233 array(
234 'number' => 0,
235 'offset' => 0,
236 'taxonomy' => $taxonomy,
237 )
238 )
239 );
240
241 // vars
242 $length = count( $this_terms );
243 $offset = 0;
244
245 // find starting point (offset)
246 foreach ( $all_terms as $i => $term ) {
247 if ( $term->term_id == $this_terms[0]->term_id ) {
248 $offset = $i;
249 break;
250 }
251 }
252
253 // order terms
254 $parent = acf_maybe_get( $args, 'parent', 0 );
255 $parent = acf_maybe_get( $args, 'child_of', $parent );
256 $ordered_terms = _get_term_children( $parent, $all_terms, $taxonomy );
257
258 // compare aray lengths
259 // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
260 // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
261 if ( count( $ordered_terms ) == count( $all_terms ) ) {
262 $this_terms = array_slice( $ordered_terms, $offset, $length );
263 }
264 }
265
266 // populate group
267 $data[ $label ] = array();
268 foreach ( $this_terms as $term ) {
269 $data[ $label ][ $term->term_id ] = $term;
270 }
271 }
272
273 // return
274 return $data;
275 }
276
277 /**
278 * _acf_terms_clauses
279 *
280 * Used in the 'terms_clauses' filter to order terms by taxonomy name.
281 *
282 * @date 2/8/18
283 * @since 5.7.2
284 *
285 * @param array $pieces Terms query SQL clauses.
286 * @param array $taxonomies An array of taxonomies.
287 * @param array $args An array of terms query arguments.
288 * @return array $pieces
289 */
290 function _acf_terms_clauses( $pieces, $taxonomies, $args ) {
291
292 // prepend taxonomy to 'orderby' SQL
293 if ( is_array( $taxonomies ) ) {
294 $sql = "FIELD(tt.taxonomy,'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')";
295 $pieces['orderby'] = str_replace( 'ORDER BY', "ORDER BY $sql,", $pieces['orderby'] );
296 }
297
298 // return
299 return $pieces;
300 }
301
302 /**
303 * acf_get_pretty_taxonomies
304 *
305 * Deprecated in favor of acf_get_taxonomy_labels() function.
306 *
307 * @date 7/10/13
308 * @since 5.0.0
309 * @deprecated 5.7.2
310 */
311 function acf_get_pretty_taxonomies( $taxonomies = array() ) {
312 return acf_get_taxonomy_labels( $taxonomies );
313 }
314
315 /**
316 * acf_get_term
317 *
318 * Similar to get_term() but with some extra functionality.
319 *
320 * @date 19/8/18
321 * @since 5.7.3
322 *
323 * @param mixed $term_id The term ID or a string of "taxonomy:slug".
324 * @param string $taxonomy The taxonomyname.
325 * @return WP_Term
326 */
327 function acf_get_term( $term_id, $taxonomy = '' ) {
328
329 // allow $term_id parameter to be a string of "taxonomy:slug" or "taxonomy:id"
330 if ( is_string( $term_id ) && strpos( $term_id, ':' ) ) {
331 list( $taxonomy, $term_id ) = explode( ':', $term_id );
332 $term = get_term_by( 'slug', $term_id, $taxonomy );
333 if ( $term ) {
334 return $term;
335 }
336 }
337
338 // return
339 return get_term( $term_id, $taxonomy );
340 }
341
342 /**
343 * acf_encode_term
344 *
345 * Returns a "taxonomy:slug" string for a given WP_Term.
346 *
347 * @date 27/8/18
348 * @since 5.7.4
349 *
350 * @param WP_Term $term The term object.
351 * @return string
352 */
353 function acf_encode_term( $term ) {
354 return "{$term->taxonomy}:{$term->slug}";
355 }
356
357 /**
358 * acf_decode_term
359 *
360 * Decodes a "taxonomy:slug" string into an array of taxonomy and slug.
361 *
362 * @date 27/8/18
363 * @since 5.7.4
364 *
365 * @param WP_Term $term The term object.
366 * @return string
367 */
368 function acf_decode_term( $string ) {
369 if ( is_string( $string ) && strpos( $string, ':' ) ) {
370 list( $taxonomy, $slug ) = explode( ':', $string );
371 return compact( 'taxonomy', 'slug' );
372 }
373 return false;
374 }
375
376 /**
377 * acf_get_encoded_terms
378 *
379 * Returns an array of WP_Term objects from an array of encoded strings
380 *
381 * @date 9/9/18
382 * @since 5.7.5
383 *
384 * @param array $values The array of encoded strings.
385 * @return array
386 */
387 function acf_get_encoded_terms( $values ) {
388
389 // vars
390 $terms = array();
391
392 // loop over values
393 foreach ( (array) $values as $value ) {
394
395 // find term from string
396 $term = acf_get_term( $value );
397
398 // append
399 if ( $term instanceof WP_Term ) {
400 $terms[] = $term;
401 }
402 }
403
404 // return
405 return $terms;
406 }
407
408 /**
409 * acf_get_choices_from_terms
410 *
411 * Returns an array of choices from the terms provided.
412 *
413 * @date 8/9/18
414 * @since 5.7.5
415 *
416 * @param array $values and array of WP_Terms objects or encoded strings.
417 * @param string $format The value format (term_id, slug).
418 * @return array
419 */
420 function acf_get_choices_from_terms( $terms, $format = 'term_id' ) {
421
422 // vars
423 $groups = array();
424
425 // get taxonomy lables
426 $labels = acf_get_taxonomy_labels();
427
428 // convert array of encoded strings to terms
429 $term = reset( $terms );
430 if ( ! $term instanceof WP_Term ) {
431 $terms = acf_get_encoded_terms( $terms );
432 }
433
434 // loop over terms
435 foreach ( $terms as $term ) {
436 $group = $labels[ $term->taxonomy ];
437 $choice = acf_get_choice_from_term( $term, $format );
438 $groups[ $group ][ $choice['id'] ] = $choice['text'];
439 }
440
441 // return
442 return $groups;
443 }
444
445 /**
446 * acf_get_choices_from_grouped_terms
447 *
448 * Returns an array of choices from the grouped terms provided.
449 *
450 * @date 8/9/18
451 * @since 5.7.5
452 *
453 * @param array $value A grouped array of WP_Terms objects.
454 * @param string $format The value format (term_id, slug).
455 * @return array
456 */
457 function acf_get_choices_from_grouped_terms( $value, $format = 'term_id' ) {
458
459 // vars
460 $groups = array();
461
462 // loop over values
463 foreach ( $value as $group => $terms ) {
464 $groups[ $group ] = array();
465 foreach ( $terms as $term_id => $term ) {
466 $choice = acf_get_choice_from_term( $term, $format );
467 $groups[ $group ][ $choice['id'] ] = $choice['text'];
468 }
469 }
470
471 // return
472 return $groups;
473 }
474
475 /**
476 * acf_get_choice_from_term
477 *
478 * Returns an array containing the id and text for this item.
479 *
480 * @date 10/9/18
481 * @since 5.7.6
482 *
483 * @param object $item The item object such as WP_Post or WP_Term.
484 * @param string $format The value format (term_id, slug)
485 * @return array
486 */
487 function acf_get_choice_from_term( $term, $format = 'term_id' ) {
488
489 // vars
490 $id = $term->term_id;
491 $text = acf_get_term_title( $term );
492
493 // return format
494 if ( $format == 'slug' ) {
495 $id = acf_encode_term( $term );
496 }
497
498 // return
499 return array(
500 'id' => $id,
501 'text' => $text,
502 );
503 }
504
505 /**
506 * Returns a valid post_id string for a given term and taxonomy.
507 * No longer needed since WP introduced the termmeta table in WP 4.4.
508 *
509 * @date 6/2/17
510 * @since 5.5.6
511 * @deprecated 5.9.2
512 *
513 * @param $taxonomy (string) The taxonomy type.
514 * @param $term_id (int) The term ID.
515 * @return (string)
516 */
517 function acf_get_term_post_id( $taxonomy, $term_id ) {
518 _deprecated_function( __FUNCTION__, '5.9.2', 'string format term_%d' );
519 return 'term_' . $term_id;
520 }
521