NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

formats.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. function make_taxonomy_endpoint_for( $terms ) {
  3. $type_slugs = [];
  4. foreach ($terms as $type_slug) {
  5. array_push($type_slugs, $type_slug->slug);
  6. }
  7. return $type_slugs;
  8. }
  9. function default_post_format( $item, $include_content ) {
  10. $filtered = [];
  11. $filtered[id] = $item->ID;
  12. $filtered[slug] = $item->post_name;
  13. $filtered[type] = $item->post_type;
  14. $filtered[title] = $item->post_title;
  15. $filtered[excerpt] = $item->post_excerpt;
  16. $filtered[date] = $item->post_date;
  17. if($include_content) {
  18. $content = $item->post_content;
  19. $content = apply_filters('the_content', $content);
  20. $filtered[content] = wp_specialchars_decode($content);
  21. }
  22. if(!$filtered[excerpt]) {
  23. $excerpt = get_the_excerpt($item);
  24. $filtered[excerpt] = wp_specialchars_decode($excerpt);
  25. }
  26. $filtered[featured] = get_the_post_thumbnail_url($item);
  27. if(!$filtered[featured]) {
  28. $thumbnailId = get_post_meta( $item->ID, '_thumbnail_id', true );
  29. $filtered[featured] = get_post($thumbnailId)->guid;
  30. }
  31. $filtered[thumb] = get_the_post_thumbnail_url($item, 'medium');
  32. if(!$filtered[thumb]) {
  33. $thumbnailId = get_post_meta( $item->ID, '_thumbnail_id', true );
  34. $filtered[thumb] = get_post($thumbnailId)->guid;
  35. }
  36. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  37. // Materials + type endpoints
  38. $posts_with_type = ['artist', 'exhibition', 'event', 'short', 'technique', 'guide', 'object', 'publication' ];
  39. if( in_array($item->post_type, $posts_with_type) ) {
  40. $filtered[materials] = make_taxonomy_endpoint_for(get_the_terms( $item, 'material'));
  41. $filtered[subtypes] = make_taxonomy_endpoint_for(get_the_terms( $item, $item->post_type . '_type' ));
  42. }
  43. // Custom fields endpoint
  44. if($item->post_type === 'episode' && $include_content) $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
  45. if($item->post_type === 'artist') $filtered[sortname] = get_post_meta( $item->ID, 'artist-sort-name', true );
  46. $posts_with_date = ['exhibition', 'event'];
  47. if( in_array($item->post_type, $posts_with_date) ) {
  48. $key_prefix = $item->post_type;
  49. $key_suffix = 'time';
  50. // Exhibition meta key is very old so we change it back to exhibit
  51. if($item->post_type == 'exhibition') {
  52. $key_prefix = 'exhibit';
  53. $key_suffix = 'date';
  54. }
  55. $filtered[start] = get_post_meta($item->ID, $key_prefix . '-start-' . $key_suffix, true);
  56. $filtered[end] = get_post_meta($item->ID, $key_prefix . '-end-' . $key_suffix, true);
  57. $filtered[current] = time() <= (int)$filtered[end] && time() >= (int)$filtered[start];
  58. }
  59. // Post categories and tags (store just the slugs)
  60. if($item->post_type === 'post') {
  61. $filtered[categories] = make_taxonomy_endpoint_for(get_the_terms( $item, 'category' ));
  62. // $filtered[tags] = make_taxonomy_endpoint_for(get_the_terms( $item, 'post_tag' ));
  63. }
  64. if($item->related_episode) {
  65. $filtered[related_episode] = strtolower($item->related_episode);
  66. }
  67. return $filtered;
  68. }
  69. function minimal_post_format( $item ) {
  70. $filtered = [];
  71. $filtered[id] = $item->ID;
  72. $filtered[slug] = $item->post_name;
  73. $filtered[type] = $item->post_type;
  74. $filtered[title] = $item->post_title;
  75. $filtered[thumb] = get_the_post_thumbnail_url($item, 'medium');
  76. if(!$filtered[thumb]) {
  77. $thumbnailId = get_post_meta( $item->ID, '_thumbnail_id', true );
  78. $filtered[thumb] = get_post($thumbnailId)->guid;
  79. }
  80. // Always include a small excerpt for artists
  81. // because they appear on the episode page
  82. $posts_with_excerpt = ['artist'];
  83. if( in_array($item->post_type, $posts_with_excerpt) ) {
  84. $excerpt = get_the_excerpt($item);
  85. $filtered[excerpt] = wp_specialchars_decode($excerpt);
  86. }
  87. // Always include a date for date based
  88. $posts_with_date = ['exhbition', 'event'];
  89. if( in_array($item->post_type, $posts_with_date) ) {
  90. $filtered[start] = get_post_meta($item->ID, $item->post_type . '-start-time', true);
  91. $filtered[end] = get_post_meta($item->ID, $item->post_type . '-end-time', true);
  92. $filtered[current] = time() <= (int)$filtered[end] && time() >= (int)$filtered[start];
  93. }
  94. return $filtered;
  95. }
  96. ?>