Fix log file deletion functionality.

This commit is contained in:
Gerhard Potgieter 2018-07-09 13:01:23 +02:00
parent 404fbca655
commit 1fdcf2a238
4 changed files with 6 additions and 8 deletions

View File

@ -162,7 +162,7 @@ class WC_Admin_Status {
* @return string
*/
public static function get_log_file_handle( $filename ) {
return substr( $filename, 0, strlen( $filename ) > 37 ? strlen( $filename ) - 37 : strlen( $filename ) - 4 );
return substr( $filename, 0, strlen( $filename ) > 48 ? strlen( $filename ) - 48 : strlen( $filename ) - 4 );
}
/**

View File

@ -15,8 +15,8 @@ if ( ! defined( 'ABSPATH' ) ) {
<div class="alignleft">
<h2>
<?php echo esc_html( $viewed_log ); ?>
<?php if ( ! empty( $handle ) ) : ?>
<a class="page-title-action" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'handle' => $handle ), admin_url( 'admin.php?page=wc-status&tab=logs' ) ), 'remove_log' ) ); ?>" class="button"><?php esc_html_e( 'Delete log', 'woocommerce' ); ?></a>
<?php if ( ! empty( $viewed_log ) ) : ?>
<a class="page-title-action" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'handle' => $viewed_log ), admin_url( 'admin.php?page=wc-status&tab=logs' ) ), 'remove_log' ) ); ?>" class="button"><?php esc_html_e( 'Delete log', 'woocommerce' ); ?></a>
<?php endif; ?>
</h2>
</div>

View File

@ -248,16 +248,14 @@ class WC_Log_Handler_File extends WC_Log_Handler {
*/
public function remove( $handle ) {
$removed = false;
$file = self::get_log_file_path( $handle );
$file = trailingslashit( WC_LOG_DIR ) . $handle;
if ( $file ) {
if ( is_file( $file ) && is_writable( $file ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
$this->close( $handle ); // Close first to be certain no processes keep it alive after it is unlinked.
$this->close( $file ); // Close first to be certain no processes keep it alive after it is unlinked.
$removed = unlink( $file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink
}
do_action( 'woocommerce_log_remove', $handle, $removed );
}
return $removed;
}

View File

@ -75,7 +75,7 @@ class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_File();
$log_name = '_test_remove';
$handler->handle( time(), 'debug', 'debug', array( 'source' => $log_name ) );
$handler->remove( $log_name );
$handler->remove( wc_get_log_file_name( $log_name ) );
$this->assertFileNotExists( WC_Log_Handler_File::get_log_file_path( $log_name ) );
}