<?php
/**
 * Functions for Hello Elementor Child Theme
 */

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

add_action( 'wp_enqueue_scripts', 'hello_elementor_child_enqueue_styles' );
function hello_elementor_child_enqueue_styles() {
	wp_enqueue_style(
		'hello-elementor-child-style',
		get_stylesheet_uri(),
		[ 'hello-elementor-theme-style' ],
		wp_get_theme()->get( 'Version' )
	);
}

/**
 * Force absolute site URLs during WooCommerce checkout / payment processing.
 */
function yucci_is_checkout_context() {
	if ( function_exists( 'is_checkout' ) && is_checkout() ) {
		return true;
	}

	if ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'order-pay' ) ) {
		return true;
	}

	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
		return true;
	}

	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		return true;
	}

	return false;
}

function yucci_absolute_site_base() {
	return 'https://yucci.nl';
}

add_filter( 'pre_option_home', 'yucci_force_home_option_absolute', 9999 );
function yucci_force_home_option_absolute( $value ) {
	if ( yucci_is_checkout_context() ) {
		return yucci_absolute_site_base();
	}
	return $value;
}

add_filter( 'pre_option_siteurl', 'yucci_force_siteurl_option_absolute', 9999 );
function yucci_force_siteurl_option_absolute( $value ) {
	if ( yucci_is_checkout_context() ) {
		return yucci_absolute_site_base();
	}
	return $value;
}

function yucci_make_absolute_url( $url ) {
	if ( empty( $url ) || ! is_string( $url ) ) {
		return $url;
	}

	if ( 0 === strpos( $url, 'http://' ) || 0 === strpos( $url, 'https://' ) ) {
		return $url;
	}

	if ( 0 === strpos( $url, '/' ) ) {
		return yucci_absolute_site_base() . $url;
	}

	return yucci_absolute_site_base() . '/' . ltrim( $url, '/' );
}

add_filter( 'woocommerce_get_checkout_order_received_url', 'yucci_force_absolute_order_received_url', 9999, 2 );
function yucci_force_absolute_order_received_url( $url, $order ) {
	return yucci_make_absolute_url( $url );
}

add_filter( 'woocommerce_get_return_url', 'yucci_force_absolute_return_url', 9999, 2 );
function yucci_force_absolute_return_url( $url, $order = null ) {
	return yucci_make_absolute_url( $url );
}
add_filter('mollie_wc_gateway_return_url', function ($url, $order) {
    if (empty($url)) {
        return $url;
    }

    // Maak ALTIJD absolute URL
    if (strpos($url, 'http') !== 0) {
        $url = 'https://yucci.nl' . $url;
    }

    return $url;
}, 9999, 2);