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.

From Nothing to Profit in 40 Days

As I near the end of my college career, I think I’ve finally decided which route I’ll be taking in the coming months, and hopefully years. My strategy is three-pronged, but in this post, and subsequent ones in this series, I’d like to elaborate on one of the less conservative approaches I am taking.

Ever since I took a job last year as an internet contracter, a position that was literally “work from anywhere,” I’ve been hesitant about going back to work for someone where I have to go to an office and have someone else dictate where I have to do my work. I think work should be based on results, and sitting in a cubicle somewhere isn’t going to help me be more productive.

As such, I’ve been thinking a lot about how to manage working from home full time. I consider myself a talented web developer and code slinger, so I knew I could leverage that to build a business. The problem stems from the fact that there are countless numbers of web developers out there, many of whom would be cheaper than me. I knew I needed to find a niche to fill, and that’s when inspiration struck me.

Most of my web development work has been writing plugins for popular open-source publishing packages like WordPress and Drupal. That’s certainly a niche area, and plugin development is something that is in high demand right now as individuals attempt to grow their online presence. As such, I’ve determined that I can make an impact, at first, as just a plugin developer.

I’m determined to make sure this isn’t another one of my ideas that goes for naught. As such, I’ve made a guarantee to myself that I will go from nothing to profit with this business venture in 40 days. That gives me enough time to get my product to market and in front of an audience that demands my services, and it also gives me enough time to complete a few projects that can earn me some much needed cash.

I’ll be chronicling this journey right here on this blog. To start, I’d like to summarize my current state of affairs. I started this project on April 18, 2008 by registering three domains that I couldn’t believe weren’t already taken. In the last two weeks I’ve been developing the website for this business venture, and I’m almost ready to push it live. I just need a logo developed and I’m good to go.

So far, my expenses have been as follows:

  • Domain Registration — $29.85

I fully expect these expenses to increase as I go. I need to start using some type of project management tool, but I’m not sure what it will be yet. I’m investigating my options right now. I also need to get around to registering my business as an LLC, but that will probably wait until after the 40 days are over. That makes it easier to claim a profit, at least. Finally, I’ll have to do some marketing to get my name and website out there, so to speak. I expect to spend at least $150 on that, targeting it appropriately.

In the next post, I’ll be revealing my website to the world and talking about the marketing that I’m doing, as well as posting about any progress I’m making as far as project goes.

2007 Year In Review

Seeing how it is now March of 2008, I decided it was probably about time to break down my life in 2007 in some detail. I achieved a lot and had some really important life events that I think are of particular noteworthiness.

Personal

2007 was a big year for my personal life. First, and most importantly, I got engaged to my one true love, Angela. That happened on December 18, our 3 year anniversary of dating. To me, this was something that will stand out forever in my life: standing at the top of the Sears Tower and asking the woman that I love to marry me.

In other personal areas, I made more friends in 2007 than I think I have during any other part of my life. My dormitory floor, Blumberg 2 at Rose-Hulman is the best group of guys that I can imagine hanging out with. Its a really talented bunch, and I’m happy to call most of them good friends of mine.

Finally, I achieved a number of fitness goals that I set for myself at the beginning of the year. Through a dedicated plan of exercise and nutrition, I was able to throw up 300 pounds on bench press, lift 515 on deadlift, and squat 395. I also was able to lower my bodyfat to single digits for the first time in my life. Almost every fitness goal I set at the beginning of the year was fulfilled.

Business

In 2007, I realized the style of work I want to do the rest of my life. From February of 2007 through September of 2007, I worked as a contract web developer for a really great company, Power Advent. Most of my time was spent on one project, and I learned more about web development in that 6 month period than I had in the 2 years prior.

From this experience, I’ve realized that I want to work from home once I graduate. I want to continue telecommuting and doing software development. It can be desktop or web development, but the feeling of freedom that working from home and working at my own (generally quick) pace was the most fun I’ve ever had doing work. As such, I’m attempting to set myself up to continue the same type of work.

Continuing to reference the programming realm, I learned a lot about numerous programming languages. My Java skills increased a point where I believe I could do an adequate job of desktop development with the language. I’ve gained experience with a number of PHP open source projects and frameworks, as well as increasing my overall knowledge of the structure of the language and the tricks you can use. Finally, I’ve spent the last couple of months dedicating myself to learning C#. While I would say I’m generally proficient with the language, I’m not exactly an expert yet. There are a lot of tricks, like anonymous delegates for method prioritizing, that I haven’t learned to use effectively yet. However, I think that WPF and C# 3.0 are the forefront of desktop development for the Windows platform, and I have dedicated myself to learning these things.

Look for my follow up post on my 2008 goals.