Initial commit

This commit is contained in:
Dan Phiffer committed 2011-03-25 12:22:44 -04:00
commit 86c1e5f82b
40 files changed
+4362

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
<?php
class JSON_API_Attachment {
var $id; // Integer
var $url; // String
var $slug; // String
var $title; // String
var $description; // String
var $caption; // String
var $parent; // Integer
var $mime_type; // String
function JSON_API_Attachment($wp_attachment = null) {
if ($wp_attachment) {
$this->import_wp_object($wp_attachment);
if ($this->is_image()) {
$this->query_images();
}
}
}
function import_wp_object($wp_attachment) {
$this->id = (int) $wp_attachment->ID;
$this->url = $wp_attachment->guid;
$this->slug = $wp_attachment->post_name;
$this->title = $wp_attachment->post_title;
$this->description = $wp_attachment->post_content;
$this->caption = $wp_attachment->post_excerpt;
$this->parent = (int) $wp_attachment->post_parent;
$this->mime_type = $wp_attachment->post_mime_type;
}
function is_image() {
return (substr($this->mime_type, 0, 5) == 'image');
}
function query_images() {
$sizes = array('thumbnail', 'medium', 'large', 'full');
if (function_exists('get_intermediate_image_sizes')) {
$sizes = array_merge(array('full'), get_intermediate_image_sizes());
}
$this->images = array();
foreach ($sizes as $size) {
list($url, $width, $height) = wp_get_attachment_image_src($this->id, $size);
$this->images[$size] = (object) array(
'url' => $url,
'width' => $width,
'height' => $height
);
}
}
}
?>
+62
View File
@@ -0,0 +1,62 @@
<?php
class JSON_API_Author {
var $id; // Integer
var $slug; // String
var $name; // String
var $first_name; // String
var $last_name; // String
var $nickname; // String
var $url; // String
var $description; // String
// Note:
// JSON_API_Author objects can include additional values by using the
// author_meta query var.
function JSON_API_Author($id = null) {
if ($id) {
$this->id = (int) $id;
} else {
$this->id = (int) get_the_author_meta('ID');
}
$this->set_value('slug', 'user_nicename');
$this->set_value('name', 'display_name');
$this->set_value('first_name', 'first_name');
$this->set_value('last_name', 'last_name');
$this->set_value('nickname', 'nickname');
$this->set_value('url', 'user_url');
$this->set_value('description', 'description');
$this->set_author_meta();
//$this->raw = get_userdata($this->id);
}
function set_value($key, $wp_key = false) {
if (!$wp_key) {
$wp_key = $key;
}
$this->$key = get_the_author_meta($wp_key, $this->id);
}
function set_author_meta() {
global $json_api;
if (!$json_api->query->author_meta) {
return;
}
$protected_vars = array(
'user_login',
'user_pass',
'user_email',
'user_activation_key'
);
$vars = explode(',', $json_api->query->author_meta);
$vars = array_diff($vars, $protected_vars);
foreach ($vars as $var) {
$this->set_value($var);
}
}
}
?>
+29
View File
@@ -0,0 +1,29 @@
<?php
class JSON_API_Category {
var $id; // Integer
var $slug; // String
var $title; // String
var $description; // String
var $parent; // Integer
var $post_count; // Integer
function JSON_API_Category($wp_category = null) {
if ($wp_category) {
$this->import_wp_object($wp_category);
}
}
function import_wp_object($wp_category) {
$this->id = (int) $wp_category->term_id;
$this->slug = $wp_category->slug;
$this->title = $wp_category->name;
$this->description = $wp_category->description;
$this->parent = (int) $wp_category->parent;
$this->post_count = (int) $wp_category->count;
}
}
?>
+80
View File
@@ -0,0 +1,80 @@
<?php
class JSON_API_Comment {
var $id; // Integer
var $name; // String
var $url; // String
var $date; // String
var $content; // String
var $parent; // Integer
var $author; // Object (only if the user was registered & logged in)
function JSON_API_Comment($wp_comment = null) {
if ($wp_comment) {
$this->import_wp_object($wp_comment);
}
}
function import_wp_object($wp_comment) {
global $json_api;
$date_format = $json_api->query->date_format;
$content = apply_filters('comment_text', $wp_comment->comment_content);
$this->id = (int) $wp_comment->comment_ID;
$this->name = $wp_comment->comment_author;
$this->url = $wp_comment->comment_author_url;
$this->date = date($date_format, strtotime($wp_comment->comment_date));
$this->content = $content;
$this->parent = (int) $wp_comment->comment_parent;
//$this->raw = $wp_comment;
if (!empty($wp_comment->user_id)) {
$this->author = new JSON_API_Author($wp_comment->user_id);
} else {
unset($this->author);
}
}
function handle_submission() {
global $comment, $wpdb;
add_action('comment_id_not_found', array(&$this, 'comment_id_not_found'));
add_action('comment_closed', array(&$this, 'comment_closed'));
add_action('comment_on_draft', array(&$this, 'comment_on_draft'));
add_filter('comment_post_redirect', array(&$this, 'comment_post_redirect'));
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['comment_post_ID'] = $_REQUEST['post_id'];
$_POST['author'] = $_REQUEST['name'];
$_POST['email'] = $_REQUEST['email'];
$_POST['url'] = empty($_REQUEST['url']) ? '' : $_REQUEST['url'];
$_POST['comment'] = $_REQUEST['content'];
$_POST['parent'] = $_REQUEST['parent'];
include ABSPATH . 'wp-comments-post.php';
}
function comment_id_not_found() {
global $json_api;
$json_api->error("Post ID '{$_REQUEST['post_id']}' not found.");
}
function comment_closed() {
global $json_api;
$json_api->error("Post is closed for comments.");
}
function comment_on_draft() {
global $json_api;
$json_api->error("You cannot comment on unpublished posts.");
}
function comment_post_redirect() {
global $comment, $json_api;
$status = ($comment->comment_approved) ? 'ok' : 'pending';
$new_comment = new JSON_API_Comment($comment);
$json_api->response->respond($new_comment, $status);
}
}
?>
+283
View File
@@ -0,0 +1,283 @@
<?php
class JSON_API_Post {
// Note:
// JSON_API_Post objects must be instantiated within The Loop.
var $id; // Integer
var $type; // String
var $slug; // String
var $url; // String
var $status; // String ("draft", "published", or "pending")
var $title; // String
var $title_plain; // String
var $content; // String (modified by read_more query var)
var $excerpt; // String
var $date; // String (modified by date_format query var)
var $modified; // String (modified by date_format query var)
var $categories; // Array of objects
var $tags; // Array of objects
var $author; // Object
var $comments; // Array of objects
var $attachments; // Array of objects
var $comment_count; // Integer
var $comment_status; // String ("open" or "closed")
var $thumbnail; // String
var $custom_fields; // Object (included by using custom_fields query var)
function JSON_API_Post($wp_post = null) {
if (!empty($wp_post)) {
$this->import_wp_object($wp_post);
}
}
function create($values = null) {
unset($values['id']);
if (empty($values) || empty($values['title'])) {
$values = array(
'title' => 'Untitled',
'content' => ''
);
}
return $this->save($values);
}
function update($values) {
$values['id'] = $this->id;
return $this->save($values);
}
function save($values = null) {
global $json_api, $user_ID;
$wp_values = array();
if (!empty($values['id'])) {
$wp_values['ID'] = $values['id'];
}
if (!empty($values['type'])) {
$wp_values['post_type'] = $values['type'];
}
if (!empty($values['status'])) {
$wp_values['post_status'] = $values['status'];
}
if (!empty($values['title'])) {
$wp_values['post_title'] = $values['title'];
}
if (!empty($values['content'])) {
$wp_values['post_content'] = $values['content'];
}
if (!empty($values['author'])) {
$author = $json_api->introspector->get_author_by_login($values['author']);
$wp_values['post_author'] = $author->id;
}
if (isset($values['categories'])) {
$categories = explode(',', $values['categories']);
foreach ($categories as $category_slug) {
$category_slug = trim($category_slug);
$category = $json_api->introspector->get_category_by_slug($category_slug);
if (empty($wp_values['post_category'])) {
$wp_values['post_category'] = array($category->id);
} else {
array_push($wp_values['post_category'], $category->id);
}
}
}
if (isset($values['tags'])) {
$tags = explode(',', $values['tags']);
foreach ($tags as $tag_slug) {
$tag_slug = trim($tag_slug);
if (empty($wp_values['tags_input'])) {
$wp_values['tags_input'] = array($tag_slug);
} else {
array_push($wp_values['tags_input'], $tag_slug);
}
}
}
if (isset($wp_values['ID'])) {
$this->id = wp_update_post($wp_values);
} else {
$this->id = wp_insert_post($wp_values);
}
if (!empty($_FILES['attachment'])) {
include_once ABSPATH . '/wp-admin/includes/file.php';
include_once ABSPATH . '/wp-admin/includes/media.php';
include_once ABSPATH . '/wp-admin/includes/image.php';
$attachment_id = media_handle_upload('attachment', $this->id);
$this->attachments[] = new JSON_API_Attachment($attachment_id);
unset($_FILES['attachment']);
}
$wp_post = get_post($this->id);
$this->import_wp_object($wp_post);
return $this->id;
}
function import_wp_object($wp_post) {
global $json_api, $post;
$date_format = $json_api->query->date_format;
$this->id = (int) $wp_post->ID;
setup_postdata($wp_post);
$this->set_value('type', $wp_post->post_type);
$this->set_value('slug', $wp_post->post_name);
$this->set_value('url', get_permalink($this->id));
$this->set_value('status', $wp_post->post_status);
$this->set_value('title', get_the_title($this->id));
$this->set_value('title_plain', strip_tags(@$this->title));
$this->set_content_value();
$this->set_value('excerpt', get_the_excerpt());
$this->set_value('date', get_the_time($date_format));
$this->set_value('modified', date($date_format, strtotime($wp_post->post_modified)));
$this->set_categories_value();
$this->set_tags_value();
$this->set_author_value($wp_post->post_author);
$this->set_comments_value();
$this->set_attachments_value();
$this->set_value('comment_count', (int) $wp_post->comment_count);
$this->set_value('comment_status', $wp_post->comment_status);
$this->set_thumbnail_value();
$this->set_custom_fields_value();
}
function set_value($key, $value) {
global $json_api;
if ($json_api->include_value($key)) {
$this->$key = $value;
} else {
unset($this->$key);
}
}
function set_content_value() {
global $json_api;
if ($json_api->include_value('content')) {
$content = get_the_content($json_api->query->read_more);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
$this->content = $content;
} else {
unset($this->content);
}
}
function set_categories_value() {
global $json_api;
if ($json_api->include_value('categories')) {
$this->categories = array();
if ($wp_categories = get_the_category($this->id)) {
foreach ($wp_categories as $wp_category) {
$category = new JSON_API_Category($wp_category);
if ($category->id == 1 && $category->slug == 'uncategorized') {
// Skip the 'uncategorized' category
continue;
}
$this->categories[] = $category;
}
}
} else {
unset($this->categories);
}
}
function set_tags_value() {
global $json_api;
if ($json_api->include_value('tags')) {
$this->tags = array();
if ($wp_tags = get_the_tags($this->id)) {
foreach ($wp_tags as $wp_tag) {
$this->tags[] = new JSON_API_Tag($wp_tag);
}
}
} else {
unset($this->tags);
}
}
function set_author_value($author_id) {
global $json_api;
if ($json_api->include_value('author')) {
$this->author = new JSON_API_Author($author_id);
} else {
unset($this->author);
}
}
function set_comments_value() {
global $json_api;
if ($json_api->include_value('comments')) {
$this->comments = $json_api->introspector->get_comments($this->id);
} else {
unset($this->comments);
}
}
function set_attachments_value() {
global $json_api;
if ($json_api->include_value('attachments')) {
$this->attachments = $json_api->introspector->get_attachments($this->id);
} else {
unset($this->attachments);
}
}
function set_thumbnail_value() {
global $json_api;
if (!$json_api->include_value('thumbnail') ||
!function_exists('get_post_thumbnail_id')) {
unset($this->thumbnail);
return;
}
$attachment_id = get_post_thumbnail_id($this->id);
if (!$attachment_id) {
unset($this->thumbnail);
return;
}
$thumbnail_size = $this->get_thumbnail_size();
list($thumbnail) = wp_get_attachment_image_src($attachment_id, $thumbnail_size);
$this->thumbnail = $thumbnail;
}
function set_custom_fields_value() {
global $json_api;
if ($json_api->include_value('custom_fields') &&
$json_api->query->custom_fields) {
$keys = explode(',', $json_api->query->custom_fields);
$wp_custom_fields = get_post_custom($this->id);
$this->custom_fields = new stdClass();
foreach ($keys as $key) {
if (isset($wp_custom_fields[$key])) {
$this->custom_fields->$key = $wp_custom_fields[$key];
}
}
} else {
unset($this->custom_fields);
}
}
function get_thumbnail_size() {
global $json_api;
if ($json_api->query->thumbnail_size) {
return $json_api->query->thumbnail_size;
} else if (function_exists('get_intermediate_image_sizes')) {
$sizes = get_intermediate_image_sizes();
if (in_array('post-thumbnail', $sizes)) {
return 'post-thumbnail';
}
}
return 'thumbnail';
}
}
?>
+26
View File
@@ -0,0 +1,26 @@
<?php
class JSON_API_Tag {
var $id; // Integer
var $slug; // String
var $title; // String
var $description; // String
function JSON_API_Tag($wp_tag = null) {
if ($wp_tag) {
$this->import_wp_object($wp_tag);
}
}
function import_wp_object($wp_tag) {
$this->id = (int) $wp_tag->term_id;
$this->slug = $wp_tag->slug;
$this->title = $wp_tag->name;
$this->description = $wp_tag->description;
$this->post_count = (int) $wp_tag->count;
}
}
?>