| @@ -1,0 +1,221 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Plugins; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\PluginClass; | |
| 6 | +use AcyMailing\Helpers\WorkflowHelper; | |
| 7 | + | |
| 8 | +trait Installed | |
| 9 | +{ | |
| 10 | + public function installed(): void | |
| 11 | + { | |
| 12 | + acym_setVar('layout', 'installed'); | |
| 13 | + | |
| 14 | + $data = []; | |
| 15 | + $data['tabs'] = $this->tabs; | |
| 16 | + $data['tab'] = 'installed'; | |
| 17 | + $data['types'] = $this->types; | |
| 18 | + $data['level'] = $this->level; | |
| 19 | + $data['plugins'] = $this->getAllPlugins(); | |
| 20 | + $data['workflowHelper'] = new WorkflowHelper(); | |
| 21 | + | |
| 22 | + acym_addScript(true, 'window.acymailingAddons = '.json_encode($data['plugins']).';'); | |
| 23 | + acym_addScript(true, 'window.acymailingAvailableAddons = '.json_encode(ACYM_AVAILABLE_PLUGINS).';'); | |
| 24 | + | |
| 25 | + parent::display($data); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public function checkUpdates(): void | |
| 29 | + { | |
| 30 | + acym_checkPluginsVersion(); | |
| 31 | + $this->installed(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public function saveSettings(): void | |
| 35 | + { | |
| 36 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 37 | + | |
| 38 | + $pluginFolderName = acym_getVar('string', 'plugin__folder_name', ''); | |
| 39 | + | |
| 40 | + if (empty($pluginFolderName)) { | |
| 41 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_SAVE_SETTINGS')); | |
| 42 | + $this->installed(); | |
| 43 | + | |
| 44 | + return; | |
| 45 | + } | |
| 46 | + | |
| 47 | + $pluginClass = new PluginClass(); | |
| 48 | + $plugin = $pluginClass->getOnePluginByFolderName($pluginFolderName); | |
| 49 | + | |
| 50 | + if (empty($plugin)) { | |
| 51 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_SAVE_SETTINGS')); | |
| 52 | + $this->installed(); | |
| 53 | + | |
| 54 | + return; | |
| 55 | + } | |
| 56 | + $plugin->settings = empty($plugin->settings) ? [] : json_decode($plugin->settings, true); | |
| 57 | + | |
| 58 | + $pluginSettings = acym_getVar('array', $pluginFolderName, []); | |
| 59 | + | |
| 60 | + if (empty($pluginSettings)) { | |
| 61 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_SAVE_SETTINGS')); | |
| 62 | + $this->installed(); | |
| 63 | + | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + | |
| 67 | + foreach ($pluginSettings as $key => $value) { | |
| 68 | + if ($value == 'on') $value = 1; | |
| 69 | + $plugin->settings[$key] = ['value' => $value]; | |
| 70 | + } | |
| 71 | + | |
| 72 | + foreach ($plugin->settings as $key => $value) { | |
| 73 | + if (!array_key_exists($key, $pluginSettings)) $plugin->settings[$key] = ['value' => '']; | |
| 74 | + } | |
| 75 | + | |
| 76 | + $plugin->settings = json_encode($plugin->settings); | |
| 77 | + $pluginClass->save($plugin); | |
| 78 | + | |
| 79 | + $this->installed(); | |
| 80 | + } | |
| 81 | + | |
| 82 | + private function isLatestAcyMailingVersion(): void | |
| 83 | + { | |
| 84 | + $currentVersion = $this->config->get('version', ''); | |
| 85 | + $latestVersion = $this->config->get('latestversion', ''); | |
| 86 | + if (!version_compare($currentVersion, $latestVersion, '>=')) { | |
| 87 | + acym_sendAjaxResponse(acym_translation('ACYM_NEED_LATEST_VERSION_TO_DOWNLOAD'), [], false); | |
| 88 | + } | |
| 89 | + } | |
| 90 | + | |
| 91 | + public function update(): void | |
| 92 | + { | |
| 93 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 94 | + | |
| 95 | + $this->isLatestAcyMailingVersion(); | |
| 96 | + | |
| 97 | + $plugin = acym_getVar('array', 'plugin'); | |
| 98 | + | |
| 99 | + $pluginClass = new PluginClass(); | |
| 100 | + $id = $pluginClass->updateAddon($plugin['folder_name']); | |
| 101 | + if (!empty($id)) { | |
| 102 | + acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_SUCCESSFULLY_UPDATED')); | |
| 103 | + } else { | |
| 104 | + acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_UPDATE_ADD_ON'), [], false); | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + public function deletePlugin(): void | |
| 109 | + { | |
| 110 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 111 | + | |
| 112 | + $pluginClass = new PluginClass(); | |
| 113 | + $id = acym_getVar('int', 'id'); | |
| 114 | + | |
| 115 | + $plugin = $pluginClass->getOneById($id); | |
| 116 | + | |
| 117 | + if (empty($plugin)) { | |
| 118 | + acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_NOT_FOUND'), [], false); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $routePlugin = dirname(acym_getPluginPath($plugin->folder_name)); | |
| 122 | + | |
| 123 | + if (file_exists($routePlugin)) { | |
| 124 | + acym_deleteFolder($routePlugin); | |
| 125 | + if ($pluginClass->delete([$id])) { | |
| 126 | + acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_SUCCESSFULLY_DELETED')); | |
| 127 | + } | |
| 128 | + } | |
| 129 | + acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_NOT_FOUND'), [], false); | |
| 130 | + } | |
| 131 | + | |
| 132 | + public function toggleActivate(): void | |
| 133 | + { | |
| 134 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 135 | + | |
| 136 | + $pluginClass = new PluginClass(); | |
| 137 | + $id = acym_getVar('int', 'id'); | |
| 138 | + | |
| 139 | + $plugin = $pluginClass->getOneById($id); | |
| 140 | + | |
| 141 | + | |
| 142 | + if (empty($plugin)) { | |
| 143 | + acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_NOT_FOUND'), [], false); | |
| 144 | + } | |
| 145 | + | |
| 146 | + $plugin->active = $plugin->active == 0 ? 1 : 0; | |
| 147 | + | |
| 148 | + $savedPluginId = $pluginClass->save($plugin); | |
| 149 | + if (empty($savedPluginId)) { | |
| 150 | + acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_SAVE_ADD_ON'), [], false); | |
| 151 | + } | |
| 152 | + | |
| 153 | + acym_sendAjaxResponse(); | |
| 154 | + } | |
| 155 | + | |
| 156 | + public function getCustomViewPlugin(): void | |
| 157 | + { | |
| 158 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 159 | + | |
| 160 | + $plugin = $this->getPluginClassAjaxCustomView(); | |
| 161 | + | |
| 162 | + $customLayoutPath = ACYM_CUSTOM_PLUGIN_LAYOUT.$plugin['folderName'].'.html'; | |
| 163 | + | |
| 164 | + $customView = ''; | |
| 165 | + | |
| 166 | + if (file_exists($customLayoutPath)) $customView = acym_fileGetContent($customLayoutPath); | |
| 167 | + if (empty($customView)) acym_trigger('getStandardStructure', [&$customView], $plugin['className']); | |
| 168 | + | |
| 169 | + acym_sendAjaxResponse('', ['content' => $customView]); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public function saveCustomViewPlugin(): void | |
| 173 | + { | |
| 174 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 175 | + | |
| 176 | + $plugin = $this->getPluginClassAjaxCustomView(); | |
| 177 | + $pluginCustomView = acym_getVar('string', 'custom_view', ''); | |
| 178 | + $pluginCustomView = urldecode($pluginCustomView); | |
| 179 | + | |
| 180 | + $customLayoutPath = ACYM_CUSTOM_PLUGIN_LAYOUT.$plugin['folderName'].'.html'; | |
| 181 | + | |
| 182 | + $result = acym_writeFile($customLayoutPath, $pluginCustomView); | |
| 183 | + | |
| 184 | + if ($result) { | |
| 185 | + acym_sendAjaxResponse(acym_translation('ACYM_CUSTOM_VIEW_WELL_SAVED')); | |
| 186 | + } else { | |
| 187 | + acym_sendAjaxResponse(acym_translation('ACYM_CUSTOM_VIEW_SAVED_FAILED'), [], false); | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + public function deleteCustomViewPlugin(): void | |
| 192 | + { | |
| 193 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 194 | + | |
| 195 | + $plugin = $this->getPluginClassAjaxCustomView(); | |
| 196 | + | |
| 197 | + $customLayoutPath = ACYM_CUSTOM_PLUGIN_LAYOUT.$plugin['folderName'].'.html'; | |
| 198 | + | |
| 199 | + if (file_exists($customLayoutPath) && !acym_deleteFile($customLayoutPath)) { | |
| 200 | + acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_DELETE_CUSTOM_VIEW'), [], false); | |
| 201 | + } | |
| 202 | + | |
| 203 | + $customView = ''; | |
| 204 | + acym_trigger('getStandardStructure', [&$customView], $plugin['className']); | |
| 205 | + | |
| 206 | + acym_sendAjaxResponse(acym_translation('ACYM_CUSTOM_VIEW_WELL_DELETED'), ['content' => $customView]); | |
| 207 | + } | |
| 208 | + | |
| 209 | + private function getPluginClassAjaxCustomView(): array | |
| 210 | + { | |
| 211 | + $return = []; | |
| 212 | + $return['folderName'] = acym_getVar('string', 'plugin'); | |
| 213 | + $return['className'] = acym_getVar('string', 'plugin_class'); | |
| 214 | + | |
| 215 | + if (empty($return['folderName']) && empty($return['className'])) { | |
| 216 | + acym_sendAjaxResponse(acym_translation('ACYM_CUSTOM_VIEW_NOT_FOUND'), [], false); | |
| 217 | + } | |
| 218 | + | |
| 219 | + return $return; | |
| 220 | + } | |
| 221 | +} | |