<?php

/*
* Logout redirection hook
*/

add_action('wp_logout', 'saprfurl_custom_logout_redirect');
function saprfurl_custom_logout_redirect() {
    $logout_redirect_url = get_option('saprfurl_user_redirect_url_after_logout', '');

    if (!empty($logout_redirect_url)) {
        wp_redirect(esc_url_raw($logout_redirect_url));
        exit;
    }
}


/* This block is for register redirection */

add_action('user_register', 'saprfurl_redirect_after_registration', 10, 1);
function saprfurl_redirect_after_registration($user_id) {
    if (wp_doing_ajax()) {
        return; // Avoid interfering with AJAX requests
    }

    // Get redirect URL from plugin settings
    $saprfurl_redirect_url = get_option('saprfurl_user_redirect_url_after_registration', '');

    if (!empty($saprfurl_redirect_url)) {
        // Delay redirect slightly to let registration complete
        wp_safe_redirect(esc_url_raw($saprfurl_redirect_url));
        exit;
    }
}
