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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. include('reformat-blocks.php');
  3. class Make_Endpoint_For extends WP_REST_Controller {
  4. private $post_type;
  5. function __construct($post_type) {
  6. $this->post_type = $post_type;
  7. }
  8. /**
  9. * Register the routes for the objects of the controller.
  10. */
  11. public function register_routes($route) {
  12. $version = '2';
  13. $namespace = 'craft/v' . $version;
  14. register_rest_route( $namespace, '/' . $route, [
  15. array(
  16. 'methods' => WP_REST_Server::READABLE,
  17. 'callback' => array( $this, 'get_items' ),
  18. 'args' => array(),
  19. ),
  20. ]);
  21. register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
  22. array(
  23. 'methods' => WP_REST_Server::READABLE,
  24. 'callback' => array( $this, 'get_item' ),
  25. 'args' => array(
  26. 'context' => array(
  27. 'default' => 'view',
  28. ),
  29. ),
  30. ),
  31. ]);
  32. }
  33. public function get_items( $request ) {
  34. $args = array(
  35. 'post_type' => $this->post_type,
  36. );
  37. //do a query, call another class, etc
  38. $items = new WP_Query($args);
  39. $data = array();
  40. foreach( $items as $item ) {
  41. // Get those Block!
  42. $item->blocks = parse_blocks( $item->post_content );;
  43. $itemdata = $this->prepare_item_for_response( $item, $request );
  44. if($item->ID > 0) {
  45. $data[$item->ID] = $this->prepare_response_for_collection( $itemdata );
  46. }
  47. }
  48. wp_reset_postdata();
  49. return new WP_REST_Response( $data, 200 );
  50. }
  51. public function get_item( $request ) {
  52. $params = $request->get_params();
  53. $args = array(
  54. 'numberposts' => 1,
  55. 'orderby' => 'date',
  56. 'order' => 'DESC',
  57. 'post_type' => $this->post_type,
  58. );
  59. //do a query, call another class, etc
  60. $item = new WP_Query($args);
  61. $data = $this->prepare_item_for_response( $item, $request );
  62. wp_reset_postdata();
  63. //return a response or error based on some conditional
  64. if ( 1 == 1 ) {
  65. return new WP_REST_Response( $data, 200 );
  66. } else {
  67. return new WP_Error( 'code', __( 'message', 'text-domain' ) );
  68. }
  69. }
  70. public function prepare_item_for_response( $item, $request ) {
  71. $filtered = array();
  72. $filtered[id] = $item->ID;
  73. $filtered[type] = $item->post_type;
  74. $filtered[title] = $item->post_title;
  75. $filtered[excerpt] = $item->post_excerpt;
  76. $filtered[date] = $item->post_date;
  77. $filtered[content] = $item->post_content;
  78. $filtered[blocks] = get_rearrange_blocks($item->blocks);
  79. // return $item;
  80. if($item->post_status === 'publish') return $filtered;
  81. }
  82. }
  83. ?>