Exploring Pluggable Content in PHP

I do a lot of plugin development for WordPress and other platforms. In fact, I recently started a business centered on doing plugin development full time. The thing is, I only had a vague idea how these platforms, mostly written in PHP, allow for you to hook into their content with your own functions, classes, and methods. I decided to go under the hood, so to speak, and write my own content filtering plugin type thing to see if I could get a feel for how the developers of WordPress and other CMS’s might do the same thing.

Now, I’d like to make it clear that I didn’t just go down into the source code and copy what they had. This is my own implementation, based on what I know and what I was able to find out through experimentation and reading the PHP function reference.

First, the plugin demonstration page. Now, let’s take a look at the source code that makes this thing run.

  1.  
  2. <?php
  3.  
  4. class PluggableRules {
  5.     private static $rules = array();
  6.  
  7.     /**
  8.      * Allows a rule to be added to the set of rules to call when a certain filter is applied.
  9.      *
  10.      * @param string $filter the name of the filter to add the rule to.
  11.      * @param mixed $function an element specifying the function that should be used when
  12.      * the rule is applied.  The element can be a string function name, an array consisting
  13.      * of a class name and a function name, or an array consisting of an object and function name.
  14.      */
  15.     public static function AddRule($filter, $function) {
  16.         if(!is_array(PluggableRules::$rules[$filter])) {
  17.             PluggableRules::$rules[$filter] = array();
  18.         }
  19.  
  20.         PluggableRules::$rules[$filter][] = $function;
  21.     }
  22.  
  23.     /**
  24.      * Applies the functions previously defined across the filterable content
  25.      * for the specific named filter.
  26.      *
  27.      * @param string $filter the name of the filter to call.
  28.      * @param mixed $filterable the item to apply the filters to.
  29.      * @return mixed the result of applying the filter.
  30.      */
  31.     public static function CallRule($filter, $filterable) {
  32.         $result = $filterable;
  33.  
  34.         if(is_array(PluggableRules::$rules[$filter])) {
  35.             foreach(PluggableRules::$rules[$filter] as $toApply) {
  36.                 if(is_array($toApply)) {
  37.                     if(is_string($toApply[0]) && is_string($toApply[1])) {
  38.                         $result = call_user_func($toApply, $result);
  39.                     } else if(is_object($toApply[0]) && is_string($toApply[1])) {
  40.                         $result = call_user_func_array($toApply, $result);
  41.                     }
  42.                 } else if(is_string($toApply)) {
  43.                     if(function_exists($toApply)) {
  44.                         $result = call_user_func($toApply, $result);
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         return $result;
  50.     }
  51.  
  52. }
  53.  
  54. ?>
  55.  

So, in essence, we have a simple content filtering system here. You can add a filtering rule, and you can call those rules at some point in the future on a piece of content. I’ll be creating more examples as time goes on, but for now, you should check out the demonstration page to see what happens so far.

The PluggableRules class is a simple wrapper for one private member and two functions that form the basic of the system. You start by adding a rule defined by a unique string. If the string hasn’t been used to define a rule type before, you create a new array to store that string’s rules in. Then, you simply add a callback function to the array of rules to apply when the rule is called.

Later, when you call the rule, it sees if any rules exist for the unique string passed. If some do, then it iterates over each rule, calling a function as appropriate and getting the result. It then returns the result to be used by your calling code.

There are numerous ways to improve this simple system. You could add priority based filtering, ensure that a callback is unique for a certain rule, and vary the number of arguments that are passed based on programmer input. I may do that at a later date, but I consider this exploration to have been a great way to learn a little bit more about PHP.

Reflections on my Final Year of College

I’ll be graduating from the Rose-Hulman Institute of Technology in less than a week. I’ve finished all my classes and I don’t have to take any finals, so this next week will be spent doing development work for clients and just relaxing.

While I’m excited about being done, a little part of me is really going to miss this school. I’ve made friends with so many people and I’ve learned so many things over the years. Every year has been good, but this year has been great for me. In this post, I’d like to share why that is.

Major Life Events

Many, many things happened over the course of the year that were life-changing in some way. The most important, at least in my opinion, is my engagement to my wonderful fiancee, Angela. While not much has changed practically, yet, there is a sense now that the course my life is going to take is a little more determined. That’s exciting to me.

I’ve also decided that my career will probably take a more entrepreneurial route. This was a decision that was a long time in coming and was partly decided by the fact that I don’t want to “work for the man.” I plan on doing a lot of contract development over the next couple of years and I have a bunch of different product ideas that I’m working on considering the feasibility and market for.

All About the People

I’ve lived in the same beautiful residence hall the last 3 years. Unfortunately, it has established a reputation as the “lame” hall that students should stay away from at all costs. Luckily, through a decided lack of residence hall space, we ended up with a great group of guys in my hall this year and I was able to make fast friends.

Josh in a backpackI’ve met so many people this year that I’m happy to call friends. They’re wild, wacky, studious, entertaining, mumbly, and sometimes even slightly fat. In spite of their differing qualities, though, all my friends are nice guys are heart. From our resident giant leprechaun to our experienced backpack occupant, all my hall mates were amazing guys. I’d like to extend my gratitude to my awesome RA, Justin for helping to bring us together through IM sports and other activities.

Of course, I’ve tried to keep up with my friends from the past, too. It’s hard, here at Rose-Hulman, to keep in contact with all the people that you’d like to over the years. Sometimes, it’s like you’re being pulled in a million different directions at once and if you don’t see someone in a class it’s hard to remain friends.

I’m happy to say, though, that I’ve been able to keep a friendship over the years that I made back when I was a sophomore. Hopefully, we’ll be able to keep in contact over the years as she heads to grad school and I move to Seattle.

Classes

As is normal here at Rose-Hulman, most of my time this year was spent in classes and doing homework for those classes. I feel that I’ve been blessed this year to have classes that I thought were interesting, challenging, and fun (for the most part.)

I learned how to design and implement a microprocessor and instruction set. I learned about XML databases and more about process and documentation that I ever thought I would learn in a formal setting. Straying from my technical education, I also had the chance to partake in a mock presidential election. I got to take my turn playing the media and writing biased news reports on our candidates.

In all, I enjoyed my classes this year, but I’m glad I won’t have to go to anymore. About 2/3 of the way through the year, I developed a great deal of apathy for actually going to my classes. I missed more class sessions this year than I did in the 3 previous years combined. I just felt that the classes weren’t worth my time when I could be doing other things and teaching myself new skills and technologies.

Final Thoughts

It’s hard to summarize an entire school year in one post. I love it here at Rose, but I’m happy to be moving on to (hopefully) bigger and better things. My classes and experiences here have taught me so much, and my gratitude toward the faculty, staff, and other students is immeasurable. I can only hope that I can take the education I’ve received and use it to transform my life and, possibly, transform the world.

Introducing Plugin-Developer.com

It is my distinct pleasure that I introduce the formal launch of a site that I’ve been working on for a while now. The site is Plugin-Developer.com, and it is the project that that I mentioned in my earlier post From Nothing to Profit in 40 Days.

What is Plugin-Developer.com?

I guess the question I get asked most frequently is “What is Plugin-Developer.com”? In essence, Plugin-Developer.com aims to serve a very unique niche in the realm of web development. There are a variety of software packages out there that make use of plugins to extend their core functionality. Many of these systems are open-source and I have programmed plugins for a good deal of them. As such, I feel I can leverage my experience and expertise to provide quality service.

Working to Success

As noted before, my success criteria is to bring this venture from nothing to profitable in 40 days. As stated before, I started on April 18th and have until May 28th to become profitable through the business. I also mentioned some of the expenses I had started to accumulate. Following is an updated list:

  • Domain Registration — $29.85
  • Logo Contest Fee — $39.00
  • Logo Contest Prize — $150.00
  • Coffee for Work Purposes — $50.00

That’s about $270.00 worth of stuff that I now need to earn back to get in the black. The question is, how am I going to do that? That’s where the plan comes in.

The Plan

I have a plan to drive targeted traffic to my website and, hopefully, get work. The first part of the plan involves advertising. I’m going to be using Adwords to attempt to find an audience that could make good use of the services I’m offering. The goal, obviously, is to get them to the quote page, where they can request for me to develop something.

The second part of the plan is to post frequently on web development forums and on the plugin forum at WordPress.org. If I can show my expertise, perhaps people will visit my site through my forum signature. Finally, I’ll be relying, somewhat, on word of mouth. I need to get people talking about my services, but I’m not entirely sure how at this point. That remains something to be determined.

Introducing Webmaster Talk

I’m a big fan of online communities. I rely on them for a lot of advice and I like to give back as much as I receive. When Earner’s Forum started a while ago, I was one of the first members, but school was killing me and I had to scale back my investment in the community. Now, I see that Earner’s Forum and Webmaster Talk are merging. Not only that, but they have a huge contest going on right now that runs through May 13th. While it is a little late to get started, the prizes are definitely worth aiming for.

I’ve been posting like crazy on the forums for the past day, and I’m already up to 50 posts today. Of course, there are people on the forum with over 5000 posts as of today, and posts are worth 2 points each. I’m obviously not going to catch up like this. I’ll have to resort to other means, like writing articles and getting blog posts.

I highly recommend that you signup for the forum and post there. I’m sure you’ll find it enlightening, and I’ll try to help you if I find you there. If you do sign up, please do so through my referral link. I appreciate it :)

WordPress Plugin Competition 2.5

Weblog Tools Collection recently announced the WordPress Plugin Competition 2.5, a chance for WordPress plugin developers to use their innovative capabilities to come up with new and useful plugins. There are always good prizes at stake, and I’m determined that I’m going to enter this time. I’d love to have a chance to win my share of some cash or hosting, like last competition’s winners did.

I already have more than a few ideas of what I might be doing, but I’m not completely sure yet. If you’d like to suggest some plugins that you think would be nice to see developed, then I’d be all for considering them. Please let me know in the comments here or by email via my contact form.