NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

class.make-endpoint.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_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_items' )
  27. ),
  28. ]);
  29. }
  30. public function get_items( $request ) {
  31. $args = array(
  32. 'numberposts' => -1,
  33. 'post_type' => $this->post_type,
  34. // Order DOES NOT WORK because you're
  35. // returning the posts by id - DUH
  36. // 'orderby' => 'date',
  37. // 'order' => 'DESC'
  38. );
  39. // Get parameters from request
  40. // /<id>?limit=<num>
  41. $params = $request->get_params();
  42. if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
  43. if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
  44. // !: Add order asc/desc
  45. // !: Add orderby
  46. return new WP_REST_Response( $this->prepare_item_for_response($args), 200 );
  47. }
  48. private function _getAttachments($item, $galleries) {
  49. // Get media
  50. $desiredSizes = ['thumbnail', 'medium', 'large', 'full'];
  51. $all_image_ids_in_gallery = array();
  52. $all_image_ids = array();
  53. foreach ($galleries as $gallery) {
  54. array_push($all_image_ids_in_gallery, $gallery[attrs][ids]);
  55. }
  56. foreach ($all_image_ids_in_gallery as $ids) {
  57. foreach ($ids as $id) {
  58. array_push($all_image_ids, $id);
  59. }
  60. }
  61. $attachment_map = array();
  62. foreach ($all_image_ids as $id) {
  63. $imageSizes = array();
  64. foreach ($desiredSizes as $size) {
  65. $imageSizes[$size] = wp_get_attachment_image_url($id, $size);
  66. }
  67. // store size:url map under image id
  68. $attachment_map[$id] = $imageSizes;
  69. }
  70. return $attachment_map;
  71. }
  72. public function prepare_item_for_response( $args ) {
  73. $collection = array();
  74. // https://developer.wordpress.org/reference/functions/get_posts/
  75. foreach( get_posts($args) as $item ) {
  76. $filtered = default_post_format($item);
  77. // Get those Block!
  78. $filtered[blocks] = get_rearrange_blocks(
  79. parse_blocks( $item->post_content )
  80. );
  81. // Galleries From blocks
  82. $media_ids = array();
  83. foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
  84. array_push($media_ids, $attached_media->ID);
  85. endforeach;
  86. $filtered[galleries] = get_ids_from_gallery_block(
  87. parse_blocks( $item->post_content )
  88. );
  89. // Find all image info
  90. $filtered[attached] = $this->_getAttachments($item, $filtered[galleries]);
  91. // For your hero URL
  92. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  93. $filtered[relatedto] = p2p_related_to($item->ID, $item->post_type);
  94. $collection[$item->ID] = $filtered;
  95. }
  96. wp_reset_postdata();
  97. return $collection;
  98. }
  99. }
  100. ?>