I’m currently working on a plugin, which uses the builtin cron functionality of wordpress.
Instead of using PHP4, I want to use PHP5 with inheritance and the singleton design pattern. I run in some limitations with the action & filter hook mechanism and wanted to ask, if you have an other approach for this problem.
I’ve stripped out all not relevant parts from the example code. If you need the full code for demostration, please post here so I can add an pastbin link.
Example:
In the base class “Cron” I want to declare all methods, which describes a cron job. To register a new cron-entry I use add_action with the hookname and a callable method.
The problem:
To call this action-hook, the builtin wordpress function do_action needs access from outside to this method. To solve the problem, I declared the method as public static.
But this doesn’t seems right either. The method should not be accessable from outside. It should be private. Only the MyCron class should have the permission to access this method. I tried different things as you can see in the comment block in the example.
Only if I declare the constructor public (which would destroy the singleton pattern), I’m able to set the method to private. But then, everytime I add an event, a new MyCron object would be created.
Any suggestions?
Thanks,
Roman
class Cron {
private $_events;
protected function __construct() {
$this->_events = array();
}
protected function addEvent($timestamp, $recurrence, $hookname, $hookmethod, $args = array()) {
$this->_events[] = $hookname;
if (!wp_next_scheduled($hookname)) {
wp_schedule_event($timestamp, $recurrence, $hookname, $args);
}
add_action($hookname, $hookmethod);
}
}
class MyCron extends Cron {
private static $_instance = null;
public static function getInstance() {
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
protected function __construct() {
parent::__construct();
/* Note:
* The passing action hook must be public static to be callable from outside. An other option is to declare the constructor
* public. But this means, everytime I add e new event I will create an new object of MyCron. This is the reason why I
* the singleton php design pattern with a private/protected constructor.
*
* This doesn't work either:
* 1) $this->addEvent(time(), 'daily', 'webeo-daily-event', 'MyCron::getInstance->myDailyEvent');
* 2) $this->addEvent(time(), 'daily', 'webeo-daily-event', 'MyCron::getInstance::myDailyEvent');
* 3) $this->addEvent(time(), 'daily', 'webeo-daily-event', array('MyCron::getInstance', 'myDailyEvent'));
* 4) $this->addEvent(time(), 'daily', 'webeo-daily-event', create_function('', 'return MyCron::getInstance()->myDailyEvent();'));
* 5) $this->addEvent(time(), 'daily', 'webeo-daily-event', function() { return MyCron::getInstance()->myDailyEvent(); });
*/
$this->addEvent(time(), 'daily', 'webeo-daily-event', array($this, 'myDailyEvent'));
}
public static function myDailyEvent() {
// do this every day with wp cron
}
}
You donât have to declare the constructor public, just the action hook function.
Example:
Anyway, I wouldnât use a Singleton. 🙂