-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrss.php
127 lines (107 loc) · 3.84 KB
/
rss.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
<?php
/*
Plugin Name: Ben's Custom RSS Plugin
Version: 1.2.1
Plugin URI: http://susantaslab.com/
Description: Makes it easy to display an RSS feed on a page. Derived from Susanta Beura's RSS plugin
Author: Susanta K Beura, Ben Perry
Author URI: http://susantaslab.com/
License: GPL v3
Usages: [rssonpage rss="Feed URL" feeds="Number of Items" excerpt="true/false" target="_blank|_self"]
*/
function fetchRSS($atts) {
extract(shortcode_atts(array(
"rss" => '',
"feeds" => '10',
"excerpt" => true,
"target" => '_blank'
),$atts));
if ($rss != "" && $rssFeed = get_rss_feed($rss)) {
$rssFeed->enable_order_by_date(false);
$maxitems = $rssFeed->get_item_quantity($feeds);
if ($maxitems == 0)
return '<ul><li>Content not available at'.$rss .'.</li></ul>';
$rss_items = $rssFeed->get_items(0,$maxitems);
$content = '';
foreach ($rss_items as $item) {
$url = explode('/',trim($item->Get_permalink(),'/'));
$handleGroup = $url[count($url)-2];
$handlePaper = $url[count($url)-1];
$title = preg_replace('!\s+!', ' ', $item->Get_title());
$authors = "";
$abstract = $item->get_description();
$lastAuthor = "";
foreach ($item->get_authors() as $a => $author){
if($author->get_name() != 1){
if($authors != ""){
$authors.= " and ";
}
$authors .=$author->get_name();
$lastAuthor = $author->get_name();
}
}
$iAuthors = strpos($abstract,$lastAuthor);
$abstract = substr($abstract,$iAuthors+strlen($lastAuthor)+1);
$content.="@techreport{handle:$handleGroup:$handlePaper,\n";
$content.="Title = {".$title."},\n";
$content.="Author = {".$authors."},\n";
$content.="institution = { NSF Census Research Network - NCRN-Cornell },\n";//TODO: Pull from feed
$content.="type = {Preprint} ,\n";
$content.="Year = {".$item->get_date('Y')."},\n";
$content.="number={".$handleGroup.':'.$handlePaper."},\n";
$content.="URL = {".trim($item->get_permalink())."},\n";
$content.="abstract ={".$abstract."}\n";
$content.="}\n";
}
}
return trim($content);
}
add_shortcode('rss-ncrn','fetchRSS');
add_action( 'wp', 'prefix_setup_schedule' );
function prefix_setup_schedule() {
if(!wp_next_scheduled( 'prefix_hourly_event' )){
wp_schedule_event( time(), 'hourly', 'prefix_hourly_event');
}
}
add_action( 'prefix_hourly_event', 'fetchRSS2' );
function fetchRSS2(){
$a = array('rss' => 'http://ecommons.cornell.edu/feed/atom_1.0/1813/30503', 'excerpt' => 'summary true', 'target' => '_blank');
$content = fetchRSS($a);
//TODO: Check if bibtex is valid in a better way
if(!empty($content)){
$file = fopen("wp-content/cache/ecommons.bib","w");
fwrite($file,$content);
fclose($file);
}
return;
}
add_shortcode('rss-ncrn-cache','fetchRSS2');
function checkRSSCache(){
if(!file_exists("wp-content/cache/ecommons.bib")){
fetchRSS2();
}
$a = array('rss' => 'http://ecommons.cornell.edu/feed/atom_1.0/1813/30503', 'excerpt' => 'summary true', 'target' => '_blank');
$content = fetchRSS($a);
return;
}
add_shortcode('rss-ncrn-cache-check','checkRSSCache');
if (!function_exists('get_rss_feed')){
function get_rss_feed($url) {
require_once(ABSPATH . WPINC . '/class-feed.php');
$feed = new SimplePie();
$feed->set_sanitize_class('WP_SimplePie_Sanitize_KSES');
$feed->sanitize = new WP_SimplePie_Sanitize_KSES();
$feed->set_useragent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/37.0.2062.120 Safari/537.36');
$feed->set_cache_class('WP_Feed_Cache');
$feed->set_file_class('WP_SimplePie_File');
$feed->set_feed_url($url);
$feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime',12 * HOUR_IN_SECONDS,$url));
do_action_ref_array('wp_feed_options',array(&$feed,$url));
$feed->init();
$feed->handle_content_type();
if ($feed->error())
return new WP_Error('simplepie-error',$feed->error());
return $feed;
}
}
?>