Commit 09a5dd49 authored by Michael Iseard's avatar Michael Iseard
Browse files

Truncate log automatically after 100 rows

parent 1e7e81d4
......@@ -11,6 +11,7 @@ use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Assets;
use Kudos\Helpers\Settings;
use Kudos\Helpers\WpDb;
use Kudos\Service\ActivatorService;
use Kudos\Service\AdminNotice;
use Kudos\Service\LoggerService;
......@@ -559,6 +560,14 @@ class Admin {
}
/**
* Truncates the log file when over certain length.
* Length defined by LoggerService::TRUNCATE_AT const.
*/
public function truncate_log() {
LoggerService::truncate();
}
/**
* Removes the secret for the specified entity where
* it matches the provided id.
......
......@@ -90,6 +90,7 @@ class KudosDonations {
add_action( 'admin_init', [ $plugin_admin, 'admin_actions' ] );
add_action( 'rest_api_init', [ $plugin_admin, 'register_settings' ] );
add_action( 'kudos_remove_secret_action', [ $plugin_admin, 'remove_secret_action' ], 10, 2 );
add_action( 'kudos_check_log', [ $plugin_admin, 'truncate_log' ] );
add_action( 'enqueue_block_editor_assets', [ $plugin_admin, 'register_block_editor_assets' ] );
}
......
......@@ -6,6 +6,7 @@ use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Helpers\WpDb;
/**
......@@ -73,6 +74,9 @@ class ActivatorService {
update_option( '_kudos_donations_version', KUDOS_VERSION );
$logger->info( 'Kudos Donations plugin activated', ['version' => KUDOS_VERSION] );
// Schedule log file clearing.
Utils::schedule_recurring_action( strtotime( 'today midnight' ), DAY_IN_SECONDS, 'kudos_check_log' );
}
/**
......
......@@ -9,6 +9,8 @@ use Kudos\Helpers\WpDb;
class LoggerService extends Logger {
const TRUNCATE_AT = 100;
/**
* Table name without prefix
*
......@@ -22,7 +24,7 @@ class LoggerService extends Logger {
public function __construct( WpDb $wpdb ) {
parent::__construct(
'kudos',
[ new DatabaseHandler($wpdb) ],
[ new DatabaseHandler( $wpdb ) ],
[],
new DateTimeZone( wp_timezone_string() )
);
......@@ -50,7 +52,9 @@ class LoggerService extends Logger {
public static function get_table_name(): string {
global $wpdb;
/** @var \wpdb $wpdb */
$wpdb = new WpDb();
return $wpdb->prefix . self::TABLE;
}
......@@ -62,12 +66,42 @@ class LoggerService extends Logger {
*/
public static function clear() {
global $wpdb;
$table = $wpdb->prefix . self::TABLE;
return $wpdb->query("TRUNCATE TABLE `{$table}`");
/** @var \wpdb $wpdb */
$wpdb = new WpDb();
$table = self::get_table_name();
return $wpdb->query( "TRUNCATE TABLE `{$table}`" );
}
/**
* Truncates the log table keeping
* the last 'TRUNCATE_AT' records.
*
* @return bool|int
*/
public static function truncate() {
/** @var \wpdb $wpdb */
$wpdb = new WpDb();
$table = self::get_table_name();
$last_row = $wpdb->get_row( $wpdb->prepare("
SELECT `id` FROM {$table}
LIMIT %d,1
", (self::TRUNCATE_AT - 1) ) );
if($last_row) {
$last_id = $last_row->id;
return $wpdb->query($wpdb->prepare("
DELETE FROM {$table}
WHERE `id` > %d
", $last_id));
}
return false;
}
/**
* Returns the log table contents as an array.
*
......@@ -75,8 +109,9 @@ class LoggerService extends Logger {
*/
public static function get_as_array() {
global $wpdb;
$table = $wpdb->prefix . self::TABLE;
return $wpdb->get_results("SELECT * FROM {$table} ORDER BY `date` DESC LIMIT 100",ARRAY_A);
$table = self::get_table_name();
return $wpdb->get_results( "SELECT * FROM {$table} ORDER BY `id` DESC LIMIT 100", ARRAY_A );
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment