Inheritance with MVC Starter Plugin for WordPress
Except for Models, all classes in MVC Starter Plugin for WordPress are extensions of the Parent Class, defined in plugin-name.php. This makes it possible to call methods like load_lib() from anywhere in the child classes. This is important also because MVCSP works with singletons whenever possible.
Yes, I’m aware of the war regarding singletons, but when you look at debug logs and see that WordPress reloads your plugin at least twice in some cases, you really want to make sure that you’re not doing things more often than you should. If you still want to avoid singletons in load_lib(), you can. Refer to the section on that method to know more.
Support for PHP < 5.3
MVC Starter Plugin for WordPress comes with a bootstrap() method that instantiates/serves singletons. It relies on the native function get_called_class(), which was made available in PHP version 5.3. For those few who still run on PHP 5.2 and under, I strongly recommend that you have your hosting company upgrade or switch hosting companies to a more serious one. If that’s not a possibility, you can still use this Starter Plugin because it implements its own get_called_class() if the native one doesn’t exist. Like all methods, it is slower than native calls, so it really is better if you upgrade to a modern PHP version.
In order to keep my code easily maintainable, I tend to follow these guidelines:
- Place add_action and add_filter calls in the __construct() of Controllers
- Make sure to check that admin-related logic is indeed running under the admin with checks to parent::is_admin().
- Do the same for front-end logic, checking that parent::is_admin() is false.
- Avoid having calls to non-controller methods inside the Parent Class constructor, with the odd exception of an abstract class that needs to be loaded beforehand.
- NEVER have a closing PHP tag at the end of any file. Nobody likes to deal with “headers already sent” errors caused by extra space or newlines that shouldn’t be there. Instead, use a comment to indicate that the file has finished.
/* End of file file-name.php */
/* Location: plugin-dir/file-name.php */
Let me know if there are any other best practices you’d like me to add here.
After you download the MVC Starter Plugin for WordPress, you’ll need to replace a few template variables across all existing files with values of your choosing. They are:
PLUGIN_NAME, PLUGIN_DESC, PluginClass, and <plugin-dir>
- PLUGIN_NAME: The actual name of your plugin. This will appear in the Installed Plugins screen and in the Admin -> Settings submenu.
- PLUGIN_DESC: The description of the plugin. This will appear in the Installed Plugins screen.
- PluginClass: The overall class of your plugin. MVCSP is Object Oriented and all Controllers, DAOs, and Views should extend the parent class.
- <plugin-dir>: MVCSP borrowed the end-of-file style from CodeIgniter, in the sense that it does not close PHP tags but instead has a comment to indicate that the file is not truncated. The <plugin-dir> tag is only used as part of that comment.
The Set-Up Helper Script
There are several methods to replace the template variables with your desired values. I personally like to use grep or find piped into a Perl one-liner. To make things easier, I included a setup.pl script which takes a few parameters and runs some shell commands in the background. This only works for people using Mac or Linux machines, or probably Windows with Cygwin (not having used Windows in years, I haven’t had a chance to test it). Run it without any parameters and this is what you get:
Usage: perl ./setup.pl --long-name="The Plugin Name" --desc="Full Plugin Description" --class-name="DesiredPHPClassName"
All 3 parameters are required. Use a valid class name or your plugin will throw an error during activation.
The parameters are self-explanatory.
Note: Once you’ve run the setup.pl script, the file plugin-name.php will be renamed to whatever name of the domain is. So suppose you extract the Starter Plugin zip file contents in to a directory called my-plugin, the file plugin-name.php will be renamed to my-plugin.php. WordPress will then have my-plugin/my-plugin.php as the plugin signature.
That’s all there is to setting up the Starter Plugin. You can now activate it in your Admin -> Installed Plugins.
If you want to set up your plugin to run tests, refer the Running Tests section.
A Little Background
This is the seventh anniversary of UseStrict Consulting. It was born out of my passion for Perl, long before I ever thought of writing plugins for WordPress.
A lot has changed since then – I fell in love with building WordPress plugins in 2011. Like with Perl back in 1998, it started as an itch that needed scratching. My first plugin wasn’t a simple task. I wanted to get live shipping rates from Canada Post to use on my wife’s online pharmacy running eShop. The end result was messy, but it worked… and still does!
Fast forward 4 years. Enter the MVC Starter Plugin for WordPress.
MVC Starter Plugin for WordPress
The MVC Starter Plugin for WordPress is the result of best practices adopted from CodeIgniter, Catalyst, and of course, the WordPress Codex. It is a robust Object Oriented Model/View/Controller framework for WordPress plugins. I haven’t released it to Github yet, as it’d be nice to get some peer feedback before I do that.
The framework comes with a few handy methods and classes, as well as test files which can serve as templates for your own tests. They are designed to work with the WordPress Unit Test Suite and phpunit. I’ll be adding the documentation below as time permits, but feel free to download MVC Starter Plugin and play around with it!
- Set Up
- The Parent Class
- Method: bootstrap()
- Method: __construct()
- Method: is_admin()
- Method: load_lib()
- Method: load_all()
- Method: render_template()
- Method: set_env()
- Method: get_env()
- Method: log_msg()
- Admin Notices Controller
- The Notice Pool
- Method: set_notice()
- The Settings API
- Setting Controller Class
- Settings DAO and Model Classes
- The Settings View
- The Ajax Controller
- The Ajax Request Model