| @@ -1,0 +1,131 @@ | ||
| 1 | +<?php | |
| 2 | +defined('ABSPATH') || die('Restricted Access'); | |
| 3 | + | |
| 4 | +use AcyMailing\Classes\PluginClass; | |
| 5 | + | |
| 6 | +global $acymPlugins; | |
| 7 | +global $acymAddonsForSettings; | |
| 8 | + | |
| 9 | +function acym_trigger(string $method, array $args = [], ?string $plugin = null, ?callable $callbackOnePlugin = null): ?array | |
| 10 | +{ | |
| 11 | + // On WordPress we load the addons before the tables are created on installation | |
| 12 | + if (!in_array(acym_getPrefix().'acym_configuration', acym_getTableList())) { | |
| 13 | + return null; | |
| 14 | + } | |
| 15 | + | |
| 16 | + // Handle multilingual | |
| 17 | + if (in_array($method, ['replaceContent', 'replaceUserInformation']) && !empty($args[0]->language)) { | |
| 18 | + $previousLanguage = acym_setLanguage($args[0]->language); | |
| 19 | + acym_loadLanguage($args[0]->language); | |
| 20 | + } | |
| 21 | + | |
| 22 | + global $acymPlugins; | |
| 23 | + global $acymAddonsForSettings; | |
| 24 | + if (empty($acymPlugins)) { | |
| 25 | + acym_loadPlugins(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + $result = []; | |
| 29 | + $listAddons = $acymPlugins; | |
| 30 | + if ($method === 'onAcymAddSettings') { | |
| 31 | + $listAddons = $acymAddonsForSettings; | |
| 32 | + } | |
| 33 | + | |
| 34 | + foreach ($listAddons as $class => $onePlugin) { | |
| 35 | + if (is_callable($callbackOnePlugin)) $callbackOnePlugin($onePlugin); | |
| 36 | + if (!method_exists($onePlugin, $method)) continue; | |
| 37 | + if (!empty($plugin) && $class !== $plugin) continue; | |
| 38 | + | |
| 39 | + // There may be an error here, but I don't know how to handle it. At least don't block the execution | |
| 40 | + try { | |
| 41 | + $value = call_user_func_array([$onePlugin, $method], $args); | |
| 42 | + if (isset($value)) { | |
| 43 | + $result[] = $value; | |
| 44 | + } | |
| 45 | + | |
| 46 | + if (!empty($onePlugin->errors)) { | |
| 47 | + $onePlugin->errorCallback(); | |
| 48 | + } | |
| 49 | + } catch (\Throwable $e) { | |
| 50 | + acym_logError('An error occurred when triggering the method '.$method.': '.$e->getMessage()); | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + if (!empty($previousLanguage)) { | |
| 55 | + acym_setLanguage($previousLanguage); | |
| 56 | + acym_loadLanguage($previousLanguage); | |
| 57 | + } | |
| 58 | + | |
| 59 | + return $result; | |
| 60 | +} | |
| 61 | + | |
| 62 | +function acym_checkPluginsVersion(): bool | |
| 63 | +{ | |
| 64 | + $pluginClass = new PluginClass(); | |
| 65 | + $pluginsInstalled = $pluginClass->getMatchingElements(); | |
| 66 | + $pluginsInstalled = $pluginsInstalled['elements']; | |
| 67 | + | |
| 68 | + if (empty($pluginsInstalled)) { | |
| 69 | + return true; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $response = acym_fileGetContent(ACYM_UPDATEME_API_URL.'public/addons'); | |
| 73 | + $pluginsAvailable = @json_decode($response, true); | |
| 74 | + if (empty($pluginsAvailable)) { | |
| 75 | + return true; | |
| 76 | + } | |
| 77 | + | |
| 78 | + foreach ($pluginsInstalled as $key => $pluginInstalled) { | |
| 79 | + foreach ($pluginsAvailable as $pluginAvailable) { | |
| 80 | + if ($pluginAvailable['file_name'] === $pluginInstalled->folder_name && !version_compare($pluginInstalled->version, $pluginAvailable['version'], '>=')) { | |
| 81 | + $pluginInstalled->uptodate = 0; | |
| 82 | + $pluginInstalled->latest_version = $pluginAvailable['version']; | |
| 83 | + $pluginClass->save($pluginInstalled); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 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]; | |
| 131 | +} | |