<?php


function dragblock_get_current_browser()
{
    static $browser_name = null;
    if ($browser_name !== null) {
        // Return the cached result if available
        return $browser_name;
    }

    $browser_name = '';

    if (empty($_SERVER['HTTP_USER_AGENT'])) {
        return $browser_name;
    }
    $user_agent =  sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT']));


    if (strpos($user_agent, 'Chrome') !== false) {
        $browser_name = 'chrome';
    } elseif (strpos($user_agent, 'Firefox') !== false) {
        $browser_name = 'firefox';
    } elseif (strpos($user_agent, 'Edge') !== false) {
        $browser_name = 'edge';
    } elseif (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident/') !== false) {
        $browser_name = 'ie';
    } elseif (strpos($user_agent, 'Opera') !== false) {
        $browser_name = 'opera';
    } elseif (strpos($user_agent, 'Safari') !== false) {
        $browser_name = 'safari';
    } elseif (strpos($user_agent, 'SamsungBrowser') !== false) {
        $browser_name = 'samsungi';
    }

    return strtolower($browser_name);
}


function dragblock_get_current_device()
{
    static $device = null;
    if ($device !== null) {
        // Return the cached result if available
        return $device;
    }
    $device = 'desktop';

    if (empty($_SERVER['HTTP_USER_AGENT'])) {
        return $device;
    }
    $user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT']));

    $tablet_patterns = array(
        '/ipad/i',
        '/android(?!.*mobile)/i',
    );

    $mobile_patterns = array(
        '/iphone/i',
        '/ipod/i',
        '/android.*mobile/i',
        '/windows phone/i',
        '/blackberry/i',
        '/nokia/i',
        '/sony/i',
        '/lg/i',
        '/htc/i',
        '/mot/i',
        '/samsung/i',
        '/palm/i',
        '/zte/i',
        '/fennec/i',
        '/opera mobi/i',
        '/opera mini/i',
        '/iemobile/i',
    );

    foreach ($tablet_patterns as $pattern) {
        if (preg_match($pattern, $user_agent)) {
            $device = 'tablet';
            break;
        }
    }

    foreach ($mobile_patterns as $pattern) {
        if (preg_match($pattern, $user_agent)) {
            $device = 'mobile';
            break;
        }
    }

    // If no tablet or mobile patterns match, assume it's a desktop device
    return $device;
}


function dragblock_get_current_os()
{
    static $os = null;
    if ($os !== null) {
        // Return the cached result if available
        return $os;
    }
    $os = '';

    if (empty($_SERVER['HTTP_USER_AGENT'])) {
        return $os;
    }
    $user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT']));

    $os_platforms = array(
        'windows' => '/windows/i',
        'linux' => '/linux/i',
        'macintosh' => '/macintosh|mac os x/i',
        'ios' => '/iphone|ipad|ipod/i',
        'android' => '/android/i',
    );

    foreach ($os_platforms as $platform => $pattern) {
        if (preg_match($pattern, $user_agent)) {
            $os = $platform;
        }
    }

    // If no OS/platform patterns match, return 'unknown'
    return $os;
}
