source: trunk/www.guidonia.net/wp/wp-content/plugins/rss-aggregator.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 7.4 KB
Line 
1<?php /*
2Plugin Name: RSS Aggregator
3Plugin URI: http://www.anthonyshapley.co.uk/tools/wordpress-rss-aggregator/
4Description: Outputs RSS Feeds into Posts
5Version: 0.0.2
6Author: Anthony Shapley
7Author URI: http://www.anthonyshapley.co.uk
8
9Copyright (C) 2008, Anthony Shapley
10All rights reserved.
11
12Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
13Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
14Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
15Neither the name of Anthony Shapley nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
17
18// Options
19
20$options = array();
21$options['feeds'] = "http://feeds.feedburner.com/AnthonyShapleyAUkSeo";
22$options['categories'] = "Aggregated Content";
23$options['linkback'] = true;
24$options['nofollow'] = true;
25$options['seconds'] = true;
26add_option("shapley_rss_aggregator",$option);
27
28if ( ! class_exists( 'shapley_aggregator' ) ) {
29
30 class shapley_aggregator
31 {
32 function add_config_page()
33 {
34 global $wpdb;
35 if ( function_exists('add_submenu_page') )
36 {
37 add_options_page('Shapley RSS Aggregator', 'Aggregator', 10, basename(__FILE__),array('shapley_aggregator','config_page'));
38 }
39 }
40
41 function config_page()
42 {
43 if ( isset($_POST['submit']) )
44 {
45 if (!current_user_can('manage_options')) die(__('You cannot edit the Shapley RSS Aggregator options.'));
46 check_admin_referer('shapley-aggregator-updatesettings');
47
48 foreach (array('feeds','categories','seconds') as $option_name)
49 {
50 if (isset($_POST[$option_name]))
51 {
52 $option[$option_name] = htmlentities(html_entity_decode($_POST[$option_name]));
53 }
54 }
55 foreach (array('linkback','nofollow') as $option_name)
56 {
57 if(isset($_POST[$option_name]))
58 {
59 $option[$option_name] = true;
60 }
61 else
62 {
63 $option[$option_name] = false;
64 }
65 }
66 update_option("shapley_rss_aggregator",$option);
67 }
68
69 $options = get_option("shapley_rss_aggregator");
70 ?>
71 <!-- Admin Form -->
72 <div class="wrap">
73 <h2>Shapley RSS Aggregator Configuration</h2>
74 <form action="" method="post" id="shapleyaggregator-conf">
75 <table class="form-table">
76 <?php if (function_exists('wp_nonce_field')) { wp_nonce_field('shapley-aggregator-updatesettings'); } ?>
77 <tr>
78 <th scope="row" valign="top">
79 <label for="feeds">RSS Feeds</label>
80 </th>
81 <td>
82 <textarea name="feeds" id="feeds" style="width:500px; height:100px;">
83 <?php echo htmlentities($options['feeds']); ?>
84 </textarea>
85 </td>
86 </tr>
87
88 <tr>
89 <th scope="row" valign="top">
90 <label for="seconds">Seconds between each feed load:</label>
91 </th>
92 <td>
93 <input type="text" name="seconds" id="seconds" value="<?php echo htmlentities($options['seconds']); ?>" />
94 </td>
95 </tr>
96
97 <tr>
98 <th>
99 <label for="categories">Category for the aggregated data?</label>
100 </th>
101 <td>
102 <?php wp_dropdown_categories("name=categories&show_option_none=-- None --&selected=".$options['categories']); ?>
103 </td>
104 </tr>
105
106 <tr>
107 <th>
108 <label for="linkback">Link back to original post?</label>
109 </th>
110 <td>
111 <input type="checkbox" id="linkback" name="linkback" <?php if ($options['linkback']) { echo 'checked="checked"'; } ?>/>
112 </td>
113 </tr>
114
115 <tr>
116 <th>
117 <label for="nofollow">No Follow the link back?</label>
118 </th>
119 <td>
120 <input type="checkbox" id="nofollow" name="nofollow" <?php if ($options['nofollow']) { echo 'checked="checked"'; } ?>/>
121 </td>
122 </tr>
123
124 </table>
125
126 <br/>
127 <span class="submit" style="border: 0;"><input type="submit" name="submit" value="Save Settings" /></span>
128
129 </form>
130 </div>
131 <?
132 }
133
134 }
135
136}
137// Checks to see if the post has already been submitted, it uses URL's to do this.
138function unique_post($rss_url)
139{
140 $exists = false;
141 $all_posts = get_posts('numberposts=1000&post_type=post&post_status=');
142 foreach ($all_posts as $blog_post)
143 {
144 if($rss_url == get_post_meta($blog_post->ID, "url", true))
145 {
146 $exists = true;
147 }
148 }
149 return $exists;
150}
151function shapley_rss_aggregate()
152{
153 $options = get_option("shapley_rss_aggregator");
154
155// Only check every 24 hours (this will eventually be an option)
156if ($options['lastcheck'] < ( mktime() - $options['seconds'] ) )
157{
158 $feed_list = split("[\n\r\t ]+",$options['feeds']);
159
160 foreach($feed_list as $feed)
161 {
162 $valid = strpos($feed, "http://" , 0);
163 if($valid === false) // string contains http://
164 {
165 // do nothing if the string doesn't contain HTTP
166 }
167 else
168 {
169 if( ! $rssxml = simplexml_load_file( $feed ))
170 {
171 echo "<p>Unable to access the RSS Feed</p>";
172 }
173 else
174 {
175 foreach( $rssxml->channel->item as $items)
176 {
177 // This function will check to see if the post is unique
178 $unique = unique_post($items->link);
179
180 // if the post is unique, create a new one
181 if ($unique === false)
182 {
183 // Create post array
184 $post_content = $items->description;
185 if($options['nofollow'] === true) { $rel = "rel='nofollow'"; } else { $rel = ""; }
186 if($options['linkback'] === true)
187 {
188 $post_content .= "<p>Originally posted from <a href='".$items->link."' ".$rel.">".$items->title."</a>";
189 }
190 $my_post = array();
191 $my_post['post_title'] = $items->title;
192 $my_post['post_content'] = $post_content;
193 $my_post['post_status'] = 'publish';
194 $my_post['post_author'] = 1;
195 $my_post['post_category'] = array($options['categories']);
196
197 // wp_insert_post, returns the post id, ideal we can then add the source URL to check for uniqueness
198 $post_id = wp_insert_post( $my_post );
199
200 // adds the source URL, this is purely to check that each post is unique
201 add_post_meta( $post_id, 'url', "".$items->link."", true);
202 }
203 }
204 }
205 }
206
207 }
208 $options['lastcheck']= mktime();
209 update_option("shapley_rss_aggregator",$options);
210}
211}
212// Run the plugin each time the footer is loaded, the plugin will cache and run hourly, could be controllable from the admin page
213 add_action('wp_footer', 'shapley_rss_aggregate');
214// Add Admin Page
215 add_action('admin_menu', array('shapley_aggregator','add_config_page'));
216
217?>
Note: See TracBrowser for help on using the repository browser.