| 1 |
<?php |
| 2 |
namespace StoreEngine\Classes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class LogCleanup { |
| 9 |
|
| 10 |
/** |
| 11 |
* main cleanup logic |
| 12 |
*/ |
| 13 |
public static function execute_cleanup() { |
| 14 |
global $wpdb; |
| 15 |
$table = $wpdb->prefix . 'storeengine_logs'; |
| 16 |
|
| 17 |
// admin setting |
| 18 |
$settings = get_option( 'storeengine_log_settings',[ |
| 19 |
'retention_days' => 30, |
| 20 |
'cleanup_statuses' => [ 'success' ] |
| 21 |
] ); |
| 22 |
|
| 23 |
$days = (int) ( $settings['retention_days'] ?? 30 ); |
| 24 |
$statuses = $settings['cleanup_statuses'] ?? [ 'success' ]; |
| 25 |
|
| 26 |
if ( $days <= 0 || empty( $statuses ) || ! is_array( $statuses ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// date calculation — UTC, matching the UTC timestamps the logger writes. |
| 31 |
$threshold_date = gmdate( 'Y-m-d H:i:s', time() - ( $days * DAY_IN_SECONDS ) ); |
| 32 |
|
| 33 |
// SQL query — table via %i identifier placeholder, all values via prepare(). |
| 34 |
$placeholders = implode( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
| 35 |
|
| 36 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Dynamic IN() list on a custom StoreEngine log table: %i identifier + every value (%s) bound through prepare(); $placeholders holds only literal "%s" tokens. The static analyzer cannot count the array_merge() replacement args. |
| 37 |
$wpdb->query( |
| 38 |
$wpdb->prepare( |
| 39 |
"DELETE FROM %i WHERE date < %s AND status IN ($placeholders)", |
| 40 |
array_merge( [ $table, $threshold_date ], $statuses ) |
| 41 |
) |
| 42 |
); |
| 43 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
| 44 |
} |
| 45 |
} |