NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

class.make-endpoint.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. include('settings.php');
  3. include('reformat-blocks.php');
  4. include('related-items.php');
  5. include('formats.php');
  6. class Make_Endpoint_For extends WP_REST_Controller {
  7. private $post_type;
  8. function __construct($post_type) {
  9. $this->post_type = $post_type;
  10. }
  11. /**
  12. * Register the routes for the objects of the controller.
  13. */
  14. public function register_custom_route($route) {
  15. $version = '2';
  16. $namespace = 'craft/v' . $version;
  17. register_rest_route( $namespace, '/' . $route, [
  18. array(
  19. 'methods' => WP_REST_Server::READABLE,
  20. 'callback' => array( $this, 'get_all_items' )
  21. ),
  22. ]);
  23. register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
  24. array(
  25. 'methods' => WP_REST_Server::READABLE,
  26. 'callback' => array( $this, 'get_single_item' )
  27. ),
  28. ]);
  29. }
  30. public function get_single_item( $request ) {
  31. $args = array(
  32. 'numberposts' => 1,
  33. 'post_type' => $this->post_type
  34. );
  35. $params = $request->get_params();
  36. if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
  37. if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
  38. return new WP_REST_Response( $this->prepare_single_item_for_response($args), 200 );
  39. }
  40. public function get_all_items( $request ) {
  41. $args = array(
  42. 'numberposts' => 999999,
  43. 'post_type' => $this->post_type,
  44. // Order DOES NOT WORK because you're
  45. // returning the posts by id - DUH
  46. // 'orderby' => 'date',
  47. // 'order' => 'DESC'
  48. );
  49. // Get parameters from request
  50. // /<id>?limit=<num>
  51. $params = $request->get_params();
  52. if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
  53. if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
  54. // !: Add order asc/desc
  55. // !: Add orderby
  56. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  57. }
  58. public function prepare_single_item_for_response( $args ) {
  59. $collection = array();
  60. // https://developer.wordpress.org/reference/functions/get_posts/
  61. foreach( get_posts($args) as $item ) {
  62. $filtered = default_post_format( $item, false );
  63. $filtered[galleries] = get_ids_from_gallery_block(
  64. parse_blocks($item->post_content)
  65. );
  66. $filtered[attached] = get_images_from_content($item->post_content);
  67. // For your hero URL
  68. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  69. $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type );
  70. $content = $item->post_content;
  71. $content = apply_filters('the_content', $content);
  72. $filtered[content] = str_replace(']]>', ']]&gt;', $content);
  73. if($item->post_type === 'episode') $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
  74. $collection[$item->ID] = $filtered;
  75. }
  76. wp_reset_postdata();
  77. return $collection;
  78. }
  79. public function prepare_all_items_for_response( $args ) {
  80. $collection = array();
  81. $alphabet_section = 'a';
  82. // https://developer.wordpress.org/reference/functions/get_posts/
  83. foreach( get_posts($args) as $item ) {
  84. // print_r($args);
  85. // print_r($item);
  86. $filtered = default_post_format( $item, true );
  87. // For your hero URL
  88. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  89. // Sticky
  90. $filtered[sticky] = get_post_meta( $item->ID, 'is_sticky', true );
  91. if($item->post_type == 'page') {
  92. $filtered[content] = $item->post_content;
  93. }
  94. if($item->post_type == 'artist') {
  95. // This is where you add to your letter counter
  96. }
  97. array_push($collection, $filtered);
  98. }
  99. wp_reset_postdata();
  100. return $collection;
  101. }
  102. }
  103. ?>