implementors
3 days ago
plugins
3 days ago
api.php
3 days ago
autoload.php
3 days ago
error.php
3 days ago
event.php
3 days ago
index.html
3 days ago
response.php
3 days ago
user.php
3 days ago
event.php
161 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * The API event (plugin) representation. |
| 16 | * The classname of a plugin must follow the standard below: |
| 17 | * e.g. File = driver.php Class = VAPApiEventDriver |
| 18 | * e.g. File = driver_name.php Class = VAPApiEventDriverName |
| 19 | * |
| 20 | * @see VAPApiResponse |
| 21 | * |
| 22 | * @since 1.7 |
| 23 | */ |
| 24 | abstract class VAPApiEvent |
| 25 | { |
| 26 | /** |
| 27 | * The name of the event. Usually equal to the filename. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | private $name; |
| 32 | |
| 33 | /** |
| 34 | * Internal configuration registry. |
| 35 | * |
| 36 | * @var JObject |
| 37 | */ |
| 38 | private $options; |
| 39 | |
| 40 | /** |
| 41 | * Class constructor. |
| 42 | * |
| 43 | * @param string $name The name of the event. |
| 44 | * @param array $options A configuration array/object. |
| 45 | */ |
| 46 | public function __construct($name = '', $options = array()) |
| 47 | { |
| 48 | $this->name = strlen($name) ? $name : uniqid(); |
| 49 | |
| 50 | // create configuration registry |
| 51 | $this->options = new JObject($options); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the name of the event. |
| 56 | * |
| 57 | * @return string The name of the event. |
| 58 | */ |
| 59 | public function getName() |
| 60 | { |
| 61 | return $this->name; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the title of the event, a more readable representation of the plugin name. |
| 66 | * |
| 67 | * @return string The title of the event. |
| 68 | */ |
| 69 | public function getTitle() |
| 70 | { |
| 71 | return ucwords(str_replace('_', ' ', $this->name)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the description of the plugin. |
| 76 | * |
| 77 | * @return string An empty string. To display a description, |
| 78 | * override this method from the child class. |
| 79 | */ |
| 80 | public function getDescription() |
| 81 | { |
| 82 | return ''; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns true if the plugin is always authorised, otherwise false. |
| 87 | * When this value is false, the system will need to authorise the plugin |
| 88 | * through the ACL of the user. |
| 89 | * |
| 90 | * @return boolean Always false. To allow always this plugin, |
| 91 | * override this method from the child class. |
| 92 | */ |
| 93 | public function alwaysAllowed() |
| 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Event configuration getter. |
| 100 | * |
| 101 | * @param string $key The setting name. |
| 102 | * @param mixed $def The default value to use. |
| 103 | * |
| 104 | * @return mixed The setting value or the default one. |
| 105 | */ |
| 106 | final public function get($key, $def = null) |
| 107 | { |
| 108 | return $this->options->get($key, $def); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Event configuration setter. |
| 113 | * |
| 114 | * @param string $key The setting name. |
| 115 | * @param mixed $val The setting value. |
| 116 | * |
| 117 | * @return self This object to support chaining. |
| 118 | */ |
| 119 | final public function set($key, $val) |
| 120 | { |
| 121 | return $this->options->set($key, $val); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the event configuration as array. |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | final public function getOptions() |
| 130 | { |
| 131 | return $this->options->getProperties(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Perform the action of the event. |
| 136 | * |
| 137 | * @param array $args The provided arguments for the event. |
| 138 | * @param VAPApiResponse $response The response object for admin. |
| 139 | * |
| 140 | * @return mixed The response to output or the error message (VAPApiError). |
| 141 | * |
| 142 | * @uses doAction() |
| 143 | */ |
| 144 | final public function run(array $args, VAPApiResponse $response) |
| 145 | { |
| 146 | return $this->doAction($args, $response); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * The custom action that the event have to perform. |
| 151 | * This method should not contain any exit or die function, |
| 152 | * otherwise the event won't be properly terminated. |
| 153 | * |
| 154 | * @param array $args The provided arguments for the event. |
| 155 | * @param VAPApiResponse $response The response object for admin. |
| 156 | * |
| 157 | * @return mixed The response to output or the error message (VAPApiError). |
| 158 | */ |
| 159 | abstract protected function doAction(array $args, VAPApiResponse $response); |
| 160 | } |
| 161 |