-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-rss-feeds.php
349 lines (239 loc) · 8.47 KB
/
wp-rss-feeds.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
// Register WP RSS Feeds Stylesheet Last (PHP_INT_MAX)
add_action('wp_enqueue_scripts', 'wp_rss_feeds_style', PHP_INT_MAX);
// Register WP RSS Feeds
add_shortcode( "wp-rss-feeds", "wp_rss_feeds_function" );
function wp_rss_feeds_function( $atts ) {
$atts = shortcode_atts(array(
'feeds' => '',
'entrylimit' => 30,
'charlimit' => 0,
'fullcat' => 0,
'order' => 0,
'target' => '_blank',
'timeout' => 4,
'timezone' => 'server',
'dateformat' => 'D, dS F Y g:i:s A',
'dofutureposts' => 0,
'fallback' => 0,
'tmp' => get_temp_dir() . 'rss/'
), $atts);
extract( $atts );
// Cast integer attr (is this necessary?)
$entrylimit = (int) $entrylimit;
$charlimit = (int) $charlimit;
$fullcat = (int) $fullcat;
$order = (int) $order;
$target = ( in_array( $target, array('_blank','_self','_parent','_top') ) ) ? $target : '_blank';
$timeout = (int) $timeout;
$dofutureposts = (int) $dofutureposts;
$fallback = (int) $fallback;
$template_dir = get_template_directory();
$time = time();
$feedfiles = array();
$feedsarray = array();
$fallback = false;
// Any feeds available?
if ( $feeds == '' )
return 'No RSS feed(s) supplied.';
// Feeds list as array
$feeds = explode( ',', $feeds );
// Setup timezone
if ( $timezone == 'server' ) {
$timezone = new DateTimeZone( date_default_timezone_get() );
} else {
if ( ! ( isValidTimezone( $timezone ) ) ) {
$timezone = new DateTimeZone( date_default_timezone_get() );
} else {
$timezone = new DateTimeZone($timezone);
}
}
// Cache directory available?
if ( ! ( is_dir ( $tmp ) ) )
if ( ! ( @mkdir( $tmp ) ) )
$fallback = true;
// Setup SimpleXML Object Files
foreach ( $feeds as $f ) {
// Cache file
$cache = $tmp . 'rss-' . md5( $f ) . '.dat';
// Multiply hours by milliseconds
$ctm = ( (int) $ct * 3600 );
// Load live or from cache
if ( $ctm > 0 && is_file( $cache ) && ( filemtime($cache) + $ctm > time() ) && ! ( $fallback ) ) {
$rss = @simplexml_load_file( $cache );
} else {
$xml = @file_get_contents( $f );
if ( $xml !== false ) {
$rss = @simplexml_load_string( $xml );
if ( $ctm > 0 && ! ( $fallback ) && $rss !== false )
$rss->asXML( $cache );
}
}
if ( $rss !== false )
array_push($feedfiles, $rss);
}
foreach ( $feedfiles as $v ) {
// Channel Title
$title = sanitize_text_field( (string) $v->channel->title );
// Iterate through items
if ( isset( $v->channel->item ) ) {
foreach ( $v->channel->item as $item ) {
$description = wp_kses( $item->description, array(
'a' => array(
'href' => array(),
'title' => array()
),
'img' => array(
'src' => array(),
'alt' => array()
),
'b' => array(),
'strong' => array(),
'i' => array(),
'u' => array(),
'em' => array(),
'br' => array()
) );
if ( ! ( empty( $description ) ) ) {
// Extract first image
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $description, $image);
$src = $image['src'];
// Remove Image Links
$desc = preg_replace( '/(<a.*?<img.*?>.*?<\/a>)/', '', $description );
// Add link targets
$desc = str_replace( '<a ', '<a target="' . $target . '" ', $desc );
}
$dateTime = strtotime( $item->pubDate );
// Extract creator or use channel title
$creator = ( isset( $item->children('dc', true)->creator ) ) ? (string) $item->children('dc', true)->creator : $title;
// Define Categories
$category = null;
if ( ! ( $fullcat ) ) {
$category = (string) $item->category[ count( $item->category )-1 ];
} else {
if ( count( $item->category ) > 0 ) {
$i = 0;
foreach ( $item->category as $cat ) {
$suffix = '';
if ( $i < count( $item->category )-1 )
$suffix = ' > ';
// No runaway duplicate categories
if ( strpos( $category, (string) $cat ) == false )
$category .= sanitize_text_field( (string) $cat ) . $suffix;
$i++;
}
} else {
$category = $title;
}
}
// Define Item Array
$item = array(
'site' => $title,
'title' => sanitize_text_field( (string) $item->title ),
'desc' => $desc,
'link' => esc_url_raw( (string) $item->link ),
'image' => esc_url_raw( $src ),
'date' => $dateTime,
'creator' => sanitize_text_field( $creator ),
'category' => $category
);
// Store or discard future posts
if ( ! ( $dofutureposts ) ) {
if ( $dateTime <= $time )
array_push( $feedsarray, $item );
} else {
array_push( $feedsarray, $item );
}
}
}
}
// Check for feed entries
if ( empty( $feedsarray ) )
return 'No entries available from feeds(s).';
// Sort Feed Items by Date
if ( $order ) {
usort($feedsarray, function ( $a, $b): int {
return $a['date'] <=> $b['date'];
});
} else {
usort($feedsarray, function ( $a, $b): int {
return $b['date'] <=> $a['date'];
});
}
$c = 0;
// Render output
ob_start();
// Include entries header
include( $template_dir . '/template-parts/wp-rss-feeds/wp-rss-feeds-header.php' );
// Iterate through entries
foreach ( $feedsarray as $feed ) {
// Limit entries to entrylimit attr or default
if ( $c == $entrylimit )
break;
// Format date
$date = new DateTime();
$date->setTimestamp( $feed['date'] );
$date->setTimezone( $timezone );
// Limit entry description if charlimit attr set
$desc = ( $charlimit > 0 && strlen( $feed['desc'] ) > $charlimit ) ? str_limit_html( $feed['desc'], $charlimit ) .
' <a class="wp-rss-feed-entry-desc-readmore" href="'. $feed['link'] . '" target="' . $target . '" title="'. $feed['title'] . '">Read more</a>' : $feed['desc'];
// Include entry template
include( $template_dir . '/template-parts/wp-rss-feeds/wp-rss-feeds-entry.php' );
$c++;
}
// Include entries footer
include( $template_dir . '/template-parts/wp-rss-feeds/wp-rss-feeds-footer.php' );
// Get rendered output
$result = ob_get_contents();
ob_end_clean();
// Return rendered output
return $result;
}
function wp_rss_feeds_style() {
$style = get_template_directory_uri() . '/wp-rss-feeds.css';
wp_enqueue_style('wp-rss-feeds-stylesheet', $style, array(), rand(100,9999), 'all');
}
// Check if valid timezone ID
function isValidTimezone( $id ) {
if ( empty( $id ) )
return false;
foreach ( timezone_abbreviations_list() as $z ) {
foreach ( $z as $item ) {
if ( $item["timezone_id"] == $id )
return true;
}
}
return false;
}
/**
* Limit string without break html tags.
* Supports UTF8
*
* @param string $value
* @param int $limit Default 100
*
* Created by SnakeDrak
*/
function str_limit_html($value, $limit = 100)
{
if (mb_strwidth($value, 'UTF-8') <= $limit) {
return $value;
}
// Strip text with HTML tags, sum html len tags too.
// Is there another way to do it?
do {
$len = mb_strwidth($value, 'UTF-8');
$len_stripped = mb_strwidth(strip_tags($value), 'UTF-8');
$len_tags = $len - $len_stripped;
$value = mb_strimwidth($value, 0, $limit + $len_tags, '', 'UTF-8');
} while ($len_stripped > $limit);
// Load as HTML ignoring errors
$dom = new DOMDocument();
@$dom->loadHTML('<?xml encoding="utf-8" ?>'.$value, LIBXML_HTML_NODEFDTD);
// Fix the html errors
$value = $dom->saveHtml($dom->getElementsByTagName('body')->item(0));
// Remove body tag
$value = mb_strimwidth($value, 6, mb_strwidth($value, 'UTF-8') - 13, '', 'UTF-8'); // <body> and </body>
// Remove empty tags
return preg_replace('/<(\w+)\b(?:\s+[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|"[^"]*"|[\w\-.:]+))?)*\s*\/?>\s*<\/\1\s*>/', '', $value);
}