forked from dphiffer/wp-json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.php
85 lines (79 loc) · 2.97 KB
/
posts.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
<?php
/*
Controller name: Posts
Controller description: Data manipulation methods for posts
*/
class JSON_API_Posts_Controller {
public function create_post() {
global $json_api;
if (!current_user_can('edit_posts')) {
$json_api->error("You need to login with a user that has 'edit_posts' capacity.", 403);
}
if (!$json_api->query->nonce) {
$json_api->error("You must include a 'nonce' value to create posts. Use the `get_nonce` Core API method.", 403);
}
$nonce_id = $json_api->get_nonce_id('posts', 'create_post');
if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
$json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.", 403);
}
nocache_headers();
$post = new JSON_API_Post();
$id = $post->create($_REQUEST);
if (empty($id)) {
$json_api->error("Could not create post.", 500);
}
return array(
'post' => $post
);
}
public function update_post() {
global $json_api;
$post = $json_api->introspector->get_current_post();
if (empty($post)) {
$json_api->error("Post not found.");
}
if (!current_user_can('edit_post', $post->ID)) {
$json_api->error("You need to login with a user that has the 'edit_post' capacity for that post.", 403);
}
if (!$json_api->query->nonce) {
$json_api->error("You must include a 'nonce' value to update posts. Use the `get_nonce` Core API method.", 403);
}
$nonce_id = $json_api->get_nonce_id('posts', 'update_post');
if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
$json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.", 403);
}
nocache_headers();
$post = new JSON_API_Post($post);
$post->update($_REQUEST);
return array(
'post' => $post
);
}
public function delete_post() {
global $json_api;
$post = $json_api->introspector->get_current_post();
if (empty($post)) {
$json_api->error("Post not found.");
}
if (!current_user_can('edit_post', $post->ID)) {
$json_api->error("You need to login with a user that has the 'edit_post' capacity for that post.", 403);
}
if (!current_user_can('delete_posts')) {
$json_api->error("You need to login with a user that has the 'delete_posts' capacity.", 403);
}
if ($post->post_author != get_current_user_id() && !current_user_can('delete_other_posts')) {
$json_api->error("You need to login with a user that has the 'delete_other_posts' capacity.", 403);
}
if (!$json_api->query->nonce) {
$json_api->error("You must include a 'nonce' value to update posts. Use the `get_nonce` Core API method.", 403);
}
$nonce_id = $json_api->get_nonce_id('posts', 'delete_post');
if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
$json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.", 403);
}
nocache_headers();
wp_delete_post($post->ID);
return array();
}
}
?>