PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wp-content/plugins/advanced-custom-fields-pro/includes/api/api-term.php

https://bitbucket.org/sebastianpisula/wordpress-starter
PHP | 498 lines | 186 code | 87 blank | 225 comment | 30 complexity | 0f9b4fcda7f266b753e61525c14f1cab MD5 | raw file
Possible License(s): GPL-2.0, 0BSD
  1. <?php
  2. /*
  3. * acf_get_taxonomies
  4. *
  5. * Returns an array of taxonomy names.
  6. *
  7. * @date 7/10/13
  8. * @since 5.0.0
  9. *
  10. * @param array $args An array of args used in the get_taxonomies() function.
  11. * @return array An array of taxonomy names.
  12. */
  13. function acf_get_taxonomies( $args = array() ) {
  14. // vars
  15. $taxonomies = array();
  16. // get taxonomy objects
  17. $objects = get_taxonomies( $args, 'objects' );
  18. // loop
  19. foreach( $objects as $i => $object ) {
  20. // bail early if is builtin (WP) private post type
  21. // - nav_menu_item, revision, customize_changeset, etc
  22. if( $object->_builtin && !$object->public ) continue;
  23. // append
  24. $taxonomies[] = $i;
  25. }
  26. // custom post_type arg which does not yet exist in core
  27. if( isset($args['post_type']) ) {
  28. $taxonomies = acf_get_taxonomies_for_post_type($args['post_type']);
  29. }
  30. // filter
  31. $taxonomies = apply_filters('acf/get_taxonomies', $taxonomies, $args);
  32. // return
  33. return $taxonomies;
  34. }
  35. /**
  36. * acf_get_taxonomies_for_post_type
  37. *
  38. * Returns an array of taxonomies for a given post type(s)
  39. *
  40. * @date 7/9/18
  41. * @since 5.7.5
  42. *
  43. * @param string|array $post_types The post types to compare against.
  44. * @return array
  45. */
  46. function acf_get_taxonomies_for_post_type( $post_types = 'post' ) {
  47. // vars
  48. $taxonomies = array();
  49. // loop
  50. foreach( (array) $post_types as $post_type ) {
  51. $object_taxonomies = get_object_taxonomies( $post_type );
  52. foreach( (array) $object_taxonomies as $taxonomy ) {
  53. $taxonomies[] = $taxonomy;
  54. }
  55. }
  56. // remove duplicates
  57. $taxonomies = array_unique($taxonomies);
  58. // return
  59. return $taxonomies;
  60. }
  61. /*
  62. * acf_get_taxonomy_labels
  63. *
  64. * Returns an array of taxonomies in the format "name => label" for use in a select field.
  65. *
  66. * @date 3/8/18
  67. * @since 5.7.2
  68. *
  69. * @param array $taxonomies Optional. An array of specific taxonomies to return.
  70. * @return array
  71. */
  72. function acf_get_taxonomy_labels( $taxonomies = array() ) {
  73. // default
  74. if( empty($taxonomies) ) {
  75. $taxonomies = acf_get_taxonomies();
  76. }
  77. // vars
  78. $ref = array();
  79. $data = array();
  80. // loop
  81. foreach( $taxonomies as $taxonomy ) {
  82. // vars
  83. $object = get_taxonomy( $taxonomy );
  84. $label = $object->labels->singular_name;
  85. // append
  86. $data[ $taxonomy ] = $label;
  87. // increase counter
  88. if( !isset($ref[ $label ]) ) {
  89. $ref[ $label ] = 0;
  90. }
  91. $ref[ $label ]++;
  92. }
  93. // show taxonomy name next to label for shared labels
  94. foreach( $data as $taxonomy => $label ) {
  95. if( $ref[$label] > 1 ) {
  96. $data[ $taxonomy ] .= ' (' . $taxonomy . ')';
  97. }
  98. }
  99. // return
  100. return $data;
  101. }
  102. /**
  103. * acf_get_term_title
  104. *
  105. * Returns the title for this term object.
  106. *
  107. * @date 10/9/18
  108. * @since 5.0.0
  109. *
  110. * @param object $term The WP_Term object.
  111. * @return string
  112. */
  113. function acf_get_term_title( $term ) {
  114. // set to term name
  115. $title = $term->name;
  116. // allow for empty name
  117. if( $title === '' ) {
  118. $title = __('(no title)', 'acf');
  119. }
  120. // prepent ancestors indentation
  121. if( is_taxonomy_hierarchical($term->taxonomy) ) {
  122. $ancestors = get_ancestors( $term->term_id, $term->taxonomy );
  123. $title = str_repeat('- ', count($ancestors)) . $title;
  124. }
  125. // return
  126. return $title;
  127. }
  128. /**
  129. * acf_get_grouped_terms
  130. *
  131. * Returns an array of terms for the given query $args and groups by taxonomy name.
  132. *
  133. * @date 2/8/18
  134. * @since 5.7.2
  135. *
  136. * @param array $args An array of args used in the get_terms() function.
  137. * @return array
  138. */
  139. function acf_get_grouped_terms( $args ) {
  140. // vars
  141. $data = array();
  142. // defaults
  143. $args = wp_parse_args($args, array(
  144. 'taxonomy' => null,
  145. 'hide_empty' => false,
  146. 'update_term_meta_cache' => false,
  147. ));
  148. // vars
  149. $taxonomies = acf_get_taxonomy_labels( acf_get_array($args['taxonomy']) );
  150. $is_single = (count($taxonomies) == 1);
  151. // specify exact taxonomies required for _acf_terms_clauses() to work.
  152. $args['taxonomy'] = array_keys($taxonomies);
  153. // add filter to group results by taxonomy
  154. if( !$is_single ) {
  155. add_filter('terms_clauses', '_acf_terms_clauses', 10, 3);
  156. }
  157. // get terms
  158. $terms = get_terms( $args );
  159. // remove this filter (only once)
  160. if( !$is_single ) {
  161. remove_filter('terms_clauses', '_acf_terms_clauses', 10, 3);
  162. }
  163. // loop
  164. foreach( $taxonomies as $taxonomy => $label ) {
  165. // vars
  166. $this_terms = array();
  167. // populate $this_terms
  168. foreach( $terms as $term ) {
  169. if( $term->taxonomy == $taxonomy ) {
  170. $this_terms[] = $term;
  171. }
  172. }
  173. // bail early if no $items
  174. if( empty($this_terms) ) continue;
  175. // sort into hierachial order
  176. // this will fail if a search has taken place because parents wont exist
  177. if( is_taxonomy_hierarchical($taxonomy) && empty($args['s'])) {
  178. // get all terms from this taxonomy
  179. $all_terms = get_terms(array_merge($args, array(
  180. 'number' => 0,
  181. 'offset' => 0,
  182. 'taxonomy' => $taxonomy
  183. )));
  184. // vars
  185. $length = count($this_terms);
  186. $offset = 0;
  187. // find starting point (offset)
  188. foreach( $all_terms as $i => $term ) {
  189. if( $term->term_id == $this_terms[0]->term_id ) {
  190. $offset = $i;
  191. break;
  192. }
  193. }
  194. // order terms
  195. $parent = acf_maybe_get( $args, 'parent', 0 );
  196. $parent = acf_maybe_get( $args, 'child_of', $parent );
  197. $ordered_terms = _get_term_children( $parent, $all_terms, $taxonomy );
  198. // compare aray lengths
  199. // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
  200. // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
  201. if( count($ordered_terms) == count($all_terms) ) {
  202. $this_terms = array_slice($ordered_terms, $offset, $length);
  203. }
  204. }
  205. // populate group
  206. $data[ $label ] = array();
  207. foreach( $this_terms as $term ) {
  208. $data[ $label ][ $term->term_id ] = $term;
  209. }
  210. }
  211. // return
  212. return $data;
  213. }
  214. /**
  215. * _acf_terms_clauses
  216. *
  217. * Used in the 'terms_clauses' filter to order terms by taxonomy name.
  218. *
  219. * @date 2/8/18
  220. * @since 5.7.2
  221. *
  222. * @param array $pieces Terms query SQL clauses.
  223. * @param array $taxonomies An array of taxonomies.
  224. * @param array $args An array of terms query arguments.
  225. * @return array $pieces
  226. */
  227. function _acf_terms_clauses( $pieces, $taxonomies, $args ) {
  228. // prepend taxonomy to 'orderby' SQL
  229. if( is_array($taxonomies) ) {
  230. $sql = "FIELD(tt.taxonomy,'" . implode("', '", array_map('esc_sql', $taxonomies)) . "')";
  231. $pieces['orderby'] = str_replace("ORDER BY", "ORDER BY $sql,", $pieces['orderby']);
  232. }
  233. // return
  234. return $pieces;
  235. }
  236. /**
  237. * acf_get_pretty_taxonomies
  238. *
  239. * Deprecated in favor of acf_get_taxonomy_labels() function.
  240. *
  241. * @date 7/10/13
  242. * @since 5.0.0
  243. * @deprecated 5.7.2
  244. */
  245. function acf_get_pretty_taxonomies( $taxonomies = array() ) {
  246. return acf_get_taxonomy_labels( $taxonomies );
  247. }
  248. /**
  249. * acf_get_term
  250. *
  251. * Similar to get_term() but with some extra functionality.
  252. *
  253. * @date 19/8/18
  254. * @since 5.7.3
  255. *
  256. * @param mixed $term_id The term ID or a string of "taxonomy:slug".
  257. * @param string $taxonomy The taxonomyname.
  258. * @return WP_Term
  259. */
  260. function acf_get_term( $term_id, $taxonomy = '' ) {
  261. // allow $term_id parameter to be a string of "taxonomy:slug" or "taxonomy:id"
  262. if( is_string($term_id) && strpos($term_id, ':') ) {
  263. list( $taxonomy, $term_id ) = explode(':', $term_id);
  264. $term = get_term_by( 'slug', $term_id, $taxonomy );
  265. if( $term ) return $term;
  266. }
  267. // return
  268. return get_term( $term_id, $taxonomy );
  269. }
  270. /**
  271. * acf_encode_term
  272. *
  273. * Returns a "taxonomy:slug" string for a given WP_Term.
  274. *
  275. * @date 27/8/18
  276. * @since 5.7.4
  277. *
  278. * @param WP_Term $term The term object.
  279. * @return string
  280. */
  281. function acf_encode_term( $term ) {
  282. return "{$term->taxonomy}:{$term->slug}";
  283. }
  284. /**
  285. * acf_decode_term
  286. *
  287. * Decodes a "taxonomy:slug" string into an array of taxonomy and slug.
  288. *
  289. * @date 27/8/18
  290. * @since 5.7.4
  291. *
  292. * @param WP_Term $term The term object.
  293. * @return string
  294. */
  295. function acf_decode_term( $string ) {
  296. if( is_string($string) && strpos($string, ':') ) {
  297. list( $taxonomy, $slug ) = explode(':', $string);
  298. return compact( 'taxonomy', 'slug' );
  299. }
  300. return false;
  301. }
  302. /**
  303. * acf_get_encoded_terms
  304. *
  305. * Returns an array of WP_Term objects from an array of encoded strings
  306. *
  307. * @date 9/9/18
  308. * @since 5.7.5
  309. *
  310. * @param array $values The array of encoded strings.
  311. * @return array
  312. */
  313. function acf_get_encoded_terms( $values ) {
  314. // vars
  315. $terms = array();
  316. // loop over values
  317. foreach( (array) $values as $value ) {
  318. // find term from string
  319. $term = acf_get_term( $value );
  320. // append
  321. if( $term instanceof WP_Term ) {
  322. $terms[] = $term;
  323. }
  324. }
  325. // return
  326. return $terms;
  327. }
  328. /**
  329. * acf_get_choices_from_terms
  330. *
  331. * Returns an array of choices from the terms provided.
  332. *
  333. * @date 8/9/18
  334. * @since 5.7.5
  335. *
  336. * @param array $values and array of WP_Terms objects or encoded strings.
  337. * @param string $format The value format (term_id, slug).
  338. * @return array
  339. */
  340. function acf_get_choices_from_terms( $terms, $format = 'term_id' ) {
  341. // vars
  342. $groups = array();
  343. // get taxonomy lables
  344. $labels = acf_get_taxonomy_labels();
  345. // convert array of encoded strings to terms
  346. $term = reset($terms);
  347. if( !$term instanceof WP_Term ) {
  348. $terms = acf_get_encoded_terms( $terms );
  349. }
  350. // loop over terms
  351. foreach( $terms as $term ) {
  352. $group = $labels[ $term->taxonomy ];
  353. $choice = acf_get_choice_from_term( $term, $format );
  354. $groups[ $group ][ $choice['id'] ] = $choice['text'];
  355. }
  356. // return
  357. return $groups;
  358. }
  359. /**
  360. * acf_get_choices_from_grouped_terms
  361. *
  362. * Returns an array of choices from the grouped terms provided.
  363. *
  364. * @date 8/9/18
  365. * @since 5.7.5
  366. *
  367. * @param array $value A grouped array of WP_Terms objects.
  368. * @param string $format The value format (term_id, slug).
  369. * @return array
  370. */
  371. function acf_get_choices_from_grouped_terms( $value, $format = 'term_id' ) {
  372. // vars
  373. $groups = array();
  374. // loop over values
  375. foreach( $value as $group => $terms ) {
  376. $groups[ $group ] = array();
  377. foreach( $terms as $term_id => $term ) {
  378. $choice = acf_get_choice_from_term( $term, $format );
  379. $groups[ $group ][ $choice['id'] ] = $choice['text'];
  380. }
  381. }
  382. // return
  383. return $groups;
  384. }
  385. /**
  386. * acf_get_choice_from_term
  387. *
  388. * Returns an array containing the id and text for this item.
  389. *
  390. * @date 10/9/18
  391. * @since 5.7.6
  392. *
  393. * @param object $item The item object such as WP_Post or WP_Term.
  394. * @param string $format The value format (term_id, slug)
  395. * @return array
  396. */
  397. function acf_get_choice_from_term( $term, $format = 'term_id' ) {
  398. // vars
  399. $id = $term->term_id;
  400. $text = acf_get_term_title( $term );
  401. // return format
  402. if( $format == 'slug' ) {
  403. $id = acf_encode_term($term);
  404. }
  405. // return
  406. return array(
  407. 'id' => $id,
  408. 'text' => $text
  409. );
  410. }
  411. ?>