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