factory.php
134 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.mail.template'); |
| 15 | |
| 16 | /** |
| 17 | * Factory class used to instantiate the right notification provider. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | abstract class VAPMailFactory |
| 22 | { |
| 23 | /** |
| 24 | * Returns an instance of the notification provider. |
| 25 | * |
| 26 | * @param string $id The id/alias of the provider to load. |
| 27 | * @param mixed $args... Some additional arguments to use when |
| 28 | * instantiating the provider. |
| 29 | * |
| 30 | * @return VAPMailTemplate |
| 31 | */ |
| 32 | public static function getInstance($id) |
| 33 | { |
| 34 | // try to load the file |
| 35 | if (!VAPLoader::import('libraries.mail.templates.' . $id)) |
| 36 | { |
| 37 | // file not found |
| 38 | throw new Exception(sprintf('Mail template [%s] not found', $id), 404); |
| 39 | } |
| 40 | |
| 41 | // fetch class name |
| 42 | $classname = 'VAPMailTemplate' . ucfirst($id); |
| 43 | |
| 44 | // make sure the class exists |
| 45 | if (!class_exists($classname)) |
| 46 | { |
| 47 | // class not found |
| 48 | throw new Exception(sprintf('Mail template [%s] class not found', $classname), 404); |
| 49 | } |
| 50 | |
| 51 | // fetch arguments to pass to the constructor by excluding the first argument |
| 52 | $args = func_get_args(); |
| 53 | $args = array_splice($args, 1); |
| 54 | |
| 55 | // create reflection of the provider |
| 56 | $class = new ReflectionClass($classname); |
| 57 | // instantiate provider class |
| 58 | $obj = $class->newInstanceArgs($args); |
| 59 | |
| 60 | // make sure the class is a valid instance |
| 61 | if (!$obj instanceof VAPMailTemplate) |
| 62 | { |
| 63 | // not a valid instance |
| 64 | throw new Exception(sprintf('Mail template [%s] class is not a valid instance', $classname), 400); |
| 65 | } |
| 66 | |
| 67 | return $obj; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Helper method used to trigger a plugin event before sending the e-mail. |
| 72 | * Any other parameter specified after the target will be included as |
| 73 | * argument for the plugin event. |
| 74 | * |
| 75 | * @param string $id The ID of the mail handler (filename). |
| 76 | * @param string $what Either "subject", "content" or "attachment", |
| 77 | * depending on what it is needed to edit. |
| 78 | * @param mixed &$target The content of the target to be edited. |
| 79 | * |
| 80 | * @return boolean False in case the e-mail sending has been prevented, true otherwise. |
| 81 | */ |
| 82 | public static function letPluginsManipulateMail($id, $what, &$target) |
| 83 | { |
| 84 | // get event dispatcher |
| 85 | $dispatcher = VAPFactory::getEventDispatcher(); |
| 86 | |
| 87 | // fetch event name based on what we need to fetch and mail alias |
| 88 | $event = 'onBeforeSendMail' . ucfirst($what) . ucfirst($id); |
| 89 | |
| 90 | // get all arguments |
| 91 | $args = func_get_args(); |
| 92 | // keep only the additional arguments |
| 93 | $args = array_splice($args, 3); |
| 94 | |
| 95 | // merge target within arguments |
| 96 | $args = array_merge(array(&$target), $args); |
| 97 | |
| 98 | try |
| 99 | { |
| 100 | /** |
| 101 | * Triggers an event to let the plugins be able to handle the subject of |
| 102 | * the e-mail and the HTML contents of the related template. |
| 103 | * |
| 104 | * The event name is built as: |
| 105 | * onBeforeSendMail[Subject|Content|Attachment][Class] |
| 106 | * |
| 107 | * The event might specified additional arguments, such as the details of |
| 108 | * the reservation/order. |
| 109 | * |
| 110 | * When registering an attachment, the file path should be set as key of |
| 111 | * the array and the value should be "0" to auto-delete the file after |
| 112 | * sending the e-mail or "1" to always keep it. |
| 113 | * |
| 114 | * @param mixed &$target Either the subject, the HTML content, or an |
| 115 | * array or e-mail attachments, depending on the |
| 116 | * $what argument that was passed to this method. |
| 117 | * |
| 118 | * @return boolean Return false to prevent e-mail sending. |
| 119 | * |
| 120 | * @since 1.7 |
| 121 | */ |
| 122 | $res = $dispatcher->trigger($event, $args); |
| 123 | } |
| 124 | catch (Exception $e) |
| 125 | { |
| 126 | // do not break the process because of a plugin error |
| 127 | $res = array(); |
| 128 | } |
| 129 | |
| 130 | // check if at least a plugin returned FALSE to prevent e-mail sending |
| 131 | return !in_array(false, $res, true); |
| 132 | } |
| 133 | } |
| 134 |