dispatcher.php
143 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.sms |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 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 | JLoader::import('adapter.sms.driver'); |
| 15 | |
| 16 | /** |
| 17 | * Abstract factory used to instantiate different SMS drivers. |
| 18 | * The example below describes how to hook the drivers to this dispatcher. |
| 19 | * |
| 20 | * add_action('load_sms_driver_myplugin', function(&$drivers, $driver) |
| 21 | * { |
| 22 | * if ($driver == 'clickatell') |
| 23 | * { |
| 24 | * JLoader::import('driver.clickatell', MYPLUGIN_BASE); |
| 25 | * $drivers[] = 'MyPluginSmsClickatell'; |
| 26 | * } |
| 27 | * }, 10, 2); |
| 28 | * |
| 29 | * It is mandatory to indicate also the number of accepted arguments. |
| 30 | * |
| 31 | * @since 10.1.30 |
| 32 | */ |
| 33 | class JSmsDispatcher |
| 34 | { |
| 35 | /** |
| 36 | * A list containing all the driver instances. |
| 37 | * |
| 38 | * @var JSmsDriver[] |
| 39 | */ |
| 40 | protected static $instances = array(); |
| 41 | |
| 42 | /** |
| 43 | * Provides a new instance for the specified arguments. |
| 44 | * |
| 45 | * @param string $plugin The name of the plugin that requested the driver. |
| 46 | * @param string $driver The name of the driver that should be instantiated. |
| 47 | * @param mixed $order The details of the order that has to be notified. |
| 48 | * @param mixed $config The driver configuration array or a JSON string. |
| 49 | * |
| 50 | * @return JSmsDriver |
| 51 | * |
| 52 | * @throws RuntimeException In case the requested driver doesn't exist. |
| 53 | */ |
| 54 | public static function getInstance($plugin, $driver, $order = array(), $config = array()) |
| 55 | { |
| 56 | if (substr($driver, -4) == '.php') |
| 57 | { |
| 58 | // make sure the driver doesn't contain PHP extension |
| 59 | $driver = substr($driver, 0, -4); |
| 60 | } |
| 61 | |
| 62 | // create unique identifier |
| 63 | $sign = $plugin . '.' . $driver; |
| 64 | |
| 65 | // check if the driver was already instantiated |
| 66 | if (!isset(static::$instances[$sign])) |
| 67 | { |
| 68 | $classname = null; |
| 69 | $drivers = array(); |
| 70 | |
| 71 | /** |
| 72 | * Trigger action to obtain a list of classnames of the sms driver. |
| 73 | * The action should autoload the file that contains the classname. |
| 74 | * In case the sms driver should be loaded, the classname MUST be |
| 75 | * pushed within the &$drivers array. |
| 76 | * Fires before the instantiation of the returned classname. |
| 77 | * |
| 78 | * @param array A reference to the list of available drivers. |
| 79 | * @param string The name of the driver to load. |
| 80 | * |
| 81 | * @since 10.1.30 |
| 82 | */ |
| 83 | do_action_ref_array('load_sms_driver_' . $plugin, array(&$drivers, $driver)); |
| 84 | |
| 85 | // use the last driver in the list |
| 86 | $classname = array_pop($drivers); |
| 87 | |
| 88 | if (!$classname || !class_exists($classname)) |
| 89 | { |
| 90 | // driver not found, raise an exception |
| 91 | throw new RuntimeException('The SMS driver [' . $driver . '] for [' . $plugin . '] does not exist.', 404); |
| 92 | } |
| 93 | |
| 94 | // instantiate the driver |
| 95 | $driver = new $classname($plugin, $order, $config); |
| 96 | |
| 97 | if (!$driver instanceof JSmsDriver) |
| 98 | { |
| 99 | // the class is not an instance of JSmsDriver, raise an exception |
| 100 | throw new RuntimeException('The SMS driver [' . $classname . '] is not a valid instance.', 500); |
| 101 | } |
| 102 | |
| 103 | // cache the driver |
| 104 | static::$instances[$sign] = $driver; |
| 105 | } |
| 106 | |
| 107 | return static::$instances[$sign]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns a list of all the drivers supported by the specified plugin. |
| 112 | * The SMS drivers will be returned in ascending order. |
| 113 | * |
| 114 | * @param string $plugin The name of the plugin. |
| 115 | * |
| 116 | * @return array A list of paths. |
| 117 | */ |
| 118 | public static function getSupportedDrivers($plugin) |
| 119 | { |
| 120 | // init drivers array |
| 121 | $drivers = array(); |
| 122 | |
| 123 | /** |
| 124 | * Hook used to filter the list of all the supported drivers. |
| 125 | * Every plugin attached to this filter will be able to push one |
| 126 | * or more drivers within the $drivers array. |
| 127 | * |
| 128 | * @param array An array containing the list of the supported drivers. |
| 129 | * |
| 130 | * @since 10.1.30 |
| 131 | */ |
| 132 | $drivers = apply_filters('get_supported_sms_drivers_' . $plugin, $drivers); |
| 133 | |
| 134 | // remove duplicated records |
| 135 | $drivers = array_unique($drivers); |
| 136 | |
| 137 | // sort by ascending driver name |
| 138 | sort($drivers); |
| 139 | |
| 140 | return $drivers; |
| 141 | } |
| 142 | } |
| 143 |