Version Description
Added a new data manipulation method: create_post
.
Download this release
Release Info
Developer | dphiffer |
Plugin | JSON API |
Version | 0.9 |
Comparing to | |
See all releases |
Code changes from version 0.8.3 to 0.9
- json-api.php +1 -1
- models/post.php +111 -14
- readme.txt +28 -7
- singletons/controller.php +15 -6
- singletons/introspector.php +15 -1
json-api.php
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
Plugin Name: JSON API
|
4 |
Plugin URI: http://wordpress.org/extend/plugins/json-api/
|
5 |
Description: A RESTful API for WordPress
|
6 |
-
Version: 0.
|
7 |
Author: Dan Phiffer
|
8 |
Author URI: http://phiffer.org/
|
9 |
*/
|
3 |
Plugin Name: JSON API
|
4 |
Plugin URI: http://wordpress.org/extend/plugins/json-api/
|
5 |
Description: A RESTful API for WordPress
|
6 |
+
Version: 0.9
|
7 |
Author: Dan Phiffer
|
8 |
Author URI: http://phiffer.org/
|
9 |
*/
|
models/post.php
CHANGED
@@ -8,6 +8,7 @@ class JSON_API_Post {
|
|
8 |
var $id; // Integer
|
9 |
var $slug; // String
|
10 |
var $url; // String
|
|
|
11 |
var $title; // String
|
12 |
var $title_plain; // String
|
13 |
var $content; // String (modified by read_more query var)
|
@@ -23,25 +24,121 @@ class JSON_API_Post {
|
|
23 |
var $comment_status; // String ("open" or "closed")
|
24 |
var $custom_fields; // Object (included by using custom_fields query var)
|
25 |
|
26 |
-
function JSON_API_Post() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
global $json_api, $post;
|
28 |
$date_format = $json_api->query->date_format;
|
29 |
-
$this->id = (int)
|
30 |
-
|
31 |
-
$this->set_value('
|
32 |
-
$this->set_value('
|
33 |
-
$this->set_value('
|
|
|
|
|
34 |
$this->set_content_value();
|
35 |
$this->set_value('excerpt', get_the_excerpt());
|
36 |
$this->set_value('date', get_the_time($date_format));
|
37 |
-
$this->set_value('modified', date($date_format, strtotime($
|
38 |
$this->set_categories_value();
|
39 |
$this->set_tags_value();
|
40 |
-
$this->set_author_value();
|
41 |
$this->set_comments_value();
|
42 |
$this->set_attachments_value();
|
43 |
-
$this->set_value('comment_count', (int) $
|
44 |
-
$this->set_value('comment_status', $
|
45 |
$this->set_custom_fields_value();
|
46 |
}
|
47 |
|
@@ -68,7 +165,7 @@ class JSON_API_Post {
|
|
68 |
global $json_api;
|
69 |
if ($json_api->include_value('categories')) {
|
70 |
$this->categories = array();
|
71 |
-
if ($wp_categories = get_the_category()) {
|
72 |
foreach ($wp_categories as $wp_category) {
|
73 |
$category = new JSON_API_Category($wp_category);
|
74 |
if ($category->id == 1 && $category->slug == 'uncategorized') {
|
@@ -85,7 +182,7 @@ class JSON_API_Post {
|
|
85 |
global $json_api;
|
86 |
if ($json_api->include_value('tags')) {
|
87 |
$this->tags = array();
|
88 |
-
if ($wp_tags = get_the_tags()) {
|
89 |
foreach ($wp_tags as $wp_tag) {
|
90 |
$this->tags[] = new JSON_API_Tag($wp_tag);
|
91 |
}
|
@@ -93,10 +190,10 @@ class JSON_API_Post {
|
|
93 |
}
|
94 |
}
|
95 |
|
96 |
-
function set_author_value() {
|
97 |
global $json_api;
|
98 |
if ($json_api->include_value('author')) {
|
99 |
-
$this->author = new JSON_API_Author();
|
100 |
}
|
101 |
}
|
102 |
|
8 |
var $id; // Integer
|
9 |
var $slug; // String
|
10 |
var $url; // String
|
11 |
+
var $status; // String ("draft", "published", or "pending")
|
12 |
var $title; // String
|
13 |
var $title_plain; // String
|
14 |
var $content; // String (modified by read_more query var)
|
24 |
var $comment_status; // String ("open" or "closed")
|
25 |
var $custom_fields; // Object (included by using custom_fields query var)
|
26 |
|
27 |
+
function JSON_API_Post($wp_post = null) {
|
28 |
+
if (!empty($wp_post)) {
|
29 |
+
$this->import_wp_object($wp_post);
|
30 |
+
}
|
31 |
+
}
|
32 |
+
|
33 |
+
function create($values = null) {
|
34 |
+
unset($values['id']);
|
35 |
+
if (empty($values) || empty($values['title'])) {
|
36 |
+
$values = array(
|
37 |
+
'title' => 'Untitled',
|
38 |
+
'content' => ''
|
39 |
+
);
|
40 |
+
}
|
41 |
+
return $this->save($values);
|
42 |
+
}
|
43 |
+
|
44 |
+
function update($values) {
|
45 |
+
$values['id'] = $this->id;
|
46 |
+
return $this->save($values);
|
47 |
+
}
|
48 |
+
|
49 |
+
function save($values = null) {
|
50 |
+
global $json_api, $user_ID;
|
51 |
+
|
52 |
+
$wp_values = array();
|
53 |
+
|
54 |
+
if (!empty($values['id'])) {
|
55 |
+
$wp_values['ID'] = $values['id'];
|
56 |
+
}
|
57 |
+
|
58 |
+
if (!empty($values['status'])) {
|
59 |
+
$wp_values['post_status'] = $values['status'];
|
60 |
+
}
|
61 |
+
|
62 |
+
if (!empty($values['title'])) {
|
63 |
+
$wp_values['post_title'] = $values['title'];
|
64 |
+
}
|
65 |
+
|
66 |
+
if (!empty($values['content'])) {
|
67 |
+
$wp_values['post_content'] = $values['content'];
|
68 |
+
}
|
69 |
+
|
70 |
+
if (!empty($values['author'])) {
|
71 |
+
$author = $json_api->introspector->get_author_by_login($values['author']);
|
72 |
+
$wp_values['post_author'] = $author->id;
|
73 |
+
}
|
74 |
+
|
75 |
+
if (isset($values['categories'])) {
|
76 |
+
$categories = explode(',', $values['categories']);
|
77 |
+
foreach ($categories as $category_slug) {
|
78 |
+
$category_slug = trim($category_slug);
|
79 |
+
$category = $json_api->introspector->get_category_by_slug($category_slug);
|
80 |
+
if (empty($wp_values['post_category'])) {
|
81 |
+
$wp_values['post_category'] = array($category->id);
|
82 |
+
} else {
|
83 |
+
array_push($wp_values['post_category'], $category->id);
|
84 |
+
}
|
85 |
+
}
|
86 |
+
}
|
87 |
+
|
88 |
+
if (isset($values['tags'])) {
|
89 |
+
$tags = explode(',', $values['tags']);
|
90 |
+
foreach ($tags as $tag_slug) {
|
91 |
+
$tag_slug = trim($tag_slug);
|
92 |
+
if (empty($wp_values['tags_input'])) {
|
93 |
+
$wp_values['tags_input'] = array($tag_slug);
|
94 |
+
} else {
|
95 |
+
array_push($wp_values['tags_input'], $tag_slug);
|
96 |
+
}
|
97 |
+
}
|
98 |
+
}
|
99 |
+
|
100 |
+
if (isset($wp_values['ID'])) {
|
101 |
+
$this->id = wp_update_post($wp_values);
|
102 |
+
} else {
|
103 |
+
$this->id = wp_insert_post($wp_values);
|
104 |
+
}
|
105 |
+
|
106 |
+
if (!empty($_FILES['attachment'])) {
|
107 |
+
include_once ABSPATH . '/wp-admin/includes/file.php';
|
108 |
+
include_once ABSPATH . '/wp-admin/includes/media.php';
|
109 |
+
include_once ABSPATH . '/wp-admin/includes/image.php';
|
110 |
+
$attachment_id = media_handle_upload('attachment', $this->id);
|
111 |
+
$this->attachments[] = new JSON_API_Attachment($attachment_id);
|
112 |
+
unset($_FILES['attachment']);
|
113 |
+
}
|
114 |
+
|
115 |
+
$wp_post = get_post($this->id);
|
116 |
+
$this->import_wp_object($wp_post);
|
117 |
+
|
118 |
+
return $this->id;
|
119 |
+
}
|
120 |
+
|
121 |
+
function import_wp_object($wp_post) {
|
122 |
global $json_api, $post;
|
123 |
$date_format = $json_api->query->date_format;
|
124 |
+
$this->id = (int) $wp_post->ID;
|
125 |
+
setup_postdata($wp_post);
|
126 |
+
$this->set_value('slug', $wp_post->post_name);
|
127 |
+
$this->set_value('url', get_permalink($this->id));
|
128 |
+
$this->set_value('status', $wp_post->post_status);
|
129 |
+
$this->set_value('title', get_the_title($this->id));
|
130 |
+
$this->set_value('title_plain', strip_tags($this->title));
|
131 |
$this->set_content_value();
|
132 |
$this->set_value('excerpt', get_the_excerpt());
|
133 |
$this->set_value('date', get_the_time($date_format));
|
134 |
+
$this->set_value('modified', date($date_format, strtotime($wp_post->post_modified)));
|
135 |
$this->set_categories_value();
|
136 |
$this->set_tags_value();
|
137 |
+
$this->set_author_value($wp_post->post_author);
|
138 |
$this->set_comments_value();
|
139 |
$this->set_attachments_value();
|
140 |
+
$this->set_value('comment_count', (int) $wp_post->comment_count);
|
141 |
+
$this->set_value('comment_status', $wp_post->comment_status);
|
142 |
$this->set_custom_fields_value();
|
143 |
}
|
144 |
|
165 |
global $json_api;
|
166 |
if ($json_api->include_value('categories')) {
|
167 |
$this->categories = array();
|
168 |
+
if ($wp_categories = get_the_category($this->id)) {
|
169 |
foreach ($wp_categories as $wp_category) {
|
170 |
$category = new JSON_API_Category($wp_category);
|
171 |
if ($category->id == 1 && $category->slug == 'uncategorized') {
|
182 |
global $json_api;
|
183 |
if ($json_api->include_value('tags')) {
|
184 |
$this->tags = array();
|
185 |
+
if ($wp_tags = get_the_tags($this->id)) {
|
186 |
foreach ($wp_tags as $wp_tag) {
|
187 |
$this->tags[] = new JSON_API_Tag($wp_tag);
|
188 |
}
|
190 |
}
|
191 |
}
|
192 |
|
193 |
+
function set_author_value($author_id) {
|
194 |
global $json_api;
|
195 |
if ($json_api->include_value('author')) {
|
196 |
+
$this->author = new JSON_API_Author($author_id);
|
197 |
}
|
198 |
}
|
199 |
|
readme.txt
CHANGED
@@ -3,7 +3,7 @@ Contributors: dphiffer
|
|
3 |
Tags: json, api, ajax, cms, admin, integration, moma
|
4 |
Requires at least: 2.8
|
5 |
Tested up to: 2.9
|
6 |
-
Stable tag: 0.
|
7 |
|
8 |
A RESTful API for WordPress
|
9 |
|
@@ -547,6 +547,21 @@ __Incomplete__
|
|
547 |
The data manipulation methods are still very incomplete.
|
548 |
|
549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
550 |
== Method: submit_comment ==
|
551 |
|
552 |
Submits a comment to a WordPress post.
|
@@ -572,6 +587,9 @@ Submits a comment to a WordPress post.
|
|
572 |
|
573 |
== Changelog ==
|
574 |
|
|
|
|
|
|
|
575 |
= 0.8.3 (2010-01-27): =
|
576 |
* Fixed the stable tag version
|
577 |
|
@@ -607,14 +625,17 @@ Submits a comment to a WordPress post.
|
|
607 |
|
608 |
== Upgrade Notice ==
|
609 |
|
610 |
-
= 0.
|
611 |
-
Added
|
612 |
|
613 |
-
= 0.8.
|
614 |
-
|
615 |
|
616 |
= 0.8.2 =
|
617 |
Just fixing a mislabeled 0.8.1 release in the changelog.
|
618 |
|
619 |
-
= 0.8.
|
620 |
-
|
|
|
|
|
|
3 |
Tags: json, api, ajax, cms, admin, integration, moma
|
4 |
Requires at least: 2.8
|
5 |
Tested up to: 2.9
|
6 |
+
Stable tag: 0.9
|
7 |
|
8 |
A RESTful API for WordPress
|
9 |
|
547 |
The data manipulation methods are still very incomplete.
|
548 |
|
549 |
|
550 |
+
== Method: create_post ==
|
551 |
+
|
552 |
+
Creates a new post.
|
553 |
+
|
554 |
+
= Optional arguments =
|
555 |
+
|
556 |
+
* `status` - sets the post status ("draft" or "publish"), default is "draft"
|
557 |
+
* `title` - the post title
|
558 |
+
* `content` - the post content
|
559 |
+
* `author` - the post's author (login name), default is the current logged in user
|
560 |
+
* `categories` - a comma-separated list of categories (URL slugs)
|
561 |
+
* `tags` - a comma-separated list of tags (URL slugs)
|
562 |
+
|
563 |
+
Note: including a file upload field called `attachment` will cause an attachment to be stored with your new post.
|
564 |
+
|
565 |
== Method: submit_comment ==
|
566 |
|
567 |
Submits a comment to a WordPress post.
|
587 |
|
588 |
== Changelog ==
|
589 |
|
590 |
+
= 0.9 (2010-02-04): =
|
591 |
+
* Added a `create_post` method
|
592 |
+
|
593 |
= 0.8.3 (2010-01-27): =
|
594 |
* Fixed the stable tag version
|
595 |
|
625 |
|
626 |
== Upgrade Notice ==
|
627 |
|
628 |
+
= 0.9 =
|
629 |
+
Added a new data manipulation method: `create_post`.
|
630 |
|
631 |
+
= 0.8.3 =
|
632 |
+
Oh dear, I didn't tag 0.8.2 in the stable tags thing.
|
633 |
|
634 |
= 0.8.2 =
|
635 |
Just fixing a mislabeled 0.8.1 release in the changelog.
|
636 |
|
637 |
+
= 0.8.1 =
|
638 |
+
This is a bug fix release for JSONP support. Thanks to Ben Wilson for reporting it!
|
639 |
+
|
640 |
+
= 0.8 =
|
641 |
+
Added what may be the last introspection feature: post attachments. You can now see images and other media that have been added to posts.
|
singletons/controller.php
CHANGED
@@ -19,6 +19,9 @@ class JSON_API_Controller {
|
|
19 |
$this->setup();
|
20 |
$this->query->setup();
|
21 |
|
|
|
|
|
|
|
22 |
// Run the method
|
23 |
$result = $this->$method();
|
24 |
|
@@ -195,14 +198,20 @@ class JSON_API_Controller {
|
|
195 |
));
|
196 |
}
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
function submit_comment() {
|
199 |
nocache_headers();
|
200 |
-
$required = array(
|
201 |
-
'post_id',
|
202 |
-
'name',
|
203 |
-
'email',
|
204 |
-
'content'
|
205 |
-
);
|
206 |
if (empty($_REQUEST['post_id'])) {
|
207 |
$this->error("No post specified. Include 'post_id' var in your request.");
|
208 |
} else if (empty($_REQUEST['name']) ||
|
19 |
$this->setup();
|
20 |
$this->query->setup();
|
21 |
|
22 |
+
// Run Plugin hooks for method
|
23 |
+
do_action("json_api_$method");
|
24 |
+
|
25 |
// Run the method
|
26 |
$result = $this->$method();
|
27 |
|
198 |
));
|
199 |
}
|
200 |
|
201 |
+
function create_post() {
|
202 |
+
nocache_headers();
|
203 |
+
$post = new JSON_API_Post();
|
204 |
+
$id = $post->create($_REQUEST);
|
205 |
+
if (empty($id)) {
|
206 |
+
$this->error("Could not create post.");
|
207 |
+
}
|
208 |
+
return $this->response->get_json(array(
|
209 |
+
'post' => $post
|
210 |
+
));
|
211 |
+
}
|
212 |
+
|
213 |
function submit_comment() {
|
214 |
nocache_headers();
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
if (empty($_REQUEST['post_id'])) {
|
216 |
$this->error("No post specified. Include 'post_id' var in your request.");
|
217 |
} else if (empty($_REQUEST['name']) ||
|
singletons/introspector.php
CHANGED
@@ -3,12 +3,13 @@
|
|
3 |
class JSON_API_Introspector {
|
4 |
|
5 |
function get_posts($query = '') {
|
|
|
6 |
// Returns an array of JSON_API_Post objects
|
7 |
$this->set_posts_query($query);
|
8 |
$output = array();
|
9 |
while (have_posts()) {
|
10 |
the_post();
|
11 |
-
$output[] = new JSON_API_Post();
|
12 |
}
|
13 |
return $output;
|
14 |
}
|
@@ -143,9 +144,22 @@ class JSON_API_Introspector {
|
|
143 |
}
|
144 |
|
145 |
function get_author($id) {
|
|
|
|
|
|
|
|
|
146 |
return new JSON_API_Author($id);
|
147 |
}
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
function get_current_author() {
|
150 |
$author_id = get_query_var('author');
|
151 |
return $this->get_author($author_id);
|
3 |
class JSON_API_Introspector {
|
4 |
|
5 |
function get_posts($query = '') {
|
6 |
+
global $post;
|
7 |
// Returns an array of JSON_API_Post objects
|
8 |
$this->set_posts_query($query);
|
9 |
$output = array();
|
10 |
while (have_posts()) {
|
11 |
the_post();
|
12 |
+
$output[] = new JSON_API_Post($post);
|
13 |
}
|
14 |
return $output;
|
15 |
}
|
144 |
}
|
145 |
|
146 |
function get_author($id) {
|
147 |
+
return $this->get_author_by_id($id);
|
148 |
+
}
|
149 |
+
|
150 |
+
function get_author_by_id($id) {
|
151 |
return new JSON_API_Author($id);
|
152 |
}
|
153 |
|
154 |
+
function get_author_by_login($login) {
|
155 |
+
$id = $wpdb->get_var($wpdb->prepare("
|
156 |
+
SELECT ID
|
157 |
+
FROM $wpdb->users
|
158 |
+
WHERE user_login = %s
|
159 |
+
", $login));
|
160 |
+
return $this->get_author_by_id($id);
|
161 |
+
}
|
162 |
+
|
163 |
function get_current_author() {
|
164 |
$author_id = get_query_var('author');
|
165 |
return $this->get_author($author_id);
|