Filters
2 years ago
Scanning
5 years ago
DebugLogReader.php
2 years ago
DirectoryListing.php
2 years ago
DiskWriteCheck.php
3 years ago
FileObject.php
2 years ago
Filesystem.php
2 years ago
FilesystemExceptions.php
5 years ago
FilterableDirectoryIterator.php
2 years ago
LogCleanup.php
2 years ago
LogFiles.php
2 years ago
MissingFileException.php
3 years ago
OPcache.php
2 years ago
PathChecker.php
2 years ago
PathIdentifier.php
2 years ago
Permissions.php
5 years ago
WpUploadsFolderSymlinker.php
4 years ago
LogCleanup.php
47 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Filesystem; |
| 4 | |
| 5 | use WPStaging\Core\Utils\Logger; |
| 6 | |
| 7 | class LogCleanup |
| 8 | { |
| 9 | protected $logger; |
| 10 | |
| 11 | public function __construct(Logger $logger) |
| 12 | { |
| 13 | $this->logger = $logger; |
| 14 | } |
| 15 | |
| 16 | public function cleanOldLogs() |
| 17 | { |
| 18 | try { |
| 19 | $it = new \DirectoryIterator($this->logger->getLogDir()); |
| 20 | } catch (\Exception $e) { |
| 21 | // Early bail: Couldn't open directory. |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | // Delete logs older than 14 days by default |
| 26 | $deleteOlderThanDays = absint(apply_filters('wpstg.logs.deleteOlderThanDays', 14)); |
| 27 | |
| 28 | // Delete logs bigger than 5mb by default |
| 29 | $deleteBiggerThan = absint(apply_filters('wpstg.logs.deleteBiggerThanBytes', 5 * MB_IN_BYTES)); |
| 30 | |
| 31 | /** @var \SplFileInfo $splFileInfo */ |
| 32 | foreach ($it as $splFileInfo) { |
| 33 | if ($splFileInfo->isFile() && !$splFileInfo->isLink() && $splFileInfo->getExtension() === 'log') { |
| 34 | if ($splFileInfo->getSize() > $deleteBiggerThan) { |
| 35 | unlink($splFileInfo->getPathname()); |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | if ($splFileInfo->getMTime() < strtotime("-$deleteOlderThanDays days")) { |
| 40 | // Not silenced nor logged |
| 41 | unlink($splFileInfo->getPathname()); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 |