PluginProbe
Super RSS Reader – Add attractive RSS Feed Widget / 4.3
Super RSS Reader – Add attractive RSS Feed Widget v4.3
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.3, at includes/feed.php

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