PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.2
Defender Security – Malware Scanner, Login Security & Firewall v6.2.2
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / logger / class-rotation-logger.php

class-rotation-logger.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at src/component/logger/class-rotation-logger.php

104 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The log rotation class.
4 *
5 * @package WP_Defender\Component\Logger
6 */
7
8 namespace WP_Defender\Component\Logger;
9
10 use GlobIterator;
11 use WP_Defender\Traits\IO;
12 use WP_Defender\Component\Network_Cron_Manager;
13
14 /**
15 * Deals rotation log file naming & purging old logs.
16 */
17 class Rotation_Logger implements Rotation_Logger_Interface {
18
19 use IO;
20
21 public const ROTATION_FREQUENCY = 7;
22 public const ROTATION_UNIT = 'day';
23
24 /**
25 * Class constructor to clear file status cache.
26 */
27 public function __construct() {
28 clearstatcache();
29 }
30
31 /**
32 * Generate a new filename with date suffix.
33 *
34 * @param string $filename Filename which requires date suffix.
35 *
36 * @return string Formatted date suffixed file name.
37 */
38 public function generate_file_name( $filename ) {
39 $file_parts = pathinfo( $filename );
40
41 $file_name = $file_parts['filename'] . wp_date( '-Y-m-d' );
42
43 $directory_path = $this->get_tmp_path();
44 $iterator = new GlobIterator( $directory_path . '/' . $file_name . '*' );
45
46 if ( file_exists( $iterator->getPathname() ) ) {
47 $file_name = $iterator->getBasename();
48 } else {
49 $file_name = uniqid( $file_name . '-' );
50
51 if ( isset( $file_parts['extension'] ) && '' !== $file_parts['extension'] ) {
52 $file_name .= '.' . $file_parts['extension'];
53 }
54 }
55
56 return $file_name;
57 }
58
59 /**
60 * Purge logs whose modified time exceeded the threshold limit.
61 *
62 * @param string $directory_path Pathname of the directory.
63 * @param int $count A period as integer value.
64 * @param string $unit A standard datetime relative format unit, like days or months.
65 */
66 public function purge_old_log(
67 $directory_path = '',
68 $count = self::ROTATION_FREQUENCY,
69 $unit = self::ROTATION_UNIT
70 ) {
71 $threshold_timestamp = strtotime( '-' . $count . $unit );
72 if ( '' === $directory_path ) {
73 $directory_path = $this->get_tmp_path();
74 }
75 $iterator = new GlobIterator( $directory_path . '/*.log' );
76
77 while ( $iterator->valid() ) {
78
79 if ( $iterator->getMTime() < $threshold_timestamp ) {
80 wp_delete_file( $iterator->getPathname() );
81 }
82
83 $iterator->next();
84 }
85 }
86
87 /**
88 * Invoke all init methods.
89 */
90 public function init() {
91 /**
92 * Delete old logs rotationally.
93 *
94 * @var Network_Cron_Manager $network_cron_manager
95 */
96 $network_cron_manager = wd_di()->get( Network_Cron_Manager::class );
97 $network_cron_manager->register_callback(
98 'wpdef_log_rotational_delete',
99 array( $this, 'purge_old_log' ),
100 DAY_IN_SECONDS
101 );
102 }
103 }
104