|
|
@@ -10,76 +10,50 @@ class Make_Endpoint_For extends WP_REST_Controller {
|
|
10
|
10
|
/**
|
|
11
|
11
|
* Register the routes for the objects of the controller.
|
|
12
|
12
|
*/
|
|
13
|
|
- public function register_routes($route) {
|
|
|
13
|
+ public function register_custom_route($route) {
|
|
14
|
14
|
$version = '2';
|
|
15
|
15
|
$namespace = 'craft/v' . $version;
|
|
16
|
16
|
|
|
17
|
17
|
register_rest_route( $namespace, '/' . $route, [
|
|
18
|
18
|
array(
|
|
19
|
19
|
'methods' => WP_REST_Server::READABLE,
|
|
20
|
|
- 'callback' => array( $this, 'get_items' ),
|
|
21
|
|
- 'args' => array(),
|
|
|
20
|
+ 'callback' => array( $this, 'get_items' )
|
|
22
|
21
|
),
|
|
23
|
22
|
]);
|
|
24
|
23
|
register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
|
|
25
|
24
|
array(
|
|
26
|
25
|
'methods' => WP_REST_Server::READABLE,
|
|
27
|
|
- 'callback' => array( $this, 'get_item' ),
|
|
28
|
|
- 'args' => array(
|
|
29
|
|
- 'context' => array(
|
|
30
|
|
- 'default' => 'view',
|
|
31
|
|
- ),
|
|
32
|
|
- ),
|
|
|
26
|
+ 'callback' => array( $this, 'get_items' )
|
|
33
|
27
|
),
|
|
34
|
28
|
]);
|
|
35
|
29
|
}
|
|
36
|
30
|
|
|
37
|
31
|
public function get_items( $request ) {
|
|
38
|
|
- $args = array(
|
|
39
|
|
- 'post_type' => $this->post_type,
|
|
40
|
|
- );
|
|
|
32
|
+ // Get parameters from request
|
|
|
33
|
+ // /<id>?limit=<num>
|
|
|
34
|
+ $params = $request->get_params();
|
|
41
|
35
|
|
|
42
|
|
- //do a query, call another class, etc
|
|
43
|
|
- $items = new WP_Query($args);
|
|
44
|
|
-
|
|
45
|
|
- $data = array();
|
|
46
|
|
- foreach( $items as $item ) {
|
|
|
36
|
+ $args = array( 'numberposts' => -1, 'post_type' => $this->post_type );
|
|
|
37
|
+ if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
|
|
|
38
|
+ if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
|
|
|
39
|
+ // !: Add order asc/desc
|
|
|
40
|
+ // !: Add orderby
|
|
|
41
|
+
|
|
|
42
|
+ $collection = array();
|
|
|
43
|
+ foreach( get_posts($args) as $item ) {
|
|
47
|
44
|
// 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
|
|
- }
|
|
|
45
|
+ $item->blocks = parse_blocks( $item->post_content );
|
|
|
46
|
+ $data = $this->prepare_item_for_response( $item, $request );
|
|
|
47
|
+ $collection[$item->ID] = $this->prepare_response_for_collection( $data );
|
|
53
|
48
|
}
|
|
54
|
49
|
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
|
|
- }
|
|
|
50
|
+ return new WP_REST_Response( $collection, 200 );
|
|
78
|
51
|
}
|
|
79
|
52
|
|
|
80
|
53
|
public function prepare_item_for_response( $item, $request ) {
|
|
81
|
54
|
$filtered = array();
|
|
82
|
55
|
$filtered[id] = $item->ID;
|
|
|
56
|
+ $filtered[slug] = $item->post_name;
|
|
83
|
57
|
$filtered[type] = $item->post_type;
|
|
84
|
58
|
$filtered[title] = $item->post_title;
|
|
85
|
59
|
$filtered[excerpt] = $item->post_excerpt;
|
|
|
@@ -87,8 +61,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
|
|
87
|
61
|
$filtered[content] = $item->post_content;
|
|
88
|
62
|
$filtered[blocks] = get_rearrange_blocks($item->blocks);
|
|
89
|
63
|
|
|
90
|
|
- // return $item;
|
|
91
|
|
- if($item->post_status === 'publish') return $filtered;
|
|
|
64
|
+ return $filtered;
|
|
92
|
65
|
}
|
|
93
|
66
|
}
|
|
94
|
67
|
|