Backup
2 months ago
FileList
4 months ago
Restore
2 months ago
Backup.php
2 years ago
BackupDownloader.php
1 day ago
BackupSizeCalculator.php
1 day ago
BackupSpeedIndex.php
9 months ago
BaseFileList.php
1 year ago
BaseListing.php
9 months ago
Delete.php
4 months ago
Edit.php
4 months ago
Explore.php
3 months ago
ExploreCache.php
3 months ago
FileInfo.php
9 months ago
FileList.php
1 year ago
Listing.php
7 months ago
Parts.php
1 day ago
Restore.php
2 years ago
ScheduleList.php
1 year ago
Upload.php
1 month ago
Parts.php
186 lines
| 1 | <?php |
| 2 | |
| 3 | // TODO PHP7.x; declare(strict_type=1); |
| 4 | |
| 5 | namespace WPStaging\Backup\Ajax; |
| 6 | |
| 7 | use WPStaging\Backup\Entity\BackupMetadata; |
| 8 | use WPStaging\Backup\Exceptions\BackupRuntimeException; |
| 9 | use WPStaging\Backup\Service\BackupsFinder; |
| 10 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 11 | use WPStaging\Framework\Facades\Sanitize; |
| 12 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 13 | use WPStaging\Framework\Utils\Urls; |
| 14 | |
| 15 | class Parts extends AbstractTemplateComponent |
| 16 | { |
| 17 | /** |
| 18 | * @var BackupsFinder |
| 19 | */ |
| 20 | private $backupsFinder; |
| 21 | |
| 22 | /** |
| 23 | * @var Urls |
| 24 | */ |
| 25 | private $urls; |
| 26 | |
| 27 | public function __construct(TemplateEngine $templateEngine, BackupsFinder $backupsFinder, Urls $urls) |
| 28 | { |
| 29 | parent::__construct($templateEngine); |
| 30 | $this->backupsFinder = $backupsFinder; |
| 31 | $this->urls = $urls; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @throws BackupRuntimeException |
| 36 | */ |
| 37 | public function render() |
| 38 | { |
| 39 | if (!$this->canRenderAjax()) { |
| 40 | wp_send_json([ |
| 41 | 'error' => true, |
| 42 | 'message' => 'You are not allowed to access this page!', |
| 43 | ]); |
| 44 | } |
| 45 | |
| 46 | $backupDir = wp_normalize_path($this->backupsFinder->getBackupsDirectory()); |
| 47 | $indexFile = isset($_POST['filePath']) ? Sanitize::sanitizePath($_POST['filePath']) : ''; |
| 48 | |
| 49 | if ($indexFile === '') { |
| 50 | wp_send_json([ |
| 51 | 'error' => true, |
| 52 | 'message' => 'Backup file path not provided!', |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | $file = $this->getFullPath($backupDir, $indexFile); |
| 57 | $info = null; |
| 58 | try { |
| 59 | $info = (new BackupMetadata())->hydrateByFilePath($file); |
| 60 | } catch (\Exception $e) { |
| 61 | wp_send_json([ |
| 62 | 'error' => true, |
| 63 | 'message' => $e->getMessage(), |
| 64 | ]); |
| 65 | } |
| 66 | |
| 67 | $metadata = $info->getMultipartMetadata(); |
| 68 | |
| 69 | $parts = array_merge( |
| 70 | $this->addParts('Database', $metadata->getDatabaseParts(), $backupDir), |
| 71 | $this->addParts('Medias', $metadata->getUploadsParts(), $backupDir), |
| 72 | $this->addParts('Themes', $metadata->getThemesParts(), $backupDir), |
| 73 | $this->addParts('Plugins', $metadata->getPluginsParts(), $backupDir), |
| 74 | $this->addParts('Mu Plugins', $metadata->getMuPluginsParts(), $backupDir), |
| 75 | $this->addParts('Others', $metadata->getOthersParts(), $backupDir), |
| 76 | $this->addParts('Root Files', $metadata->getOtherWpRootParts(), $backupDir) |
| 77 | ); |
| 78 | |
| 79 | $result = $this->renderTemplate('backup/modal/backup-parts.php', [ |
| 80 | 'backupParts' => $parts, |
| 81 | ]); |
| 82 | wp_send_json($result); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param string $backupDir |
| 87 | * @param string $relativePath |
| 88 | * @return string |
| 89 | */ |
| 90 | private function getFullPath(string $backupDir, string $relativePath): string |
| 91 | { |
| 92 | return $backupDir . str_replace($backupDir, '', wp_normalize_path(untrailingslashit($relativePath))); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param string $type |
| 97 | * @param int $key |
| 98 | * @param string $fileName |
| 99 | * @param string $fullPath |
| 100 | * @param int $totalParts |
| 101 | * @return array |
| 102 | * @throws \WPStaging\Backup\Exceptions\BackupRuntimeException |
| 103 | */ |
| 104 | private function getPart(string $type, int $key, string $fileName, string $fullPath, int $totalParts): array |
| 105 | { |
| 106 | $partName = $type; |
| 107 | $currentKey = $key + 1; |
| 108 | $partType = strtolower(str_replace(' ', '_', $type)); |
| 109 | $partIndex = ''; |
| 110 | if ($totalParts > 1) { |
| 111 | $partIndex .= " {$currentKey} / {$totalParts}"; |
| 112 | } |
| 113 | |
| 114 | return [ |
| 115 | 'partType' => $partType, |
| 116 | 'partIndex' => $partIndex, |
| 117 | 'description' => $this->getPartDescription($partType), |
| 118 | 'icon' => $this->getIcon($partType), |
| 119 | 'name' => $partName, |
| 120 | 'fileSize' => size_format(filesize($fullPath), 2), |
| 121 | 'downloadLink' => $this->urls->getBackupUrl() . $fileName, |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param string $type |
| 127 | * @param array $files |
| 128 | * @param string $backupDir |
| 129 | * @return array |
| 130 | * @throws \WPStaging\Backup\Exceptions\BackupRuntimeException |
| 131 | */ |
| 132 | private function addParts(string $type, array $files, string $backupDir): array |
| 133 | { |
| 134 | $total = count($files); |
| 135 | $parts = []; |
| 136 | |
| 137 | foreach ($files as $key => $fileName) { |
| 138 | $fullPath = $this->getFullPath($backupDir, $fileName); |
| 139 | $parts[] = $this->getPart($type, $key, $fileName, $fullPath, $total); |
| 140 | } |
| 141 | |
| 142 | return $parts; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param string $partType |
| 147 | * @return string |
| 148 | */ |
| 149 | private function getIcon(string $partType): string |
| 150 | { |
| 151 | $icons = [ |
| 152 | 'database' => 'database', |
| 153 | 'plugins' => 'admin-plugins', |
| 154 | 'mu_plugins' => 'plugins-checked', |
| 155 | 'themes' => 'layout', |
| 156 | 'medias' => 'images-alt', |
| 157 | 'others' => 'admin-generic', |
| 158 | 'root_files' => 'root-folder', |
| 159 | ]; |
| 160 | if (isset($icons[$partType])) { |
| 161 | return $icons[$partType]; |
| 162 | } |
| 163 | |
| 164 | return ''; |
| 165 | } |
| 166 | |
| 167 | private function getPartDescription(string $partType): string |
| 168 | { |
| 169 | $partsDesc = [ |
| 170 | 'database' => __('Complete WordPress database with all content and settings.', 'wp-staging'), |
| 171 | 'plugins' => __('All installed WordPress plugins and their configurations.', 'wp-staging'), |
| 172 | 'mu_plugins' => __('Must-use plugins that are always active.', 'wp-staging'), |
| 173 | 'themes' => __('WordPress themes, customizations, and design assets.', 'wp-staging'), |
| 174 | 'medias' => __('Media files such as images or documents in the media library.', 'wp-staging'), |
| 175 | 'others' => __('Files in wp-content excl. plugins, themes, uploads and mu-plugins.', 'wp-staging'), |
| 176 | 'root_files' => __('Root folders only: excludes wp-config.php and staging sites.', 'wp-staging'), |
| 177 | ]; |
| 178 | |
| 179 | if (isset($partsDesc[$partType])) { |
| 180 | return $partsDesc[$partType]; |
| 181 | } |
| 182 | |
| 183 | return ''; |
| 184 | } |
| 185 | } |
| 186 |