Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Michael Iseard
Kudos-Donations
Commits
09a5dd49
Commit
09a5dd49
authored
Oct 14, 2021
by
Michael Iseard
Browse files
Truncate log automatically after 100 rows
parent
1e7e81d4
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/Controller/Admin.php
View file @
09a5dd49
...
...
@@ -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.
...
...
app/KudosDonations.php
View file @
09a5dd49
...
...
@@ -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'
]
);
}
...
...
app/Service/ActivatorService.php
View file @
09a5dd49
...
...
@@ -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'
);
}
/**
...
...
app/Service/LoggerService.php
View file @
09a5dd49
...
...
@@ -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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment