<?php

 /**
 * =============================================================================
 * @package		        Blue Raven (Free Version)
 * Plugin URI		    https://enterrahost.com/plugins/blue-raven
 * 
 * @Author		        EnterraHost
 * @Author URI		    https://enterrahost.com
 * @License		        GPL-2.0-or-later
 *  
 * @Copyright		    (c) 2025 EnterraHost
 * @File Description    Log Reports to Enterrahost
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - ERROR LOGGING
 * ||   - EMERGENCY EMAIL ALERT
 * || 
 * =============================================================================
 */

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

/*
============================================================
    ERROR LOGGING
============================================================
*/

function BlueRaven_SendError($errorType, $file, $context, $description, $super = false) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'BlueRavenConfig';
    $table_name_log = $wpdb->prefix . 'BlueRavenLogs';

    $eTypeDebugging = $wpdb->get_var($wpdb->prepare("SELECT value FROM $table_name WHERE setting = %s",'eType_debugging'));
    
    if (isset($eTypeDebugging) && filter_var($eTypeDebugging, FILTER_VALIDATE_BOOLEAN)){
        $wpdb->insert(
            $table_name_log,
            [
                'type'        => sanitize_text_field($errorType),
                'file'        => sanitize_text_field($file),
                'context'     => sanitize_text_field($context),
                'description' => sanitize_textarea_field($description),
            ]
        );
    }
}

/*
============================================================
    CLEAR
============================================================
*/

global $wpdb;
$table_name_clear = $wpdb->prefix . 'BlueRavenLogs';

if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name_clear}'" ) === $table_name_clear ) {
    $wpdb->query(
        $wpdb->prepare(
            "DELETE FROM $table_name_clear WHERE created_at < %s",
            gmdate('Y-m-d H:i:s', strtotime('-1 month'))
        )
    );
}