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

cia-end-points.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-sortby.php');
  19. add_action( 'rest_api_init', function () {
  20. /**
  21. * The standard wordpress post_types
  22. */
  23. $page_controller = new Make_Endpoint_For('page');
  24. $page_controller->register_custom_route('pages');
  25. $media_controller = new Make_Endpoint_For('media');
  26. $media_controller->register_custom_route('media');
  27. $post_controller = new Make_Endpoint_For('post');
  28. $post_controller->register_custom_route('posts');
  29. /**
  30. * Craft in America custom post_types
  31. */
  32. $episode_controller = new Make_Endpoint_For('episode');
  33. $episode_controller->register_custom_route('episodes');
  34. $artist_controller = new Make_Endpoint_For('artist');
  35. $artist_controller->register_custom_route('artists');
  36. $event_controller = new Make_Endpoint_For('event');
  37. $event_controller->register_custom_route('events');
  38. $exhibition_controller = new Make_Endpoint_For('exhibition');
  39. $exhibition_controller->register_custom_route('exhibitions');
  40. /**
  41. * Craft in America custom sort_types
  42. */
  43. $sort_controller = new Make_Sort_By('artist', 'by_alpha');
  44. $sort_controller->register_custom_route('artists/by-alpha');
  45. $sort_controller = new Make_Sort_By('artist', 'by_material');
  46. $sort_controller->register_custom_route('artists/by-material');
  47. });
  48. /**
  49. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  50. * Depends on: WP REST Cache
  51. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  52. */
  53. add_filter('wp_rest_cache/allowed_endpoints', function () {
  54. // The standard wordpress post_types
  55. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('posts', $allowed_endpoints['craft/v2']) )
  56. $allowed_endpoints['craft/v2'][] = 'posts';
  57. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('pages', $allowed_endpoints['craft/v2']) )
  58. $allowed_endpoints['craft/v2'][] = 'pages';
  59. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
  60. $allowed_endpoints['craft/v2'][] = 'media';
  61. // Craft in America custom post_types
  62. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episodes', $allowed_endpoints['craft/v2']) )
  63. $allowed_endpoints['craft/v2'][] = 'episodes';
  64. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('events', $allowed_endpoints['craft/v2']) )
  65. $allowed_endpoints['craft/v2'][] = 'events';
  66. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibitions', $allowed_endpoints['craft/v2']) )
  67. $allowed_endpoints['craft/v2'][] = 'exhibitions';
  68. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artists', $allowed_endpoints['craft/v2']) )
  69. $allowed_endpoints['craft/v2'][] = 'artists';
  70. return $allowed_endpoints;
  71. }, 10, 1);