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

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