WebSub/PubSubHubbub - Version 1.5

Version Description

  • Added filter to modify $feed_urls
  • Re-Added Stephen Paul Webers changes
Download this release

Release Info

Developer joshfraz
Plugin Icon 128x128 WebSub/PubSubHubbub
Version 1.5
Comparing to
See all releases

Version 1.5

Files changed (4) hide show
  1. publisher.php +86 -0
  2. pubsubhubbub.php +204 -0
  3. readme.txt +76 -0
  4. screenshot-1.png +0 -0
publisher.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // a PHP client library for pubsubhubbub
4
+ // as defined at http://code.google.com/p/pubsubhubbub/
5
+ // written by Josh Fraser | joshfraser.com | joshfraz@gmail.com
6
+ // Released under Apache License 2.0
7
+
8
+ class Publisher {
9
+
10
+ protected $hub_url;
11
+ protected $last_response;
12
+
13
+ // create a new Publisher
14
+ public function __construct($hub_url) {
15
+
16
+ if (!isset($hub_url))
17
+ throw new Exception('Please specify a hub url');
18
+
19
+ if (!preg_match("|^https?://|i",$hub_url))
20
+ throw new Exception('The specified hub url does not appear to be valid: '.$hub_url);
21
+
22
+ $this->hub_url = $hub_url;
23
+ }
24
+
25
+ // accepts either a single url or an array of urls
26
+ public function publish_update($topic_urls, $http_function = false) {
27
+ if (!isset($topic_urls))
28
+ throw new Exception('Please specify a topic url');
29
+
30
+ // check that we're working with an array
31
+ if (!is_array($topic_urls)) {
32
+ $topic_urls = array($topic_urls);
33
+ }
34
+
35
+ // set the mode to publish
36
+ $post_string = "hub.mode=publish";
37
+ // loop through each topic url
38
+ foreach ($topic_urls as $topic_url) {
39
+
40
+ // lightweight check that we're actually working w/ a valid url
41
+ if (!preg_match("|^https?://|i",$topic_url))
42
+ throw new Exception('The specified topic url does not appear to be valid: '.$topic_url);
43
+
44
+ // append the topic url parameters
45
+ $post_string .= "&hub.url=".urlencode($topic_url);
46
+ }
47
+
48
+ // make the http post request and return true/false
49
+ // easy to over-write to use your own http function
50
+ if ($http_function)
51
+ return $http_function($this->hub_url,$post_string);
52
+ else
53
+ return $this->http_post($this->hub_url,$post_string);
54
+ }
55
+
56
+ // returns any error message from the latest request
57
+ public function last_response() {
58
+ return $this->last_response;
59
+ }
60
+
61
+ // default http function that uses curl to post to the hub endpoint
62
+ private function http_post($url, $post_string) {
63
+
64
+ // add any additional curl options here
65
+ $options = array(CURLOPT_URL => $url,
66
+ CURLOPT_POST => true,
67
+ CURLOPT_POSTFIELDS => $post_string,
68
+ CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0");
69
+
70
+ $ch = curl_init();
71
+ curl_setopt_array($ch, $options);
72
+
73
+ $response = curl_exec($ch);
74
+ $this->last_response = $response;
75
+ $info = curl_getinfo($ch);
76
+
77
+ curl_close($ch);
78
+
79
+ // all good
80
+ if ($info['http_code'] == 204)
81
+ return true;
82
+ return false;
83
+ }
84
+ }
85
+
86
+ ?>
pubsubhubbub.php ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: PubSubHubbub
4
+ Plugin URI: http://code.google.com/p/pubsubhubbub/
5
+ Description: A better way to tell the world when your blog is updated.
6
+ Version: 1.5
7
+ Author: Josh Fraser
8
+ Author Email: joshfraz@gmail.com
9
+ Author URI: http://www.joshfraser.com
10
+ */
11
+
12
+ include("publisher.php");
13
+
14
+ /**
15
+ * beeing backwards compatible
16
+ * @deprecated
17
+ */
18
+ function publish_to_hub($post_id, $feed_urls = null) {
19
+ pshb_publish_to_hub($post_id, $feed_urls);
20
+ }
21
+
22
+ // function that is called whenever a new post is published
23
+ // the ability for other plugins to hook into the PuSH code was added by Stephen Paul Weber (http://singpolyma.net)
24
+ function pshb_publish_to_hub($post_id, $feed_urls = null) {
25
+
26
+ // we want to notify the hub for every feed
27
+ if (!$feed_urls) {
28
+ $feed_urls = array();
29
+ $feed_urls[] = get_bloginfo('atom_url');
30
+ $feed_urls[] = get_bloginfo('rss_url');
31
+ $feed_urls[] = get_bloginfo('rdf_url');
32
+ $feed_urls[] = get_bloginfo('rss2_url');
33
+ // customize default feeds
34
+ $feed_urls = apply_filters('pshb_feed_urls', $feed_urls);
35
+ }
36
+ // remove dups (ie. they all point to feedburner)
37
+ $feed_urls = array_unique($feed_urls);
38
+ // get the list of hubs
39
+ $hub_urls = pshb_get_pubsub_endpoints();
40
+ // loop through each hub
41
+ foreach ($hub_urls as $hub_url) {
42
+ $p = new Publisher($hub_url);
43
+ // publish the update to each hub
44
+ if (!$p->publish_update($feed_urls)) {
45
+ // TODO: add better error handling here
46
+ }
47
+ }
48
+ return $post_id;
49
+ }
50
+
51
+ function pshb_add_atom_link_tag() {
52
+ $hub_urls = pshb_get_pubsub_endpoints();
53
+ foreach ($hub_urls as $hub_url) {
54
+ echo '<link rel="hub" href="'.$hub_url.'" />';
55
+ }
56
+ }
57
+
58
+ function pshb_add_rss_link_tag() {
59
+ $hub_urls = pshb_get_pubsub_endpoints();
60
+ foreach ($hub_urls as $hub_url) {
61
+ echo '<atom:link rel="hub" href="'.$hub_url.'"/>';
62
+ }
63
+ }
64
+
65
+ function pshb_add_rdf_ns_link() {
66
+ echo 'xmlns:atom="http://www.w3.org/2005/Atom"';
67
+ }
68
+
69
+ // hack to add the atom definition to the RSS feed
70
+ // start capturing the feed output. this is run at priority 9 (before output)
71
+ function pshb_start_rss_link_tag() {
72
+ ob_start();
73
+ }
74
+
75
+ // this is run at priority 11 (after output)
76
+ // add in the xmlns atom definition link
77
+ function pshb_end_rss_link_tag() {
78
+ $feed = ob_get_clean();
79
+ $pattern = '/<rss version="(.+)">/i';
80
+ $replacement = '<rss version="$1" xmlns:atom="http://www.w3.org/2005/Atom">';
81
+ // change <rss version="X.XX"> to <rss version="X.XX" xmlns:atom="http://www.w3.org/2005/Atom">
82
+ echo preg_replace($pattern, $replacement, $feed);
83
+ }
84
+
85
+ // add a link to our settings page in the WP menu
86
+ function pshb_add_plugin_menu() {
87
+ add_options_page('PubSubHubbub Settings', 'PubSubHubbub', 8, __FILE__, 'pshb_add_settings_page');
88
+ }
89
+
90
+ // get the endpoints from the wordpress options table
91
+ // valid parameters are "publish" or "subscribe"
92
+ function pshb_get_pubsub_endpoints() {
93
+ $endpoints = get_option('pubsub_endpoints');
94
+ $hub_urls = explode("\n",$endpoints);
95
+
96
+ // if no values have been set, revert to the defaults (pubsubhubbub on app engine & superfeedr)
97
+ if (!$endpoints) {
98
+ $hub_urls[] = "http://pubsubhubbub.appspot.com";
99
+ $hub_urls[] = "http://pubsubhubbub.superfeedr.com";
100
+ }
101
+
102
+ // clean out any blank values
103
+ foreach ($hub_urls as $key => $value) {
104
+ if (is_null($value) || $value=="") {
105
+ unset($hub_urls[$key]);
106
+ } else {
107
+ $hub_urls[$key] = trim($hub_urls[$key]);
108
+ }
109
+ }
110
+
111
+ return $hub_urls;
112
+ }
113
+
114
+ // write the content for our settings page that allows you to define your endpoints
115
+ function pshb_add_settings_page() { ?>
116
+ <div class="wrap">
117
+ <h2>Define custom hubs</h2>
118
+
119
+ <form method="post" action="options.php">
120
+ <?php //wp_nonce_field('update-options'); ?>
121
+ <!-- starting -->
122
+ <?php settings_fields('my_settings_group'); ?>
123
+ <?php do_settings_sections('my_settings_section'); ?>
124
+ <!-- ending -->
125
+
126
+ <?php
127
+
128
+ // load the existing pubsub endpoint list from the wordpress options table
129
+ $pubsub_endpoints = trim(implode("\n",pshb_get_pubsub_endpoints()),"\n");
130
+
131
+ ?>
132
+
133
+ <table class="form-table">
134
+
135
+ <tr valign="top">
136
+ <th scope="row">Hubs (one per line)</th>
137
+ <td><textarea name="pubsub_endpoints" style='width:600px;height:100px'><?php echo $pubsub_endpoints; ?></textarea></td>
138
+ </tr>
139
+
140
+ </table>
141
+
142
+ <input type="hidden" name="action" value="update" />
143
+ <input type="hidden" name="page_options" value="pubsub_endpoints" />
144
+
145
+ <p class="submit">
146
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
147
+ </p>
148
+
149
+ </form>
150
+
151
+ <br /><br />
152
+ <div style='background-color:#FFFEEB;border:1px solid #CCCCCC;padding:12px'>
153
+ <strong>Thanks for using PubSubHubbub!</strong><br />
154
+ Visit these links to learn more about PubSubHubbub and the author of this plugin:<br />
155
+ <ul>
156
+ <li><a href='http://www.onlineaspect.com'>Subscribe to Online Aspect</a></li>
157
+ <li><a href='http://www.twitter.com/joshfraser'>Follow Josh Fraser on twitter</a></li>
158
+ <li><a href='http://code.google.com/p/pubsubhubbub/'>Learn more about the PubSubHubbub protocol</a></li>
159
+ </ul>
160
+ </div>
161
+
162
+ </div>
163
+
164
+ <?php }
165
+
166
+ // add a settings link next to deactive / edit
167
+ function pshb_add_settings_link( $links, $file ) {
168
+ if( $file == 'pubsubhubbub/pubsubhubbub.php' && function_exists( "admin_url" ) ) {
169
+ $settings_link = '<a href="' . admin_url( 'options-general.php?page=pubsubhubbub/pubsubhubbub' ) . '">' . __('Settings') . '</a>';
170
+ array_unshift( $links, $settings_link ); // before other links
171
+ }
172
+ return $links;
173
+ }
174
+
175
+ // attach the handler that gets called every time you publish a post
176
+ add_action('publish_post', 'pshb_publish_to_hub');
177
+ // add the link to our settings page in the WP menu structure
178
+ add_action('admin_menu', 'pshb_add_plugin_menu');
179
+
180
+ // keep WPMU happy
181
+ add_action('admin_init', 'pshb_register_my_settings');
182
+ function pshb_register_my_settings() {
183
+ register_setting('my_settings_group','pubsub_endpoints');
184
+ }
185
+
186
+ // add the link tag that points to the hub in the header of our template...
187
+
188
+ // to our atom feed
189
+ add_action('atom_head', 'pshb_add_atom_link_tag');
190
+ // to our RSS 0.92 feed (requires a bit of a hack to include the ATOM namespace definition)
191
+ add_action('do_feed_rss', 'pshb_start_rss_link_tag', 9); // run before output
192
+ add_action('do_feed_rss', 'pshb_end_rss_link_tag', 11); // run after output
193
+ add_action('rss_head', 'pshb_add_rss_link_tag');
194
+ // to our RDF / RSS 1 feed
195
+ add_action('rdf_ns', 'pshb_add_rdf_ns_link');
196
+ add_action('rdf_header', 'pshb_add_rss_link_tag');
197
+ // to our RSS 2 feed
198
+ add_action('rss2_head', 'pshb_add_rss_link_tag');
199
+ // to our main HTML header -- not sure if we want to include this long-term or not.
200
+ add_action('wp_head', 'pshb_add_atom_link_tag');
201
+
202
+ add_filter('plugin_action_links', 'pshb_add_settings_link', 10, 2);
203
+
204
+ ?>
readme.txt ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: joshfraz, pfefferle
3
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5426516
4
+ Tags: pubsubhubbub
5
+ Requires at least: 2.5
6
+ Tested up to: 3.0.4
7
+ Stable tag: 1.5
8
+
9
+ A better way to tell the world when your blog is updated.
10
+
11
+ == Description ==
12
+
13
+ This [PubSubHubbub](http://code.google.com/p/pubsubhubbub/ "PubSubHubbub") plugin is a simple way to let people know in real-time when your blog is updated. PubSubHubbub is quickly gaining adoption and is already being used by Google Reader, Google Alerts, FriendFeed and more.
14
+
15
+ This plugin:
16
+
17
+ * Now supports multiple hubs!
18
+ * Supports all of the feed formats used by WordPress, not just ATOM and RSS2
19
+ * Announces which hubs you are using by adding `<link rel="hub" ...>` declarations to your template header and ATOM feed
20
+ * Adds `<atom:link rel="hub" ...>` to your RSS feeds along with the necessary XMLNS declaration for RSS 0.92/1.0
21
+
22
+ By default this plugin will ping the following hubs:
23
+
24
+ * [Demo hub on Google App Engine](http://pubsubhubbub.appspot.com "Demo hub on Google App Engine")
25
+ * [SuperFeedr](http://pubsubhubbub.superfeedr.com "SuperFeedr")
26
+
27
+ Please contact me if you operate a hub that you would like to be included as a default option.
28
+
29
+ == Installation ==
30
+
31
+ 1. Upload the `pubsubhubbub` directory to your `/wp-content/plugins/` directory
32
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
33
+ 3. Select custom hubs under your PubSubHubbub Settings (optional)
34
+
35
+ Note: PHP 5.0 or better is required.
36
+
37
+ == Frequently Asked Questions ==
38
+
39
+ = Where can I learn more about the PubSubHubbub protocol? =
40
+
41
+ You can visit [PubSubHubbb on Google Code](http://code.google.com/p/pubsubhubbub/ "PubSubHubbb on Google Code")
42
+
43
+ = Where can I learn more about the author of this plugin? =
44
+
45
+ You can learn more about [Josh Fraser](http://www.joshfraser.com "Josh Fraser") at [Online Aspect](http://www.onlineaspect.com "Online Aspect")
46
+ and [Matthias Pfefferle](http://pfefferle.org "Matthias Pfefferle") at [Notizblog](http://notizblog.org/ "Notizblog")
47
+
48
+ == Screenshots ==
49
+
50
+ 1. The PubSubHubbub Settings page allows you to define which hubs you want to use
51
+
52
+ == Changelog ==
53
+
54
+ = 1.5 =
55
+ * Added filter to modify $feed_urls
56
+ * Re-Added Stephen Paul Webers changes
57
+
58
+ = 1.4 =
59
+ * Added name spacing to avoid conflicts with other plugins & added patch from pfefferle
60
+
61
+ = 1.3 =
62
+ * Added multi-user support and now tested up to 2.9.1
63
+
64
+ = 1.2 =
65
+ * Added support for multiple hubs
66
+
67
+ = 1.1 =
68
+ * Added RSS support
69
+
70
+ = 1.0 =
71
+ * First attempt
72
+
73
+ == Upgrade Notice ==
74
+
75
+ = 1.4 =
76
+ Upgrade eliminates conflicts with other Wordpress plugins
screenshot-1.png ADDED
Binary file