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

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