Fix Issue #56 Podcast's feed.rss.twig overide

This commit is contained in:
Jeremy Gonyea
2022-03-19 02:08:46 -04:00
parent a4fd7749e4
commit 8c48ddfe68
4 changed files with 92 additions and 28 deletions

View File

@@ -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 # v3.0.6
## 03/16/2021 ## 03/16/2021

View File

@@ -1,5 +1,5 @@
name: Podcast name: Podcast
version: 3.0.6 version: 3.0.7
description: Creates Podcast page types and related podcast RSS feeds description: Creates Podcast page types and related podcast RSS feeds
icon: microphone icon: microphone
author: author:

View File

@@ -4,6 +4,7 @@ namespace Grav\Plugin;
use Grav\Common\Grav; use Grav\Common\Grav;
use Grav\Common\Page\Header; use Grav\Common\Page\Header;
use Grav\Common\Page\Interfaces\PageInterface; use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Flex\Types\Pages\PageObject;
use Grav\Common\Plugin; use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event; use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File; use RocketTheme\Toolbox\File\File;
@@ -12,7 +13,7 @@ use Grav\Plugin\GetID3Plugin;
/** /**
* Class PodcastPlugin * Class PodcastPlugin
* *
* @package Grav\Plugin * @package Grav\Plugin
*/ */
class PodcastPlugin extends Plugin class PodcastPlugin extends Plugin
@@ -32,7 +33,7 @@ class PodcastPlugin extends Plugin
* callable (or function) as well as the priority. The * callable (or function) as well as the priority. The
* higher the number the higher the priority. * higher the number the higher the priority.
*/ */
public static function getSubscribedEvents() public static function getSubscribedEvents(): array
{ {
return [ return [
'onPluginsInitialized' => ['onPluginsInitialized', 0], 'onPluginsInitialized' => ['onPluginsInitialized', 0],
@@ -42,7 +43,7 @@ class PodcastPlugin extends Plugin
/** /**
* Initialize the plugin * Initialize the plugin
*/ */
public function onPluginsInitialized() public function onPluginsInitialized(): void
{ {
// If in an Admin page. // If in an Admin page.
if ($this->isAdmin()) { if ($this->isAdmin()) {
@@ -75,41 +76,48 @@ class PodcastPlugin extends Plugin
*/ */
public function onTwigTemplatePaths() 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'] $this->grav['assets']->addCss('plugin://' . $this->name . '/assets/css/podcast.css');
->addCss('plugin://podcast/assets/css/podcast.css');
} }
/** /**
* Modifies the page header being saved to include getID3 metadata. * 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']; $page = $event['object'];
// Process only onAdminSave events on pages. // Process only onAdminSave events on pages.
if (!$page instanceof PageInterface) { if (!$page instanceof PageInterface) {
return; return;
} }
/** @var Header $header */
$header = $page->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. // 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')); $date = date($this->grav['config']->get('system.pages.dateformat.default', 'H:i d-m-Y'));
$header['date'] = $date; $header['date'] = $date;
} }
// Fix for Feed plugin 1.8.2+ requiring 'content' instead of 'feed' in the header. // 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['content'] = $header['feed'];
$header->undef('feed'); $header->undef('feed');
} }
} else {
// Refrain from editing pages not of template "podcast-*".
return;
} }
// Return with just updated header content if not podcast-episode. // Return with just updated header content if not podcast-episode.
@@ -188,9 +196,9 @@ class PodcastPlugin extends Plugin
* @param string $file * @param string $file
* Path to audio file. * Path to audio file.
* @return type * @return type
* Audio filesize * Audio filesize, in bytes.
*/ */
public static function retreiveAudioLength($file) public static function retreiveAudioLength($file): int
{ {
$id3 = GetID3Plugin::analyzeFile($file); $id3 = GetID3Plugin::analyzeFile($file);
return ($id3['filesize']); return ($id3['filesize']);
@@ -201,10 +209,10 @@ class PodcastPlugin extends Plugin
* *
* @param string $file * @param string $file
* Path to audio file. * Path to audio file.
* @return type * @return string
* Audio filesize * Audio file type.
*/ */
public static function retreiveAudioType($file) public static function retreiveAudioType($file): string
{ {
$id3 = GetID3Plugin::analyzeFile($file); $id3 = GetID3Plugin::analyzeFile($file);
return ($id3['mime_type']); return ($id3['mime_type']);
@@ -218,7 +226,7 @@ class PodcastPlugin extends Plugin
* @return string * @return string
* Audio file duration. * Audio file duration.
*/ */
public static function retreiveAudioDuration($file) public static function retreiveAudioDuration($file): string
{ {
$id3 = GetID3Plugin::analyzeFile($file); $id3 = GetID3Plugin::analyzeFile($file);
return ($id3['playtime_string']); return ($id3['playtime_string']);
@@ -232,7 +240,7 @@ class PodcastPlugin extends Plugin
* @return string * @return string
* filepath to temp file. * filepath to temp file.
*/ */
public function getRemoteAudio($url) public function getRemoteAudio($url): string
{ {
// Make sure the url is reachable. // Make sure the url is reachable.
$ch = curl_init($url); $ch = curl_init($url);
@@ -252,7 +260,8 @@ class PodcastPlugin extends Plugin
// Download file to temp location. // Download file to temp location.
if ($remote_file = fopen($url, 'rb')) { 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"); $handle = fopen($local_file, "w");
$contents = stream_get_contents($remote_file); $contents = stream_get_contents($remote_file);
@@ -270,9 +279,9 @@ class PodcastPlugin extends Plugin
* @return array * @return array
* Array of options for select list. * 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'; $data_file_path = __DIR__ . DS . 'data' . DS . 'iTunesCategories.yaml';
$file = File::instance($data_file_path); $file = File::instance($data_file_path);
$data = Yaml::parse($file->content()); $data = Yaml::parse($file->content());
@@ -291,9 +300,9 @@ class PodcastPlugin extends Plugin
* @return array * @return array
* Array of options for select list. * 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'; $data_file_path = __DIR__ . DS . 'data' . DS . 'iTunesCategories.yaml';
$file = File::instance($data_file_path); $file = File::instance($data_file_path);
$data = Yaml::parse($file->content()); $data = Yaml::parse($file->content());
@@ -316,9 +325,9 @@ class PodcastPlugin extends Plugin
* @return array * @return array
* Array of options for select list. * 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'; $data_file_path = __DIR__ . DS . 'data' . DS . 'languages.yaml';
$file = File::instance($data_file_path); $file = File::instance($data_file_path);
$languages = Yaml::parse($file->content()); $languages = Yaml::parse($file->content());

View File

@@ -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' %} {% if page.template == 'podcast-channel' %}
{% set channel = page %} {% set channel = page %}
{% elseif page.template == 'podcast-series' %} {% elseif page.template == 'podcast-series' %}
@@ -53,4 +53,46 @@
{% endfor %} {% endfor %}
</channel> </channel>
</rss> </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 %} {% endif %}