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