NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cia-end-points.php 5.4KB

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/class.make-endpoint.php');
  18. require_once('includes/class.make-sticky.php');
  19. require_once('includes/class.make-sortby.php');
  20. add_action( 'rest_api_init', function () {
  21. /**
  22. * The standard wordpress post_types
  23. */
  24. $page_controller = new Make_Endpoint_For('page');
  25. $page_controller->register_custom_route('page');
  26. $media_controller = new Make_Endpoint_For('media');
  27. $media_controller->register_custom_route('media');
  28. $post_controller = new Make_Endpoint_For('post');
  29. $post_controller->register_custom_route('post');
  30. /**
  31. * Craft in America custom post_types
  32. */
  33. $episode_controller = new Make_Endpoint_For('episode');
  34. $episode_controller->register_custom_route('episode');
  35. $artist_controller = new Make_Endpoint_For('artist');
  36. $artist_controller->register_custom_route('artist');
  37. $event_controller = new Make_Endpoint_For('event');
  38. $event_controller->register_custom_route('event');
  39. $exhibition_controller = new Make_Endpoint_For('exhibition');
  40. $exhibition_controller->register_custom_route('exhibition');
  41. $guide_controller = new Make_Endpoint_For('guide');
  42. $guide_controller->register_custom_route('guide');
  43. $short_controller = new Make_Endpoint_For('short');
  44. $short_controller->register_custom_route('short');
  45. $object_controller = new Make_Endpoint_For('object');
  46. $object_controller->register_custom_route('object');
  47. $publication_controller = new Make_Endpoint_For('publication');
  48. $publication_controller->register_custom_route('publication');
  49. $sticky_controller = new Make_Sticky_Endpoint();
  50. $sticky_controller->register_custom_route('sticky');
  51. /**
  52. * Craft in America custom sort_types
  53. */
  54. $sort_controller = new Make_Sort_By('artist', 'by_alpha');
  55. $sort_controller->register_custom_route('artist/by-alpha');
  56. $sort_controller = new Make_Sort_By('artist', 'by_material');
  57. $sort_controller->register_custom_route('artist/by-material');
  58. $sort_controller = new Make_Sort_By('event', 'by_past');
  59. $sort_controller->register_custom_route('event/by-past');
  60. $sort_controller = new Make_Sort_By('exhibition', 'by_past');
  61. $sort_controller->register_custom_route('exhibition/by-past');
  62. $sort_controller = new Make_Sort_By('event', 'by_current');
  63. $sort_controller->register_custom_route('event/by-current');
  64. $sort_controller = new Make_Sort_By('exhibition', 'by_current');
  65. $sort_controller->register_custom_route('exhibition/by-current');
  66. $sort_controller = new Make_Sort_By('event', 'by_current_and_upcoming');
  67. $sort_controller->register_custom_route('event/by-current-and-upcoming');
  68. $sort_controller = new Make_Sort_By('exhibition', 'by_current_and_upcoming');
  69. $sort_controller->register_custom_route('exhibition/by-current-and-upcoming');
  70. });
  71. /**
  72. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  73. * Depends on: WP REST Cache
  74. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  75. */
  76. add_filter('wp_rest_cache/allowed_endpoints', function () {
  77. // The standard wordpress post_types
  78. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('post', $allowed_endpoints['craft/v2']) )
  79. $allowed_endpoints['craft/v2'][] = 'post';
  80. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('page', $allowed_endpoints['craft/v2']) )
  81. $allowed_endpoints['craft/v2'][] = 'page';
  82. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
  83. $allowed_endpoints['craft/v2'][] = 'media';
  84. // Craft in America custom post_types
  85. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episode', $allowed_endpoints['craft/v2']) )
  86. $allowed_endpoints['craft/v2'][] = 'episode';
  87. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('event', $allowed_endpoints['craft/v2']) )
  88. $allowed_endpoints['craft/v2'][] = 'event';
  89. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibition', $allowed_endpoints['craft/v2']) )
  90. $allowed_endpoints['craft/v2'][] = 'exhibition';
  91. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artist', $allowed_endpoints['craft/v2']) )
  92. $allowed_endpoints['craft/v2'][] = 'artist';
  93. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('guide', $allowed_endpoints['craft/v2']) )
  94. $allowed_endpoints['craft/v2'][] = 'guide';
  95. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('short', $allowed_endpoints['craft/v2']) )
  96. $allowed_endpoints['craft/v2'][] = 'short';
  97. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('object', $allowed_endpoints['craft/v2']) )
  98. $allowed_endpoints['craft/v2'][] = 'object';
  99. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('publication', $allowed_endpoints['craft/v2']) )
  100. $allowed_endpoints['craft/v2'][] = 'publication';
  101. return $allowed_endpoints;
  102. }, 10, 1);
  103. add_filter('excerpt_length', function ($length) {
  104. return 60;
  105. });