NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

cia-end-points.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Craft in America Custom Endpoints Plugin
  4. *
  5. * @since 1.0.0
  6. * @package cia_endpoints
  7. *
  8. * @wordpress-plugin
  9. * Plugin Name: Craft in America - API Endpoints
  10. * Plugin URI:
  11. * Description: Plugin that adds custom rest interface
  12. * Version: 1.0.0
  13. * Author: TOJ <john@yvvas.com>
  14. */
  15. // If this file is called directly, abort.
  16. if ( ! defined( 'WPINC' ) ) { die; }
  17. require_once('includes/all-types.php');
  18. require_once('includes/class.make-oneofeach.php');
  19. require_once('includes/class.make-endpoint.php');
  20. require_once('includes/class.make-terms.php');
  21. require_once('includes/class.make-sticky.php');
  22. require_once('includes/class.make-sortby.php');
  23. require_once('includes/class.make-search.php');
  24. function _unsnake($input) {
  25. return str_replace('_', '-', $input);
  26. }
  27. function _make_sorts($post_types, $sorts_types) {
  28. foreach ($sorts_types as $sort) {
  29. $unsnaked = _unsnake($sort);
  30. foreach ($post_types as $type) {
  31. $sort_controller = new Make_Sort_By($type, $sort);
  32. $sort_controller->register_custom_route("$type/$unsnaked");
  33. }
  34. }
  35. }
  36. add_action( 'rest_api_init', function () {
  37. /**
  38. * Custom post type endpoints
  39. */
  40. $types = get_all_post_types();
  41. foreach($types as $type) {
  42. $controller = new Make_Endpoint_For($type);
  43. $controller->register_custom_route($type);
  44. }
  45. /**
  46. * Sticky endpoint
  47. */
  48. $sticky_controller = new Make_Sticky_Endpoint();
  49. $sticky_controller->register_custom_route('sticky');
  50. /**
  51. * OneEach endpoint
  52. */
  53. $oneeach_controller = new Make_One_Each_Endpoint();
  54. $oneeach_controller->register_custom_route('oneofeach');
  55. /**
  56. * Terms endpoint
  57. */
  58. $terms_controller = new Make_Terms_Endpoint();
  59. $terms_controller->register_custom_route('terms');
  60. /**
  61. * Search endpoint
  62. */
  63. $search_controller = new Make_Search_Endpoint();
  64. $search_controller->register_custom_route('search');
  65. /**
  66. * Craft in America custom sort_types
  67. */
  68. $artist_sorts = ['by_alpha'];
  69. $by_alpha_types = ['artist'];
  70. _make_sorts($by_alpha_types, $artist_sorts);
  71. $date_sorts = ['by_past', 'by_current_and_upcoming'];
  72. $by_date_types = ['exhibition', 'event'];
  73. _make_sorts($by_date_types, $date_sorts);
  74. $episode_sorts = ['by_episode'];
  75. $by_episode_types = [
  76. 'artist', 'guide', 'short',
  77. 'technique'
  78. ];
  79. _make_sorts($by_episode_types, $episode_sorts);
  80. $material_sorts = ['by_material'];
  81. $by_material_types = [
  82. 'artist', 'exhibition', 'short', 'technique', 'guide', 'object', 'publication'
  83. ];
  84. _make_sorts($by_material_types, $material_sorts);
  85. $subtype_sorts = ['by_type'];
  86. $by_subtype_types = [
  87. 'artist', 'event', 'post'
  88. ];
  89. _make_sorts($by_subtype_types, $subtype_sorts);
  90. });
  91. /**
  92. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  93. * Depends on: WP REST Cache
  94. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  95. */
  96. add_filter('wp_rest_cache/allowed_endpoints', function () {
  97. $types = get_all_post_types();
  98. foreach($types as $type) {
  99. if ( !isset($allowed_endpoints['craft/v2']) || !in_array($type, $allowed_endpoints['craft/v2']) )
  100. $allowed_endpoints['craft/v2'][] = $type;
  101. }
  102. return $allowed_endpoints;
  103. }, 10, 1);
  104. add_filter('excerpt_length', function ($length) {
  105. return 30;
  106. });