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

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