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

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