PHP Class
It is possible to implement a new API event (or plugin) by creating an apposite PHP file, containing a new class that inherits the methods defined by the E4J\VikRestaurants\API\Event
abstraction.
The class can be named as you prefer, there are no restrictions applied. It is good practice to always include PluginAPI
as class suffix.
Further details about the implementable methods are specified from the related articles under this handbook.
<?php
// No direct access
defined('ABSPATH') or die('No script kiddies please!');
use E4J\VikRestaurants\API\Event;
use E4J\VikRestaurants\API\Response;
class CustomEventPluginAPI extends Event
{
/**
* @inheritDoc
*/
public function getTitle()
{
return 'Custom Event';
}
/**
* @inheritDoc
*/
public function getShortDescription()
{
return 'This is the short description displayed within list containing all the installed API events.';
}
/**
* @inheritDoc
*/
public function getDescription()
{
return 'This is the extended description displayed within the details page of the API event.';
}
/**
* inheritDoc
*/
public function alwaysAllowed()
{
// in this case, the method implementation can be omitted
return false;
}
/**
* @inheritDoc
*/
protected function execute(array $args, Response $response)
{
/**
* @todo do stuff here
*/
}
}
Where should I place that file?
You can place that file wherever you want and name it as you wish. Just remember to automatically load it during the registration of the event provider, as described by the onStartVikRestaurantsAPI
hook.
Let's take the following plugin structure.
- plg_vikrestaurants_myplugin/
- myplugin.php
- events/
- custom_event_one.php
- custom_event_two.php
According to the structure of these files, both the events can be supported by using the following code, which should be placed within the main myplugin.php file.
// subscribe to the hook used to set up the API framework of VikRestaurants
public function onStartVikRestaurantsAPI(&$api)
{
// register an event provider to create the first event
$api->registerEventProvider('custom_event_one', function(string $event, array $options) {
// autoload the file ./events/custom_event_one.php, which must declare a class named as:
// CustomEventOnePluginAPI
require_once dirname(__FILE__) . '/events/custom_event_one.php';
// instantiate the class
return new CustomEventOnePluginAPI($event, $options);
});
// register an event provider to create the second event
$api->registerEventProvider('custom_event_two', function(string $event, array $options) {
// autoload the file ./events/custom_event_two.php, which must declare a class named as:
// CustomEventTwoPluginAPI
require_once dirname(__FILE__) . '/events/custom_event_two.php';
// instantiate the class
return new CustomEventTwoPluginAPI($event, $options);
});
}