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

cia-end-points.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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-endpoint.php');
  19. require_once('includes/class.make-terms.php');
  20. require_once('includes/class.make-sticky.php');
  21. require_once('includes/class.make-sortby.php');
  22. function _unsnake($input) {
  23. return str_replace('_', '-', $input);
  24. }
  25. function _make_sorts($post_types, $sorts_types) {
  26. foreach ($sorts_types as $sort) {
  27. $unsnaked = _unsnake($sort);
  28. foreach ($post_types as $type) {
  29. $sort_controller = new Make_Sort_By($type, $sort);
  30. $sort_controller->register_custom_route("$type/$unsnaked");
  31. }
  32. }
  33. }
  34. add_action( 'rest_api_init', function () {
  35. /**
  36. * Custom post type endpoints
  37. */
  38. $types = get_all_post_types();
  39. foreach($types as $type) {
  40. $controller = new Make_Endpoint_For($type);
  41. $controller->register_custom_route($type);
  42. }
  43. /**
  44. * Sticky endpoint
  45. */
  46. $sticky_controller = new Make_Sticky_Endpoint();
  47. $sticky_controller->register_custom_route('sticky');
  48. /**
  49. * Terms endpoint
  50. */
  51. $terms_controller = new Make_Terms_Endpoint();
  52. $terms_controller->register_custom_route('terms');
  53. /**
  54. * Craft in America custom sort_types
  55. */
  56. $artist_sorts = ['by_alpha'];
  57. $by_alpha_types = ['artist'];
  58. _make_sorts($by_alpha_types, $artist_sorts);
  59. $date_sorts = ['by_past', 'by_current_and_upcoming'];
  60. $by_date_types = ['exhibition', 'event'];
  61. _make_sorts($by_date_types, $date_sorts);
  62. $episode_sorts = ['by_episode'];
  63. $by_episode_types = [
  64. 'artist', 'guide', 'short'
  65. ];
  66. _make_sorts($by_episode_types, $episode_sorts);
  67. $material_sorts = ['by_material'];
  68. $by_material_types = [
  69. 'artist', 'guide', 'short',
  70. 'object', 'publication',
  71. ];
  72. _make_sorts($by_material_types, $material_sorts);
  73. $subtype_sorts = ['by_type'];
  74. $by_subtype_types = [
  75. 'artist', 'event', 'post'
  76. ];
  77. _make_sorts($by_subtype_types, $subtype_sorts);
  78. });
  79. /**
  80. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  81. * Depends on: WP REST Cache
  82. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  83. */
  84. add_filter('wp_rest_cache/allowed_endpoints', function () {
  85. $types = get_all_post_types();
  86. foreach($types as $type) {
  87. if ( !isset($allowed_endpoints['craft/v2']) || !in_array($type, $allowed_endpoints['craft/v2']) )
  88. $allowed_endpoints['craft/v2'][] = $type;
  89. }
  90. return $allowed_endpoints;
  91. }, 10, 1);
  92. add_filter('excerpt_length', function ($length) {
  93. return 30;
  94. });