# super-rss-reader/3.1/includes/widget.php

Super RSS Reader – Add attractive RSS Feed Widget, version 3.1. 285 lines.

- Page: https://pluginprobe.com/plugins/super-rss-reader/3.1/code/includes/widget.php
- Raw: https://pluginprobe.com/plugins/super-rss-reader/3.1/raw/includes/widget.php
- Modified: 2020-04-16T15:40:44+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/super-rss-reader/3.1/code/includes/widget.php#L10-L20`.

```php
<?php
/**
    * Contains core logic for the widget
    *
*/

if( ! defined( 'ABSPATH' ) ) exit;

class SRR_Widget{
    
    public static function init(){

        add_action( 'widgets_init', array( __class__, 'init_widget' ) );

        add_action( 'wp_enqueue_scripts', array( __class__, 'public_scripts' ) );

        add_action( 'admin_enqueue_scripts', array( __class__, 'admin_scripts' ) );

    }

    public static function init_widget(){

        register_widget( 'super_rss_reader_widget' );

    }

    public static function public_scripts(){

        wp_enqueue_script( 'jquery-easy-ticker-js', SRR_URL . 'public/js/jquery.easy-ticker.min.js', array( 'jquery', 'super-rss-reader-js' ), SRR_VERSION );
        wp_enqueue_script( 'super-rss-reader-js', SRR_URL . 'public/js/script.min.js', array( 'jquery' ), SRR_VERSION );
        wp_enqueue_style( 'super-rss-reader-css', SRR_URL . 'public/css/style.min.css', array(), SRR_VERSION );

    }

    public static function admin_scripts( $hook ){

        if( $hook == 'widgets.php' ){
            wp_enqueue_style( 'srr_admin_css', SRR_URL . 'admin/css/style.css' );
            wp_enqueue_script( 'srr_admin_js', SRR_URL . 'admin/js/script.js' );
        }

    }

    public static function default_settings(){
        return array(
            'title' => '',
            'urls' => '',
            'tab_titles' => '',
            'count' => 5,
            'show_date' => 0,
            'show_desc' => 1,
            'show_author' => 0,
            'show_thumb' => 1,
            'open_newtab' => 1,
            'strip_desc' => 100,
            'add_nofollow' => 1,
            'read_more' => '[...]',
            'rich_desc' => 0 ,
            'color_style' => 'none',
            'enable_ticker' => 1,
            'visible_items' => 5,
            'strip_title' => 0,
            'ticker_speed' => 4,
        );
    }

    public static function color_themes(){
        return array(
            'No style' => 'none',
            'Grey' => 'grey',
            'Dark' => 'dark',
            'Orange' => 'orange',
            'Simple modern' => 'smodern'
        );
    }

    public static function render_feed( $settings ){

        $settings = wp_parse_args( $settings, self::default_settings() );

        $urls = stripslashes( $settings['urls'] );
        $tab_titles = stripslashes( $settings['tab_titles'] );
        $count = intval( $settings['count'] );

        $show_date = intval( $settings['show_date'] );
        $show_desc = intval( $settings['show_desc'] );
        $show_author = intval( $settings['show_author'] );
        $show_thumb = stripslashes( $settings['show_thumb'] );
        $open_newtab = intval( $settings['open_newtab'] );
        $add_nofollow = intval( $settings['add_nofollow'] );
        $strip_desc = intval( $settings['strip_desc'] );
        $strip_title = intval( $settings['strip_title'] );
        $read_more = htmlspecialchars( $settings['read_more'] );
        $rich_desc = intval( $settings['rich_desc'] );
        
        $color_theme = stripslashes( $settings['color_style'] );
        $enable_ticker = intval( $settings['enable_ticker'] );
        $visible_items = intval( $settings['visible_items'] );
        $ticker_speed = intval( $settings['ticker_speed'] ) * 1000;

        if( empty( $urls ) ){
            return '';
        }

        $url_delim = strpos( $urls, ',' ) !== false ? ',' : "\n";
        $tab_title_delim = strpos( $tab_titles, ',' ) !== false ? ',' : "\n";

        $rand = array();
        $url = explode( $url_delim, $urls );
        $tab_title = explode( $tab_title_delim, $tab_titles );
        $url_count = count( $url );

        // Generating the tabs
        if( $url_count > 1 ){
            echo '<ul class="srr-tab-wrap srr-tab-style-' . $color_theme . ' srr-clearfix">';
            for( $i=0; $i < $url_count; $i++ ){

                // Get the Feed URL
                $feed_url = trim( $url[$i] );
                $rss = fetch_feed( $feed_url );
                $rand[$i] = rand(100, 999);

                if( !is_wp_error( $rss ) ){
                    
                    if( isset( $tab_title[$i] ) && !empty( $tab_title[$i] ) ){
                        $rss_title = $tab_title[$i];
                    }else{
                        $rss_title = esc_attr( strip_tags( $rss->get_title() ) );
                    }

                    echo '<li data-tab="srr-tab-' . $rand[$i] . '">' . $rss_title . '</li>';
                }else{
                    echo '<li data-tab="srr-tab-' . $rand[$i] . '">Error</li>';
                }

            }
            echo '</ul>';
        }
        
        // Generating the feed items
        for( $i=0; $i < $url_count; $i++ ){

            // Get the Feed URL
            $feed_url = trim( $url[$i] );
            if( isset( $url[$i] ) ){
                $rss = fetch_feed( $feed_url );
            }else{
                return '';
            }

            if( method_exists( $rss, 'enable_order_by_date' ) ){
                $rss->enable_order_by_date( false );
            }

            // Check for feed errors
            if (!is_wp_error( $rss ) ){
                $max_items = $rss->get_item_quantity( $count );
                $rss_items = $rss->get_items( 0, $max_items ); 
                $rss_title = esc_attr( strip_tags( $rss->get_title() ) );
                $rss_desc = esc_attr( strip_tags( $rss->get_description() ) );
            }else{
                echo '<div class="srr-wrap srr-style-' . $color_theme .'" data-id="srr-tab-' . $rand[$i] . '"><p>RSS Error: ' . $rss->get_error_message() . '</p></div>';
                continue;
            }

            $randAttr = isset($rand[$i]) ? ' data-id="srr-tab-' . $rand[$i] . '" ' : '';

            // Outer Wrap start
            echo '<div class="srr-wrap ' . (($enable_ticker == 1 ) ? 'srr-vticker' : '' ) . ' srr-style-' . $color_theme . '" data-visible="' . $visible_items . '" data-speed="' . $ticker_speed . '"' . $randAttr . '><div>';

            // Check feed items
            if ($max_items == 0){
                echo '<div>No items.</div>';
            }else{
                $j=1;
                // Loop through each feed item
                foreach ($rss_items as $item){

                    // Getting link
                    $link = $item->get_link();
                    while ( stristr( $link, 'http' ) != $link ){ $link = substr( $link, 1 ); }
                    $link = esc_url(strip_tags($link));
                    
                    // Getting title
                    $title = esc_attr(strip_tags($item->get_title()));
                    if ( empty($title) )
                        $title = __('No Title');
                    
                    if( $strip_title != 0 ){
                        $titleLen = strlen($title);
                        $title = wp_html_excerpt( $title, $strip_title );
                        $title = ($titleLen > $strip_title) ? $title . ' ...' : $title;
                    }
                    
                    // Open links in new tab
                    $new_tab = ($open_newtab) ? ' target="_blank"' : '';
                    
                    // Add no follow attribute
                    $no_follow = ($add_nofollow) ? ' rel="nofollow"' : '';
                    
                    // Getting date
                    $date = $item->get_date('j F Y');
                    
                    // Getting thumbnail if present @since v2.2
                    $thumb = '';
                    if ($show_thumb == 1 && $enclosure = $item->get_enclosure()){
                        $thumb_url = $enclosure->get_thumbnail();
                        if(!empty($thumb_url))
                        $thumb = '<img src="' . $thumb_url . '" alt="' . $title . '" class="srr-thumb" align="left"/>';
                    }
                    
                    // Getting description
                    $desc = '';
                    if( $rich_desc == 1 ){
                        $desc = strip_tags( str_replace( 'eval', '', $item->get_description() ) , '<p><a><img><em><strong><font><strike><s><u><i>');
    
                    }else{
                        $read_more = '';
                        $desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
                        if($strip_desc != 0){
                            $desc = wp_html_excerpt( $desc, $strip_desc );
                            $read_more = ( !empty( $read_more ) ) ? '<a href="' . $link . '" title="Read more"' . $new_tab . $no_follow . '>' . $read_more . '</a>' : '';
                            
                            if ( '[...]' == substr( $desc, -5 ) )
                                $desc = substr( $desc, 0, -5 );
                            elseif ( '[&hellip;]' != substr( $desc, -10 ) )
                                $desc .= '';
                            
                            $desc = esc_html( $desc );
                        }
                        
                        $desc = $thumb . $desc . ' ' . $read_more;
                        
                    }
                    
                    // Getting the author
                    $author = $item->get_author();
                    if ( is_object($author) ) {
                        $author = $author->get_name();
                        $author = esc_html(strip_tags($author));
                    }

                    echo "\n\t";

                    // Display the feed items
                    echo '<div class="srr-item ' . (($j%2 == 0) ? 'even' : 'odd') . '">';
                    echo '<div class="srr-title"><a href="' . $link . '"' . $new_tab . $no_follow . ' title="Posted on ' . $date . '">' . $title . '</a></div>';
                    echo '<div class="srr-meta">';

                    if($show_date && !empty($date))
                        echo '<time class="srr-date">' . $date . '</time>';

                    if($show_author && !empty($author))
                        echo ' - <cite class="srr-author">' . $author . '</cite>';

                    echo '</div>';

                    if($show_desc)
                        echo '<p class="srr-summary srr-clearfix">' . $desc . '</p>';

                    echo '</div>';
                    // End display

                    $j++;
                }
            }
            
            // Outer wrap end
            echo "\n\n</div>
            </div>\n\n" ;
            
            if ( !is_wp_error($rss) )
                $rss->__destruct();

            unset($rss);

        }

    }

}

SRR_Widget::init();

?>
```
