/**
 * A polyfill function for str_contains(), which was introduced in PHP 8.
 * This function allows the use of str_contains() in PHP versions below 8.
 *
 * @param string $haystack The string to search in.
 * @param string $needle The substring to search for.
 * @return bool Returns true if the given string contains the substring; otherwise, returns false.
 */
if (!function_exists('str_contains')) {
    /*
     * Polyfill for the str_contains() function.
     * Source: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/compat.php#L423
    */
    function str_contains($haystack, $needle) {
        if ('' === $needle) {
            return true;
        }
        return false !== strpos($haystack, $needle);
    }
}

/**
 * A function to detect and check for mobile devices.
 * It determines whether a device is mobile by checking the HTTP_SEC_CH_UA_MOBILE header 
 * and analyzing the HTTP_USER_AGENT.
 *
 * @return bool Returns true if a mobile device is detected; otherwise, returns false.
 */
function is_mobile() {
    if (isset($_SERVER['HTTP_SEC_CH_UA_MOBILE'])) {
        // If the HTTP_SEC_CH_UA_MOBILE header exists and its value is '?1', it is considered a mobile device.
        // Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile
        return ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ); 
    } elseif (!empty($_SERVER['HTTP_USER_AGENT'])) {
        // Analyzes the HTTP_USER_AGENT to identify mobile characteristics.
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        return str_contains($user_agent, 'Mobile')
            || str_contains($user_agent, 'Android')
            || str_contains($user_agent, 'Silk/')
            || str_contains($user_agent, 'Kindle')
            || str_contains($user_agent, 'BlackBerry')
            || str_contains($user_agent, 'Opera Mini')
            || str_contains($user_agent, 'Opera Mobi');
    } else {
        // If neither the HTTP_SEC_CH_UA_MOBILE header nor the HTTP_USER_AGENT is present,
        // it is considered a non-mobile device.
        return false;
    }
}

/* Execute different code depending on whether the device is mobile. */
if(is_mobile()) {
    // Code for mobile environments
} else {
    // Code for desktop environments
}