Revision: 315
Author: [email protected]
Date: Sat Jan 23 17:35:52 2010
Log: v1.3 -- added support for multi-user installs and verified everything still works in WP 2.9.1
http://code.google.com/p/pubsubhubbub/source/detail?r=315

Added:
 /trunk/publisher_clients/wordpress/1.3
 /trunk/publisher_clients/wordpress/1.3/publisher.php
 /trunk/publisher_clients/wordpress/1.3/pubsubhubbub.php
 /trunk/publisher_clients/wordpress/1.3/readme.txt
 /trunk/publisher_clients/wordpress/1.3/screenshot-1.png

=======================================
--- /dev/null
+++ /trunk/publisher_clients/wordpress/1.3/publisher.php Sat Jan 23 17:35:52 2010
@@ -0,0 +1,86 @@
+<?php
+
+// a PHP client library for pubsubhubbub
+// as defined at http://code.google.com/p/pubsubhubbub/
+// written by Josh Fraser | joshfraser.com | [email protected]
+// Released under Apache License 2.0
+
+class Publisher {
+
+    protected $hub_url;
+    protected $last_response;
+
+    // create a new Publisher
+    public function __construct($hub_url) {
+
+        if (!isset($hub_url))
+            throw new Exception('Please specify a hub url');
+
+        if (!preg_match("|^https?://|i",$hub_url))
+ throw new Exception('The specified hub url does not appear to be valid: '.$hub_url);
+
+        $this->hub_url = $hub_url;
+    }
+
+    // accepts either a single url or an array of urls
+    public function publish_update($topic_urls, $http_function = false) {
+        if (!isset($topic_urls))
+            throw new Exception('Please specify a topic url');
+
+        // check that we're working with an array
+        if (!is_array($topic_urls)) {
+            $topic_urls = array($topic_urls);
+        }
+
+        // set the mode to publish
+        $post_string = "hub.mode=publish";
+        // loop through each topic url
+        foreach ($topic_urls as $topic_url) {
+
+            // lightweight check that we're actually working w/ a valid url
+            if (!preg_match("|^https?://|i",$topic_url))
+ throw new Exception('The specified topic url does not appear to be valid: '.$topic_url);
+
+            // append the topic url parameters
+            $post_string .= "&hub.url=".urlencode($topic_url);
+        }
+
+        // make the http post request and return true/false
+        // easy to over-write to use your own http function
+        if ($http_function)
+            return $http_function($this->hub_url,$post_string);
+        else
+            return $this->http_post($this->hub_url,$post_string);
+    }
+
+    // returns any error message from the latest request
+    public function last_response() {
+        return $this->last_response;
+    }
+
+    // default http function that uses curl to post to the hub endpoint
+    private function http_post($url, $post_string) {
+
+        // add any additional curl options here
+        $options = array(CURLOPT_URL => $url,
+                         CURLOPT_POST => true,
+                         CURLOPT_POSTFIELDS => $post_string,
+ CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0");
+
+       $ch = curl_init();
+       curl_setopt_array($ch, $options);
+
+        $response = curl_exec($ch);
+        $this->last_response = $response;
+        $info = curl_getinfo($ch);
+
+        curl_close($ch);
+
+        // all good
+        if ($info['http_code'] == 204)
+            return true;
+        return false;
+    }
+}
+
+?>
=======================================
--- /dev/null
+++ /trunk/publisher_clients/wordpress/1.3/pubsubhubbub.php Sat Jan 23 17:35:52 2010
@@ -0,0 +1,221 @@
+<?php
+/*
+Plugin Name: PubSubHubbub
+Plugin URI: http://code.google.com/p/pubsubhubbub/
+Description: A better way to tell the world when your blog is updated.
+Version: 1.3
+Author: Josh Fraser
+Author Email: [email protected]
+Author URI: http://www.joshfraser.com
+*/
+
+include("publisher.php");
+
+// function that is called whenever a new post is published
+function publish_to_hub($post_id)  {
+
+    // we want to notify the hub for every feed
+    $feed_urls = array();
+    $feed_urls[] = get_bloginfo('atom_url');
+    $feed_urls[] = get_bloginfo('rss_url');
+    $feed_urls[] = get_bloginfo('rdf_url');
+    $feed_urls[] = get_bloginfo('rss2_url');
+    // remove dups (ie. they all point to feedburner)
+    $feed_urls = array_unique($feed_urls);
+    // get the list of hubs
+    $hub_urls = get_pubsub_endpoints();
+    // loop through each hub
+    foreach ($hub_urls as $hub_url) {
+        $p = new Publisher($hub_url);
+        // publish the update to each hub
+        if (!$p->publish_update($feed_urls, "http_post_wp")) {
+            // TODO: add better error handling here
+        }
+    }
+    return $post_id;
+}
+
+function add_atom_link_tag() {
+    $hub_urls = get_pubsub_endpoints();
+    foreach ($hub_urls as $hub_url) {
+        echo '<link rel="hub" href="'.$hub_url.'" />';
+    }
+}
+
+function add_rss_link_tag() {
+    $hub_urls = get_pubsub_endpoints();
+    foreach ($hub_urls as $hub_url) {
+        echo '<atom:link rel="hub" href="'.$hub_url.'"/>';
+    }
+}
+
+function add_rdf_ns_link() {
+    echo 'xmlns:atom="http://www.w3.org/2005/Atom";';
+}
+
+// hack to add the atom definition to the RSS feed
+// start capturing the feed output. this is run at priority 9 (before output)
+function start_rss_link_tag() {
+    ob_start();
+}
+
+// this is run at priority 11 (after output)
+// add in the xmlns atom definition link
+function end_rss_link_tag() {
+    $feed = ob_get_clean();
+    $pattern = '/<rss version="(.+)">/i';
+ $replacement = '<rss version="$1" xmlns:atom="http://www.w3.org/2005/Atom";>'; + // change <rss version="X.XX"> to <rss version="X.XX" xmlns:atom="http://www.w3.org/2005/Atom";>
+    echo preg_replace($pattern, $replacement, $feed);
+}
+
+// add a link to our settings page in the WP menu
+function add_plugin_menu() {
+ add_options_page('PubSubHubbub Settings', 'PubSubHubbub', 8, __FILE__, 'add_settings_page');
+}
+
+// get the endpoints from the wordpress options table
+// valid parameters are "publish" or "subscribe"
+function get_pubsub_endpoints() {
+    $endpoints = get_option('pubsub_endpoints');
+    $hub_urls = explode("\n",$endpoints);
+
+ // if no values have been set, revert to the defaults (pubsubhubbub on app engine & superfeedr)
+    if (!$endpoints) {
+        $hub_urls[] = "http://pubsubhubbub.appspot.com";;
+        $hub_urls[] = "http://superfeedr.com/hubbub";;
+    }
+
+    // clean out any blank values
+    foreach ($hub_urls as $key => $value) {
+        if (is_null($value) || $value=="") {
+            unset($hub_urls[$key]);
+        } else {
+            $hub_urls[$key] = trim($hub_urls[$key]);
+        }
+    }
+
+    return $hub_urls;
+}
+
+// write the content for our settings page that allows you to define your endpoints
+function add_settings_page() { ?>
+    <div class="wrap">
+    <h2>Define custom hubs</h2>
+
+    <form method="post" action="options.php">
+    <?php //wp_nonce_field('update-options'); ?>
+    <!-- starting -->
+    <?php settings_fields('my_settings_group'); ?>
+    <?php do_settings_sections('my_settings_section'); ?>
+    <!-- ending -->
+
+    <?php
+
+ // load the existing pubsub endpoint list from the wordpress options table
+    $pubsub_endpoints = trim(implode("\n",get_pubsub_endpoints()),"\n");
+
+    ?>
+
+    <table class="form-table">
+
+    <tr valign="top">
+    <th scope="row">Hubs (one per line)</th>
+ <td><textarea name="pubsub_endpoints" style='width:600px;height:100px'><?php echo $pubsub_endpoints; ?></textarea></td>
+    </tr>
+
+    </table>
+
+    <input type="hidden" name="action" value="update" />
+    <input type="hidden" name="page_options" value="pubsub_endpoints" />
+
+    <p class="submit">
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
+    </p>
+
+    </form>
+
+    <br /><br />
+ <div style='background-color:#FFFEEB;border:1px solid #CCCCCC;padding:12px'>
+        <strong>Thanks for using PubSubHubbub!</strong><br />
+ Visit these links to learn more about PubSubHubbub and the author of this plugin:<br />
+        <ul>
+ <li><a href='http://www.onlineaspect.com'>Subscribe to Online Aspect</a></li> + <li><a href='http://www.twitter.com/joshfraser'>Follow Josh Fraser on twitter</a></li> + <li><a href='http://code.google.com/p/pubsubhubbub/'>Learn more about the PubSubHubbub protocol</a></li>
+        </ul>
+    </div>
+
+    </div>
+
+<?php }
+
+
+// helper function to use the WP-friendly snoopy library
+if (!function_exists('get_snoopy')) {
+       function get_snoopy() {
+               include_once(ABSPATH.'/wp-includes/class-snoopy.php');
+               return new Snoopy;
+       }
+}
+
+// over-ride the default curl http function to post to the hub endpoints
+function http_post_wp($url, $post_vars) {
+
+    // turn the query string into an array for snoopy
+    parse_str($post_vars);
+    $post_vars = array();
+ $post_vars['hub.mode'] = $hub_mode; // PHP converts the periods to underscores
+    $post_vars['hub.url'] = $hub_url;
+
+    // more universal than curl
+    $snoopy = get_snoopy();
+    $snoopy->agent = "(PubSubHubbub-Publisher-WP/1.0)";
+       $snoopy->submit($url,$post_vars);
+       $response = $snoopy->results;
+       // TODO: store the last_response.  requires a litle refactoring work.
+       $response_code = $snoopy->response_code;
+       if ($response_code == 204)
+           return true;
+    return false;
+}
+
+// add a settings link next to deactive / edit
+function add_settings_link( $links, $file ) {
+ if( $file == 'pubsubhubbub/pubsubhubbub.php' && function_exists( "admin_url" ) ) { + $settings_link = '<a href="' . admin_url( 'options-general.php?page=pubsubhubbub/pubsubhubbub' ) . '">' . __('Settings') . '</a>';
+               array_unshift( $links, $settings_link ); // before other links
+       }
+       return $links;
+}
+
+// attach the handler that gets called every time you publish a post
+add_action('publish_post', 'publish_to_hub');
+// add the link to our settings page in the WP menu structure
+add_action('admin_menu', 'add_plugin_menu');
+
+// keep WPMU happy
+add_action('admin_init', 'register_my_settings');
+function register_my_settings() {
+    register_setting('my_settings_group','pubsub_endpoints');
+}
+
+// add the link tag that points to the hub in the header of our template...
+
+// to our atom feed
+add_action('atom_head', 'add_atom_link_tag');
+// to our RSS 0.92 feed (requires a bit of a hack to include the ATOM namespace definition)
+add_action('do_feed_rss', 'start_rss_link_tag', 9); // run before output
+add_action('do_feed_rss', 'end_rss_link_tag', 11); // run after output
+add_action('rss_head', 'add_rss_link_tag');
+// to our RDF / RSS 1 feed
+add_action('rdf_ns', 'add_rdf_ns_link');
+add_action('rdf_header', 'add_rss_link_tag');
+// to our RSS 2 feed
+add_action('rss2_head', 'add_rss_link_tag');
+// to our main HTML header -- not sure if we want to include this long-term or not.
+add_action('wp_head', 'add_atom_link_tag');
+
+add_filter('plugin_action_links', 'add_settings_link', 10, 2);
+
+?>
=======================================
--- /dev/null
+++ /trunk/publisher_clients/wordpress/1.3/readme.txt Sat Jan 23 17:35:52 2010
@@ -0,0 +1,54 @@
+=== Plugin Name ===
+Contributors: joshfraz
+Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5426516
+Tags: pubsubhubbub
+Requires at least: 2.5
+Tested up to: 2.9.1
+Stable tag: /trunk/
+
+A better way to tell the world when your blog is updated.
+
+== Description ==
+
+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.
+
+This plugin:
+
+* Now supports multi-user installations!
+* Supports multiple hubs!
+* Supports all of the feed formats used by WordPress, not just ATOM and RSS2 +* Announces which hubs you are using by adding `<link rel="hub" ...>` declarations to your template header and ATOM feed +* Adds `<atom:link rel="hub" ...>` to your RSS feeds along with the necessary XMLNS declaration for RSS 0.92/1.0
+
+By default this plugin will ping the following hubs:
+
+* [Demo hub on Google App Engine](http://pubsubhubbub.appspot.com "Demo hub on Google App Engine")
+* [SuperFeedr](http://superfeedr.com/hubbub "SuperFeedr")
+
+Please contact me if you operate a hub that you would like to be included as a default option.
+
+== Installation ==
+
+1. Upload the `pubsubhubbub` directory to your `/wp-content/plugins/` directory
+2. Activate the plugin through the 'Plugins' menu in WordPress
+3. Select custom hubs under your PubSubHubbub Settings (optional)
+
+Note: PHP 5.0 or better is required.
+
+== Frequently Asked Questions ==
+
+= Where can I learn more about the PubSubHubbub protocol? =
+
+You can visit [PubSubHubbb on Google Code](http://code.google.com/p/pubsubhubbub/ "PubSubHubbb on Google Code")
+
+= Where can I learn more about the author of this plugin? =
+
+You can learn more about [Josh Fraser](http://www.joshfraser.com "Josh Fraser") at [Online Aspect](http://www.onlineaspect.com "Online Aspect")
+
+= Does this plugin work with MU? =
+
+Multi-user support was added in version 1.3
+
+== Screenshots ==
+
+1. The PubSubHubbub Settings page allows you to define which hubs you want to use
=======================================
--- /dev/null   
+++ /trunk/publisher_clients/wordpress/1.3/screenshot-1.png Sat Jan 23 17:35:52 2010
Binary file, no diff available.

Reply via email to