| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | +defined('ABSPATH') || die('Restricted Access'); | |
| 2 | 3 | |
| 3 | 4 | use AcyMailing\Classes\PluginClass; |
| 4 | 5 | |
| 5 | 6 | global $acymPlugins; |
| @@ -4,14 +5,16 @@ | ||
| 4 | 5 | |
| 5 | 6 | global $acymPlugins; |
| 6 | 7 | global $acymAddonsForSettings; |
| 7 | 8 | |
| 8 | -function acym_trigger($method, $args = [], $plugin = null, $callbackOnePlugin = null) | |
| 9 | +function acym_trigger(string $method, array $args = [], ?string $plugin = null, ?callable $callbackOnePlugin = null): ?array | |
| 9 | 10 | { |
| 11 | + // On WordPress we load the addons before the tables are created on installation | |
| 10 | 12 | if (!in_array(acym_getPrefix().'acym_configuration', acym_getTableList())) { |
| 11 | 13 | return null; |
| 12 | 14 | } |
| 13 | 15 | |
| 16 | + // Handle multilingual | |
| 14 | 17 | if (in_array($method, ['replaceContent', 'replaceUserInformation']) && !empty($args[0]->language)) { |
| 15 | 18 | $previousLanguage = acym_setLanguage($args[0]->language); |
| 16 | 19 | acym_loadLanguage($args[0]->language); |
| 17 | 20 | } |
| @@ -17,9 +20,11 @@ | ||
| 17 | 20 | } |
| 18 | 21 | |
| 19 | 22 | global $acymPlugins; |
| 20 | 23 | global $acymAddonsForSettings; |
| 21 | - if (empty($acymPlugins)) acym_loadPlugins(); | |
| 24 | + if (empty($acymPlugins)) { | |
| 25 | + acym_loadPlugins(); | |
| 26 | + } | |
| 22 | 27 | |
| 23 | 28 | $result = []; |
| 24 | 29 | $listAddons = $acymPlugins; |
| 25 | 30 | if ($method === 'onAcymAddSettings') { |
| @@ -28,17 +33,21 @@ | ||
| 28 | 33 | |
| 29 | 34 | foreach ($listAddons as $class => $onePlugin) { |
| 30 | 35 | if (is_callable($callbackOnePlugin)) $callbackOnePlugin($onePlugin); |
| 31 | 36 | if (!method_exists($onePlugin, $method)) continue; |
| 32 | - if (!empty($plugin) && $class != $plugin) continue; | |
| 37 | + if (!empty($plugin) && $class !== $plugin) continue; | |
| 33 | 38 | |
| 39 | + // There may be an error here, but I don't know how to handle it. At least don't block the execution | |
| 34 | 40 | try { |
| 35 | 41 | $value = call_user_func_array([$onePlugin, $method], $args); |
| 36 | - if (isset($value)) $result[] = $value; | |
| 42 | + if (isset($value)) { | |
| 43 | + $result[] = $value; | |
| 44 | + } | |
| 45 | + | |
| 37 | 46 | if (!empty($onePlugin->errors)) { |
| 38 | 47 | $onePlugin->errorCallback(); |
| 39 | 48 | } |
| 40 | - } catch (Exception $e) { | |
| 49 | + } catch (\Throwable $e) { | |
| 41 | 50 | acym_logError('An error occurred when triggering the method '.$method.': '.$e->getMessage()); |
| 42 | 51 | } |
| 43 | 52 | } |
| 44 | 53 | |
| @@ -76,5 +85,47 @@ | ||
| 76 | 85 | } |
| 77 | 86 | } |
| 78 | 87 | |
| 79 | 88 | return true; |
| 89 | +} | |
| 90 | + | |
| 91 | +/** | |
| 92 | + * Displays the option fields of a trigger / filter / condition / action declared by an add-on | |
| 93 | + */ | |
| 94 | +function acym_displayAddonOption(object $declaration, ?string $fieldName = null): void | |
| 95 | +{ | |
| 96 | + if (isset($declaration->displayOptions) && is_callable($declaration->displayOptions)) { | |
| 97 | + ($declaration->displayOptions)($fieldName); | |
| 98 | + | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + | |
| 102 | + if (empty($declaration->option)) { | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + | |
| 106 | + // TODO: remove this on version 12, only there for retro-compat of add-ons/plugins not updated along with AcyMailing | |
| 107 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Add-on that still declares its option as a string, escaped on its side. Migrate it to declareTrigger(). | |
| 108 | + echo preg_replace_callback(ACYM_REGEX_SWITCHES, 'acym_prefixSwitchIds', $declaration->option); | |
| 109 | +} | |
| 110 | + | |
| 111 | +function acym_getAddonOption(object $declaration, ?string $fieldName = null): string | |
| 112 | +{ | |
| 113 | + ob_start(); | |
| 114 | + try { | |
| 115 | + acym_displayAddonOption($declaration, $fieldName); | |
| 116 | + } finally { | |
| 117 | + $html = ob_get_clean(); | |
| 118 | + } | |
| 119 | + | |
| 120 | + return $html === false ? '' : $html; | |
| 121 | +} | |
| 122 | + | |
| 123 | +/** | |
| 124 | + * Makes the ids of a switch unique per row of a filter/condition form. | |
| 125 | + * | |
| 126 | + * TODO: remove this on version 12, along with the string options | |
| 127 | + */ | |
| 128 | +function acym_prefixSwitchIds(array $matches): string | |
| 129 | +{ | |
| 130 | + return '__numand__'.$matches[1].$matches[2].'__numand__'.$matches[3].'__numand__'.$matches[4].'__numand__'.$matches[5]; | |
| 80 | 131 | } |