diff options
author | Lukas Winkler <github@lw1.at> | 2019-01-22 08:16:33 +0300 |
---|---|---|
committer | diosmosis <diosmosis@users.noreply.github.com> | 2019-01-22 08:16:33 +0300 |
commit | fb633f5f649df67caffa4a7f64a7002656b0332d (patch) | |
tree | 62288ec9fad80762bcd93ff35e3c10920a117700 /plugins/RssWidget/RssRenderer.php | |
parent | d24e794df6aa7e8ad31ed190de551a456df57f6f (diff) |
improve RSS fetching (#13904)
* improve RSS fetching
* add cache to rss fetching and remove "appeared first on" appendix
* remove special handling of RSS description
* Make sure links in rss content open in new tab and have noreferrer noopener.
Diffstat (limited to 'plugins/RssWidget/RssRenderer.php')
-rw-r--r-- | plugins/RssWidget/RssRenderer.php | 75 |
1 files changed, 47 insertions, 28 deletions
diff --git a/plugins/RssWidget/RssRenderer.php b/plugins/RssWidget/RssRenderer.php index a2df5c1d8d..b2d6a691a9 100644 --- a/plugins/RssWidget/RssRenderer.php +++ b/plugins/RssWidget/RssRenderer.php @@ -8,6 +8,7 @@ */ namespace Piwik\Plugins\RssWidget; +use Piwik\Cache; use Piwik\Http; /** @@ -19,10 +20,16 @@ class RssRenderer protected $count = 3; protected $showDescription = false; protected $showContent = false; + /** + * @var Cache\Lazy + */ + private $cache; + public function __construct($url) { $this->url = $url; + $this->cache = Cache::getLazyCache(); } public function showDescription($bool) @@ -42,42 +49,54 @@ class RssRenderer public function get() { - try { - $content = Http::fetchRemoteFile($this->url); - $rss = simplexml_load_string($content); - } catch (\Exception $e) { - throw new \Exception("Error while importing feed: {$e->getMessage()}\n"); - } + $cacheId = 'RSS_' . md5($this->url); - $output = '<div style="padding:10px 15px;"><ul class="rss">'; - $i = 0; + $output = $this->cache->fetch($cacheId); - $items = array(); - if (!empty($rss->channel->item)) { - $items = $rss->channel->item; - } - foreach ($items as $post) { - $title = $post->title; - $date = @strftime("%B %e, %Y", strtotime($post->pubDate)); - $link = $post->link; - - $output .= '<li><a class="rss-title" title="" target="_blank" rel="noreferrer noopener" href="' . htmlspecialchars($link, ENT_COMPAT, 'UTF-8') . '">' . $title . '</a>' . - '<span class="rss-date">' . $date . '</span>'; - if ($this->showDescription) { - $output .= '<div class="rss-description">' . $post->description . '</div>'; + if (!$output) { + try { + $content = Http::fetchRemoteFile($this->url); + $rss = simplexml_load_string($content); + } catch (\Exception $e) { + throw new \Exception("Error while importing feed: {$e->getMessage()}\n"); } - if ($this->showContent) { - $output .= '<div class="rss-content">' . $post->content . '</div>'; + $output = '<div style="padding:10px 15px;"><ul class="rss">'; + $i = 0; + + $items = array(); + if (!empty($rss->channel->item)) { + $items = $rss->channel->item; } - $output .= '</li>'; + foreach ($items as $post) { + $title = $post->title; + $date = @strftime("%B %e, %Y", strtotime($post->pubDate)); + $link = $post->link; + + $output .= '<li><a class="rss-title" title="" target="_blank" rel="noreferrer noopener" href="' . htmlspecialchars($link, ENT_COMPAT, 'UTF-8') . '">' . $title . '</a>' . + '<span class="rss-date">' . $date . '</span>'; + if ($this->showDescription) { + $output .= '<div class="rss-description">' . $this->addTargetBlankAndNoReferrerToLinks($post->description) . '</div>'; + } + + if ($this->showContent) { + $output .= '<div class="rss-content">' . $this->addTargetBlankAndNoReferrerToLinks($post->content) . '</div>'; + } + $output .= '</li>'; - if (++$i == $this->count) { - break; + if (++$i == $this->count) { + break; + } } - } - $output .= '</ul></div>'; + $output .= '</ul></div>'; + $this->cache->save($cacheId, $output, 60 * 60 * 24); + } return $output; } + + private function addTargetBlankAndNoReferrerToLinks($content) + { + return str_replace('<a ', '<a target="_blank" rel="noreferrer noopener"', $content); + } } |