English (United Kingdom)

Custom Cron Jobs

Cron Jobs Development

This section is only for PHP experts. The documentation below shows the requirement of the Class File to extend the Cron Jobs Framework of the plugin. By creating a PHP file that declares the class below, you will gain access to the whole framework and you will be able to do any sort of operation by executing queries on the database and by launching commands directly from your own PHP file.

The Jobs that can be executed by VikBooking, after scheduling them with your Cron Server Utility, must be placed in the directory below:

/wp-content/plugins/vikbooking/admin/cronjobs/

Follow the schema below to see the structure of the Class File:

Cron Jobs Schema
<?php
defined('ABSPATH') or die('No script kiddies please!');

class VikCronJob {
	
	public $cron_id;
	public $params;
	public $debug;
	private $checktype;
	private $cron_data;
	private $flag_char;
	private $exec_flag_char;
	public $log;
	
	/**
	* This method defines the parameters of the job for the back-end setup
	*/
	static function getAdminParameters () {
		return array(
				'cron_lbl' => array('type' => 'custom', 'label' => '', 'html' => '<h4><i class="vboicn-mail4"></i><i class="vboicn-alarm"></i>Your Custom Cron Job</h4>'),
				'checktype' => array('type' => 'select', 'label' => 'Execution Type:', 'options' => array('checkin', 'payment', 'checkout')),
				'remindbefored' => array('type' => 'number', 'label' => 'Remind Type://Days in Advance from Check-in/Check-out', 'default' => '2', 'attributes' => array('style' => 'width: 60px !important;')),
				'subject' => array('type' => 'text', 'label' => 'Subject:', 'default' => 'eMail Subject'),
				'tpl_text' => array('type' => 'textarea', 'label' => 'Message:', 'default' => 'Dear Customer,'."\n".'This is an automated message to remind you...', 'attributes' => array('id' => 'tpl_text', 'style' => 'width: 70%; height: 80px;')),
		);
	}
	
	/**
	* The constructor method requires two arguments: the ID and the parameters array of the job coming from the back-end setup
	* VikBooking will do everything by itself but this method must have a similar structure to this example
	*/
	public function __construct ($cron_id, $params = array()) {
		$this->cron_id = $cron_id;
		$this->params = $params;
		$this->debug = false; //debug is set to true by the back-end manual execution to print the debug messages
		$this->checktype = $params['checktype'] == 'payment' ? 'payment' : ($params['checktype'] == 'checkout' ? 'checkout' : 'checkin');
		$this->cron_data = array();
		$this->flag_char = array();
		$this->exec_flag_char = array();
		$this->params['remindbefored'] = intval($this->params['remindbefored']);
	}
	
	/**
	* This method must always exist. It's the main method called by the framework
	*/
	public function run () {
		$dbo = JFactory::getDBO();
		if(!($this->params['remindbefored'] > 0)) {
			$this->log = 'Days in Advance: empty or invalid';
			return false;
		}
		//Set the cron job data in the class variable $this->cron_data
		$this->getCronData();
		//do your queries
		//.......
		
		//output debug messages for the manual execution from the back-end
		if($this->debug) {
			echo '<p>The job is running</p>';
			echo '<span>my command is ....</span>';
		}

		//Set logs for the Execution Logs
		$log_str = '';
		$log_str .= 'No bookings found....';
		if(!empty($log_str)) {
			$this->log = $log_str;
		}
		//you do not need to return anything, just execute what's necessary
		return true;
	}
	
	/**
	* This method must always exist. It's called after the cron has been executed so after the run() method
	*/
	public function afterRun ($extra = array()) {
		$dbo = JFactory::getDBO();
		$log_str = '';
		if(strlen($this->log) && count($this->cron_data) > 0) {
			$log_str = date('c')."\n".$this->log."\n----------\n".$this->cron_data['logs'];
		}
		//this variable could be populated from other class-variabled set by the run() method.
		//Useful to store information in JSON format about some Booking IDs that were notified by this execution
		$new_flag_str = '';

		//update cron record
		$q = "UPDATE `#__vikbooking_cronjobs` SET `last_exec`=".time().(!empty($log_str) ? ", `logs`=".$dbo->quote($log_str) : "").(!empty($new_flag_str) ? ", `flag_char`=".$dbo->quote($new_flag_str) : "")." WHERE `id`=".(int)$this->cron_id.";";
		$dbo->setQuery($q);
		$dbo->Query($q);
	}

	/**
	* This method is optional but this example uses it in the run() method
	* Useful to get the flag_char from previous executions
	*/
	private function getCronData() {
		$dbo = JFactory::getDBO();
		$q = "SELECT * FROM `#__vikbooking_cronjobs` WHERE `id`=".(int)$this->cron_id.";";
		$dbo->setQuery($q);
		$dbo->Query($q);
		if($dbo->getNumRows() == 1) {
			$this->cron_data = $dbo->loadAssoc();
			if(!empty($this->cron_data['flag_char'])) {
				$this->flag_char = json_decode($this->cron_data['flag_char'], true);
			}
		}
	}

	/**
	* Build here any other needed method. You have full access to the whole framework
	*/
	
}

?>
This site uses cookies. By continuing to browse you accept their use. Further information