DeleteConfirm.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Ajax\Delete; |
| 4 | |
| 5 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 6 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 7 | use WPStaging\Framework\Utils\Sanitize; |
| 8 | use WPStaging\Staging\Sites; |
| 9 | use WPStaging\Staging\Traits\WithStagingDatabase; |
| 10 | |
| 11 | class DeleteConfirm extends AbstractTemplateComponent |
| 12 | { |
| 13 | use WithStagingDatabase; |
| 14 | |
| 15 | /** @var Sites */ |
| 16 | private $sites; |
| 17 | |
| 18 | /** @var Sanitize */ |
| 19 | private $sanitize; |
| 20 | |
| 21 | public function __construct(Sites $sites, Sanitize $sanitize, TemplateEngine $templateEngine) |
| 22 | { |
| 23 | parent::__construct($templateEngine); |
| 24 | $this->sites = $sites; |
| 25 | $this->sanitize = $sanitize; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return void |
| 30 | */ |
| 31 | public function ajaxConfirm() |
| 32 | { |
| 33 | if (!$this->canRenderAjax()) { |
| 34 | wp_send_json_error('Invalid request.'); |
| 35 | } |
| 36 | |
| 37 | $cloneId = $this->sanitize->sanitizeString(isset($_POST['cloneId']) ? $_POST['cloneId'] : ''); |
| 38 | if (empty($cloneId)) { |
| 39 | wp_send_json_error('Invalid request. Clone ID missing!'); |
| 40 | } |
| 41 | |
| 42 | $stagingSiteDto = null; |
| 43 | try { |
| 44 | $stagingSiteDto = $this->sites->getStagingSiteDtoByCloneId($cloneId); |
| 45 | } catch (\Throwable $e) { |
| 46 | wp_send_json_error($e->getMessage()); |
| 47 | } |
| 48 | |
| 49 | $tables = []; |
| 50 | $connected = false; |
| 51 | try { |
| 52 | $this->initStagingDatabase($stagingSiteDto); |
| 53 | $tables = $this->getStagingTablesStatus($stagingSiteDto->getUsedPrefix()); |
| 54 | $connected = true; |
| 55 | } catch (\Throwable $e) { |
| 56 | $tables = []; |
| 57 | $connected = false; |
| 58 | } |
| 59 | |
| 60 | $result = $this->templateEngine->render( |
| 61 | 'staging/confirm-delete.php', |
| 62 | [ |
| 63 | 'stagingSite' => $stagingSiteDto, |
| 64 | 'tables' => $tables === null ? [] : $tables, |
| 65 | 'isDatabaseConnected' => $connected, |
| 66 | 'stagingSiteSize' => '', // TODO: not-available but still used in UI, find a way how we can get this efficiently |
| 67 | ] |
| 68 | ); |
| 69 | |
| 70 | wp_send_json_success([ |
| 71 | 'stagingSiteName' => $stagingSiteDto->getSiteName(), |
| 72 | 'html' => $result, |
| 73 | ]); |
| 74 | } |
| 75 | } |
| 76 |