rules
2 days ago
types
2 days ago
control.php
2 days ago
emptyloader.php
2 days ago
factory.php
2 days ago
field.php
2 days ago
index.html
2 days ago
loader.php
2 days ago
renderer.php
2 days ago
requestor.php
2 days ago
rule.php
2 days ago
factory.php
244 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 | VAPLoader::import('libraries.customfields.field'); |
| 15 | VAPLoader::import('libraries.customfields.rule'); |
| 16 | |
| 17 | /** |
| 18 | * VikAppointments custom fields factory. |
| 19 | * |
| 20 | * @since 1.7 |
| 21 | */ |
| 22 | final class VAPCustomFieldsFactory |
| 23 | { |
| 24 | /** |
| 25 | * Returns a list of supported custom fields types. |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public static function getSupportedTypes() |
| 30 | { |
| 31 | $types = array(); |
| 32 | |
| 33 | // load all files inside types folder |
| 34 | $files = glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'types' . DIRECTORY_SEPARATOR . '*.php'); |
| 35 | |
| 36 | foreach ($files as $file) |
| 37 | { |
| 38 | // extract type from file name |
| 39 | $type = preg_replace("/\.php$/i", '', basename($file)); |
| 40 | |
| 41 | try |
| 42 | { |
| 43 | // try to instantiate the type |
| 44 | $field = VAPCustomField::getInstance($type); |
| 45 | |
| 46 | // attach type to list |
| 47 | $types[$type] = $field->getType(); |
| 48 | } |
| 49 | catch (Exception $e) |
| 50 | { |
| 51 | // catch error and go ahead |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Trigger hook to allow external plugins to support custom types. |
| 57 | * New types have to be appended to the given associative array. |
| 58 | * The key of the array is the unique ID of the type, the value is |
| 59 | * a readable name of the type. |
| 60 | * |
| 61 | * @param array &$types An array of types. |
| 62 | * |
| 63 | * @return void |
| 64 | * |
| 65 | * @since 1.7 |
| 66 | */ |
| 67 | VAPFactory::getEventDispatcher()->trigger('onLoadCustomFieldsTypes', array(&$types)); |
| 68 | |
| 69 | // sort types by ascending name and preserve keys |
| 70 | asort($types); |
| 71 | |
| 72 | return $types; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns a list of supported rules. |
| 77 | * |
| 78 | * @return array |
| 79 | */ |
| 80 | public static function getSupportedRules() |
| 81 | { |
| 82 | $rules = array(); |
| 83 | |
| 84 | // load all files inside rules folder |
| 85 | $files = glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rules' . DIRECTORY_SEPARATOR . '*.php'); |
| 86 | |
| 87 | foreach ($files as $file) |
| 88 | { |
| 89 | // extract rule from file name |
| 90 | $rule = preg_replace("/\.php$/i", '', basename($file)); |
| 91 | |
| 92 | try |
| 93 | { |
| 94 | // try to instantiate the rule |
| 95 | $rule = VAPCustomFieldRule::getInstance($rule); |
| 96 | |
| 97 | // attach rule to list |
| 98 | $rules[$rule->getID()] = $rule->getName(); |
| 99 | } |
| 100 | catch (Exception $e) |
| 101 | { |
| 102 | // catch error and go ahead |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Trigger hook to allow external plugins to support custom rules. |
| 108 | * New rules have to be appended to the given associative array. |
| 109 | * The key of the array is the unique ID of the rule, the value is |
| 110 | * a readable name of the rule. |
| 111 | * |
| 112 | * @param array &$rules An array of rules. |
| 113 | * |
| 114 | * @return void |
| 115 | * |
| 116 | * @since 1.7 |
| 117 | */ |
| 118 | VAPFactory::getEventDispatcher()->trigger('onLoadCustomFieldsRules', array(&$rules)); |
| 119 | |
| 120 | // sort rules by ascending name |
| 121 | asort($rules); |
| 122 | |
| 123 | return $rules; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Dispatches a field rule. |
| 128 | * |
| 129 | * @param VAPCustomField $field The custom field instance. |
| 130 | * @param mixed $value The value of the field set in request. |
| 131 | * @param array &$args The array data to fill-in in case of |
| 132 | * specific rules (name, e-mail, etc...). |
| 133 | * |
| 134 | * @return void |
| 135 | */ |
| 136 | public static function dispatchRule(VAPCustomField $field, $value, &$args) |
| 137 | { |
| 138 | if (!$field->get('rule')) |
| 139 | { |
| 140 | // missing rule, do not go ahead... |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Trigger hook to allow external plugins to dispatch a custom rule. |
| 146 | * It is possible to access the rule of the field with: |
| 147 | * $field->get('rule'); |
| 148 | * |
| 149 | * @param VAPCustomField $field The custom field instance. |
| 150 | * @param mixed $value The value of the field set in request. |
| 151 | * @param array &$args The array data to fill-in in case of |
| 152 | * specific rules (name, e-mail, etc...). |
| 153 | * |
| 154 | * @return boolean True to avoid dispatching the default system rules. |
| 155 | * |
| 156 | * @since 1.7 |
| 157 | */ |
| 158 | $did = VAPFactory::getEventDispatcher()->true('onDispatchCustomFieldRule', array($field, $value, &$args)); |
| 159 | |
| 160 | if ($did) |
| 161 | { |
| 162 | // do not need to go ahead, a plugin did all the needed stuff |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | try |
| 167 | { |
| 168 | // create rule instance |
| 169 | $rule = VAPCustomFieldRule::getInstance($field->get('rule')); |
| 170 | } |
| 171 | catch (Exception $e) |
| 172 | { |
| 173 | // the rule has been probably added by a third party plugin, which does not |
| 174 | // need to dispatch a custom action |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // dispatch the rule |
| 179 | $rule->dispatch($value, $args, $field); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Renders a field rule. |
| 184 | * |
| 185 | * @param VAPCustomField $field The custom field instance. |
| 186 | * @param mixed $value The value of the field set in request. |
| 187 | * @param array &$args The array data to fill-in in case of |
| 188 | * specific rules (name, e-mail, etc...). |
| 189 | * |
| 190 | * @return null|string |
| 191 | */ |
| 192 | public static function renderRule(VAPCustomField $field, &$data) |
| 193 | { |
| 194 | if (!$field->get('rule')) |
| 195 | { |
| 196 | // missing, rule do not go ahead... |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Trigger hook to allow external plugins to manipulate the data to |
| 202 | * display or the type of layout to render. In case one of the attached |
| 203 | * plugins returned a string, then the field will use it as HTML in |
| 204 | * place of the default layout. |
| 205 | * |
| 206 | * It is possible to access the rule of the field with: |
| 207 | * $field->get('rule'); |
| 208 | * |
| 209 | * @param VAPCustomField $field The custom field instance. |
| 210 | * @param array &$data An array of display data. |
| 211 | * |
| 212 | * @param mixed The new layout of the field. Do not return anything |
| 213 | * to keep using the layout defined by the field. |
| 214 | * |
| 215 | * @since 1.7 |
| 216 | */ |
| 217 | $result = VAPFactory::getEventDispatcher()->trigger('onRenderCustomFieldRule', array($field, &$data)); |
| 218 | |
| 219 | // implode list of returned HTML |
| 220 | $input = implode("\n", array_filter($result)); |
| 221 | |
| 222 | if ($input) |
| 223 | { |
| 224 | // return the input fetched by the plugin |
| 225 | return $input; |
| 226 | } |
| 227 | |
| 228 | try |
| 229 | { |
| 230 | // create rule instance |
| 231 | $rule = VAPCustomFieldRule::getInstance($field->get('rule')); |
| 232 | } |
| 233 | catch (Exception $e) |
| 234 | { |
| 235 | // the rule has been probably added by a third party plugin, which does not |
| 236 | // need to display a custom input |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | // render the rule |
| 241 | return $rule->render($data, $field); |
| 242 | } |
| 243 | } |
| 244 |