PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pubsubhubbub-ro/publisher_clients/wordpress/1.3/pubsubhubbub.php

https://github.com/nfiedel/Buzz-to-Blogger
PHP | 221 lines | 139 code | 39 blank | 43 comment | 12 complexity | f596bfb32bcb1e3cb30bb5fca8311073 MD5 | raw file
  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.3
  7. Author: Josh Fraser
  8. Author Email: josh@eventvue.com
  9. Author URI: http://www.joshfraser.com
  10. */
  11. include("publisher.php");
  12. // function that is called whenever a new post is published
  13. function publish_to_hub($post_id) {
  14. // we want to notify the hub for every feed
  15. $feed_urls = array();
  16. $feed_urls[] = get_bloginfo('atom_url');
  17. $feed_urls[] = get_bloginfo('rss_url');
  18. $feed_urls[] = get_bloginfo('rdf_url');
  19. $feed_urls[] = get_bloginfo('rss2_url');
  20. // remove dups (ie. they all point to feedburner)
  21. $feed_urls = array_unique($feed_urls);
  22. // get the list of hubs
  23. $hub_urls = get_pubsub_endpoints();
  24. // loop through each hub
  25. foreach ($hub_urls as $hub_url) {
  26. $p = new Publisher($hub_url);
  27. // publish the update to each hub
  28. if (!$p->publish_update($feed_urls, "http_post_wp")) {
  29. // TODO: add better error handling here
  30. }
  31. }
  32. return $post_id;
  33. }
  34. function add_atom_link_tag() {
  35. $hub_urls = get_pubsub_endpoints();
  36. foreach ($hub_urls as $hub_url) {
  37. echo '<link rel="hub" href="'.$hub_url.'" />';
  38. }
  39. }
  40. function add_rss_link_tag() {
  41. $hub_urls = get_pubsub_endpoints();
  42. foreach ($hub_urls as $hub_url) {
  43. echo '<atom:link rel="hub" href="'.$hub_url.'"/>';
  44. }
  45. }
  46. function add_rdf_ns_link() {
  47. echo 'xmlns:atom="http://www.w3.org/2005/Atom"';
  48. }
  49. // hack to add the atom definition to the RSS feed
  50. // start capturing the feed output. this is run at priority 9 (before output)
  51. function start_rss_link_tag() {
  52. ob_start();
  53. }
  54. // this is run at priority 11 (after output)
  55. // add in the xmlns atom definition link
  56. function end_rss_link_tag() {
  57. $feed = ob_get_clean();
  58. $pattern = '/<rss version="(.+)">/i';
  59. $replacement = '<rss version="$1" xmlns:atom="http://www.w3.org/2005/Atom">';
  60. // change <rss version="X.XX"> to <rss version="X.XX" xmlns:atom="http://www.w3.org/2005/Atom">
  61. echo preg_replace($pattern, $replacement, $feed);
  62. }
  63. // add a link to our settings page in the WP menu
  64. function add_plugin_menu() {
  65. add_options_page('PubSubHubbub Settings', 'PubSubHubbub', 8, __FILE__, 'add_settings_page');
  66. }
  67. // get the endpoints from the wordpress options table
  68. // valid parameters are "publish" or "subscribe"
  69. function get_pubsub_endpoints() {
  70. $endpoints = get_option('pubsub_endpoints');
  71. $hub_urls = explode("\n",$endpoints);
  72. // if no values have been set, revert to the defaults (pubsubhubbub on app engine & superfeedr)
  73. if (!$endpoints) {
  74. $hub_urls[] = "http://pubsubhubbub.appspot.com";
  75. $hub_urls[] = "http://superfeedr.com/hubbub";
  76. }
  77. // clean out any blank values
  78. foreach ($hub_urls as $key => $value) {
  79. if (is_null($value) || $value=="") {
  80. unset($hub_urls[$key]);
  81. } else {
  82. $hub_urls[$key] = trim($hub_urls[$key]);
  83. }
  84. }
  85. return $hub_urls;
  86. }
  87. // write the content for our settings page that allows you to define your endpoints
  88. function add_settings_page() { ?>
  89. <div class="wrap">
  90. <h2>Define custom hubs</h2>
  91. <form method="post" action="options.php">
  92. <?php //wp_nonce_field('update-options'); ?>
  93. <!-- starting -->
  94. <?php settings_fields('my_settings_group'); ?>
  95. <?php do_settings_sections('my_settings_section'); ?>
  96. <!-- ending -->
  97. <?php
  98. // load the existing pubsub endpoint list from the wordpress options table
  99. $pubsub_endpoints = trim(implode("\n",get_pubsub_endpoints()),"\n");
  100. ?>
  101. <table class="form-table">
  102. <tr valign="top">
  103. <th scope="row">Hubs (one per line)</th>
  104. <td><textarea name="pubsub_endpoints" style='width:600px;height:100px'><?php echo $pubsub_endpoints; ?></textarea></td>
  105. </tr>
  106. </table>
  107. <input type="hidden" name="action" value="update" />
  108. <input type="hidden" name="page_options" value="pubsub_endpoints" />
  109. <p class="submit">
  110. <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  111. </p>
  112. </form>
  113. <br /><br />
  114. <div style='background-color:#FFFEEB;border:1px solid #CCCCCC;padding:12px'>
  115. <strong>Thanks for using PubSubHubbub!</strong><br />
  116. Visit these links to learn more about PubSubHubbub and the author of this plugin:<br />
  117. <ul>
  118. <li><a href='http://www.onlineaspect.com'>Subscribe to Online Aspect</a></li>
  119. <li><a href='http://www.twitter.com/joshfraser'>Follow Josh Fraser on twitter</a></li>
  120. <li><a href='http://code.google.com/p/pubsubhubbub/'>Learn more about the PubSubHubbub protocol</a></li>
  121. </ul>
  122. </div>
  123. </div>
  124. <?php }
  125. // helper function to use the WP-friendly snoopy library
  126. if (!function_exists('get_snoopy')) {
  127. function get_snoopy() {
  128. include_once(ABSPATH.'/wp-includes/class-snoopy.php');
  129. return new Snoopy;
  130. }
  131. }
  132. // over-ride the default curl http function to post to the hub endpoints
  133. function http_post_wp($url, $post_vars) {
  134. // turn the query string into an array for snoopy
  135. parse_str($post_vars);
  136. $post_vars = array();
  137. $post_vars['hub.mode'] = $hub_mode; // PHP converts the periods to underscores
  138. $post_vars['hub.url'] = $hub_url;
  139. // more universal than curl
  140. $snoopy = get_snoopy();
  141. $snoopy->agent = "(PubSubHubbub-Publisher-WP/1.0)";
  142. $snoopy->submit($url,$post_vars);
  143. $response = $snoopy->results;
  144. // TODO: store the last_response. requires a litle refactoring work.
  145. $response_code = $snoopy->response_code;
  146. if ($response_code == 204)
  147. return true;
  148. return false;
  149. }
  150. // add a settings link next to deactive / edit
  151. function add_settings_link( $links, $file ) {
  152. if( $file == 'pubsubhubbub/pubsubhubbub.php' && function_exists( "admin_url" ) ) {
  153. $settings_link = '<a href="' . admin_url( 'options-general.php?page=pubsubhubbub/pubsubhubbub' ) . '">' . __('Settings') . '</a>';
  154. array_unshift( $links, $settings_link ); // before other links
  155. }
  156. return $links;
  157. }
  158. // attach the handler that gets called every time you publish a post
  159. add_action('publish_post', 'publish_to_hub');
  160. // add the link to our settings page in the WP menu structure
  161. add_action('admin_menu', 'add_plugin_menu');
  162. // keep WPMU happy
  163. add_action('admin_init', 'register_my_settings');
  164. function register_my_settings() {
  165. register_setting('my_settings_group','pubsub_endpoints');
  166. }
  167. // add the link tag that points to the hub in the header of our template...
  168. // to our atom feed
  169. add_action('atom_head', 'add_atom_link_tag');
  170. // to our RSS 0.92 feed (requires a bit of a hack to include the ATOM namespace definition)
  171. add_action('do_feed_rss', 'start_rss_link_tag', 9); // run before output
  172. add_action('do_feed_rss', 'end_rss_link_tag', 11); // run after output
  173. add_action('rss_head', 'add_rss_link_tag');
  174. // to our RDF / RSS 1 feed
  175. add_action('rdf_ns', 'add_rdf_ns_link');
  176. add_action('rdf_header', 'add_rss_link_tag');
  177. // to our RSS 2 feed
  178. add_action('rss2_head', 'add_rss_link_tag');
  179. // to our main HTML header -- not sure if we want to include this long-term or not.
  180. add_action('wp_head', 'add_atom_link_tag');
  181. add_filter('plugin_action_links', 'add_settings_link', 10, 2);
  182. ?>