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.
-
-
<?php
-
-
class PluggableRules {
-
-
/**
-
* Allows a rule to be added to the set of rules to call when a certain filter is applied.
-
*
-
* @param string $filter the name of the filter to add the rule to.
-
* @param mixed $function an element specifying the function that should be used when
-
* the rule is applied. The element can be a string function name, an array consisting
-
* of a class name and a function name, or an array consisting of an object and function name.
-
*/
-
}
-
-
PluggableRules::$rules[$filter][] = $function;
-
}
-
-
/**
-
* Applies the functions previously defined across the filterable content
-
* for the specific named filter.
-
*
-
* @param string $filter the name of the filter to call.
-
* @param mixed $filterable the item to apply the filters to.
-
* @return mixed the result of applying the filter.
-
*/
-
$result = $filterable;
-
-
foreach(PluggableRules::$rules[$filter] as $toApply) {
-
}
-
}
-
}
-
}
-
}
-
return $result;
-
}
-
-
}
-
-
?>
-
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.
I’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