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