Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to wordpress svn version 1.1.0 #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.DS_Store
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();
}

}

?>
108 changes: 108 additions & 0 deletions controllers/widgets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/*
Controller name: Widgets
Controller description: Retrieve sidebar widgets
*/

class JSON_API_Widgets_Controller {

function get_sidebar() {
global $json_api;
$index = @$_REQUEST['sidebar_id'];
if (empty($_REQUEST['sidebar_id'])) {
$json_api->error("No sidebar specified. Include 'sidebar_id' var in your request.");
} else if (!is_active_sidebar($index)) {
$json_api->error("Sidebar '$index' is not active.");
}

$widget_params = array(
'before_widget',
'after_widget',
'before_title',
'after_title'
);
$json_api_params = array();
foreach ($widget_params as $param) {
if (isset($_REQUEST[$param])) {
$json_api_params[$param] = $_REQUEST[$param];
}
}

$widgets = array();

global $wp_registered_sidebars, $wp_registered_widgets;

if ( is_int($index) ) {
$index = "sidebar-$index";
} else {
$index = sanitize_title($index);
foreach ( (array) $wp_registered_sidebars as $key => $value ) {
if ( sanitize_title($value['name']) == $index ) {
$index = $key;
break;
}
}
}

$sidebars_widgets = wp_get_sidebars_widgets();

if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
return false;

$sidebar = $wp_registered_sidebars[$index];

$did_one = false;
foreach ( (array) $sidebars_widgets[$index] as $id ) {

if ( !isset($wp_registered_widgets[$id]) ) continue;

$params = array_merge(
array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']), $json_api_params ) ),
(array) $wp_registered_widgets[$id]['params']
);


// Substitute HTML id and class attributes into before_widget
$classname_ = '';
foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
if ( is_string($cn) )
$classname_ .= '_' . $cn;
elseif ( is_object($cn) )
$classname_ .= '_' . get_class($cn);
}
$classname_ = ltrim($classname_, '_');
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);

$params = apply_filters( 'dynamic_sidebar_params', $params );

$callback = $wp_registered_widgets[$id]['callback'];

do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );

if ( is_callable($callback) ) {
ob_start();
$object = $callback[0];
$settings = $object->get_settings();
$widget_params = $wp_registered_widgets[$id]['params'];
$number = $widget_params[0]['number'];
$instance = $settings[$number];
call_user_func_array($callback, $params);
$widgets[] = array(
'id' => $id,
'widget' => trim(ob_get_contents()),
'params' => $params[0],
'instance' => $instance
);
ob_end_clean();
}
}

return array(
'sidebar_id' => $index,
'widgets' => $widgets
);
}

}

?>
14 changes: 8 additions & 6 deletions json-api.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
/*
Plugin Name: JSON API
Plugin URI: http://wordpress.org/extend/plugins/json-api/
Description: A RESTful API for WordPress
Version: 1.0.7
Author: Dan Phiffer
Author URI: http://phiffer.org/
Plugin Name: JSON API for apfelcheck.de
Description: Extension for JSON API 1.1.1 by Dan Phiffer
Version: 1.1.2
*/

// Description: A RESTful API for WordPress
// Version: 1.1.0
// Author: Dan Phiffer
// Author URI: http://phiffer.org/

$dir = json_api_dir();
@include_once "$dir/singletons/api.php";
@include_once "$dir/singletons/query.php";
Expand Down
Loading