Skip to content

Commit 53ab573

Browse files
committed
Syncing to version 1.1.1
1 parent 86c1e5f commit 53ab573

34 files changed

Lines changed: 1622 additions & 215 deletions

controllers/core.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,30 @@ public function get_recent_posts() {
4242
return $this->posts_result($posts);
4343
}
4444

45+
public function get_posts() {
46+
global $json_api;
47+
$url = parse_url($_SERVER['REQUEST_URI']);
48+
$defaults = array(
49+
'ignore_sticky_posts' => true
50+
);
51+
$query = wp_parse_args($url['query']);
52+
unset($query['json']);
53+
unset($query['post_status']);
54+
$query = array_merge($defaults, $query);
55+
$posts = $json_api->introspector->get_posts($query);
56+
$result = $this->posts_result($posts);
57+
$result['query'] = $query;
58+
return $result;
59+
}
60+
4561
public function get_post() {
4662
global $json_api, $post;
47-
extract($json_api->query->get(array('id', 'slug', 'post_id', 'post_slug')));
48-
if ($id || $post_id) {
49-
if (!$id) {
50-
$id = $post_id;
51-
}
52-
$posts = $json_api->introspector->get_posts(array(
53-
'p' => $id
54-
), true);
55-
} else if ($slug || $post_slug) {
56-
if (!$slug) {
57-
$slug = $post_slug;
58-
}
59-
$posts = $json_api->introspector->get_posts(array(
60-
'name' => $slug
61-
), true);
62-
} else {
63-
$json_api->error("Include 'id' or 'slug' var in your request.");
64-
}
65-
if (count($posts) == 1) {
66-
$post = $posts[0];
63+
$post = $json_api->introspector->get_current_post();
64+
if ($post) {
6765
$previous = get_adjacent_post(false, '', true);
6866
$next = get_adjacent_post(false, '', false);
69-
$post = new JSON_API_Post($post);
7067
$response = array(
71-
'post' => $post
68+
'post' => new JSON_API_Post($post)
7269
);
7370
if ($previous) {
7471
$response['previous_url'] = get_permalink($previous->ID);
@@ -211,7 +208,13 @@ public function get_date_index() {
211208

212209
public function get_category_index() {
213210
global $json_api;
214-
$categories = $json_api->introspector->get_categories();
211+
$args = null;
212+
if (!empty($json_api->query->parent)) {
213+
$args = array(
214+
'parent' => $json_api->query->parent
215+
);
216+
}
217+
$categories = $json_api->introspector->get_categories($args);
215218
return array(
216219
'count' => count($categories),
217220
'categories' => $categories
@@ -239,10 +242,12 @@ public function get_author_index() {
239242
public function get_page_index() {
240243
global $json_api;
241244
$pages = array();
245+
$post_type = $json_api->query->post_type ? $json_api->query->post_type : 'page';
246+
242247
// Thanks to blinder for the fix!
243248
$numberposts = empty($json_api->query->count) ? -1 : $json_api->query->count;
244249
$wp_posts = get_posts(array(
245-
'post_type' => 'page',
250+
'post_type' => $post_type,
246251
'post_parent' => 0,
247252
'order' => 'ASC',
248253
'orderby' => 'menu_order',

controllers/posts.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class JSON_API_Posts_Controller {
99
public function create_post() {
1010
global $json_api;
1111
if (!current_user_can('edit_posts')) {
12-
$json_api->error("You need to login with a user capable of creating posts.");
12+
$json_api->error("You need to login with a user that has 'edit_posts' capacity.");
1313
}
1414
if (!$json_api->query->nonce) {
1515
$json_api->error("You must include a 'nonce' value to create posts. Use the `get_nonce` Core API method.");
@@ -29,6 +29,57 @@ public function create_post() {
2929
);
3030
}
3131

32+
public function update_post() {
33+
global $json_api;
34+
$post = $json_api->introspector->get_current_post();
35+
if (empty($post)) {
36+
$json_api->error("Post not found.");
37+
}
38+
if (!current_user_can('edit_post', $post->ID)) {
39+
$json_api->error("You need to login with a user that has the 'edit_post' capacity for that post.");
40+
}
41+
if (!$json_api->query->nonce) {
42+
$json_api->error("You must include a 'nonce' value to update posts. Use the `get_nonce` Core API method.");
43+
}
44+
$nonce_id = $json_api->get_nonce_id('posts', 'update_post');
45+
if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
46+
$json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
47+
}
48+
nocache_headers();
49+
$post = new JSON_API_Post($post);
50+
$post->update($_REQUEST);
51+
return array(
52+
'post' => $post
53+
);
54+
}
55+
56+
public function delete_post() {
57+
global $json_api;
58+
$post = $json_api->introspector->get_current_post();
59+
if (empty($post)) {
60+
$json_api->error("Post not found.");
61+
}
62+
if (!current_user_can('edit_post', $post->ID)) {
63+
$json_api->error("You need to login with a user that has the 'edit_post' capacity for that post.");
64+
}
65+
if (!current_user_can('delete_posts')) {
66+
$json_api->error("You need to login with a user that has the 'delete_posts' capacity.");
67+
}
68+
if ($post->post_author != get_current_user_id() && !current_user_can('delete_other_posts')) {
69+
$json_api->error("You need to login with a user that has the 'delete_other_posts' capacity.");
70+
}
71+
if (!$json_api->query->nonce) {
72+
$json_api->error("You must include a 'nonce' value to update posts. Use the `get_nonce` Core API method.");
73+
}
74+
$nonce_id = $json_api->get_nonce_id('posts', 'delete_post');
75+
if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
76+
$json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
77+
}
78+
nocache_headers();
79+
wp_delete_post($post->ID);
80+
return array();
81+
}
82+
3283
}
3384

3485
?>

json-api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
/*
33
Plugin Name: JSON API
4-
Plugin URI: http://wordpress.org/extend/plugins/json-api/
4+
Plugin URI: http://wordpress.org/plugins/json-api/
55
Description: A RESTful API for WordPress
6-
Version: 1.0.7
6+
Version: 1.1.1
77
Author: Dan Phiffer
88
Author URI: http://phiffer.org/
99
*/

0 commit comments

Comments
 (0)