<?php
/**
 * Atom Feed Template for displaying Atom Posts feed.
 *
 * @package WordPress
 */

function echo_feed_subtitle() {
	$feed_subtitle=get_bloginfo_rss("description");
	if(!empty($feed_subtitle)) {
		echo '<subtitle type="text">'.$feed_subtitle.'</subtitle>'."\n";
	}
}
function get_query_id() {
	global $wp_query;
	return $wp_query->get_queried_object_id();
}
function echo_atom_feed_links($selfLink,$alternateLink,$repliesLink) {
	echo '<link rel="self" type="application/atom+xml" href="'.$selfLink.'" />'."\n".'<link rel="alternate" type="text/html" href="'.$alternateLink.'" />'."\n".'<link rel="replies" type="application/atom+xml" href="'.$repliesLink.'" />'."\n";
}

header('Content-Type: ' . feed_content_type('atom') . '; charset=' . get_option('blog_charset'), true);
$more = 1;
$feed_type="atom";

?>
<?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:thr="http://purl.org/syndication/thread/1.0"
  xml:lang="<?php echo get_option('rss_language'); ?>"
  xml:base="<?php bloginfo_rss('home') ?>/wp-atom.php"
  <?php do_action('atom_ns'); ?>
 >
	<id><?php bloginfo('atom_url'); ?></id>
	<title type="text"><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
<?php
if(is_category()) {
	$cat_id=get_query_id();
	echo_atom_feed_links(get_category_feed_link($cat_id),get_category_link($cat_id),get_category_comments_feed_link($cat_id,$feed_type));
}
elseif(is_tag()) {
	$tag_id=get_query_id();
	echo_atom_feed_links(get_tag_feed_link($tag_id),get_tag_link($tag_id),get_tag_comments_feed_link($tag_id,$feed_type));
}
elseif(is_author()) {
	$author_id=get_query_id();
	echo_atom_feed_links(get_author_feed_link($author_id),get_author_posts_url($author_id),get_author_comments_feed_link($author_id,$feed_type));
}
elseif(is_date()) {
	/* To be done */
}
elseif(is_search()) {
	$search_query=get_search_query();
	echo_atom_feed_links(get_search_feed_link($search_query,$feed_type),get_search_link($search_query),get_search_comments_feed_link($search_query,$feed_type));
}
else {
	echo_feed_subtitle();
	echo_atom_feed_links(get_bloginfo('atom_url'),get_bloginfo_rss('url').'/',get_bloginfo_rss('comments_atom_url'));
}?>
	<?php the_generator( 'atom' ); ?>
	<?php do_action('atom_head'); ?>
	<updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT')); ?></updated>
	<?php while (have_posts()) : the_post(); ?>
	<entry>
		<author>
			<name><?php the_author() ?></name>
			<?php $author_url = get_the_author_url(); if ( !empty($author_url) ) : ?>
			<uri><?php the_author_url()?></uri>
			<?php endif; ?>
		</author>
		<title type="<?php html_type_rss(); ?>"><![CDATA[<?php the_title_rss() ?>]]></title>
		<link rel="alternate" type="text/html" href="<?php the_permalink_rss() ?>" />
		<id><?php the_guid(); ?></id>
		<updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
		<published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
		<?php the_category_rss($feed_type) ?>
		<summary type="<?php html_type_rss(); ?>"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
<?php if ( !get_option('rss_use_excerpt') ) : ?>
		<content type="<?php html_type_rss(); ?>" xml:base="<?php the_permalink_rss() ?>"><![CDATA[<?php the_content('', 0, '') ?>]]></content>
<?php endif; ?>
<?php atom_enclosure(); ?>
<?php do_action('atom_entry'); ?>
		<link rel="replies" type="text/html" href="<?php the_permalink_rss() ?>#comments" thr:count="<?php echo get_comments_number()?>"/>
		<link rel="replies" type="application/atom+xml" href="<?php echo get_post_comments_feed_link(0,$feed_type) ?>" thr:count="<?php echo get_comments_number()?>"/>
		<thr:total><?php echo get_comments_number()?></thr:total>
	</entry>
	<?php endwhile ; ?>
</feed>
