<?php
/**
 * 1MZ Music Player — CRON
 *
 * Apaga contas do leitor não verificadas há mais de 30 dias.
 *
 * @package 1MZ_Music_Player
 */

if ( ! defined( 'ABSPATH' ) ) exit;

add_action( 'init', 'mp_schedule_cron' );

function mp_schedule_cron() {
    if ( ! wp_next_scheduled( 'mp_purge_unverified' ) ) {
        wp_schedule_event( time() + 3600, 'daily', 'mp_purge_unverified' );
    }
}

add_action( 'mp_purge_unverified', 'mp_purge_unverified_accounts' );

function mp_purge_unverified_accounts() {
    global $wpdb;
    $table = $wpdb->prefix . 'mp_accounts';
    // 30 dias em segundos.
    $cutoff = gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS );
    $wpdb->query( $wpdb->prepare(
        "DELETE FROM {$table} WHERE verified = 0 AND created_at < %s",
        $cutoff
    ) );
}
