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
FileInfo.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Ajax; |
| 4 | |
| 5 | use Exception; |
| 6 | use WPStaging\Backup\Entity\BackupMetadata; |
| 7 | use WPStaging\Backup\Service\BackupsFinder; |
| 8 | use WPStaging\Backup\Task\RestoreTask; |
| 9 | use WPStaging\Backup\Task\Tasks\JobRestore\CleanExistingMediaTask; |
| 10 | use WPStaging\Backup\Task\Tasks\JobRestore\RestoreMuPluginsTask; |
| 11 | use WPStaging\Backup\Task\Tasks\JobRestore\RestoreOtherFilesInWpContentTask; |
| 12 | use WPStaging\Backup\Task\Tasks\JobRestore\RestorePluginsTask; |
| 13 | use WPStaging\Backup\Task\Tasks\JobRestore\RestoreThemesTask; |
| 14 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 15 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 16 | use WPStaging\Framework\Facades\Hooks; |
| 17 | use WPStaging\Framework\Filesystem\PartIdentifier; |
| 18 | |
| 19 | class FileInfo extends AbstractTemplateComponent |
| 20 | { |
| 21 | /** @var BackupsFinder */ |
| 22 | private $backupsFinder; |
| 23 | |
| 24 | /** @var string[] */ |
| 25 | private $excludedBackupParts; |
| 26 | |
| 27 | public function __construct(TemplateEngine $templateEngine, BackupsFinder $backupsFinder) |
| 28 | { |
| 29 | parent::__construct($templateEngine); |
| 30 | $this->backupsFinder = $backupsFinder; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return void |
| 35 | */ |
| 36 | public function render() |
| 37 | { |
| 38 | if (!$this->canRenderAjax()) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | $filePath = isset($_POST['filePath']) ? sanitize_text_field($_POST['filePath']) : ''; |
| 43 | // Make sure path is inside Backups Directory |
| 44 | $backupDir = wp_normalize_path($this->backupsFinder->getBackupsDirectory()); |
| 45 | $file = $backupDir . str_replace($backupDir, '', wp_normalize_path(untrailingslashit($filePath))); |
| 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 | $this->excludedBackupParts = Hooks::applyFilters(RestoreTask::FILTER_EXCLUDE_BACKUP_PARTS, []); |
| 57 | |
| 58 | $excluded = [ |
| 59 | 'database' => $this->isBackupPartExcluded(PartIdentifier::DATABASE_PART_IDENTIFIER), |
| 60 | 'plugins' => $this->isBackupPartExcluded(PartIdentifier::PLUGIN_PART_IDENTIFIER), |
| 61 | 'themes' => $this->isBackupPartExcluded(PartIdentifier::THEME_PART_IDENTIFIER), |
| 62 | 'muPlugins' => $this->isBackupPartExcluded(PartIdentifier::MU_PLUGIN_PART_IDENTIFIER), |
| 63 | 'uploads' => $this->isBackupPartExcluded(PartIdentifier::UPLOAD_PART_IDENTIFIER), |
| 64 | 'wpContent' => $this->isBackupPartExcluded(PartIdentifier::WP_CONTENT_PART_IDENTIFIER), |
| 65 | 'wpRoot' => $this->isBackupPartExcluded(PartIdentifier::WP_ROOT_PART_IDENTIFIER), |
| 66 | ]; |
| 67 | |
| 68 | $replaced = [ |
| 69 | 'plugins' => $this->isBackupPartReplaced(RestorePluginsTask::FILTER_KEEP_EXISTING_PLUGINS, $info), |
| 70 | 'themes' => $this->isBackupPartReplaced(RestoreThemesTask::FILTER_KEEP_EXISTING_THEMES, $info), |
| 71 | 'muPlugins' => $this->isBackupPartReplaced(RestoreMuPluginsTask::FILTER_KEEP_EXISTING_MUPLUGINS, $info), |
| 72 | 'wpContent' => $this->isBackupPartReplaced(RestoreOtherFilesInWpContentTask::FILTER_KEEP_EXISTING_OTHER_FILES, $info), |
| 73 | 'wpRoot' => false, // Other files in WP root folder are not cleaned up before restoring, no need to use filter then. |
| 74 | // Each subsite has separate uploads folder, so only filter check is needed! |
| 75 | 'uploads' => !Hooks::applyFilters(CleanExistingMediaTask::FILTER_KEEP_EXISTING_MEDIA, false), |
| 76 | ]; |
| 77 | |
| 78 | $viewData = [ |
| 79 | 'info' => $info, |
| 80 | 'excluded' => $excluded, |
| 81 | 'replaced' => $replaced, |
| 82 | ]; |
| 83 | |
| 84 | $result = $this->templateEngine->render( |
| 85 | 'backup/modal/confirm-restore.php', |
| 86 | $viewData |
| 87 | ); |
| 88 | |
| 89 | wp_send_json($result); |
| 90 | } |
| 91 | |
| 92 | protected function isBackupPartExcluded(string $partName): bool |
| 93 | { |
| 94 | if (empty($this->excludedBackupParts)) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | return in_array($partName, $this->excludedBackupParts); |
| 99 | } |
| 100 | |
| 101 | protected function isBackupPartReplaced(string $filter, BackupMetadata $metadata): bool |
| 102 | { |
| 103 | // Should only be allowed to replace when restoring on single site or when restoring a full network backup |
| 104 | if (!is_multisite() || $metadata->getBackupType() === BackupMetadata::BACKUP_TYPE_MULTISITE) { |
| 105 | // If filter returns true, it means the existing files should be kept not replaced |
| 106 | return !Hooks::applyFilters($filter, false); |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 |