7.20.2007

Alpha Wordpress Plugin for Delicious with Zend Framework's Zend_Service_Delicious

I have Wordpress set up on PHP5. While Wordpress is largely a PHP4 entity, I wanted to see if it could play nicely with the Zend Framework, which is a PHP5-only entity. With some careful fiddling, it works pretty well.

With my Zend Framework code set up in my PHP include path, the first thing I needed to do to create a Wordpress Plugin was to enable my plugin to call the Zend Framework classes. First I tried this towards the end of my wp-config.php file:

require_once 'Zend/Loader.php'; 
function __autoload($class) 
{ 
  Zend_Loader::loadClass($class); 
} 


This caused Wordpress to freak out like a chihuahua on speed. Not good. I figured we'd have to do something special because we don't want ALL the classes to autoload, just the Zend ones. So I scrapped my previous __autoload and tried this instead:

require_once 'Zend/Loader.php'; 
function __autoload($class) 
{ 
  $pos = strpos($class, 'Zend'); 
  if ($pos !== false) 
  { 
    Zend_Loader::loadClass($class); 
  } 
} 

Ahh, success. Sweeter then a brownie sundae with extra chocolate sauce.

From there, the rest is easy. Here's some basic code for the rz_delicious plugin, a basic PHP file you can drop into your Wordpress Plugins dir. It uses Zend_Cache and Zend_Service_Delicious to put your del.icio.us bookmarks onto your Wordpress blog. It first checks to see if there's a cached version already, if not, it uses the del.icio.us API to fetch your latest bookmarks. It does some list formatting there too because I didn't feel like handling that in my theme.

< ?php 
/* 
Plugin Name: RZ Delicious 
Plugin URI: http://www.boringuys.com/ 
Description: YADelicious Plugin 
Author: Rich Zygler 
Version: 1.0 
Author URI: http://www.boringguys.com/ 
*/ 

/* 
called from theme via: 
get_rz_delicious() 
*/ 

function get_rz_delicious() 
{ 
  $output = ''; 
  $username = 'username'; 
  $password = 'password'; 
  $tag      = 'tagname'; 
  $numPosts      = 15; 
  $cacheTime     = 3600;      // seconds 
  $cacheDir      = '/tmp/'; 

$frontendOptions = array( 
  'lifetime' => $cacheTime,                  // in seconds 
  'automatic_serialization' => false  // this is default anyway 
); 

$backendOptions = array('cache_dir' => $cacheDir); 

$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions); 

// we pass a unique identifier to the start() method 
if(!$cache->start('rz_delicious')) 
{ 
  try 
  { 
    $delicious = new Zend_Service_Delicious($username, $password); 
    $posts = $delicious->getRecentPosts($tag,$numPosts);  //$tag 

    if (count($posts) > 0) 
    { 
      $output .= '
    '; } foreach ($posts as $post) { $output .= '
  • ';
    $output .= $post->getTitle() . '
    ';

    $strTags = ' ( tags: ';
    foreach ($post->getTags() as $tag)
    {
    if ($tag != 'boringguys.com')
    {
    $strTags .= '';
    $strTags .= $tag . '
    | ';
    }
    }
    $strTags = trim($strTags, '| ');
    $output .= $strTags . ' ) ';
    }
    if (count($posts) > 0)
    {
    $output .= '
'; } } catch (Zend_Service_Delicious_Exception $e) { // largely ignore the delicious service error $output .= '
    '; $output .= '
  • del.icio.us service unavailable
  • '; $output .= ''; } echo $output; $cache->end(); // the output is saved and sent to the browser } }

1 comment:

Janice said...

This is a sweet plugin . Great work! This is definitely a useful plugin that will be a mainstay in all of my blog setups.

Post a Comment