Advertisement
ridgey28

Class wow_Rss - Display Multiple RSS in your WP Theme

Apr 24th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.86 KB | None | 0 0
  1. <?php
  2. /**
  3. *   Class to return multiple RSS feeds in WordPress
  4. *       Instructions:
  5. *       1.  Upload this class to theme folder
  6. *       2.  Include this class in functions.php  ie:  include "mytheme/wow_rss.php";
  7.  
  8. *       In theme template:
  9. *       3.  Create new object of class ie: $obj = new wow_Rss ("http://www.mywebsite.co.uk/feed","5");
  10. *               Enter feed url as 1st argument and how many items from that feed as the 2nd
  11. *       4.  Display that object ie: echo '<ul class="myfeed">'.$obj.'</ul>';
  12. *               Didn't include <ul> in class so that you can style the individual classes
  13. *       5.  Repeat 3 & 4 with other feeds
  14. *
  15. *       Optional - You can limit the class to use on certain pages or posts using WP is_page(), is_post() etc
  16. *       Tips -     Get it working first before editing display_rss(),make small changes to track errors easier
  17. *
  18. **/
  19.  
  20. class wow_Rss
  21.     {
  22.         public $feed = null;
  23.         public $url = null;
  24.         public $maxitems = null;
  25.  
  26.         /**
  27.          *   __construct
  28.          *
  29.          *    pass values when class is istantiated
  30.          *
  31.          * @param string  $url      The URL You want to fetch
  32.          * @param numeric $maxitems How many items you want to fetch
  33.          */
  34.  
  35.  
  36.         public function __construct($url, $maxitems)
  37.             {
  38.                 $this->url      = $url;
  39.                 $this->maxitems = $maxitems;
  40.             }
  41.  
  42.         /**
  43.          *   get_first_image_url
  44.          *
  45.          *   Looks for first image in string
  46.          *
  47.          * @param  string   $html
  48.          * @return string
  49.          */
  50.  
  51.         public function get_first_image_url($html)
  52.             {
  53.                 if (preg_match('/<img.+?src="(.+?)"/', $html, $matches))
  54.                     {
  55.                         return $matches[1];
  56.                     }
  57.             }
  58.  
  59.         /**
  60.          *   shorten
  61.          *
  62.          *   Creates an excerpt of description
  63.          *
  64.          * @param  string $string  The feed
  65.          * @param  numeric $length  How many characters to limit to
  66.          * @return string   returns shortened text
  67.          */
  68.  
  69.         public function shorten($string, $length)
  70.             {
  71.                 $suffix     = '&hellip;';
  72.                 $short_desc = trim(str_replace(array(
  73.                         "/r",
  74.                         "/n",
  75.                         "/t"
  76.                 ), ' ', strip_tags($string)));
  77.                 $desc       = trim(substr($short_desc, 0, $length));
  78.                 $lastchar   = substr($desc, -1, 1);
  79.                 if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?')
  80.                         $suffix = '';
  81.                 $desc .= $suffix;
  82.                 return $desc;
  83.             }
  84.         /**
  85.          *   get_comments
  86.          *
  87.          *   Gets the number of comments
  88.          *
  89.          * @param  string $item  the feed
  90.          * @return string The correct number of comments
  91.          */
  92.  
  93.         public function get_comments($item)
  94.             {
  95.                 $comments = $item->get_item_tags('http://purl.org/rss/1.0/modules/slash/', 'comments');
  96.                 $number   = $comments[0]['data'];
  97.                 if ($number == '1')
  98.                     {
  99.                         return $number . '&nbsp;' . 'Comment';
  100.                     }
  101.                 else
  102.                     {
  103.                         return $number . '&nbsp;' . 'Comments';
  104.                     }
  105.             }
  106.         /**
  107.          *   __tostring
  108.          *
  109.          *   Converts obj to a string
  110.          *
  111.          * @return string displays rss feed
  112.          */
  113.  
  114.         public function __toString()
  115.             {
  116.                 return $this->display_rss();
  117.             }
  118.  
  119.         /**
  120.          *   display_rss
  121.          *
  122.          *   fetches feed , checks for errors, uses get_comments, shorten, get_first_img_url
  123.          *
  124.          */
  125.  
  126.         public function display_rss()
  127.             {
  128.                 $feed      = '';
  129.                 $rss_items = '';
  130.  
  131.                 $rss = fetch_feed($this->url);//uses built in simplepie to fetch feeds
  132.  
  133.                 if (!is_wp_error($rss)):
  134.                         $maxitems  = $rss->get_item_quantity($this->maxitems);
  135.                         $rss_items = $rss->get_items(0, $maxitems);
  136.                 endif;
  137.  
  138.                 if (!is_array($rss_items) || $maxitems == 0)
  139.                     {
  140.                         $feed .= '<li>No items.</li>';
  141.                     }
  142.                 else
  143.                 foreach ($rss_items as $item):
  144.                         $feed .= '<li class="item">';
  145.  
  146.                         $feed .= '<span class="rss-image">
  147.                                    <img src="' . $this->get_first_image_url($item->get_content()) . '"/>
  148.                                  </span>';
  149.  
  150.                         $feed .= '<span class"data">
  151.                                    <h5>
  152.                                        <a href="'
  153.                                         . esc_url($item->get_permalink()) . '" title="'
  154.                                         . esc_html($item->get_title()) . '">'
  155.                                         . esc_html($item->get_title()) .
  156.                                         '</a>
  157.  
  158.                                    </h5>';
  159.                         $feed .= '<span class="date-image">&nbsp;</span>';
  160.                         $feed .= '<small>' . $item->get_date('F Y') . ' </small>';
  161.                         $feed .= '<span class="comment-image">&nbsp;</span>';
  162.                         $feed .= '<small>' . $this->get_comments($item) . '</small>';
  163.                         $feed .= '<p>' . $this->shorten($item->get_description(), '150') . '</p>
  164.  
  165.                                </span>
  166.                                </li>';
  167.                 endforeach;
  168.                 return $feed;
  169.             }
  170.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement