NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class.make-endpoint.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. include('reformat-blocks.php');
  3. include('related-items.php');
  4. include('formats.php');
  5. class Make_Endpoint_For extends WP_REST_Controller {
  6. private $post_type;
  7. function __construct($post_type) {
  8. $this->post_type = $post_type;
  9. }
  10. /**
  11. * Register the routes for the objects of the controller.
  12. */
  13. public function register_custom_route($route) {
  14. $version = '2';
  15. $namespace = 'craft/v' . $version;
  16. register_rest_route( $namespace, '/' . $route, [
  17. array(
  18. 'methods' => WP_REST_Server::READABLE,
  19. 'callback' => array( $this, 'get_items' )
  20. ),
  21. ]);
  22. register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
  23. array(
  24. 'methods' => WP_REST_Server::READABLE,
  25. 'callback' => array( $this, 'get_items' )
  26. ),
  27. ]);
  28. }
  29. public function get_items( $request ) {
  30. $args = array(
  31. 'numberposts' => -1,
  32. 'post_type' => $this->post_type,
  33. // Order DOES NOT WORK because you're
  34. // returning the posts by id - DUH
  35. // 'orderby' => 'date',
  36. // 'order' => 'DESC'
  37. );
  38. // Get parameters from request
  39. // /<id>?limit=<num>
  40. $params = $request->get_params();
  41. if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
  42. if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
  43. // !: Add order asc/desc
  44. // !: Add orderby
  45. return new WP_REST_Response( $this->prepare_item_for_response($args), 200 );
  46. }
  47. public function prepare_item_for_response( $args ) {
  48. $collection = array();
  49. // https://developer.wordpress.org/reference/functions/get_posts/
  50. foreach( get_posts($args) as $item ) {
  51. $filtered = default_post_format($item);
  52. // Get those Block!
  53. $filtered[blocks] = get_rearrange_blocks(
  54. parse_blocks( $item->post_content )
  55. );
  56. // Get media
  57. $media_ids = [];
  58. foreach (get_attached_media( '', $item->ID ) as $attached_media):
  59. array_push($media_ids, $attached_media->ID);
  60. endforeach;
  61. $filtered[attached] = $media_ids;
  62. // Galleries From blocks
  63. $galleries = [];
  64. foreach (get_attached_media( '', $item->ID ) as $attached_media):
  65. array_push($media_ids, $attached_media->ID);
  66. endforeach;
  67. $filtered[galleries] = get_ids_from_gallery_block(
  68. parse_blocks( $item->post_content )
  69. );
  70. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  71. $filtered[relatedto] = p2p_related_to($item->ID, $item->post_type);
  72. $collection[$item->ID] = $filtered;
  73. }
  74. wp_reset_postdata();
  75. return $collection;
  76. }
  77. }
  78. ?>