Skip to content

Commit

Permalink
Fix Issue #56 Podcast's feed.rss.twig overide
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonyea committed Mar 19, 2022
1 parent a4fd774 commit 8c48ddf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 28 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# v3.0.7
## 03/19/2021

1. [](#new)

2. [](#improved)
* Some minor PSR2 fixes.
* Updated a few hardcoded paths to use Grav's locator instead.
* Remote files are now downloaded to Grav's tmp folder instead of the system's /tmp folder.

3. [](#bugfix)
* Bandaid fixed feed.rss.twig override clobbering regular rss feeds.

# v3.0.6
## 03/16/2021

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Podcast
version: 3.0.6
version: 3.0.7
description: Creates Podcast page types and related podcast RSS feeds
icon: microphone
author:
Expand Down
61 changes: 35 additions & 26 deletions podcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Grav\Common\Grav;
use Grav\Common\Page\Header;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Flex\Types\Pages\PageObject;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File;
Expand All @@ -12,7 +13,7 @@

/**
* Class PodcastPlugin
*
*
* @package Grav\Plugin
*/
class PodcastPlugin extends Plugin
Expand All @@ -32,7 +33,7 @@ class PodcastPlugin extends Plugin
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
Expand All @@ -42,7 +43,7 @@ public static function getSubscribedEvents()
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
public function onPluginsInitialized(): void
{
// If in an Admin page.
if ($this->isAdmin()) {
Expand Down Expand Up @@ -75,41 +76,48 @@ public function onGetPageTemplates(Event $event)
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
$this->grav['twig']->twig_paths[] = $this->grav['locator']->findResource('plugin://' . $this->name . '/templates');
}

public function onTwigSiteVariables()
/**
* Adds podcast stylesheet to page.
*/
public function onTwigSiteVariables(): void
{
$this->grav['assets']
->addCss('plugin://podcast/assets/css/podcast.css');
$this->grav['assets']->addCss('plugin://' . $this->name . '/assets/css/podcast.css');
}

/**
* Modifies the page header being saved to include getID3 metadata.
*/
public function onAdminSave($event)
public function onAdminSave(Event $event): void
{

/** @var PageObject $page */
$page = $event['object'];

// Process only onAdminSave events on pages.
if (!$page instanceof PageInterface) {
return;
}
/** @var Header $header */
$header = $page->header();

if ( str_starts_with($page->template(), 'podcast-') ) {
if (str_starts_with($page->template(), 'podcast-')) {
// Set autodate field on all podcast-* page types.
if (!isset($header->date)){
if (!isset($header->date)) {
$date = date($this->grav['config']->get('system.pages.dateformat.default', 'H:i d-m-Y'));
$header['date'] = $date;
}

// Fix for Feed plugin 1.8.2+ requiring 'content' instead of 'feed' in the header.
if ( isset($header['feed']) == true ) {
if (isset($header['feed']) == true) {
$header['content'] = $header['feed'];
$header->undef('feed');
}

} else {
// Refrain from editing pages not of template "podcast-*".
return;
}

// Return with just updated header content if not podcast-episode.
Expand Down Expand Up @@ -188,9 +196,9 @@ public function onAdminSave($event)
* @param string $file
* Path to audio file.
* @return type
* Audio filesize
* Audio filesize, in bytes.
*/
public static function retreiveAudioLength($file)
public static function retreiveAudioLength($file): int
{
$id3 = GetID3Plugin::analyzeFile($file);
return ($id3['filesize']);
Expand All @@ -201,10 +209,10 @@ public static function retreiveAudioLength($file)
*
* @param string $file
* Path to audio file.
* @return type
* Audio filesize
* @return string
* Audio file type.
*/
public static function retreiveAudioType($file)
public static function retreiveAudioType($file): string
{
$id3 = GetID3Plugin::analyzeFile($file);
return ($id3['mime_type']);
Expand All @@ -218,7 +226,7 @@ public static function retreiveAudioType($file)
* @return string
* Audio file duration.
*/
public static function retreiveAudioDuration($file)
public static function retreiveAudioDuration($file): string
{
$id3 = GetID3Plugin::analyzeFile($file);
return ($id3['playtime_string']);
Expand All @@ -232,7 +240,7 @@ public static function retreiveAudioDuration($file)
* @return string
* filepath to temp file.
*/
public function getRemoteAudio($url)
public function getRemoteAudio($url): string
{
// Make sure the url is reachable.
$ch = curl_init($url);
Expand All @@ -252,7 +260,8 @@ public function getRemoteAudio($url)

// Download file to temp location.
if ($remote_file = fopen($url, 'rb')) {
$local_file = tempnam('/tmp', 'podcast');
$tmp_dir = $this->grav['locator']->findResource('tmp://', true, true);
$local_file = tempnam($tmp_dir, 'podcast');
$handle = fopen($local_file, "w");
$contents = stream_get_contents($remote_file);

Expand All @@ -270,9 +279,9 @@ public function getRemoteAudio($url)
* @return array
* Array of options for select list.
*/
public static function getCategoryOptions()
public static function getCategoryOptions(): array
{
$options = array();
$options = [];
$data_file_path = __DIR__ . DS . 'data' . DS . 'iTunesCategories.yaml';
$file = File::instance($data_file_path);
$data = Yaml::parse($file->content());
Expand All @@ -291,9 +300,9 @@ public static function getCategoryOptions()
* @return array
* Array of options for select list.
*/
public static function getSubCategoryOptions()
public static function getSubCategoryOptions(): array
{
$options = array();
$options = [];
$data_file_path = __DIR__ . DS . 'data' . DS . 'iTunesCategories.yaml';
$file = File::instance($data_file_path);
$data = Yaml::parse($file->content());
Expand All @@ -316,9 +325,9 @@ public static function getSubCategoryOptions()
* @return array
* Array of options for select list.
*/
public static function getLanguageOptions()
public static function getLanguageOptions(): array
{
$options = array();
$options = [];
$data_file_path = __DIR__ . DS . 'data' . DS . 'languages.yaml';
$file = File::instance($data_file_path);
$languages = Yaml::parse($file->content());
Expand Down
44 changes: 43 additions & 1 deletion templates/feed.rss.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{# Need to find if we're at the channel or a series page #}
{# Need to find if we're at the podcast-channel or a podcast-series page #}
{% if page.template == 'podcast-channel' %}
{% set channel = page %}
{% elseif page.template == 'podcast-series' %}
Expand Down Expand Up @@ -53,4 +53,46 @@
{% endfor %}
</channel>
</rss>
{% else %}
{# Copied from the default feed.rss.twig #}
{# Format specification: https://www.rssboard.org/rss-specification #}
{% set collection = collection|default(page.collection) %}
{% set lastBuildDate = 0 %}
{% for page in collection %}
{%- set lastBuildDate = max(lastBuildDate, page.date) %}
{%- if collection.params.show_last_modified %}
{%- set lastBuildDate = max(feed_updated, page.modified) %}
{%- endif %}
{% endfor %}
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>{{ collection.params.title }}</title>
<link>{{ page.url(true) }}</link>
<atom:link href="{{ uri.rootUrl(true)~uri.uri() }}" rel="self" type="application/rss+xml"/>
<description>{{ collection.params.description }}</description>
<language>{{ grav.language.getLanguage|default(config.system.language.default_lang)|default('en') }}</language>
<lastBuildDate>{{ lastBuildDate|date('D, d M Y H:i:s O') }}</lastBuildDate>
{% for item in collection %}
{% set banner = item.media.images|first %}
<item>
<title>{{ item.title|e }}</title>
<link>{{ item.url(true) }}</link>
<guid>{{ item.url(true) }}</guid>
<pubDate>{{ item.date|date('D, d M Y H:i:s O') }}</pubDate>
<description>
<![CDATA[
{% if banner %}
{{ banner.cropZoom(1200,800).html|absolute_url|raw }}
{% endif %}
{{ item.content|safe_truncate_html(collection.params.length)|raw }}
]]>
</description>
{% for tag in item.taxonomy.tag %}
<category>{{ tag|e }}</category>
{% endfor %}
</item>
{% endfor %}
</channel>
</rss>
{% endif %}

0 comments on commit 8c48ddf

Please sign in to comment.