wordpress : fun with pluggable classes

I was checking some idea i had about writing a small user class with an option to ‘plug in’ functions for wordpress.

This page covers most of it :
dynamically add functions to php classes @ www.gen-x-design.com. The class construct at the end of the comment thread, Martin Pietschmann’s contribution, is rather useful. This pattern revolves around importing functionality from pluggable classes and exposing it through one object instance (the ‘decorator’ pattern mentioned is mostly used for writing extended classes, different objects with the same base data and functionality, ‘views’ sort of).

I can include the file with base, import and user class into function.php, and on making a user object have it read the directory and import functions modules (or load functionality conditional based on user role/authorization).

For this example I used the wordpress options table. I write a new functions class

  1. class UserBogusPlugin extends MI_Importable
  2. {
  3.  public function the_anchor() {
  4. //user_url and user_nicename are exposed through the user class
  5. //I plug the functions class into, i can use the $this reference
  6. //as if i am writing code in the user class
  7.                 if($this->user_url<>'') {
  8.                     return '<a href="'.$this->user_url.'">'.$this->user_nicename .'</a>, ' . $this->first_name;
  9.                 }
  10. //no url, no anchor….  
  11.   return $this->user_nicename;
  12.  }
  13. }

…store the added class name in the options table…

  1. //load $arr from options table
  2. $modules = get_option('usermodules');
  3. if($modules) $usermodules = json_decode($modules);
  4. //add module
  5. $usermodules[] = 'UserBogusPlugin';
  6. //store back in options
  7. add_option('usermodules', json_encode($usermodules));

…and load the plugin classes when instantiating the user object :

  1. class User extends MI_Base
  2. {
  3.  public function __construct($ID) {
  4.             $modules = get_option('usermodules');
  5.             if($modules) {
  6.                 $usermodules = json_decode($modules);
  7.                 foreach($usermodules as $module) {
  8.                   if(class_exists($module)) $this->import(new $module);
  9.                  //or..
  10.                  //include('plugclass/'.$module.'.class.php');
  11.                  //$this->import(new $module);
  12.                 }
  13.             }
  14.             $this->ID=$ID;
  15.  }
  16. }

In the wordpress template i can use the added functionality through the user instance :

  1.       $my_user = new User($user_id);
  2.      
  3.       echo $my_user->the_anchor();

Fun with classes :)

add. 3-8 (qed) :
a function to load a directory with plugin files, i add a header /* plugin pluginfilename */ and check the files if there is a header.

  1.        public function getPlugins()
  2.         {
  3.             $plugdir = TEMPLATEPATH .'/plug';
  4.             if ($handle = opendir($plugdir)) {
  5.                 $retval = array();
  6.                 while (false !== ($file = readdir($handle))) {
  7.                     if (($file <> ".") && ($file <> "..")) {
  8.                         $fh = fopen($plugdir.'/'.$file, 'r');
  9.                         $contents = '';
  10.                           $contents .= fread($fh, 1024);
  11.                         fclose($fh);
  12.                         if(preg_match('/lugin/', $contents))
  13.                         {   //check for header : plugin, grab pluginname
  14.                             $a = strpos($contents, 'lugin');
  15.                             $a += 6;
  16.                             $b = strpos($contents, ' ', $a);
  17.                             $plugname = substr($contents, $a, $b$a);
  18.                             $retval[] = array($plugdir.'/'.$file, $plugname);
  19.                 }  }  }
  20.                 closedir($handle);
  21.             }
  22.             return $retval;
  23.         }
  24.  
  25.  
  26.  public function __construct($id) {
  27. //get he array with plugin files
  28.             $usermodules = $this->getPlugins();
  29.             if($usermodules) {
  30.                 foreach($usermodules as $module) {
  31.  
  32.                   //use require to load and import to add the function
  33.  
  34.                    require_once($module[0]);
  35.                    $this->import(new $module[1]);    
  36.                 }
  37.             }
  38.             $this->ID=$id;  
  39.  }
Posted in php, wordpress and tagged .

Leave a Reply

Your email address will not be published. Required fields are marked *