prefix . 'monsterinsights_cache'; // Delete all expired entries in one query // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is safely constructed. $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE expires_at < %s", current_time( 'mysql' ) ) ); // phpcs:enable return $deleted; } /** * Get cleanup statistics. * * Returns counts of expired vs valid cache entries. * Useful for monitoring cache health. * * @since 9.11.0 * @return array { * Cache statistics. * * @type int $total_entries Total number of cache entries. * @type int $valid_entries Number of non-expired entries. * @type int $expired_entries Number of expired entries. * } */ public static function get_cleanup_stats() { global $wpdb; $table_name = $wpdb->prefix . 'monsterinsights_cache'; $now = current_time( 'mysql' ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is safely constructed. $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$table_name}" ); $valid = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table_name} WHERE expires_at >= %s", $now ) ); // phpcs:enable return array( 'total_entries' => (int) $total, 'valid_entries' => (int) $valid, 'expired_entries' => (int) ( $total - $valid ), ); } } // Initialize cleanup hooks MonsterInsights_Cache_Cleanup::init();