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