NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cia-end-points.php 2.7KB

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