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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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' => -1,
  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. private function _getAttachments($item, $galleries) {
  59. // Get media
  60. $desiredSizes = ['thumbnail', 'medium', 'large', 'full'];
  61. $all_image_ids_in_gallery = array();
  62. $all_image_ids = array();
  63. foreach ( $galleries as $gallery ) {
  64. array_push( $all_image_ids_in_gallery, $gallery[attrs][ids] );
  65. }
  66. foreach ( $all_image_ids_in_gallery as $ids ) {
  67. foreach ( $ids as $id ) {
  68. array_push($all_image_ids, $id);
  69. }
  70. }
  71. $attachment_map = array();
  72. foreach ( $all_image_ids as $id ) {
  73. $imageSizes = array();
  74. foreach ( $desiredSizes as $size ) {
  75. $imageSizes[$size] = wp_get_attachment_image_url( $id, $size );
  76. }
  77. // store size:url map under image id
  78. $attachment_map[$id] = $imageSizes;
  79. }
  80. return $attachment_map;
  81. }
  82. public function prepare_single_item_for_response( $args ) {
  83. $collection = array();
  84. // https://developer.wordpress.org/reference/functions/get_posts/
  85. foreach( get_posts($args) as $item ) {
  86. $filtered = default_post_format( $item, false );
  87. // Get those Block!
  88. $filtered[blocks] = get_rearrange_blocks(
  89. parse_blocks( $item->post_content )
  90. );
  91. // Galleries From blocks
  92. $media_ids = array();
  93. foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
  94. array_push($media_ids, $attached_media->ID);
  95. endforeach;
  96. $filtered[galleries] = get_ids_from_gallery_block(
  97. parse_blocks( $item->post_content )
  98. );
  99. // Find all image info
  100. $filtered[attached] = $this->_getAttachments( $item, $filtered[galleries] );
  101. // For your hero URL
  102. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  103. $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type );
  104. $collection[$item->ID] = $filtered;
  105. }
  106. wp_reset_postdata();
  107. return $collection;
  108. }
  109. public function prepare_all_items_for_response( $args ) {
  110. $collection = array();
  111. // https://developer.wordpress.org/reference/functions/get_posts/
  112. foreach( get_posts($args) as $item ) {
  113. $filtered = default_post_format( $item, true );
  114. // Find all image info
  115. $filtered[attached] = $this->_getAttachments( $item, $filtered[galleries] );
  116. // For your hero URL
  117. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  118. // Sticky
  119. $filtered[sticky] = get_post_meta( $item->ID, 'is_sticky', true );
  120. // $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type );
  121. $collection[$item->ID] = $filtered;
  122. }
  123. wp_reset_postdata();
  124. return $collection;
  125. }
  126. }
  127. ?>