PluginProbe
Advanced Custom Fields (ACF®) / 5.8.7
Advanced Custom Fields (ACF®) v5.8.7
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
501 lines 10.5 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
142 // set to term name
143 $title = $term->name;
144
145 // allow for empty name
146 if( $title === '' ) {
147 $title = __('(no title)', 'acf');
148 }
149
150 // prepent 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
157 return $title;
158 }
159
160 /**
161 * acf_get_grouped_terms
162 *
163 * Returns an array of terms for the given query $args and groups by taxonomy name.
164 *
165 * @date 2/8/18
166 * @since 5.7.2
167 *
168 * @param array $args An array of args used in the get_terms() function.
169 * @return array
170 */
171
172 function acf_get_grouped_terms( $args ) {
173
174 // vars
175 $data = array();
176
177 // defaults
178 $args = wp_parse_args($args, array(
179 'taxonomy' => null,
180 'hide_empty' => false,
181 'update_term_meta_cache' => false,
182 ));
183
184 // vars
185 $taxonomies = acf_get_taxonomy_labels( acf_get_array($args['taxonomy']) );
186 $is_single = (count($taxonomies) == 1);
187
188 // specify exact taxonomies required for _acf_terms_clauses() to work.
189 $args['taxonomy'] = array_keys($taxonomies);
190
191 // add filter to group results by taxonomy
192 if( !$is_single ) {
193 add_filter('terms_clauses', '_acf_terms_clauses', 10, 3);
194 }
195
196 // get terms
197 $terms = get_terms( $args );
198
199 // remove this filter (only once)
200 if( !$is_single ) {
201 remove_filter('terms_clauses', '_acf_terms_clauses', 10, 3);
202 }
203
204 // loop
205 foreach( $taxonomies as $taxonomy => $label ) {
206
207 // vars
208 $this_terms = array();
209
210 // populate $this_terms
211 foreach( $terms as $term ) {
212 if( $term->taxonomy == $taxonomy ) {
213 $this_terms[] = $term;
214 }
215 }
216
217 // bail early if no $items
218 if( empty($this_terms) ) continue;
219
220 // sort into hierachial order
221 // this will fail if a search has taken place because parents wont exist
222 if( is_taxonomy_hierarchical($taxonomy) && empty($args['s'])) {
223
224 // get all terms from this taxonomy
225 $all_terms = get_terms(array_merge($args, array(
226 'number' => 0,
227 'offset' => 0,
228 'taxonomy' => $taxonomy
229 )));
230
231 // vars
232 $length = count($this_terms);
233 $offset = 0;
234
235 // find starting point (offset)
236 foreach( $all_terms as $i => $term ) {
237 if( $term->term_id == $this_terms[0]->term_id ) {
238 $offset = $i;
239 break;
240 }
241 }
242
243 // order terms
244 $parent = acf_maybe_get( $args, 'parent', 0 );
245 $parent = acf_maybe_get( $args, 'child_of', $parent );
246 $ordered_terms = _get_term_children( $parent, $all_terms, $taxonomy );
247
248 // compare aray lengths
249 // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
250 // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
251 if( count($ordered_terms) == count($all_terms) ) {
252 $this_terms = array_slice($ordered_terms, $offset, $length);
253 }
254 }
255
256 // populate group
257 $data[ $label ] = array();
258 foreach( $this_terms as $term ) {
259 $data[ $label ][ $term->term_id ] = $term;
260 }
261 }
262
263 // return
264 return $data;
265 }
266
267 /**
268 * _acf_terms_clauses
269 *
270 * Used in the 'terms_clauses' filter to order terms by taxonomy name.
271 *
272 * @date 2/8/18
273 * @since 5.7.2
274 *
275 * @param array $pieces Terms query SQL clauses.
276 * @param array $taxonomies An array of taxonomies.
277 * @param array $args An array of terms query arguments.
278 * @return array $pieces
279 */
280
281 function _acf_terms_clauses( $pieces, $taxonomies, $args ) {
282
283 // prepend taxonomy to 'orderby' SQL
284 if( is_array($taxonomies) ) {
285 $sql = "FIELD(tt.taxonomy,'" . implode("', '", array_map('esc_sql', $taxonomies)) . "')";
286 $pieces['orderby'] = str_replace("ORDER BY", "ORDER BY $sql,", $pieces['orderby']);
287 }
288
289 // return
290 return $pieces;
291 }
292
293 /**
294 * acf_get_pretty_taxonomies
295 *
296 * Deprecated in favor of acf_get_taxonomy_labels() function.
297 *
298 * @date 7/10/13
299 * @since 5.0.0
300 * @deprecated 5.7.2
301 */
302
303 function acf_get_pretty_taxonomies( $taxonomies = array() ) {
304 return acf_get_taxonomy_labels( $taxonomies );
305 }
306
307 /**
308 * acf_get_term
309 *
310 * Similar to get_term() but with some extra functionality.
311 *
312 * @date 19/8/18
313 * @since 5.7.3
314 *
315 * @param mixed $term_id The term ID or a string of "taxonomy:slug".
316 * @param string $taxonomy The taxonomyname.
317 * @return WP_Term
318 */
319
320 function acf_get_term( $term_id, $taxonomy = '' ) {
321
322 // allow $term_id parameter to be a string of "taxonomy:slug" or "taxonomy:id"
323 if( is_string($term_id) && strpos($term_id, ':') ) {
324 list( $taxonomy, $term_id ) = explode(':', $term_id);
325 $term = get_term_by( 'slug', $term_id, $taxonomy );
326 if( $term ) return $term;
327 }
328
329 // return
330 return get_term( $term_id, $taxonomy );
331 }
332
333 /**
334 * acf_encode_term
335 *
336 * Returns a "taxonomy:slug" string for a given WP_Term.
337 *
338 * @date 27/8/18
339 * @since 5.7.4
340 *
341 * @param WP_Term $term The term object.
342 * @return string
343 */
344 function acf_encode_term( $term ) {
345 return "{$term->taxonomy}:{$term->slug}";
346 }
347
348 /**
349 * acf_decode_term
350 *
351 * Decodes a "taxonomy:slug" string into an array of taxonomy and slug.
352 *
353 * @date 27/8/18
354 * @since 5.7.4
355 *
356 * @param WP_Term $term The term object.
357 * @return string
358 */
359 function acf_decode_term( $string ) {
360 if( is_string($string) && strpos($string, ':') ) {
361 list( $taxonomy, $slug ) = explode(':', $string);
362 return array(
363 'taxonomy' => $taxonomy,
364 'slug' => $slug
365 );
366 }
367 return false;
368 }
369
370 /**
371 * acf_get_encoded_terms
372 *
373 * Returns an array of WP_Term objects from an array of encoded strings
374 *
375 * @date 9/9/18
376 * @since 5.7.5
377 *
378 * @param array $values The array of encoded strings.
379 * @return array
380 */
381 function acf_get_encoded_terms( $values ) {
382
383 // vars
384 $terms = array();
385
386 // loop over values
387 foreach( (array) $values as $value ) {
388
389 // find term from string
390 $term = acf_get_term( $value );
391
392 // append
393 if( $term instanceof WP_Term ) {
394 $terms[] = $term;
395 }
396 }
397
398 // return
399 return $terms;
400 }
401
402 /**
403 * acf_get_choices_from_terms
404 *
405 * Returns an array of choices from the terms provided.
406 *
407 * @date 8/9/18
408 * @since 5.7.5
409 *
410 * @param array $values and array of WP_Terms objects or encoded strings.
411 * @param string $format The value format (term_id, slug).
412 * @return array
413 */
414 function acf_get_choices_from_terms( $terms, $format = 'term_id' ) {
415
416 // vars
417 $groups = array();
418
419 // get taxonomy lables
420 $labels = acf_get_taxonomy_labels();
421
422 // convert array of encoded strings to terms
423 $term = reset($terms);
424 if( !$term instanceof WP_Term ) {
425 $terms = acf_get_encoded_terms( $terms );
426 }
427
428 // loop over terms
429 foreach( $terms as $term ) {
430 $group = $labels[ $term->taxonomy ];
431 $choice = acf_get_choice_from_term( $term, $format );
432 $groups[ $group ][ $choice['id'] ] = $choice['text'];
433 }
434
435 // return
436 return $groups;
437 }
438
439 /**
440 * acf_get_choices_from_grouped_terms
441 *
442 * Returns an array of choices from the grouped terms provided.
443 *
444 * @date 8/9/18
445 * @since 5.7.5
446 *
447 * @param array $value A grouped array of WP_Terms objects.
448 * @param string $format The value format (term_id, slug).
449 * @return array
450 */
451 function acf_get_choices_from_grouped_terms( $value, $format = 'term_id' ) {
452
453 // vars
454 $groups = array();
455
456 // loop over values
457 foreach( $value as $group => $terms ) {
458 $groups[ $group ] = array();
459 foreach( $terms as $term_id => $term ) {
460 $choice = acf_get_choice_from_term( $term, $format );
461 $groups[ $group ][ $choice['id'] ] = $choice['text'];
462 }
463 }
464
465 // return
466 return $groups;
467 }
468
469 /**
470 * acf_get_choice_from_term
471 *
472 * Returns an array containing the id and text for this item.
473 *
474 * @date 10/9/18
475 * @since 5.7.6
476 *
477 * @param object $item The item object such as WP_Post or WP_Term.
478 * @param string $format The value format (term_id, slug)
479 * @return array
480 */
481 function acf_get_choice_from_term( $term, $format = 'term_id' ) {
482
483 // vars
484 $id = $term->term_id;
485 $text = acf_get_term_title( $term );
486
487 // return format
488 if( $format == 'slug' ) {
489 $id = acf_encode_term($term);
490 }
491
492 // return
493 return array(
494 'id' => $id,
495 'text' => $text
496 );
497 }
498
499
500
501 ?>