total_results = $total_results;
$this->start_index = $start_index;
$this->items_per_page = $items_per_page;
}
static function fromWordpressResults($page_index, $page_count, $page_size) {
return new OpenSearchSearchResults($page_count * $page_size, (($page_index - 1) * $page_size) + 1, $page_size);
}
function to_xml() {
return "{$this->total_results}".
"{$this->start_index}".
"{$this->items_per_page}";
}
}
class AtomPubLink {
static function to_xml($href, $rel, $type = NULL) {
$type = isset($type) ? "type='$type'" : "";
return "";
}
function __construct($href, $rel, $type) {
$this->href = $href;
$this->rel = $rel;
$this->type = $type;
}
function __toString() {
return self::to_xml($this->href, $this->rel, $this->type);
}
}
abstract class AtomPubFeed {
static function createChildrenFeed(UrlGenerator $url_generator, PostType $post_type, $post_id, $page_index, $page_count, $page_size, $first_post_modified) {
return new ChildrenAtomPubFeed($url_generator, $post_type, $post_id, $page_index, $page_count, $page_size, $first_post_modified);
}
static function createListFeed(UrlGenerator $url_generator, PostType $post_type, $page_index, $page_count, $page_size, $first_post_modified) {
return new ListAtomPubFeed($url_generator, $post_type, $page_index, $page_count, $page_size, $first_post_modified);
}
public static function createEntryFeed(UrlGenerator $url_generator, $post_type, $post_id, $first_post_modified) {
return new EntryAtomPubFeed($url_generator, $post_type, $post_id, $first_post_modified);
}
protected function __construct(UrlGenerator $url_generator) {
global $ATOM_CONTENT_TYPE;
$this->url_generator = $url_generator;
$this->response = new AtomPubResponse();
$this->response->set_header("Content-Type: " . $ATOM_CONTENT_TYPE);
}
protected function start_feed($feed_id, $feed_title, $first_post_modified) {
global $ATOM_NS, $ATOMPUB_NS, $OPENSEARCH_NS;
$xml = <<
{$feed_id}
{$feed_title}
EOD;
$xml .= rest_to_line(" " . atompub_to_date($first_post_modified) . "");
$this->response->
add_body($xml);
}
function add_post($post, $author, $categories, $include_content = true) {
global $ATOM_CONTENT_TYPE;
global $ContentType_ATOM, $ContentType_HTML;
$guid = $post->guid;
$post_title = $post->post_title;
$post_parent = $post->post_parent;
$post_date = $post->post_date;
$post_modified = $post->post_modified;
$post_content = $post->post_content;
$post_status = $post->post_status;
$author_display_name = $author->display_name;
$post_type = PostType::from_wordpress($post->post_type);
// Work around http://core.trac.wordpress.org/ticket/15041
if (preg_match("/^.*[0-9]+$/", $guid) == 0) {
$guid .= "?p={$post->ID}";
}
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" {$guid}");
$content = str_replace(']]>', ']]>', $post_title);
list($content_type, $content) = self::encode_string($content);
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" " . atompub_to_date($post_date) . "");
$xml .= rest_to_line(" " . atompub_to_date($post_modified) . "");
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" " . $author_display_name . "");
$xml .= rest_to_line(" ");
foreach ((array) $categories as $category) {
// This scheme should be transferrable from installation to installation but now it just points to
// the current installation
$xml .= rest_to_line(" ");
}
$url = $this->url_generator->post_url($post->ID, $post_type, $ContentType_ATOM);
$link = AtomPubLink::to_xml($url, "self", $ATOM_CONTENT_TYPE);
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" " . $link);
if($post_parent != 0) {
$url = $this->url_generator->post_url($post_parent, $post_type, $ContentType_ATOM);
$link = AtomPubLink::to_xml($url, "parent", $ATOM_CONTENT_TYPE);
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" " . $link);
}
// $url = $this->url_generator->child_posts_of($post->ID, 1, $post_type);
// $link = AtomPubLink::to_xml($url, "urn:wordpress:children", $ATOM_CONTENT_TYPE);
// $xml .= rest_to_line(" ");
// $xml .= rest_to_line(" " . $link);
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" Child pages");
$xml .= rest_to_line(" $ATOM_CONTENT_TYPE;type=entry");
$xml .= rest_to_line(" ");
$xml .= rest_to_line(" ");
if ($post_status == 'publish') {
$xml .= rest_to_line(" no");
}
else {
$xml .= rest_to_line(" yes");
}
$xml .= rest_to_line(" " . atompub_to_date($post_modified) . "");
if ($include_content) {
$content = apply_filters('the_content', $post_content);
$content = str_replace(']]>', ']]>', $content);
$content = apply_filters('the_content_feed', $content, "atom");
$xml .= rest_to_line(" ");
}
else {
$xml .= rest_to_line(" ");
}
$xml .= rest_to_line(" ");
$this->response->add_body($xml);
}
/**
* @return AtomPubResponse
*/
function to_response() {
return $this->response->add_body(rest_to_line(""));
}
static function encode_string($data) {
return array('html', htmlentities($data, ENT_COMPAT, "UTF-8"));
}
}
class ListAtomPubFeed extends AtomPubFeed {
function __construct(UrlGenerator $url_generator, PostType $post_type, $page_index, $page_count, $page_size, $first_post_modified) {
global $ATOM_CONTENT_TYPE;
parent::__construct($url_generator);
$options = AtomPubOptions::get_options();
$feed_id = $url_generator->list_iri($post_type);
$feed_title = "{$post_type}s List";
$this->start_feed($feed_id, $feed_title, $first_post_modified);
$search_result = OpenSearchSearchResults::fromWordpressResults($page_index, $page_count, $page_size);
$this->response->add_body(rest_to_line($search_result->to_xml()));
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->list_url($page_index, $post_type), "self", $ATOM_CONTENT_TYPE)));
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->list_url(1, $post_type), "first", $ATOM_CONTENT_TYPE)));
if (($page_index + 1) <= $page_count) {
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->list_url($page_index + 1, $post_type), "next", $ATOM_CONTENT_TYPE)));
}
if (($page_index - 1) > 1) {
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->list_url($page_index - 1, $post_type), "prev", $ATOM_CONTENT_TYPE)));
}
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->list_url($page_count, $post_type), "last", $ATOM_CONTENT_TYPE)));
$hubs = $options->hubs();
if($hubs->is_set()) {
foreach($hubs->urls() as $url) {
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($url, "hub")));
}
}
}
}
class ChildrenAtomPubFeed extends AtomPubFeed {
function __construct(UrlGenerator $url_generator, PostType $post_type, $post_id, $page_index, $page_count, $page_size, $first_post_modified) {
global $ATOM_CONTENT_TYPE;
parent::__construct($url_generator);
$feed_id = $url_generator->child_posts_iri($post_type, $post_id);
$feed_title = "Children of #$post_id";
$this->start_feed($feed_id, $feed_title, $first_post_modified);
$search_result = OpenSearchSearchResults::fromWordpressResults($page_index, $page_count, $page_size);
$this->response->add_body(rest_to_line($search_result->to_xml()));
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->list_url($page_index, $post_type), "self", $ATOM_CONTENT_TYPE)));
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->child_posts_of($post_id, 1, $post_type), "first", $ATOM_CONTENT_TYPE)));
if (($page_index + 1) <= $page_count) {
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->child_posts_of($post_id, $page_index + 1, $post_type), "next", $ATOM_CONTENT_TYPE)));
}
if (($page_index - 1) > 1) {
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->child_posts_of($post_id, $page_index - 1, $post_type), "prev", $ATOM_CONTENT_TYPE)));
}
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->child_posts_of($post_id, $page_count, $post_type), "last", $ATOM_CONTENT_TYPE)));
}
}
class EntryAtomPubFeed extends AtomPubFeed {
function __construct(UrlGenerator $url_generator, PostType $post_type, $post_id, $first_post_modified) {
global $ATOM_CONTENT_TYPE;
global $ContentType_ATOM;
parent::__construct($url_generator);
$feed_id = $url_generator->post_iri($post_type, $post_id);
$feed_title = "Entry #$post_id";
$this->start_feed($feed_id, $feed_title, $first_post_modified);
$this->response->add_body(rest_to_line(" " . AtomPubLink::to_xml($this->url_generator->post_url($post_id, $post_type, $ContentType_ATOM), "self", $ATOM_CONTENT_TYPE)));
}
}
?>