From 8c48ddfe688337fed28bd92b0085c247cfada740 Mon Sep 17 00:00:00 2001 From: Jeremy Gonyea Date: Sat, 19 Mar 2022 02:08:46 -0400 Subject: [PATCH] Fix Issue #56 Podcast's feed.rss.twig overide --- CHANGELOG.md | 13 +++++++++ blueprints.yaml | 2 +- podcast.php | 61 +++++++++++++++++++++++------------------ templates/feed.rss.twig | 44 ++++++++++++++++++++++++++++- 4 files changed, 92 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2314fb5..dcf32cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/blueprints.yaml b/blueprints.yaml index 82c83db..4627a94 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -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: diff --git a/podcast.php b/podcast.php index 7c92390..b32282c 100644 --- a/podcast.php +++ b/podcast.php @@ -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; @@ -12,7 +13,7 @@ /** * Class PodcastPlugin - * + * * @package Grav\Plugin */ class PodcastPlugin extends Plugin @@ -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], @@ -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()) { @@ -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. @@ -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']); @@ -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']); @@ -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']); @@ -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); @@ -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); @@ -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()); @@ -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()); @@ -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()); diff --git a/templates/feed.rss.twig b/templates/feed.rss.twig index 4f58715..b8c5763 100644 --- a/templates/feed.rss.twig +++ b/templates/feed.rss.twig @@ -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' %} @@ -53,4 +53,46 @@ {% endfor %} +{% 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 %} + + + + {{ collection.params.title }} + {{ page.url(true) }} + + {{ collection.params.description }} + {{ grav.language.getLanguage|default(config.system.language.default_lang)|default('en') }} + {{ lastBuildDate|date('D, d M Y H:i:s O') }} + {% for item in collection %} + {% set banner = item.media.images|first %} + + {{ item.title|e }} + {{ item.url(true) }} + {{ item.url(true) }} + {{ item.date|date('D, d M Y H:i:s O') }} + + + + {% for tag in item.taxonomy.tag %} + {{ tag|e }} + {% endfor %} + + {% endfor %} + + {% endif %}