NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cia-end-points.php 3.3KB

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