-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-api.php
146 lines (116 loc) · 3 KB
/
class-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Bustle;
/**
* Class API
*
* @package Bustle
*/
class API {
/**
* Register the WP JSON API endpoints
*/
public function __construct() {
add_action( 'rest_api_init', function () {
register_rest_route( 'bustle', '/posts/', [
'methods' => 'GET',
'callback' => [ $this, 'posts_callback' ],
] );
register_rest_route( 'bustle', '/post/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => [ $this, 'post_callback' ],
] );
register_rest_route( 'bustle', '/user/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => [ $this, 'user_callback' ],
] );
register_rest_route( 'bustle', '/term/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => [ $this, 'term_callback' ],
] );
} );
}
/**
* Return a single WP_Post object
*
* @param object $request The request object.
*
* @return array|mixed|\WP_Error
*/
public function posts_callback( $request ) {
$posts_json = [];
$params = $request->get_params();
$posts_per_page = ( isset( $params['per_page'] ) ) ? (int) $params['per_page'] : 10;
$page = ( isset( $params['page'] ) ) ? (int) $params['page'] : 1;
$query_args = [
'posts_per_page' => $posts_per_page,
'page' => $page,
];
$posts = new \WP_Query( $query_args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
$bustle_post = new \Bustle\Post( get_post() );
$posts_json[] = $bustle_post->get_model();
}
} else {
return new \WP_Error( 'no_posts', 'No posts exist', [
'status' => 404,
] );
}
return $posts_json;
}
/**
* Return a single WP_Post object
*
* @param array $request The request object.
*
* @return array|mixed|\WP_Error
*/
public function post_callback( $request ) {
$post_id = (int) $request['id'];
$post = get_post( $post_id );
if ( ! is_a( $post, 'WP_Post' ) ) {
return new \WP_Error( 'no_post', "Post {$post_id} does not exist", [
'status' => 404,
] );
}
$bustle_post = new \Bustle\Post( $post );
return $bustle_post->get_model();
}
/**
* Return a single WP_User object
*
* @param array $request The request object.
*
* @return array|mixed|\WP_Error
*/
public function user_callback( $request ) {
$user_id = (int) $request['id'];
$user = get_user_by( 'id', $user_id );
if ( ! is_a( $user, 'WP_User' ) ) {
return new \WP_Error( 'no_user', "User {$user_id} does not exist", [
'status' => 404,
] );
}
$bustle_user = new \Bustle\User( $user );
return $bustle_user->get_model();
}
/**
* Return a single WP_Term object
*
* @param array $request The request object.
*
* @return array|mixed|\WP_Error
*/
public function term_callback( $request ) {
$term_id = (int) $request['id'];
$term = get_term( $term_id );
if ( ! is_a( $term, 'WP_Term' ) ) {
return new \WP_Error( 'no_term', "Term {$term_id} does not exist", [
'status' => 404,
] );
}
$bustle_term = new \Bustle\Term( $term );
return $bustle_term->get_model();
}
}