<?php
if(class_exists('PeepSoMaintenanceFactory')) {
    class PeepSoMaintenanceCore extends PeepSoMaintenanceFactory
    {
        public static function deleteOldNotifications()
        {
            global $wpdb;

            $days = 30;

            if(defined('PEEPSO_DELETE_NOTIFICATIONS_AFTER_DAYS')) {
                $days = intval(PEEPSO_DELETE_NOTIFICATIONS_AFTER_DAYS);
            }

            if($days == 0) { return 0; }

            // look for items more than 20 days old
            $dt = date('Y-m-d', strtotime($days.' days ago'));

            // TODO: use PeepSoNotifications class constant for table name
            $sql = "DELETE FROM `{$wpdb->prefix}peepso_notifications` " .
                " WHERE CAST(`not_timestamp` AS DATE) < %s ";
            return $wpdb->query($wpdb->prepare($sql, $dt));
        }

        // TODO: delete any mail queue items that are `status`=PeepSoMailQueue::STATUS_SENT and more than 24 hours old

        /**
         * #5471 — remove abandoned "avatar during registration" workspaces. Each visitor's
         * temp upload dir lingers if they never complete registration; purge those older
         * than the (filterable) TTL. Runs on the maintenance tick.
         * @return int number of workspaces removed
         */
        public static function purgeRegistrationAvatars()
        {
            if (!class_exists('PeepSoRegisterAvatarAjax')) {
                return 0;
            }

            $ttl = (int) apply_filters('peepso_registration_avatar_ttl', 2 * HOUR_IN_SECONDS);
            if ($ttl <= 0) {
                return 0;
            }

            return (int) PeepSoRegisterAvatarAjax::purge_old($ttl);
        }
    }
}

// EOF