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 2.8KB

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