PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.9.5
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / Ajax / FileInfo.php
wp-staging / Backup / Ajax Last commit date
Backup 2 weeks ago FileList 6 days ago Restore 3 months ago Backup.php 2 years ago BackupDownloader.php 1 month ago BackupSizeCalculator.php 1 month ago BackupSpeedIndex.php 10 months ago BaseFileList.php 1 year ago BaseListing.php 10 months ago Delete.php 6 days ago Edit.php 6 days ago Explore.php 4 months ago ExploreCache.php 4 months ago FileInfo.php 6 days ago FileList.php 1 year ago Listing.php 8 months ago Parts.php 6 days ago Restore.php 2 years ago ScheduleList.php 1 year ago Upload.php 2 months ago
FileInfo.php
116 lines
1 <?php
2
3 namespace WPStaging\Backup\Ajax;
4
5 use Exception;
6 use WPStaging\Backup\Entity\BackupMetadata;
7 use WPStaging\Backup\Task\RestoreTask;
8 use WPStaging\Backup\Task\Tasks\JobRestore\CleanExistingMediaTask;
9 use WPStaging\Backup\Task\Tasks\JobRestore\RestoreMuPluginsTask;
10 use WPStaging\Backup\Task\Tasks\JobRestore\RestoreOtherFilesInWpContentTask;
11 use WPStaging\Backup\Task\Tasks\JobRestore\RestorePluginsTask;
12 use WPStaging\Backup\Task\Tasks\JobRestore\RestoreThemesTask;
13 use WPStaging\Framework\Component\AbstractTemplateComponent;
14 use WPStaging\Framework\TemplateEngine\TemplateEngine;
15 use WPStaging\Framework\Facades\Hooks;
16 use WPStaging\Framework\Filesystem\PartIdentifier;
17 use WPStaging\Backup\Utils\BackupPathResolver;
18
19 class FileInfo extends AbstractTemplateComponent
20 {
21 /** @var BackupPathResolver */
22 private $backupPathResolver;
23
24 /** @var string[] */
25 private $excludedBackupParts;
26
27 public function __construct(TemplateEngine $templateEngine, BackupPathResolver $backupPathResolver)
28 {
29 parent::__construct($templateEngine);
30 $this->backupPathResolver = $backupPathResolver;
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 $file = $this->backupPathResolver->resolveBackupPath($filePath);
44 if ($file === '') {
45 wp_send_json([
46 'error' => true,
47 'message' => 'Invalid backup file path!',
48 ]);
49 }
50
51 try {
52 $info = (new BackupMetadata())->hydrateByFilePath($file);
53 } catch (Exception $e) {
54 wp_send_json([
55 'error' => true,
56 'message' => $e->getMessage(),
57 ]);
58 }
59
60 $this->excludedBackupParts = Hooks::applyFilters(RestoreTask::FILTER_EXCLUDE_BACKUP_PARTS, []);
61
62 $excluded = [
63 'database' => $this->isBackupPartExcluded(PartIdentifier::DATABASE_PART_IDENTIFIER),
64 'plugins' => $this->isBackupPartExcluded(PartIdentifier::PLUGIN_PART_IDENTIFIER),
65 'themes' => $this->isBackupPartExcluded(PartIdentifier::THEME_PART_IDENTIFIER),
66 'muPlugins' => $this->isBackupPartExcluded(PartIdentifier::MU_PLUGIN_PART_IDENTIFIER),
67 'uploads' => $this->isBackupPartExcluded(PartIdentifier::UPLOAD_PART_IDENTIFIER),
68 'wpContent' => $this->isBackupPartExcluded(PartIdentifier::WP_CONTENT_PART_IDENTIFIER),
69 'wpRoot' => $this->isBackupPartExcluded(PartIdentifier::WP_ROOT_PART_IDENTIFIER),
70 ];
71
72 $replaced = [
73 'plugins' => $this->isBackupPartReplaced(RestorePluginsTask::FILTER_KEEP_EXISTING_PLUGINS, $info),
74 'themes' => $this->isBackupPartReplaced(RestoreThemesTask::FILTER_KEEP_EXISTING_THEMES, $info),
75 'muPlugins' => $this->isBackupPartReplaced(RestoreMuPluginsTask::FILTER_KEEP_EXISTING_MUPLUGINS, $info),
76 'wpContent' => $this->isBackupPartReplaced(RestoreOtherFilesInWpContentTask::FILTER_KEEP_EXISTING_OTHER_FILES, $info),
77 'wpRoot' => false, // Other files in WP root folder are not cleaned up before restoring, no need to use filter then.
78 // Each subsite has separate uploads folder, so only filter check is needed!
79 'uploads' => !Hooks::applyFilters(CleanExistingMediaTask::FILTER_KEEP_EXISTING_MEDIA, false),
80 ];
81
82 $viewData = [
83 'info' => $info,
84 'excluded' => $excluded,
85 'replaced' => $replaced,
86 ];
87
88 $result = $this->templateEngine->render(
89 'backup/modal/confirm-restore.php',
90 $viewData
91 );
92
93 wp_send_json($result);
94 }
95
96 protected function isBackupPartExcluded(string $partName): bool
97 {
98 if (empty($this->excludedBackupParts)) {
99 return false;
100 }
101
102 return in_array($partName, $this->excludedBackupParts);
103 }
104
105 protected function isBackupPartReplaced(string $filter, BackupMetadata $metadata): bool
106 {
107 // Should only be allowed to replace when restoring on single site or when restoring a full network backup
108 if (!is_multisite() || $metadata->getBackupType() === BackupMetadata::BACKUP_TYPE_MULTISITE) {
109 // If filter returns true, it means the existing files should be kept not replaced
110 return !Hooks::applyFilters($filter, false);
111 }
112
113 return false;
114 }
115 }
116