WordPress Plugin – Further deveopment

Now that I have a very simple working plugin it’s time for a little clean up.

Before I start adding addition functions and build additional features in messy procedural style it makes sense at this state to make it object orientated. WordPress continues to support PHP 4.3 and upwards, hence the plugin should also support PHP 4 configurations.

Here is the class made from the original function, variables have been moved to the object’s global scope, the hooks are included in the constructor and the Render() function now contains the regex replacements:

[code lang=”php”]class WP_Thumbshot_Preview {

/**
* Which mode to use, free or paid.
*
* @var string
*/
var $mode = ‘free’;

/**
* The user id (for paid mode).
*
* @var integer
*/
var $cid;

/**
* The image width
*
* @var integer
*/
var $width;

/**
* The image height
*
* @var integer
*/
var $height;

/**
* Constructor
*
* @return void
*/
function WP_Thumbshot_Preview() {
add_filter(‘the_content’, array(&$this, ‘Render’));
}

/**
* Render the content
*
* @param $content string
* @return string
*/
function Render($content) {
// Free mode
if ($this->mode == ‘free’) {
$content = preg_replace(“/\[preview\](.+?)\[\/preview\]/i”,

$1“,
$content);
}
// Paid mode
if ($this->mode == ‘paid’) {
$content = preg_replace(“/\[preview\](.+?)\[\/preview\]/i”,
cid&v=1&w=$this->width&h=$this->height&url=$1\” />
$1“,
$content);
}
return $content;
}
}[/code]

This is much tidier than before 🙂

Now to make everything work all that is required is an instance of the object:

[code lang=”php”]new WP_Thumbshot_Preview();[/code]

Share

Leave a Reply