SLASH Architecture – My approach to building WordPress plugins

I’ve fallen into a routine when building WordPress plugins; a few general rules are:

  • Avoid creating new PHP globals.
  • Avoid executing PHP code in the global scope.
  • Generous use of Actions and Filters.

I’ve decided to name the pattern I use Slash, which stands for:

  • Singletons
  • Loaders
  • Actions
  • Screens
  • Handlers

Singletons

I didn’t love singletons in PHP at first, and I’m still not convinced they are ideal, but they work well at avoiding yet-another-WordPress-related Global variable. bbPress is a good example.

The benefit of using PHP singleton’s in WordPress is they don’t create an additional PHP global to worry about getting stomped. They also allow your plugin to always exist in memory, without another plugin being able to unset it, while still working like a traditional PHP class.

Loaders

These are basically bootstrap functions. Their only responsibility is to jumpstart a singleton, and put it in a place for easy access. See the bbpress() function in the above bbPress link. Loaders allow you to reference your Singleton without needing to use a global variable, or pass an instance around into functions and method calls. Used like: bbpress()->foo = 'bar';

Actions

By hooking everything into WordPress core actions, bbPress never runs any code inline in the global scope, other than the above loader function. Also, bbPress comes with it’s own sub-actions to piggyback WordPress’s, making writing bbPress specific plugins safe and simple.

This also includes WordPress Filters. The major difference with Filters is that they are generally more risky to use; you have to assume other plugins are already using them, and those same plugins aren’t designed to handle whatever you’re trying to do too.

Screens

Derived from BuddyPress, screens are basically “Views” if you’re familiar with MVC. They are how we output markup to the browser from inside PHP, WordPress, and our plugins. Screens can be modular (again, see bbPress) allowing them to work with shortcodes, widgets, and template parts. Screens typically contain all of the translatable strings and HTML markup, and sanitize any variables for output.

Handlers

Handlers are the equivalent of controllers in MVC. They are responsible for containing the logic that comes from requests like forms, AJAX, JSON, XML-RPC, etc… They “handle” whatever the request is, performing capability checks, validating input data, and also checking for nonces.

Why not use MVC?

Honestly? No reason. Slash isn’t intended to compete or replace anything, and like anything else it’s constantly evolved over time to become what it is today, and will likely change tomorrow too. MVC and other architectures work really well, and Slash is just an approach that’s worked well for me. Putting a name on the routine should help it grow, or educate me on better approaches.

It’s also worth noting that Slash isn’t really anything new, but rather an assembly of separate methodologies combined into a single process that helps me translate my thoughts into code.

The best way to see the Slash approach in action is to browse through the BuddyPress and bbPress codebases. If you’re an experienced developer, I’m always looking for feedback. If you’re just starting out, maybe give the Slash approach a try. Take what’s in bbPress and remix it for your own use, and let me know how it goes.

PHPUnit, MAMP Pro 2.0, Netbeans 7.1, OS X

At the time of this writing, MAMP Pro 2.0.5 comes with a corrupted pear.conf file for PHP version 5.3.6, so I had to do some digging to install PHPUnit. You should be able to open up terminal and safely run the following commands to get PHPUnit installed and working correctly:

rm /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf
/Applications/MAMP/bin/php/php5.3.6/bin/pear channel-update pear.php.net
/Applications/MAMP/bin/php/php5.3.6/bin/pear upgrade pear
/Applications/MAMP/bin/php/php5.3.6/bin/pear channel-discover pear.phpunit.de
/Applications/MAMP/bin/php/php5.3.6/bin/pear channel-discover pear.symfony-project.com
/Applications/MAMP/bin/php/php5.3.6/bin/pear install phpunit/PHPUnit

Now that it’s installed, give it a try and see if it responds:

/Applications/MAMP/bin/php/php5.3.6/bin/phpunit --version

To run phpunit directly:

mv /Applications/MAMP/bin/php/php5.3.6/bin/phpunit /usr/local/bin/phpunit

Note: *you may need to sudo to run the mv command, like so:

sudo mv /Applications/MAMP/bin/php/php5.3.6/bin/phpunit /usr/local/bin/phpunit

* after which you will be propted for you password

One last test for good measure:

phpunit --version

Great! You’ve successfully installed PHPUnit in OS X using pear bundled with MAMP Pro 2.0.5. What about Netbeans integration?

  1. Open Netbeans and visit your Preferences
  2. Select: PHP
  3. Select: Unit Testing
  4. Click: Browse
  5. Navigate to: /usr/local/bin/
  6. Select: phpunit

Boom. From here on in you’re all setup with Netbeans talking to PHPUnit safely installed via MAMP Pro’s pear. For more information on what Netbeans can do with PHPUnit, check out the knowledgebase page over at Netbeans.org.

Circles

There’s a reason why Google chose circles for its new Google + social graph, and it isn’t just because it’s a clever name. If you search Google’s archive of images for the phrase “social graph” every single relevant image that comes up portrays relationships as being circular.

In the screen shot above, it’s clear that we humans seem to naturally illustrate our relationships as being… rounded. We live our lives constantly juggling these relationships with friends, partners, family, and colleagues in an intricate network that is unique to us. What better way to depict these predictably random dynamics than with a shape that has absolutely no bias towards any direction or alignment – the circle.

Circle of Trust

It's not you, it's me... Wait no it's totally you.

Up until now, we’ve been forced to map these relationships within rigid digital constructs built by developers (see: me) to make it as easy as possible to manage that data as it scales into the millions of relationships.

It’s easy to lump people into classes and demographics and groups and categories because we receive (false) positive reinforcement from doing it, and quantifying these relationships by putting them into boxes makes them feel real.

       You are my: [ ] Friend [ ] Family [ ] Pet [ ] Dinner

Since the first time someone drew a recognizable picture of someone else, having something we can go back to and look at and touch and say “Yeah, that person exists and we are important to each other!” has been an evolutionary significance in our development that technology hasn’t been able to properly convey quite yet. With every emerging social platform that comes around we get one step closer to translating the complexities of our relationships in ways that actually make sense, beyond being friends and followers.

If nothing else, Google + (and Circles specifically) is a reminder that our lives are ever-changing and the people we know now and will meet tomorrow don’t belong in any rigid box. If you are my friend, I appreciate my judgmental classification of you in my life just a wee bit more today than I did yesterday.

OSX, HostName, and FiOS

Flash-back to 2004 when Matt figured out how to change the computer name on his new Mac; now flash-forward to today when I need to do the same thing, and his method didn’t work. Also tried Richard Bronosky’s method, and still no joy.

If you have FiOS, it could be Verizon’s fault.

I needed to change the DHCP Client ID:

Network > Settings > Advanced

…and then I needed to rename the device at the router too, otherwise terminal would show my HostName as ‘new-host’ even after the above changes…

Router Config >My Network > Rename

It’s possible I didn’t need to do all of these things, but I did anyways and finally got rid of that pesky ‘new-host’ name.

Verizon FIOS, AirPort Extreme, and Ken

Having just moved into my new apartment in Providence, Rhode Island, I was elated to discover that Verizon FIOS was an available option for my television and internet needs. If you’ve never heard of FIOS, it’s essentially a fiber optic cable fed directly into your home capable of providing ludicrous speeds and outstanding response time.

Needless to say I opted for their fasted package. 35Mb Up/Down. This is my result:

This is my result

Verizon FIOS - Speedtest.net results

Now we’re cooking with lasers! So far so good… but after getting everything hooked up and configured, there were a few random bits that weren’t working; namely MobileMe, Back to my Mac, and Synergy.

Thanks to Ken Yegelski’s really awesome blog post over at his blog Just One Point of View, I was up and running in a snap. His trick involved diving into the modem/router combo that Verizon provides you, and configuring the IP address range to accomodate for the 10.0.x.x IP’s that OSX is used to, and putting the AirPort Extreme into Bridge Mode.

Dead simple, and almost totally effective.

The only thread left hanging for me, is Synergy. Synergy is software installed on multiple machines that run over a network in a client/server fashion, allowing to share a keyboard and a mouse with each other. Previous to having FIOS, everything worked fine; now, my keyboard doesn’t share. Mouse works fine, just no keyboard.

So consider this post a giant thanks and hat-tip to Ken, and a signal flare for experience on configuring Synergy/SynergyKM between OSX and Windows7.