| @@ -1,0 +1,111 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Plugins; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\PluginClass; | |
| 6 | +use AcyMailing\Helpers\WorkflowHelper; | |
| 7 | + | |
| 8 | +trait Available | |
| 9 | +{ | |
| 10 | + public function available(): void | |
| 11 | + { | |
| 12 | + acym_setVar('layout', 'available'); | |
| 13 | + $data = []; | |
| 14 | + $data['tabs'] = $this->tabs; | |
| 15 | + $data['tab'] = 'available'; | |
| 16 | + $data['types'] = $this->types; | |
| 17 | + $data['level'] = $this->level; | |
| 18 | + $data['workflowHelper'] = new WorkflowHelper(); | |
| 19 | + | |
| 20 | + acym_addScript(true, 'window.acymailingAvailableAddons = '.json_encode(ACYM_AVAILABLE_PLUGINS).';'); | |
| 21 | + | |
| 22 | + parent::display($data); | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function download(bool $ajax = true, string $pluginFromUpdate = ''): string | |
| 26 | + { | |
| 27 | + $plugin = []; | |
| 28 | + | |
| 29 | + if (empty($pluginFromUpdate)) { | |
| 30 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 31 | + $this->isLatestAcyMailingVersion(); | |
| 32 | + $plugin = acym_getVar('array', 'plugin'); | |
| 33 | + } else { | |
| 34 | + $plugins = acym_fileGetContent(ACYM_UPDATEME_API_URL.'public/addons'); | |
| 35 | + $plugins = @json_decode($plugins, true); | |
| 36 | + if (empty($plugins)) { | |
| 37 | + $this->handleError('ACYM_ISSUE_WHILE_INSTALLING', $ajax); | |
| 38 | + } | |
| 39 | + | |
| 40 | + foreach ($plugins as $onePlugin) { | |
| 41 | + if ($pluginFromUpdate === $onePlugin['file_name']) { | |
| 42 | + $plugin = $onePlugin; | |
| 43 | + break; | |
| 44 | + } | |
| 45 | + } | |
| 46 | + | |
| 47 | + if (empty($plugin)) { | |
| 48 | + return acym_translation('ACYM_ADD_ON_NOT_FOUND'); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + $pluginClass = new PluginClass(); | |
| 53 | + $errorMessage = $pluginClass->downloadAddon($plugin['file_name'], $ajax); | |
| 54 | + if (!empty($errorMessage)) { | |
| 55 | + return $errorMessage; | |
| 56 | + } | |
| 57 | + | |
| 58 | + // We update the plugin info in DB | |
| 59 | + $pluginToSave = new \stdClass(); | |
| 60 | + $pluginToSave->title = $plugin['name']; | |
| 61 | + $pluginToSave->folder_name = $plugin['file_name']; | |
| 62 | + $pluginToSave->version = $plugin['version']; | |
| 63 | + $pluginToSave->latest_version = $plugin['version']; | |
| 64 | + $pluginToSave->description = $plugin['description']; | |
| 65 | + $pluginToSave->active = 1; | |
| 66 | + $pluginToSave->category = $plugin['category']; | |
| 67 | + $pluginToSave->level = $plugin['level']; | |
| 68 | + $pluginToSave->uptodate = 1; | |
| 69 | + $pluginToSave->id = $pluginClass->save($pluginToSave); | |
| 70 | + | |
| 71 | + if (!empty($pluginToSave->id)) { | |
| 72 | + if ($ajax) { | |
| 73 | + acym_sendAjaxResponse(acym_translation('ACYM_ADD_ON_SUCCESSFULLY_INSTALLED')); | |
| 74 | + } | |
| 75 | + | |
| 76 | + return ''; | |
| 77 | + } | |
| 78 | + | |
| 79 | + return $this->handleError('ACYM_ISSUE_WHILE_INSTALLING', $ajax); | |
| 80 | + } | |
| 81 | + | |
| 82 | + public function getAllPluginsAjax(): void | |
| 83 | + { | |
| 84 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 85 | + acym_sendAjaxResponse('', $this->getAllPlugins()); | |
| 86 | + } | |
| 87 | + | |
| 88 | + private function getAllPlugins(): array | |
| 89 | + { | |
| 90 | + $pluginClass = new PluginClass(); | |
| 91 | + $plugins = $pluginClass->getMatchingElements(['ordering' => 'title']); | |
| 92 | + | |
| 93 | + foreach ($plugins['elements'] as $key => $plugin) { | |
| 94 | + if (!empty($plugin->settings)) { | |
| 95 | + $plugins['elements'][$key]->settings = json_decode($plugin->settings, true); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + acym_trigger('onAcymAddSettings', [&$plugins['elements']]); | |
| 99 | + | |
| 100 | + return $plugins['elements']; | |
| 101 | + } | |
| 102 | + | |
| 103 | + private function handleError(string $error, bool $ajax): string | |
| 104 | + { | |
| 105 | + if ($ajax) { | |
| 106 | + acym_sendAjaxResponse(acym_translation($error), [], false); | |
| 107 | + } | |
| 108 | + | |
| 109 | + return acym_translation($error); | |
| 110 | + } | |
| 111 | +} | |