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