Advertisement
Guest User

Untitled

a guest
Nov 12th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.02 KB | None | 0 0
  1. <?php
  2. /**
  3. * Prepares the feed HTML
  4. *
  5. */
  6.  
  7. if( ! defined( 'ABSPATH' ) ) exit;
  8.  
  9. class SRR_Feed{
  10.  
  11.     public $options = array();
  12.  
  13.     public function __construct( $options ){
  14.  
  15.         $this->options = wp_parse_args( $options, SRR_Options::defaults() );
  16.  
  17.     }
  18.  
  19.     public function html(){
  20.  
  21.         $urls = stripslashes( trim( $this->options['urls'] ) );
  22.         $tab_titles = stripslashes( $this->options['tab_titles'] );
  23.         $count = intval( $this->options['count'] );
  24.  
  25.         $show_date = intval( $this->options['show_date'] );
  26.         $show_desc = intval( $this->options['show_desc'] );
  27.         $show_author = intval( $this->options['show_author'] );
  28.         $show_thumb = stripslashes( $this->options['show_thumb'] );
  29.         $open_newtab = intval( $this->options['open_newtab'] );
  30.         $add_nofollow = intval( $this->options['add_nofollow'] );
  31.         $strip_desc = intval( $this->options['strip_desc'] );
  32.         $strip_title = intval( $this->options['strip_title'] );
  33.         $read_more = htmlspecialchars( $this->options['read_more'] );
  34.         $rich_desc = intval( $this->options['rich_desc'] );
  35.         $thumbnail_position = htmlspecialchars( $this->options['thumbnail_position'] );
  36.         $thumbnail_size = htmlspecialchars( $this->options['thumbnail_size'] );
  37.  
  38.         $color_theme = stripslashes( $this->options['color_style'] );
  39.         $display_type = stripslashes( $this->options['display_type'] );
  40.         $visible_items = intval( $this->options['visible_items'] );
  41.         $ticker_speed = intval( $this->options['ticker_speed'] ) * 1000;
  42.  
  43.         if( empty( $urls ) ){
  44.             return '';
  45.         }
  46.  
  47.         $url_delim = strpos( $urls, ',' ) !== false ? ',' : "\n";
  48.         $tab_title_delim = strpos( $tab_titles, ',' ) !== false ? ',' : "\n";
  49.  
  50.         $urls = explode( $url_delim, $urls );
  51.         $tab_titles = explode( $tab_title_delim, $tab_titles );
  52.         $url_count = count( $urls );
  53.  
  54.         $feeds = array();
  55.         $html = '';
  56.  
  57.         $classes = array( 'srr-wrap', 'srr-style-' . $color_theme );
  58.         if( $display_type == 'vertical_ticker' ) array_push( $classes, 'srr-vticker' );
  59.         $class = implode( ' ', $classes );
  60.  
  61.         // Fetch the feed
  62.         for( $i=0; $i < $url_count; $i++ ){
  63.             $feed_url = trim( $urls[$i] );
  64.             $feed = fetch_feed( $feed_url );
  65.  
  66.             if( is_wp_error( $feed ) ){
  67.                 $feed_title = 'Error';
  68.             }else{
  69.                 $feed_title = ( isset( $tab_titles[$i] ) && !empty( $tab_titles[$i] ) ) ? $tab_titles[$i] : esc_attr( strip_tags( $feed->get_title() ) );
  70.             }
  71.  
  72.             $feeds[ $feed_url ] = array(
  73.                 'id' => rand( 100, 999 ),
  74.                 'feed' => $feed,
  75.                 'title' => $feed_title
  76.             );
  77.         }
  78.  
  79.         // Generate tabs
  80.         if( $url_count > 1 ){
  81.             $html .= '<ul class="srr-tab-wrap srr-tab-style-' . $color_theme . ' srr-clearfix">';
  82.             foreach( $feeds as $url => $data ){
  83.                 $id = $data[ 'id' ];
  84.                 $feed = $data[ 'feed' ];
  85.                 if( is_wp_error( $feed ) ){
  86.                     $html .= '<li data-tab="srr-tab-' . $id . '">Error</li>';
  87.                 }else{
  88.                     $html .= '<li data-tab="srr-tab-' . $id . '">' . $data[ 'title' ] . '</li>';
  89.                 }
  90.             }
  91.             $html .= '</ul>';
  92.         }
  93.  
  94.         // Generate feed items
  95.         foreach( $feeds as $url => $data ){
  96.  
  97.             $id = $data[ 'id' ];
  98.             $feed = $data[ 'feed' ];
  99.  
  100.             // Check for feed errors
  101.             if ( is_wp_error( $feed ) ){
  102.                 $html .= '<div class="srr-wrap srr-style-' . $color_theme .'" data-id="srr-tab-' . $id . '"><p>RSS Error: ' . $feed->get_error_message() . '</p></div>';
  103.                 continue;
  104.             }
  105.  
  106.             if( method_exists( $feed, 'enable_order_by_date' ) ){
  107.                 $feed->enable_order_by_date( false );
  108.             }
  109.  
  110.             $max_items = $feed->get_item_quantity( $count );
  111.             $feed_items = $feed->get_items( 0, $max_items );
  112.  
  113.             // Outer wrap start
  114.             $html .= '<div class="' . $class . '" data-visible="' . $visible_items . '" data-speed="' . $ticker_speed . '" data-id="srr-tab-' . $id . '">';
  115.             $html .= '<div>';
  116.  
  117.             // Check feed items
  118.             if ( $max_items == 0 ){
  119.                 $html .= '<div>' . __( 'No items', 'super-rss-reader' ) . '</div>';
  120.             }else{
  121.                 $j=1;
  122.                 // Loop through each feed item
  123.                 foreach( $feed_items as $item ){
  124.  
  125.                     // Link
  126.                     $link = $item->get_link();
  127.                     while ( stristr( $link, 'http' ) != $link ){ $link = substr( $link, 1 ); }
  128.                     $link = esc_url( strip_tags($link) );
  129.  
  130.                     // Title
  131.                     $title = esc_attr( strip_tags( $item->get_title() ) );
  132.                     $title_full = $title;
  133.  
  134.                     if ( empty( $title ) ){
  135.                         $title = __( 'No Title', 'super-rss-reader' );
  136.                     }
  137.  
  138.                     if( $strip_title > 0 && strlen( $title ) > $strip_title ){
  139.                         $title = wp_trim_words( $title, $strip_title );
  140.                     }
  141.  
  142.                     // Open links in new tab
  143.                     $new_tab = $open_newtab ? ' target="_blank"' : '';
  144.  
  145.                     // Add no follow attribute
  146.                     $no_follow = $add_nofollow ? ' rel="nofollow noopener noreferrer"' : '';
  147.  
  148.                     // Date
  149.                     $date = $item->get_date( 'j F Y' );
  150.                     $date_full = esc_attr( $item->get_date() );
  151.  
  152.                     // Thumbnail
  153.                     $thumb = '';
  154.                     if ( $show_thumb == 1 ){
  155.                         $thumb_url = $this->get_thumbnail_url( $item );
  156.                         if( !empty( $thumb_url ) ){
  157.                             $thumb_styles = array(
  158.                                 'width' => $thumbnail_size,
  159.                                 'height' => $thumbnail_size
  160.                             );
  161.                             $thumb_style = '';
  162.                             foreach( $thumb_styles as $prop => $val ){
  163.                                 $thumb_style .= "$prop:$val;";
  164.                             }
  165.                             $thumb = '<a href="' . $link . '" class="srr-thumb srr-thumb-' . $thumbnail_position . '" style="' . $thumb_style . '" ' . $new_tab . $no_follow . '><img src="' . $thumb_url . '" alt="' . $title_full . '" align="left" /></a>';
  166.                         }
  167.                     }
  168.  
  169.                     // Description
  170.                     $desc = '';
  171.                     if( $show_desc ){
  172.                         if( $rich_desc ){
  173.                             $desc = strip_tags( $item->get_description(), '<p><a><img><em><strong><font><strike><s><u><i>' );
  174.                         }else{
  175.  
  176.                             $desc = str_replace( array( "\n", "\r" ), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
  177.                             $read_more_link = '';
  178.  
  179.                             if( $strip_desc != 0 ){
  180.                                 $desc = wp_trim_words( $desc, $strip_desc );
  181.                                 $read_more_link = !empty( $read_more ) ? ' <a href="' . $link . '" title="' . __( 'Read more', 'super-rss-reader' ) . '"' . $new_tab . $no_follow . ' class="srr-read-more">' . $read_more . '</a>' : '';
  182.  
  183.                                 if ( '[...]' == substr( $desc, -5 ) ){
  184.                                     $desc = substr( $desc, 0, -5 );
  185.                                 }elseif ( '[&hellip;]' != substr( $desc, -10 ) ){
  186.                                     $desc .= '';
  187.                                 }
  188.  
  189.                                 $desc = esc_html( $desc );
  190.                             }
  191.  
  192.                             $desc = $desc . $read_more_link;
  193.  
  194.                         }
  195.                     }
  196.  
  197.                     // Author
  198.                     $author = $item->get_author();
  199.                     if ( is_object( $author ) ) {
  200.                         $author = $author->get_name();
  201.                         $author = esc_html( strip_tags( $author ) );
  202.                     }
  203.  
  204.                     // Display the feed items
  205.                     $html .= '<div class="srr-item ' . ( ( $j%2 == 0 ) ? 'srr-stripe' : '') . '">';
  206.                     $html .= '<div class="srr-clearfix">';
  207.  
  208.                     $html .= '<div class="srr-title"><a href="' . $link . '"' . $new_tab . $no_follow . ' title="' . $title_full . '">' . $title . '</a></div>';
  209.  
  210.                     // Metadata
  211.                     if( $show_date || $show_author ){
  212.                         $html .= '<div class="srr-meta">';
  213.                         if( $show_date && !empty( $date ) ){
  214.                             $html .= '<time class="srr-date" title="' . $date_full . '">' . $date . '</time>';
  215.                         }
  216.  
  217.                         if( $show_author && !empty( $author ) ){
  218.                             $html .= ' - <cite class="srr-author">' . $author . '</cite>';
  219.                         }
  220.                         $html .= '</div>'; // End meta
  221.                     }
  222.  
  223.                     if ( $show_thumb ){
  224.                         $html .= $thumb;
  225.                     }
  226.  
  227.                     if( $show_desc ){
  228.                         $html .= '<div class="srr-summary srr-clearfix">';
  229.                         $html .= $rich_desc ? $desc : ( '<p>' . $desc . '</p>' );
  230.                         $html .= '</div>'; // End summary
  231.                     }
  232.  
  233.                     $html .= '</div>'; // End item inner clearfix
  234.                     $html .= '</div>'; // End feed item
  235.  
  236.                     $j++;
  237.                 }
  238.             }
  239.            
  240.             // Outer wrap end
  241.             $html .= '</div></div>' ;
  242.            
  243.             if( !is_wp_error( $feed ) )
  244.                 $feed->__destruct();
  245.  
  246.             unset( $feed );
  247.  
  248.         }
  249.  
  250.         $html = '<div class="srr-main">' . $html . '</div>';
  251.  
  252.         return $html;
  253.  
  254.     }
  255.  
  256.     function get_thumbnail_url( $item ){
  257.  
  258.         // Try to get from the item enclosure
  259.         $enclosure = $item->get_enclosure();
  260.  
  261.         if ( $enclosure->get_thumbnail() ) {
  262.             return $enclosure->get_thumbnail();
  263.         }
  264.  
  265.         if ( $enclosure->get_link() ) {
  266.             return $enclosure->get_link();
  267.         }
  268.  
  269.         // Try to get from item content
  270.         $content = $item->get_content();
  271.  
  272.         preg_match_all('~<img.*?src=["\']+(.*?)["\']+~', $content, $urls);
  273.         $urls = $urls[1];
  274.  
  275.         if( !empty( $urls ) && isset( $urls[1] ) ){
  276.             return $urls[1];
  277.         }
  278.  
  279.         // Try to get the image tag finally if available
  280.         $image = $item->get_item_tags( '', 'image' );
  281.  
  282.         if( isset( $image[0]['data'] ) ){
  283.             return $image[0]['data'];
  284.         }
  285.  
  286.         return '';
  287.  
  288.     }
  289.  
  290. }
  291.  
  292. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement