NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

class.make-endpoint.php 4.7KB

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