/**
 * A polyfill function for the str_contains() function introduced in PHP 8.
 * Used to support str_contains() in PHP versions prior to 8.
 *
 * @param string $haystack The string to search in.
 * @param string $needle The substring to search for.
 * @return bool Returns true if the substring is found in the given string, false otherwise.
 */
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.
 * 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 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 identified as mobile.
        // 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 find 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 non-mobile.
        return false;
    }
}

/**
 * A function to detect and check for iOS.
 *
 * @return bool Returns true if the mobile device is iOS, false otherwise.
 */
function is_ios() {
    return is_mobile() && preg_match('/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT']);
}

/**
 * A function to detect and check for Android.
 *
 * @return bool Returns true if the mobile device is Android, false otherwise.
 */
function is_android() {
    return is_mobile() && preg_match('/Android/', $_SERVER['HTTP_USER_AGENT']);
}

/* Execute different code based on whether the device is iOS or Android. */
if (is_ios()) {
    // Code for the iOS environment
} elseif(is_android()) {
    // Code for the Android environment
} else {
    // Code for other environments
}