Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Generate podcast feeds
*/
function generate_podcast_feed() {
global $wp_query, $post;
// Get audio type
$type = $wp_query->query_vars["feed"];
// HTTPS
$schema = $_SERVER["HTTPS"] == "on" ? 'https' : 'http';
// Edit feed title
add_filter("wp_title_rss", "__return_null");
// Add iTunes podcast specs
add_action("rss2_ns", function() {
echo 'xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:psc="http://podlove.org/simple-chapters"';
});
// Alternative feeds
$alternative_feeds = array(
"mp3" => $schema . "://anyca.st/feed/mp3",
//"mp3 (via BitTorrent)" => "http://bitlove.org/bruhndsoweiter/anycast-mp3/feed",
"aac" => $schema . "://anyca.st/feed/aac",
//"aac (via BitTorrent)" => "http://bitlove.org/bruhndsoweiter/anycast-aac/feed",
"ogg" => $schema . "://anyca.st/feed/ogg",
//"ogg (via BitTorrent)" => "http://bitlove.org/bruhndsoweiter/anycast-ogg/feed",
"opus" => $schema . "://anyca.st/feed/opus",
//"opus (via BitTorrent)" => "http://bitlove.org/bruhndsoweiter/anycast-opus/feed",
);
// Add image and iTunes metadata
add_action("rss2_head", function() use($alternative_feeds, $type, $schema) {
// Add alterantive feeds
foreach ( $alternative_feeds as $alternative_feed => $url ) if ( $alternative_feed != $type )
echo "\n\t<atom:link rel=\"alternate\" href=\"{$url}\" type=\"application/rss+xml\" title=\"{$alternative_feed}\"/>";
// Add image
echo "\n\n\t<image>\n\t\t<url>{$schema}://anyca.st/wp-content/themes/anycast-v2/images/itunes.jpg</url>\n\t\t<title>anyca.st</title>\n\t\t<link>{$schema}://anyca.st</link>\n\t\t<width>144</width>\n\t\t<height>144</height>\n\t</image>\n";
// Add itunes meta data
echo "\n\t<itunes:subtitle>Internetunterhaltungsfunk mit Renke Bruhn und Dennis Morhardt</itunes:subtitle>";
echo "\n\t<itunes:summary>Tagesaktuelles, Netzpolitik und _wirklich_ Wichtiges aus Göttingen. Hoffentlich alle 14 Tage neu. Nur echt™ mit @gglnx und @bruhndsoweiter. Garantiert ohne Erdstrahlen.</itunes:summary>";
echo "\n\t<itunes:keywords>german,anycast,dennismorhardt,renkebruhn</itunes:keywords>";
echo "\n\t<itunes:author>Renke Bruhn &amp; Dennis Morhardt</itunes:author>";
echo "\n\t<itunes:block>no</itunes:block>";
echo "\n\t<itunes:explicit>no</itunes:explicit>";
echo "\n\t<itunes:image href=\"{$schema}://anyca.st/wp-content/themes/anycast-v2/images/itunes.jpg\" />";
echo "\n\t<itunes:category text=\"Society &amp; Culture\"><itunes:category text=\"Philosophy\" /></itunes:category>";
echo "\n\t<itunes:category text=\"Technology\"><itunes:category text=\"Gadgets\" /></itunes:category>";
echo "\n\t<itunes:category text=\"News &amp; Politics\" />";
echo "\n\n\t<itunes:owner>\n\t\t<itunes:name>anyca.st</itunes:name>\n\t\t<itunes:email>info@anyca.st</itunes:email>\n\t</itunes:owner>\n\n";
});
// Add title to self link
add_filter('bloginfo_rss', function($content, $show) use($type) {
// Change request uri
if ( "name" == $show ) add_filter("clean_url", function($url) use($type) { return $url . '" title="' . $type; }, 666, 2);
// Remove filter
elseif ( "url" == $show ) unset($GLOBALS['wp_filter']["clean_url"][666]);
// Return content
return $content;
}, 10, 2);
// Add post metadata
add_action("rss2_item", function() use($type, $schema) {
// Get meta data
$src = get_post_meta(get_the_ID(), 'audio', true);
$m = new mp3file('/xxx/anyca.st/' . $src . '.mp3');
$a = $m->get_metadata();
// Add itunes meta data
echo "\n\t\t<itunes:summary>" . feedSafeContent(get_the_content(), true) . "</itunes:summary>\n";
echo "\t\t<itunes:subtitle>" . feedSafeContent(mb_substr(get_the_content(), 0, 140) . "...", true) . "</itunes:subtitle>\n";
echo "\t\t<atom:link rel=\"payment\" href=\"https://flattr.com/submit/auto?url=" . get_permalink() . "&amp;user_id=anycast&amp;title=" . urlencode(get_the_title()) . "&amp;description=" . urlencode(get_the_excerpt()) . "&amp;category=audio\" type=\"text/html\" />\n";
echo "\t\t<itunes:keywords>Podcast</itunes:keywords>\n";
echo "\t\t<itunes:author>Renke Bruhn &amp; Dennis Morhardt</itunes:author>\n";
echo "\t\t<itunes:explicit>no</itunes:explicit>\n";
echo "\t\t<itunes:block>no</itunes:block>\n";
echo "\t\t<itunes:duration>" . $a["Length2"] . "</itunes:duration>\n";
// Add chapters
if ( $shownotes = get_post_meta( get_the_ID(), 'shownotes', true ) ):
echo "\n\t\t<psc:chapters version=\"1.1\">";
foreach ( $shownotes as $time => $data ):
$url = "";
if ( "<a" == substr( $data["title"], 0, 2 ) && preg_match( "/href=\"(.*)\">/", str_replace("'", '"', $data["title"]), $_m_url ) ) $url = ' href="' . $_m_url[1] . '"';
echo "\n\t\t\t<psc:chapter start=\"{$time}\" title=\"" . feedSafeContent(strip_tags($data["title"])) . "\"{$url} />";
endforeach;
echo "\n\t\t</psc:chapters>\n";
endif;
// Add audio file
$type2mine = array("mp3" => "mpeg", "aac" => "aac", "opus" => "opus", "ogg" => "ogg");
$type2file = array("mp3" => "mp3", "aac" => "m4a", "opus" => "opus", "ogg" => "ogg");
echo "\n\t\t<enclosure url=\"{$schema}://download.anyca.st/" . $src . "." . $type2file[$type] . "\" length=\"" . filesize("/var/www/anyca.st/" . $src . "." . $type2file[$type]) . "\" type=\"audio/" . $type2mine[$type] . "\" />\n";
});
// Query posts
query_posts(array("category_name" => "Podcast", "posts_per_page" => -1));
// Load feed template
require_once(ABSPATH . WPINC . '/feed-rss2.php');
}
add_feed("mp3", "generate_podcast_feed");
add_feed("aac", "generate_podcast_feed");
add_feed("ogg", "generate_podcast_feed");
add_feed("opus", "generate_podcast_feed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment