Backup
2 years ago
FileList
2 years ago
Restore
3 years ago
Backup.php
2 years ago
BackupSpeedIndex.php
2 years ago
Cancel.php
3 years ago
Delete.php
3 years ago
Edit.php
3 years ago
FileInfo.php
3 years ago
FileList.php
3 years ago
Listing.php
2 years ago
Parts.php
3 years ago
PrepareJob.php
2 years ago
Restore.php
2 years ago
ScheduleList.php
2 years ago
Status.php
3 years ago
Upload.php
2 years ago
Parts.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | // TODO PHP7.x; declare(strict_type=1); |
| 4 | // TODO PHP7.x; type hints & return types |
| 5 | |
| 6 | namespace WPStaging\Backup\Ajax; |
| 7 | |
| 8 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 9 | use WPStaging\Framework\Facades\Sanitize; |
| 10 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 11 | use WPStaging\Backup\Entity\BackupMetadata; |
| 12 | use WPStaging\Backup\Service\BackupsFinder; |
| 13 | use WPStaging\Framework\Utils\Urls; |
| 14 | |
| 15 | class Parts extends AbstractTemplateComponent |
| 16 | { |
| 17 | private $backupsFinder; |
| 18 | private $urls; |
| 19 | |
| 20 | public function __construct(TemplateEngine $templateEngine, BackupsFinder $backupsFinder, Urls $urls) |
| 21 | { |
| 22 | parent::__construct($templateEngine); |
| 23 | $this->backupsFinder = $backupsFinder; |
| 24 | $this->urls = $urls; |
| 25 | } |
| 26 | |
| 27 | public function render() |
| 28 | { |
| 29 | if (!$this->canRenderAjax()) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | // Make sure path is inside Backups Directory |
| 34 | $backupDir = wp_normalize_path($this->backupsFinder->getBackupsDirectory()); |
| 35 | |
| 36 | $indexFile = isset($_POST['filePath']) ? Sanitize::sanitizePath($_POST['filePath']) : ''; |
| 37 | |
| 38 | if ($indexFile === '') { |
| 39 | wp_send_json([ |
| 40 | 'error' => true, |
| 41 | 'message' => 'Backup file path not provided!', |
| 42 | ]); |
| 43 | } |
| 44 | |
| 45 | $file = $this->getFullPath($backupDir, $indexFile); |
| 46 | |
| 47 | try { |
| 48 | $info = (new BackupMetadata())->hydrateByFilePath($file); |
| 49 | } catch (\Exception $e) { |
| 50 | wp_send_json([ |
| 51 | 'error' => true, |
| 52 | 'message' => $e->getMessage(), |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | $parts = []; |
| 57 | |
| 58 | $metadata = $info->getMultipartMetadata(); |
| 59 | $pluginParts = $metadata->getPluginsParts(); |
| 60 | $totalPluginParts = count($pluginParts); |
| 61 | foreach ($pluginParts as $key => $fileName) { |
| 62 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 63 | $parts[] = $this->getPart('Plugins', $key, $fileName, $fullPath, $totalPluginParts); |
| 64 | } |
| 65 | |
| 66 | $themesParts = $metadata->getThemesParts(); |
| 67 | $totalThemesParts = count($themesParts); |
| 68 | foreach ($themesParts as $key => $fileName) { |
| 69 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 70 | $parts[] = $this->getPart('Themes', $key, $fileName, $fullPath, $totalThemesParts); |
| 71 | } |
| 72 | |
| 73 | $uploadsParts = $metadata->getUploadsParts(); |
| 74 | $totalUploadsParts = count($uploadsParts); |
| 75 | foreach ($uploadsParts as $key => $fileName) { |
| 76 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 77 | $parts[] = $this->getPart('Medias', $key, $fileName, $fullPath, $totalUploadsParts); |
| 78 | } |
| 79 | |
| 80 | $muPluginsParts = $metadata->getMuPluginsParts(); |
| 81 | $totalMuPluginsParts = count($muPluginsParts); |
| 82 | foreach ($muPluginsParts as $key => $fileName) { |
| 83 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 84 | $parts[] = $this->getPart('Mu Plugins', $key, $fileName, $fullPath, $totalMuPluginsParts); |
| 85 | } |
| 86 | |
| 87 | $othersParts = $metadata->getOthersParts(); |
| 88 | $totalOthersParts = count($othersParts); |
| 89 | foreach ($othersParts as $key => $fileName) { |
| 90 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 91 | $parts[] = $this->getPart('Others', $key, $fileName, $fullPath, $totalOthersParts); |
| 92 | } |
| 93 | |
| 94 | $databaseParts = $metadata->getDatabaseParts(); |
| 95 | $totalDatabaseParts = count($databaseParts); |
| 96 | foreach ($databaseParts as $key => $fileName) { |
| 97 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 98 | $parts[] = $this->getPart('Database', $key, $fileName, $fullPath, $totalDatabaseParts); |
| 99 | } |
| 100 | |
| 101 | $result = wp_send_json([ |
| 102 | 'error' => false, |
| 103 | 'parts' => $parts, |
| 104 | ]); |
| 105 | |
| 106 | wp_send_json($result); |
| 107 | } |
| 108 | |
| 109 | private function getFullPath($backupDir, $relativePath) |
| 110 | { |
| 111 | return $backupDir . str_replace($backupDir, '', wp_normalize_path(untrailingslashit($relativePath))); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param string $type |
| 116 | * @param int $key |
| 117 | * @param string $fileName |
| 118 | * @param string $fullPath |
| 119 | * @param int $totalParts |
| 120 | * @return array |
| 121 | */ |
| 122 | private function getPart($type, $key, $fileName, $fullPath, $totalParts) |
| 123 | { |
| 124 | |
| 125 | $pluginName = $type; |
| 126 | $currentKey = ($key + 1); |
| 127 | |
| 128 | if ($totalParts > 1) { |
| 129 | $pluginName = $type . ' ' . $currentKey . ' / ' . $totalParts; |
| 130 | } |
| 131 | |
| 132 | return [ |
| 133 | 'name' => $pluginName, |
| 134 | 'fileSize' => size_format(filesize($fullPath), 2), |
| 135 | 'downloadLink' => $this->urls->getBackupUrl() . $fileName |
| 136 | ]; |
| 137 | } |
| 138 | } |
| 139 |