PluginProbe
Super RSS Reader – Add attractive RSS Feed Widget / 4.1
Super RSS Reader – Add attractive RSS Feed Widget v4.1
trunk 0.8 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 3.0 3.1 3.2 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.7 4.8 All 33 releases
super-rss-reader / includes / feed.php

feed.php in Super RSS Reader – Add attractive RSS Feed Widget 4.1, at includes/feed.php

317 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $date_format = htmlspecialchars( $this->options['date_format'] );
34 $read_more = htmlspecialchars( $this->options['read_more'] );
35 $rich_desc = intval( $this->options['rich_desc'] );
36 $thumbnail_position = htmlspecialchars( $this->options['thumbnail_position'] );
37 $thumbnail_size = htmlspecialchars( $this->options['thumbnail_size'] );
38 $thumbnail_default = htmlspecialchars( $this->options['thumbnail_default'] );
39
40 $color_theme = stripslashes( $this->options['color_style'] );
41 $display_type = stripslashes( $this->options['display_type'] );
42 $visible_items = intval( $this->options['visible_items'] );
43 $ticker_speed = intval( $this->options['ticker_speed'] ) * 1000;
44
45 if( empty( $urls ) ){
46 return '';
47 }
48
49 $url_delim = strpos( $urls, ',' ) !== false ? ',' : "\n";
50 $tab_title_delim = strpos( $tab_titles, ',' ) !== false ? ',' : "\n";
51
52 $urls = explode( $url_delim, $urls );
53 $tab_titles = explode( $tab_title_delim, $tab_titles );
54 $url_count = count( $urls );
55
56 $feeds = array();
57 $html = '';
58
59 $classes = array( 'srr-wrap', 'srr-style-' . $color_theme );
60 if( $display_type == 'vertical_ticker' ) array_push( $classes, 'srr-vticker' );
61 $class = implode( ' ', $classes );
62
63 // Fetch the feed
64 for( $i=0; $i < $url_count; $i++ ){
65 $feed_url = trim( $urls[$i] );
66 $feed = fetch_feed( $feed_url );
67
68 if( is_wp_error( $feed ) ){
69 $feed_title = 'Error';
70 }else{
71 $feed_title = ( isset( $tab_titles[$i] ) && !empty( $tab_titles[$i] ) ) ? $tab_titles[$i] : esc_attr( strip_tags( $feed->get_title() ) );
72 }
73
74 $feeds[ $feed_url ] = array(
75 'id' => rand( 100, 999 ),
76 'feed' => $feed,
77 'title' => $feed_title
78 );
79 }
80
81 // Generate tabs
82 if( $url_count > 1 ){
83 $html .= '<ul class="srr-tab-wrap srr-tab-style-' . $color_theme . ' srr-clearfix">';
84 foreach( $feeds as $url => $data ){
85 $id = $data[ 'id' ];
86 $feed = $data[ 'feed' ];
87 if( is_wp_error( $feed ) ){
88 $html .= '<li data-tab="srr-tab-' . $id . '">Error</li>';
89 }else{
90 $html .= '<li data-tab="srr-tab-' . $id . '">' . $data[ 'title' ] . '</li>';
91 }
92 }
93 $html .= '</ul>';
94 }
95
96 // Generate feed items
97 foreach( $feeds as $url => $data ){
98
99 $id = $data[ 'id' ];
100 $feed = $data[ 'feed' ];
101
102 // Check for feed errors
103 if ( is_wp_error( $feed ) ){
104 $html .= '<div class="srr-wrap srr-style-' . $color_theme .'" data-id="srr-tab-' . $id . '"><p>RSS Error: ' . $feed->get_error_message() . '</p></div>';
105 continue;
106 }
107
108 if( method_exists( $feed, 'enable_order_by_date' ) ){
109 $feed->enable_order_by_date( false );
110 }
111
112 $max_items = $feed->get_item_quantity( $count );
113 $feed_items = $feed->get_items( 0, $max_items );
114
115 // Outer wrap start
116 $html .= '<div class="' . $class . '" data-visible="' . $visible_items . '" data-speed="' . $ticker_speed . '" data-id="srr-tab-' . $id . '">';
117 $html .= '<div>';
118
119 // Check feed items
120 if ( $max_items == 0 ){
121 $html .= '<div>' . __( 'No items', 'super-rss-reader' ) . '</div>';
122 }else{
123 $j=1;
124 // Loop through each feed item
125 foreach( $feed_items as $item ){
126
127 // Link
128 $link = $item->get_link();
129 while ( stristr( $link, 'http' ) != $link ){ $link = substr( $link, 1 ); }
130 $link = esc_url( strip_tags($link) );
131
132 // Title
133 $title = esc_attr( strip_tags( $item->get_title() ) );
134 $title_full = $title;
135
136 if ( empty( $title ) ){
137 $title = __( 'No Title', 'super-rss-reader' );
138 }
139
140 if( $strip_title > 0 && strlen( $title ) > $strip_title ){
141 $title = wp_trim_words( $title, $strip_title );
142 }
143
144 // Open links in new tab
145 $new_tab = $open_newtab ? ' target="_blank"' : '';
146
147 // Add no follow attribute
148 $no_follow = $add_nofollow ? ' rel="nofollow noopener noreferrer"' : '';
149
150 // Date
151 $date = date_i18n( $date_format, $item->get_date( 'U' ) );
152 $date_full = esc_attr( $item->get_date() );
153
154 // Thumbnail
155 $thumb = '';
156 if ( $show_thumb == 1 ){
157 $thumb_url = $this->get_thumbnail_url( $item, $thumbnail_default );
158 if( !empty( $thumb_url ) ){
159 $thumb_styles = array(
160 'width' => $thumbnail_size,
161 'height' => $thumbnail_size
162 );
163 $thumb_style = '';
164 foreach( $thumb_styles as $prop => $val ){
165 $thumb_style .= "$prop:$val;";
166 }
167 $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>';
168 }
169 }
170
171 // Description
172 $desc = '';
173 if( $show_desc ){
174 if( $rich_desc ){
175 $desc = strip_tags( $item->get_description(), '<p><a><img><em><strong><font><strike><s><u><i>' );
176 }else{
177
178 $desc = str_replace( array( "\n", "\r" ), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
179 $read_more_link = '';
180
181 if( $strip_desc != 0 ){
182 $desc = wp_trim_words( $desc, $strip_desc );
183 $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>' : '';
184
185 if ( '[...]' == substr( $desc, -5 ) ){
186 $desc = substr( $desc, 0, -5 );
187 }elseif ( '[&hellip;]' != substr( $desc, -10 ) ){
188 $desc .= '';
189 }
190
191 $desc = esc_html( $desc );
192 }
193
194 $desc = $desc . $read_more_link;
195
196 }
197 }
198
199 // Author
200 $author = $item->get_author();
201 if ( is_object( $author ) ) {
202 $author = $author->get_name();
203 $author = esc_html( strip_tags( $author ) );
204 }
205
206 $t_title = '';
207 $t_meta = '';
208 $t_thumb = '';
209 $t_desc = '';
210
211 $t_title .= '<div class="srr-title"><a href="' . $link . '"' . $new_tab . $no_follow . ' title="' . $title_full . '">' . $title . '</a></div>';
212
213 // Metadata
214 if( $show_date || $show_author ){
215 $t_meta .= '<div class="srr-meta">';
216 if( $show_date && !empty( $date ) ){
217 $t_meta .= '<time class="srr-date" title="' . $date_full . ' UTC">' . $date . '</time>';
218 }
219
220 if( $show_author && !empty( $author ) ){
221 $t_meta .= ' - <cite class="srr-author">' . $author . '</cite>';
222 }
223 $t_meta .= '</div>'; // End meta
224 }
225
226 if ( $show_thumb ){
227 $t_thumb .= $thumb;
228 }
229
230 if( $show_desc ){
231 $t_desc .= '<div class="srr-summary srr-clearfix">';
232 $t_desc .= $rich_desc ? $desc : ( '<p>' . $desc . '</p>' );
233 $t_desc .= '</div>'; // End summary
234 }
235
236 $f_data = apply_filters( 'srr_mod_item_html', array(
237 'title' => $t_title,
238 'meta' => $t_meta,
239 'thumbnail' => $t_thumb,
240 'description' => $t_desc,
241 'before' => '',
242 'after' => ''
243 ), $url, $item );
244
245 $f_title = !isset( $f_data[ 'title' ] ) ? '' : $f_data[ 'title' ];
246 $f_meta = !isset( $f_data[ 'meta' ] ) ? '' : $f_data[ 'meta' ];
247 $f_thumb = !isset( $f_data[ 'thumbnail' ] ) ? '' : $f_data[ 'thumbnail' ];
248 $f_desc = !isset( $f_data[ 'description' ] ) ? '' : $f_data[ 'description' ];
249 $f_before = !isset( $f_data[ 'before' ] ) ? '' : $f_data[ 'before' ];
250 $f_after = !isset( $f_data[ 'after' ] ) ? '' : $f_data[ 'after' ];
251
252 // Display the feed items
253 $html .= '<div class="srr-item ' . ( ( $j%2 == 0 ) ? 'srr-stripe' : '') . '">';
254 $html .= '<div class="srr-clearfix">';
255 $html .= $f_before;
256 $html .= $f_title . $f_meta . $f_thumb . $f_desc;
257 $html .= $f_after;
258 $html .= '</div>'; // End item inner clearfix
259 $html .= '</div>'; // End feed item
260
261 $j++;
262 }
263 }
264
265 // Outer wrap end
266 $html .= '</div></div>' ;
267
268 if( !is_wp_error( $feed ) )
269 $feed->__destruct();
270
271 unset( $feed );
272
273 }
274
275 $html = '<div class="srr-main">' . $html . '</div>';
276
277 return $html;
278
279 }
280
281 function get_thumbnail_url( $item, $thumbnail_default ){
282
283 // Try to get from the item enclosure
284 $enclosure = $item->get_enclosure();
285
286 if ( $enclosure->get_thumbnail() ) {
287 return $enclosure->get_thumbnail();
288 }
289
290 if ( $enclosure->get_link() ) {
291 return $enclosure->get_link();
292 }
293
294 // Try to get from item content
295 $content = $item->get_content();
296
297 preg_match_all('~<img.*?src=["\']+(.*?)["\']+~', $content, $urls);
298 $urls = $urls[1];
299
300 if( !empty( $urls ) ){
301 return $urls[0];
302 }
303
304 // Try to get the image tag finally if available
305 $image = $item->get_item_tags( '', 'image' );
306
307 if( isset( $image[0]['data'] ) ){
308 return $image[0]['data'];
309 }
310
311 return trim( $thumbnail_default );
312
313 }
314
315 }
316
317 ?>