NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cia-end-points.php 4.6KB

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/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_upcoming');
  59. $sort_controller->register_custom_route('event/by-upcoming');
  60. $sort_controller = new Make_Sort_By('exhibition', 'by_upcoming');
  61. $sort_controller->register_custom_route('exhibition/by-upcoming');
  62. $sort_controller = new Make_Sort_By('event', 'by_current_and_upcoming');
  63. $sort_controller->register_custom_route('event/by-current-and-upcoming');
  64. $sort_controller = new Make_Sort_By('exhibition', 'by_current_and_upcoming');
  65. $sort_controller->register_custom_route('exhibition/by-current-and-upcoming');
  66. });
  67. /**
  68. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  69. * Depends on: WP REST Cache
  70. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  71. */
  72. add_filter('wp_rest_cache/allowed_endpoints', function () {
  73. // The standard wordpress post_types
  74. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('post', $allowed_endpoints['craft/v2']) )
  75. $allowed_endpoints['craft/v2'][] = 'post';
  76. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('page', $allowed_endpoints['craft/v2']) )
  77. $allowed_endpoints['craft/v2'][] = 'page';
  78. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
  79. $allowed_endpoints['craft/v2'][] = 'media';
  80. // Craft in America custom post_types
  81. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episode', $allowed_endpoints['craft/v2']) )
  82. $allowed_endpoints['craft/v2'][] = 'episode';
  83. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('event', $allowed_endpoints['craft/v2']) )
  84. $allowed_endpoints['craft/v2'][] = 'event';
  85. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibition', $allowed_endpoints['craft/v2']) )
  86. $allowed_endpoints['craft/v2'][] = 'exhibition';
  87. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artist', $allowed_endpoints['craft/v2']) )
  88. $allowed_endpoints['craft/v2'][] = 'artist';
  89. return $allowed_endpoints;
  90. }, 10, 1);
  91. add_filter('excerpt_length', function ($length) {
  92. return 20;
  93. });