Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RssRenderer.php « RssWidget « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a2df5c1d8d6a79c4cf93bdb8eef8c0e85a3bcaae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\RssWidget;
use Piwik\Http;

/**
 *
 */
class RssRenderer
{
    protected $url = null;
    protected $count = 3;
    protected $showDescription = false;
    protected $showContent = false;

    public function __construct($url)
    {
        $this->url = $url;
    }

    public function showDescription($bool)
    {
        $this->showDescription = $bool;
    }

    public function showContent($bool)
    {
        $this->showContent = $bool;
    }

    public function setCountPosts($count)
    {
        $this->count = $count;
    }

    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");
        }

        $output = '<div style="padding:10px 15px;"><ul class="rss">';
        $i = 0;

        $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 ($this->showContent) {
                $output .= '<div class="rss-content">' . $post->content . '</div>';
            }
            $output .= '</li>';

            if (++$i == $this->count) {
                break;
            }
        }

        $output .= '</ul></div>';
        return $output;
    }
}