NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

class.make-endpoint.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 'post_type' => $this->post_type,
  43. 'post_status' => ['publish'],
  44. //(int) - number of post to show per page (available with Version 2.1). Use 'posts_per_page' => -1 to show all posts.
  45. 'posts_per_page' => -1,
  46. 'page' => 1
  47. // Order DOES NOT WORK because you're
  48. // returning the posts by id - DUH
  49. // 'orderby' => 'date',
  50. // 'order' => 'DESC'
  51. );
  52. // Get parameters from request
  53. // /<id>?limit=<num>
  54. $params = $request->get_params();
  55. if(intval($params['limit']) > 0) {
  56. $args['posts_per_page'] = intval($params['limit']);
  57. }
  58. if(intval($params['id']) > 0) {
  59. $args['include'] = array($params['id']);
  60. }
  61. if(intval($params['p']) > 1) {
  62. $args['page'] = intval($params['p']);
  63. $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page'];
  64. }
  65. // !: Add order asc/desc
  66. // !: Add orderby
  67. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  68. }
  69. public function prepare_single_item_for_response( $args ) {
  70. $collection = array();
  71. // https://developer.wordpress.org/reference/functions/get_posts/
  72. foreach( get_posts($args) as $item ) {
  73. $filtered = default_post_format( $item, false );
  74. $filtered[galleries] = get_ids_from_gallery_block(
  75. parse_blocks($item->post_content)
  76. );
  77. $filtered[attached] = get_images_from_content($item->post_content);
  78. // For your hero URL
  79. $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
  80. $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type );
  81. $content = $item->post_content;
  82. $content = apply_filters('the_content', $content);
  83. $filtered[content] = str_replace(']]>', ']]&gt;', $content);
  84. if($item->post_type === 'episode') $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
  85. array_push($collection, $filtered);
  86. }
  87. wp_reset_postdata();
  88. return $collection;
  89. }
  90. public function prepare_all_items_for_response( $args ) {
  91. $collection = [];
  92. $alphabet_section = 'a';
  93. $q = new WP_Query( $args );
  94. $args['max'] = $q->max_num_pages;
  95. $found_posts = $q->get_posts();
  96. foreach( $found_posts as $post ) {
  97. // print_r($args);
  98. // print_r($post);
  99. $formatted = default_post_format( $post, true );
  100. // For your hero URL
  101. $formatted[hero] = get_post_meta( $post->ID, 'hero_header', true );
  102. // Sticky
  103. $formatted[sticky] = get_post_meta( $post->ID, 'is_sticky', true );
  104. // Don't use the BLOCK system for pages for some reason
  105. if($post->post_type == 'page') $formatted[content] = $post->post_content;
  106. // This is where you add to your letter counter
  107. if($post->post_type == 'artist') {}
  108. array_push($collection, $formatted);
  109. }
  110. wp_reset_postdata();
  111. $collection['_meta'] = $args;
  112. return $collection;
  113. }
  114. }
  115. ?>