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

cia-end-points.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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'];
  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. });
  57. /**
  58. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  59. * Depends on: WP REST Cache
  60. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  61. */
  62. add_filter('wp_rest_cache/allowed_endpoints', function () {
  63. $types = get_all_post_types();
  64. foreach($types as $type) {
  65. if ( !isset($allowed_endpoints['craft/v2']) || !in_array($type, $allowed_endpoints['craft/v2']) )
  66. $allowed_endpoints['craft/v2'][] = $type;
  67. }
  68. return $allowed_endpoints;
  69. }, 10, 1);
  70. add_filter('excerpt_length', function ($length) {
  71. return 60;
  72. });