<?php
namespace NeoAnimateBeta\NeoGlobal; if (!defined("ABSPATH")) { die("neoWP direct access not allowed"); }


// Determine the site host exactly as in the Freemius library to avoid discrepancies that could otherwise lead to errors.


function site_host() {
    static $cache = null; return $cache ??= (function () {
        
        $get_site_url_from_wp_option = function ( $blog_id = null ) {
            global $wp_filter;

            $site_url_filters = array(
                'site_url'               => null,
                'pre_option_siteurl'     => null,
                'default_option_siteurl' => null,
                'option_siteurl'         => null,
            );


            foreach ( $site_url_filters as $hook_name => $site_url_filter ) {
                if ( ! empty( $wp_filter[ $hook_name ] ) ) {
                    $site_url_filters[ $hook_name ] = $wp_filter[ $hook_name ];
                    unset( $wp_filter[ $hook_name ] );
                }
            }

            $url = get_site_url( $blog_id );


            foreach ( $site_url_filters as $hook_name => $site_url_filter ) {
                if ( ! empty( $site_url_filter ) ) {
                    $wp_filter[ $hook_name ] = $site_url_filter;
                }
            }

            return $url;
        };

        $fs_starts_with = function ( $haystack, $needle ) {
            $length = strlen( $needle );

            return ( substr( $haystack, 0, $length ) === $needle );
        };

        $fs_strip_url_protocol = function ($url) use ($fs_starts_with) {
            if ( ! $fs_starts_with( $url, 'http' ) ) {
                return $url;
            }

            $protocol_pos = strpos( $url, '://' );

            if ( $protocol_pos > 5 ) {
                return $url;
            }

            return substr( $url, $protocol_pos + 3 );
        };

        $get_unfiltered_site_url = function ( $blog_id = null, $strip_protocol = false, $add_trailing_slash = false ) use ($get_site_url_from_wp_option, $fs_strip_url_protocol) {
            $url = ( ! is_multisite() && defined( 'WP_SITEURL' ) ) ? WP_SITEURL : $get_site_url_from_wp_option( $blog_id );

            if ( $strip_protocol ) {
                $url = $fs_strip_url_protocol( $url );
            }

            if ( $add_trailing_slash ) {
                $url = trailingslashit( $url );
            }

            return $url;
        };

        return $get_unfiltered_site_url(null, strip_protocol: true, add_trailing_slash: false);
    })();
}
