Tuesday, June 2, 2009

Wordpress Plugin: Displaying your Google Reader RSS subscriptions

I've been meaning to write this code for a while, and I really wanted to take a stab at writing a wordpress plugin so here it goes.

The following takes in Google user credentials, and allows the user to display what RSS feeds they subscribe to on their wordpress blog

Example: The RSS that I read
Update: This plugin is now hosted by wordpress. click here


/*
Plugin Name: Google Reader Subscription List
Version: 1
Author: Timothy Broder
Description: Lists a users subscribed Google Reader feeds
*/

/*  Copyright 2009  Timothy Broder (email : timothy.broder@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


if (!class_exists('GoogleReaderSubList')) { 
 class GoogleReaderSubList {
  
  var $show_list       = 'show-google-reader-sub-list';  //the hook in a page
  var $login          = '';
  var $pass          = '';
  var $source         = 'wordpress-google-reader-sub-list-';  //the source the api sees when logging into Google
  var $service         = 'reader';   
  var $login_url        = 'https://www.google.com/accounts/ServiceLoginAuth?service=mail'; //URL to login to google
  var $subscription_list_url  = 'http://www.google.com/reader/api/0/subscription/list'; //URL that holds a users subscriptions
  
  function GoogleReaderSubList() {
   $options    = $this->get_admin_options();
   $this->login  = $options['google_login'];
   $this->pass  = $options['google_pass'];

   $this->source = $this->source . $this->login;
  }
    
  function show_sub_list() {
   $stop = false;
   if ($this->login == '' || $this->login == null) {
    echo 'Google login not set
'; $stop = true; } if ($this->pass == '' || $this->pass == null) { echo 'Google password not set
'; $stop = true; } //check to see if the zend plugin has been installed and activated //http://wordpress.org/extend/plugins/zend-framework/ if (!(defined('WP_ZEND_FRAMEWORK') && WP_ZEND_FRAMEWORK)) { echo 'The Zend Framework Plugin is not active. Please install and activate it.'; $stop = true; } if ($stop) { return; } $client = new Zend_Http_Client($this->login_url); //connect, authenticate, and handshake with Google $client->setCookieJar() ->setMethod(Zend_Http_Client::POST) ->setParameterPost(array( 'continue' => $this->subscription_list_url, 'service' => 'reader', 'niu' => 1, 'hl' => 'en', 'Email' => $this->login, 'Passwd' => $this->pass, 'PersistentCookie' => 'yes', 'asts' => '' )); //$error_level = error_reporting(); //error_reporting(1); $response = $client->request('POST'); $client->setUri($this->subscription_list_url)->setMethod(Zend_Http_Client::GET); $response = $client->request()->getBody(); if ($client->request()->getStatus() == 400) { ?>Unable to login with supplied Google login/passwordlist->object as $e) { $url = $e->string[0]; $title = $e->string[1]; $cat = $e->list->object->string[1]; //make sure a feed is filed somewhere if ($cat == '') { $cat = 'unfiled'; } $t = $hashmap["$cat"]; //a category hasn't been used before if ($t == null) { $t = array($e); $hashmap["$cat"] = $t; } //category has been used before else { array_push($t, $e); $hashmap["$cat"] = $t; } } //sort the categories ksort($hashmap); //output ?>

Tags: $t) { echo "$cat"; if ($cat != $endKey) { echo ', '; } } ?>

$t) { echo ""; echo "$cat
"; foreach ($t as $e) { list($feed, $url) = split('feed/', $e->string[0]); $title = $e->string[1]; echo "$title
"; } echo '
'; } } function addContent($content) { // Only do this if this is a page and it has the appropriate custom field if (is_page()) { $cust_field_values = get_post_custom_values($this->show_list); if ($cust_field_values != NULL) { if (defined('WP_ZEND_FRAMEWORK') && WP_ZEND_FRAMEWORK) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Http_Client'); } $content = $this->show_sub_list(); } } return $content; } function init() { $this->get_admin_options(); } function get_admin_options() { $admin_options = array('google_login' => '', 'google_pass' => '', 'use_accordion' => 'true'); $options = get_option($this->adminOptionName); if (!empty($options)) { foreach ($options as $key => $option) { $admin_options[$key] = $option; } } update_option($this->admin_optionsName, $admin_options); return $admin_options; } function printAdminPage() { $options = $this->get_admin_options(); if (isset($_POST['update_greader_sub_list_settings'])) { if (isset($_POST['greader_sub_list_login'])) { $options['google_login'] = $_POST['greader_sub_list_login']; } if (isset($_POST['greader_sub_list_pass'])) { $options['google_pass'] = $_POST['greader_sub_list_pass']; } update_option($this->admin_optionsName, $options); echo '

' . _e('Settings Updated.', 'GoogleReaderSubList'). '

'; } //$submit = _e('Update Settings', 'GoogleReaderSubList'); echo "

Google Reader Subscription List

Google Login

Google Password

"; } } } if (class_exists('GoogleReaderSubList')) { $greader_sub_list = new GoogleReaderSubList(); } if (!function_exists('greader_sub_list_ap')) { function greader_sub_list_ap() { global $greader_sub_list; if (!isset($greader_sub_list)) { return; } if (function_exists('add_options_page')) { add_options_page('gReader Subscriptions', 'gReader Subscriptions', 9, basename(__FILE__), array(&$greader_sub_list, 'printAdminPage')); } } } if (isset($greader_sub_list)) { add_action('admin_menu', 'greader_sub_list_ap'); add_action('activate_google-raeder-list/google-reader-list.php', array(&$greader_sub_list, 'init')); add_filter('the_content', array(&$greader_sub_list, 'addContent'), '7'); }

4 comments:

Ryan said...

This plugin is much needed. Thank you for writing it. I have installed everything properly but my page is still blank. I have triple checked my credentials and they are right. Any ideas? Have others been having problems? Thanks.

Tim Broder said...

install from here and let me know if you have any more issues

http://wordpress.org/extend/plugins/google-reader-subscription-list/

timothy.broder@gmail.com

brt12 said...

Hi, thanks a lot for the plugin, it works great. However there is one improvement which would help me to introduce my greader list.

I was trying to put my subscriptions as a part of a page (so I would be able to write something above the list as sort of an introduction) and what I found out is that my content cannot be showed. I hope it's possible for you to fix it.

Thanks a lot again!

Tim Broder said...

I'd have to look in to how to do that (this was my first go) but yeah I'll add that in to the next update