Skip to content

Commit

Permalink
Syncing to version 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dphiffer committed Aug 20, 2015
1 parent 86c1e5f commit 53ab573
Show file tree
Hide file tree
Showing 34 changed files with 1,622 additions and 215 deletions.
53 changes: 29 additions & 24 deletions controllers/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,30 @@ public function get_recent_posts() {
return $this->posts_result($posts);
}

public function get_posts() {
global $json_api;
$url = parse_url($_SERVER['REQUEST_URI']);
$defaults = array(
'ignore_sticky_posts' => true
);
$query = wp_parse_args($url['query']);
unset($query['json']);
unset($query['post_status']);
$query = array_merge($defaults, $query);
$posts = $json_api->introspector->get_posts($query);
$result = $this->posts_result($posts);
$result['query'] = $query;
return $result;
}

public function get_post() {
global $json_api, $post;
extract($json_api->query->get(array('id', 'slug', 'post_id', 'post_slug')));
if ($id || $post_id) {
if (!$id) {
$id = $post_id;
}
$posts = $json_api->introspector->get_posts(array(
'p' => $id
), true);
} else if ($slug || $post_slug) {
if (!$slug) {
$slug = $post_slug;
}
$posts = $json_api->introspector->get_posts(array(
'name' => $slug
), true);
} else {
$json_api->error("Include 'id' or 'slug' var in your request.");
}
if (count($posts) == 1) {
$post = $posts[0];
$post = $json_api->introspector->get_current_post();
if ($post) {
$previous = get_adjacent_post(false, '', true);
$next = get_adjacent_post(false, '', false);
$post = new JSON_API_Post($post);
$response = array(
'post' => $post
'post' => new JSON_API_Post($post)
);
if ($previous) {
$response['previous_url'] = get_permalink($previous->ID);
Expand Down Expand Up @@ -211,7 +208,13 @@ public function get_date_index() {

public function get_category_index() {
global $json_api;
$categories = $json_api->introspector->get_categories();
$args = null;
if (!empty($json_api->query->parent)) {
$args = array(
'parent' => $json_api->query->parent
);
}
$categories = $json_api->introspector->get_categories($args);
return array(
'count' => count($categories),
'categories' => $categories
Expand Down Expand Up @@ -239,10 +242,12 @@ public function get_author_index() {
public function get_page_index() {
global $json_api;
$pages = array();
$post_type = $json_api->query->post_type ? $json_api->query->post_type : 'page';

// Thanks to blinder for the fix!
$numberposts = empty($json_api->query->count) ? -1 : $json_api->query->count;
$wp_posts = get_posts(array(
'post_type' => 'page',
'post_type' => $post_type,
'post_parent' => 0,
'order' => 'ASC',
'orderby' => 'menu_order',
Expand Down
53 changes: 52 additions & 1 deletion controllers/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 capable of creating posts.");
$json_api->error("You need to login with a user that has 'edit_posts' capacity.");
}
if (!$json_api->query->nonce) {
$json_api->error("You must include a 'nonce' value to create posts. Use the `get_nonce` Core API method.");
Expand All @@ -29,6 +29,57 @@ public function create_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.");
}
if (!$json_api->query->nonce) {
$json_api->error("You must include a 'nonce' value to update posts. Use the `get_nonce` Core API method.");
}
$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.");
}
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.");
}
if (!current_user_can('delete_posts')) {
$json_api->error("You need to login with a user that has the 'delete_posts' capacity.");
}
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.");
}
if (!$json_api->query->nonce) {
$json_api->error("You must include a 'nonce' value to update posts. Use the `get_nonce` Core API method.");
}
$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.");
}
nocache_headers();
wp_delete_post($post->ID);
return array();
}

}

?>
4 changes: 2 additions & 2 deletions json-api.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/*
Plugin Name: JSON API
Plugin URI: http://wordpress.org/extend/plugins/json-api/
Plugin URI: http://wordpress.org/plugins/json-api/
Description: A RESTful API for WordPress
Version: 1.0.7
Version: 1.1.1
Author: Dan Phiffer
Author URI: http://phiffer.org/
*/
Expand Down
Loading

0 comments on commit 53ab573

Please sign in to comment.