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

cia-end-points.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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('pages');
  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('posts');
  30. /**
  31. * Craft in America custom post_types
  32. */
  33. $episode_controller = new Make_Endpoint_For('episode');
  34. $episode_controller->register_custom_route('episodes');
  35. $artist_controller = new Make_Endpoint_For('artist');
  36. $artist_controller->register_custom_route('artists');
  37. $event_controller = new Make_Endpoint_For('event');
  38. $event_controller->register_custom_route('events');
  39. $exhibition_controller = new Make_Endpoint_For('exhibition');
  40. $exhibition_controller->register_custom_route('exhibitions');
  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('artists/by-alpha');
  48. $sort_controller = new Make_Sort_By('artist', 'by_material');
  49. $sort_controller->register_custom_route('artists/by-material');
  50. });
  51. /**
  52. * Register the /wp-json/craft/v2/<custom endpoint> so it will be cached
  53. * Depends on: WP REST Cache
  54. * https://medium.com/@lodewijkm/our-headless-wordpress-journey-part-i-speeding-up-the-rest-api-aef76a898418
  55. */
  56. add_filter('wp_rest_cache/allowed_endpoints', function () {
  57. // The standard wordpress post_types
  58. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('posts', $allowed_endpoints['craft/v2']) )
  59. $allowed_endpoints['craft/v2'][] = 'posts';
  60. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('pages', $allowed_endpoints['craft/v2']) )
  61. $allowed_endpoints['craft/v2'][] = 'pages';
  62. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('media', $allowed_endpoints['craft/v2']) )
  63. $allowed_endpoints['craft/v2'][] = 'media';
  64. // Craft in America custom post_types
  65. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('episodes', $allowed_endpoints['craft/v2']) )
  66. $allowed_endpoints['craft/v2'][] = 'episodes';
  67. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('events', $allowed_endpoints['craft/v2']) )
  68. $allowed_endpoints['craft/v2'][] = 'events';
  69. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('exhibitions', $allowed_endpoints['craft/v2']) )
  70. $allowed_endpoints['craft/v2'][] = 'exhibitions';
  71. if ( !isset($allowed_endpoints['craft/v2']) || !in_array('artists', $allowed_endpoints['craft/v2']) )
  72. $allowed_endpoints['craft/v2'][] = 'artists';
  73. return $allowed_endpoints;
  74. }, 10, 1);