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

cia-end-points.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $sticky_controller = new Make_Sticky_Endpoint();
  42. $sticky_controller->register_custom_route('sticky');
  43. /**
  44. * Craft in America custom sort_types
  45. */
  46. $sort_controller = new Make_Sort_By('artist', 'by_alpha');
  47. $sort_controller->register_custom_route('artist/by-alpha');
  48. $sort_controller = new Make_Sort_By('artist', 'by_material');
  49. $sort_controller->register_custom_route('artist/by-material');
  50. $sort_controller = new Make_Sort_By('event', 'by_past');
  51. $sort_controller->register_custom_route('event/by-past');
  52. $sort_controller = new Make_Sort_By('exhibition', 'by_past');
  53. $sort_controller->register_custom_route('exhibition/by-past');
  54. $sort_controller = new Make_Sort_By('event', 'by_current');
  55. $sort_controller->register_custom_route('event/by-current');
  56. $sort_controller = new Make_Sort_By('exhibition', 'by_current');
  57. $sort_controller->register_custom_route('exhibition/by-current');
  58. $sort_controller = new Make_Sort_By('event', 'by_current_and_upcoming');
  59. $sort_controller->register_custom_route('event/by-current-and-upcoming');
  60. $sort_controller = new Make_Sort_By('exhibition', 'by_current_and_upcoming');
  61. $sort_controller->register_custom_route('exhibition/by-current-and-upcoming');
  62. });
  63. /**
  64. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  65. * Depends on: WP REST Cache
  66. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  67. */
  68. add_filter('wp_rest_cache/allowed_endpoints', function () {
  69. // The standard wordpress post_types
  70. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('post', $allowed_endpoints['craft/v2']) )
  71. $allowed_endpoints['craft/v2'][] = 'post';
  72. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('page', $allowed_endpoints['craft/v2']) )
  73. $allowed_endpoints['craft/v2'][] = 'page';
  74. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
  75. $allowed_endpoints['craft/v2'][] = 'media';
  76. // Craft in America custom post_types
  77. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episode', $allowed_endpoints['craft/v2']) )
  78. $allowed_endpoints['craft/v2'][] = 'episode';
  79. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('event', $allowed_endpoints['craft/v2']) )
  80. $allowed_endpoints['craft/v2'][] = 'event';
  81. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibition', $allowed_endpoints['craft/v2']) )
  82. $allowed_endpoints['craft/v2'][] = 'exhibition';
  83. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artist', $allowed_endpoints['craft/v2']) )
  84. $allowed_endpoints['craft/v2'][] = 'artist';
  85. return $allowed_endpoints;
  86. }, 10, 1);
  87. add_filter('excerpt_length', function ($length) {
  88. return 20;
  89. });