
In Part 1, we looked at planning out our plugin.
Part 2 was all about making the tables that would support it.
In Part 3 we began work on our admin panel.
Today, we look at creating a class to help organise our code better.
Including The Class
I’m going to keep all of my class declarations in a separate file. This file will need to be included in ‘tabberlist-main.php’, so I add the following lines just under the commented metadata and license information. While I’m at it, I may as well go ahead and create a new instance of this class.
include('classes/tabberlist-class.php');
$tabberlist = new tabberlistclass;
That’s all that needs to be done here. This instance should now be available to us through the rest of the program. Or it would be, if the class actually existed yet. Better go and create it.
I open up a new text file and save it as ‘tabberlist-class.php’. I like to keep my code ultra-organised, so it gets its own folder (’classes’) too. Overkill? Maybe, but who knows what features I’ll want to add later.
In this new file, I start by making sure that this class only gets declared once (in the same way I did for the functions in ‘tabberlist-main.php’).
<?php
if ( !class_exists('tabberlistclass') ) {
class tabberlistclass {
}
}
?>
We declare the class using the ‘class’ syntax. Notice that there are no brackets after ‘tabberlistclass’.
Class Terminology
Classes don’t have variables and functions. They have attributes and methods. For our purposes, an attribute is basically a variable and a method is basically a function.
They have different names because of the way object orientated programming works. A class is an object – a discrete ‘thing’, like, for example, a ball. This object has attributes in the same way as a ball has a size and a shape. It also has methods, which can be thought of like verbs – in this case, our ball can bounce or roll.
An ‘instance’ (which we declared above) is a copy of our ball. We can make as many copies as we like, and each of these copies can have attributes applied to them. We could have different coloured or different sized balls. However, they would all share the same methods – they would all be able to roll or bounce.
For this plugin, creating a class is probably unnecessary. However, it does help to keep everything organised and it also serves as a protection against function conflicts.
If I declare a method within my class and it happens to be called ‘testFunction()’, but there’s already a function that’s been declared elsewhere in my code called ‘testFunction()’, then no conflicts arise. A good naming convention should prevent conflicts anyway, but this is serves as an extra layer of protection.
Display Methods
In order to keep our admin display code clean, I put all of the dynamic functionality into methods within the ‘tabberlistclass’ and then call them through the instance.
So, I open up the ‘tabberlist-admin.php’ file and make it look like this:
<?php
global $tabberlist;
if (isset($_POST['tabberListSave'])) {
$tabberlist->update();
}
?>
<div class="wrap">
<div id='icon-tools' class='icon32'></div>
<h2>TabberList Admin</h2>
<p class='description'>Welcome to TabberList. Select a skin, and start entering makin' some lists!</p>
<form name="form_development" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">
<?php $tabberlist->selectSkin(); ?>
<?php $tabberlist->getTitle(); ?>
<div id='tabberlistOuterTabs' class='tabberlistAdminTabs'>
<ul>
<li><a href='#categories'>Edit Categories</a></li>
<li><a href='#list'>Edit Lists</a></li>
</ul>
</div>
<div id='categories' class='clear tab'>
<p class='description'>Use the field below to add a new category. To delete categories, check the boxes next to the categories you would like to delete and hit "Save Changes". To have a category's items not counted in the final total, uncheck the box in the "Counted" column.</p>
<table class='form-table adminTabberTable'>
<tr valign='top'>
<th scope='row'><label for='newCat'>Add New Category</label></th>
<td><input type='text' name='newCat' id='newCat' size='60' /></td>
</tr>
</table>
<?php $tabberlist->tabberListDisplayCats(); ?>
</div>
<div id='list' class='clear tab'>
<h3>Current Categories:</h3>
<div id='tabberlistAdmin' class='tabberlistAdminTabs'>
<?php $tabberlist->adminCatTabs(); ?>
<?php $tabberlist->adminCatDivs(); ?>
</div>
</div>
<p class="submit">
<input type="submit" name="tabberListSave" id="tabberListSave" class='button-primary' value="Save Changes" />
</p>
</form>
</div>
The first thing we have to do is bring the $tabberlist instance into this file (remember, this file is being called from within a function in our ‘tabberlist-main.php’ file), which we do by using ‘global’. This gives us access to this instance and, therefore, each of the methods within the tabberlistclass.
I’m going to focus on one method which should give you an idea of how this all works:
<?php
global $tabberlist;
if (isset($_POST['tabberListSave'])) {
$tabberlist->update();
}
?>
To call a method within a class, we use the following syntax: ‘$instance->method();’ (to call an attribute, we would use ‘$instance->attribute;’ – note the lack of parenthesis). So, in this case we are calling the ‘update()’ method within the $tabberlist instance.
Moving back into the ‘tabberlist-class.php’ file, we need to declare a method that will take care of all of the updating for us.
<?php
if ( !class_exists('tabberlistclass') ) {
class tabberlistclass {
function update() {
}
}
}
?>
Currently, this does nothing but it has the potential to do a whole lot. From within this method, I can call a whole bunch of other methods that perform all of the data processing. For example, I could have a method that adds all the new categories to the database. It would look like this:
<?php
if ( !class_exists('tabberlistclass') ) {
class tabberlistclass {
function update() {
$this->dbAddCats();
}
function dbAddCats() {
// Code to add categories to database
}
}
}
?>
Notice the use of the ‘$this’ syntax. This tells the program that the required method is within this class.
By using a class, I can keep all of my admin panel display code free from the data processing code. This can be very helpful for debugging – if something doesn’t look right, then I go to the ‘tabberlist-admin.php’ file and debug the display.
If, on the other hand, something isn’t behaving right, then I know to look within the ‘tabberlist-class.php’ file and find out what’s causing the error.
In the final part of this series, we’ll look at using the shorttags functions built into WordPress that allows the user to quickly and easily add our lists to their posts. Subscribe to my RSS feed to make sure you don’t miss it!
So what to you think? Leave a comment and let me know!
In
Part 2 of my originally-titled Wordpress Plugin Development series continues. Today: plugin setup and database structures.