PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / trunk
WP STAGING – WordPress Backups, Restore, Migration & Clone vtrunk
4.11.1 4.11.0 4.10.0 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 / Service / TmpBackupCleaner.php
wp-staging / Backup / Service Last commit date
Compression 1 week ago Database 1 week ago DirectoryExplorer 1 week ago AbstractBackgroundBackupRequest.php 5 days ago AbstractBackupsFinder.php 1 week ago AbstractExtractor.php 1 week ago AbstractServiceProvider.php 1 week ago Archiver.php 5 days ago BackupAssets.php 1 week ago BackupContent.php 1 week ago BackupMetadataEditor.php 5 days ago BackupMetadataReader.php 1 week ago BackupSigner.php 1 week ago BackupsDirectoryResolver.php 1 week ago BackupsFinder.php 1 week ago BeforeUpdateBackupRequest.php 1 week ago BeforeUpdateBackupsService.php 1 week ago Extractor.php 1 week ago FileBackupService.php 1 week ago FileBackupServiceProvider.php 1 week ago ServiceInterface.php 1 week ago TmpBackupCleaner.php 1 week ago UpdateProtectionHealth.php 1 week ago UpdateProtectionSettings.php 1 week ago ZlibCompressor.php 1 week ago
TmpBackupCleaner.php
104 lines
1 <?php
2
3 namespace WPStaging\Backup\Service;
4
5 use DirectoryIterator;
6 use Exception;
7 use WPStaging\Framework\Network\RemoteDownloader;
8
9
10
11
12 class TmpBackupCleaner
13 {
14
15
16
17
18
19
20
21
22 public function clean(string $directory, int $maxAge = 0, int $now = 0): int
23 {
24 $directory = rtrim($directory, '/\\');
25 if (!is_dir($directory)) {
26 return 0;
27 }
28
29 try {
30 $iterator = new DirectoryIterator($directory);
31 } catch (Exception $e) {
32 return 0;
33 }
34
35 $deletedFiles = 0;
36 foreach ($iterator as $fileInfo) {
37 if ($fileInfo->isDot() || !$fileInfo->isFile()) {
38 continue;
39 }
40
41 if (!$this->isTmpBackup($fileInfo->getFilename())) {
42 continue;
43 }
44
45 $path = $fileInfo->getPathname();
46 if ($maxAge > 0 && !$this->isOlderThan($path, $maxAge, $now)) {
47 continue;
48 }
49
50 if (@unlink($path)) {
51 $deletedFiles++;
52 }
53 }
54
55 return $deletedFiles;
56 }
57
58
59
60
61
62
63
64 private function isTmpBackup(string $filename): bool
65 {
66 $extension = pathinfo($filename, PATHINFO_EXTENSION);
67
68 if ($extension === Archiver::TMP_BACKUP_EXTENSION) {
69 return true;
70 }
71
72 if ($extension !== RemoteDownloader::UPLOADING_EXTENSION) {
73 return false;
74 }
75
76 $filenameWithoutUploading = pathinfo($filename, PATHINFO_FILENAME);
77 $innerExtension = pathinfo($filenameWithoutUploading, PATHINFO_EXTENSION);
78
79 return $innerExtension === Archiver::TMP_BACKUP_EXTENSION;
80 }
81
82
83
84
85
86
87
88
89
90 private function isOlderThan(string $path, int $maxAge, int $now): bool
91 {
92 if ($now <= 0) {
93 $now = time();
94 }
95
96 $mtime = @filemtime($path);
97 if ($mtime === false) {
98 return false;
99 }
100
101 return ($now - $mtime) >= $maxAge;
102 }
103 }
104