Automattic\WooCommerce\Internal\Admin\Logging
LogHandlerFileV2::delete_logs_before_timestamp
Delete all logs older than a specified timestamp.
Method of the class: LogHandlerFileV2{}
Hooks from the method
Returns
Int. The number of files that were deleted.
Usage
$LogHandlerFileV2 = new LogHandlerFileV2(); $LogHandlerFileV2->delete_logs_before_timestamp( $timestamp ): int;
- $timestamp(int)
- All files created before this timestamp will be deleted.
LogHandlerFileV2::delete_logs_before_timestamp() LogHandlerFileV2::delete logs before timestamp code WC 10.3.5
public function delete_logs_before_timestamp( int $timestamp = 0 ): int {
if ( ! $timestamp ) {
return 0;
}
$files = $this->file_controller->get_files(
array(
'date_filter' => 'created',
'date_start' => 1,
'date_end' => $timestamp,
)
);
if ( is_wp_error( $files ) ) {
return 0;
}
$files = array_filter(
$files,
function ( $file ) use ( $timestamp ) {
/**
* Allows preventing an expired log file from being deleted.
*
* @param bool $delete True to delete the file.
* @param File $file The log file object.
* @param int $timestamp The expiration threshold.
*
* @since 8.7.0
*/
$delete = apply_filters( 'woocommerce_logger_delete_expired_file', true, $file, $timestamp );
return boolval( $delete );
}
);
if ( count( $files ) < 1 ) {
return 0;
}
$file_ids = array_map(
fn( $file ) => $file->get_file_id(),
$files
);
$deleted = $this->file_controller->delete_files( $file_ids );
$retention_days = $this->settings->get_retention_period();
if ( $deleted > 0 ) {
$this->handle(
time(),
'info',
sprintf(
esc_html(
// translators: %s is a number of log files.
_n(
'%s expired log file was deleted.',
'%s expired log files were deleted.',
$deleted,
'woocommerce'
)
),
number_format_i18n( $deleted )
),
array(
'source' => 'wc_logger',
)
);
}
return $deleted;
}