| 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 |
|