Grav\Plugin\Admin\Admin::getFeed PHP Метод

getFeed() публичный Метод

Get https://getgrav.org news feed
public getFeed ( ) : mixed
Результат mixed
    public function getFeed()
    {
        $feed_url = 'https://getgrav.org/blog.atom';
        $body = Response::get($feed_url);
        $reader = new Reader();
        $parser = $reader->getParser($feed_url, $body, 'utf-8');
        $feed = $parser->execute();
        return $feed;
    }

Usage Example

Пример #1
0
 protected function taskGetNewsFeed()
 {
     $cache = $this->grav['cache'];
     if ($this->post['refresh'] == 'true') {
         $cache->delete('news-feed');
     }
     $feed_data = $cache->fetch('news-feed');
     if (!$feed_data) {
         try {
             $feed = $this->admin->getFeed();
             if (is_object($feed)) {
                 require_once __DIR__ . '/../twig/AdminTwigExtension.php';
                 $adminTwigExtension = new AdminTwigExtension();
                 $feed_items = $feed->getItems();
                 // Feed should only every contain 10, but just in case!
                 if (count($feed_items) > 10) {
                     $feed_items = array_slice($feed_items, 0, 10);
                 }
                 foreach ($feed_items as $item) {
                     $datetime = $adminTwigExtension->adminNicetimeFilter($item->getDate()->getTimestamp());
                     $feed_data[] = '<li><span class="date">' . $datetime . '</span> <a href="' . $item->getUrl() . '" target="_blank" title="' . str_replace('"', '″', $item->getTitle()) . '">' . $item->getTitle() . '</a></li>';
                 }
             }
             // cache for 1 hour
             $cache->save('news-feed', $feed_data, 60 * 60);
         } catch (\Exception $e) {
             $this->admin->json_response = ['status' => 'error', 'message' => $e->getMessage()];
             return;
         }
     }
     $this->admin->json_response = ['status' => 'success', 'feed_data' => $feed_data];
 }