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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. private function _getAttachments($item) {
  48. // Get media
  49. $attached = array();
  50. // Image sizes you want
  51. $desiredSizes = ['thumbnail', 'medium', 'large', 'full'];
  52. foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
  53. $imageSizes = array();
  54. foreach ( $desiredSizes as $size ):
  55. $imageSizes[$size] = wp_get_attachment_image_url($attached_media->ID, $size);
  56. endforeach;
  57. // store size:url map under image id
  58. $attached[$attached_media->ID] = $imageSizes;
  59. endforeach;
  60. return $attached;
  61. }
  62. public function prepare_item_for_response( $args ) {
  63. $collection = array();
  64. // https://developer.wordpress.org/reference/functions/get_posts/
  65. foreach( get_posts($args) as $item ) {
  66. $filtered = default_post_format($item);
  67. // Get those Block!
  68. $filtered[blocks] = get_rearrange_blocks(
  69. parse_blocks( $item->post_content )
  70. );
  71. $filtered[attached] = $this->_getAttachments($item);
  72. // Galleries From blocks
  73. $media_ids = array();
  74. foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
  75. array_push($media_ids, $attached_media->ID);
  76. endforeach;
  77. $filtered[galleries] = get_ids_from_gallery_block(
  78. parse_blocks( $item->post_content )
  79. );
  80. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  81. $filtered[relatedto] = p2p_related_to($item->ID, $item->post_type);
  82. $collection[$item->ID] = $filtered;
  83. }
  84. wp_reset_postdata();
  85. return $collection;
  86. }
  87. }
  88. ?>