<?php
/**
 * Compatibility with "BuddyPress Integration for WooCommerce"
 * URL: https://wordpress.org/plugins/wc4bp/
 *
 * For the integration to work it is necessary to activate the following option:
 * Dashboard > BuddyPress for WooCommerce > Turn on 'Extra Content Tab'
 * which is vailable in the premium version of the plugin: https://themekraft.com/plugins/woobuddy-woocommerce-buddypress-integration/
 * 
 * far funzionare user switcher.
 */
 
 
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}



// For the integration code to run, both "BuddyPress" and "BuddyPress Integration for WooCommerce" have to be active.
if ( ! sfwc_is_plugin_active( 'bp-loader.php' ) || ! sfwc_is_plugin_active( 'wc4bp-basic-integration.php' ) )
	return;




// Get the User Switcher to work properly.
function sfwc_change_user_switcher_position_for_wc4bp() {
	
	remove_action( 'woocommerce_before_account_navigation', 'sfwc_user_switcher_content' );
	
	if ( bp_is_current_component( wc4bp_Manager::get_shop_slug() ) ) {
		add_action( 'bp_template_content', 'sfwc_user_switcher_content', -20);
	}
}
add_action( 'bp_actions', 'sfwc_change_user_switcher_position_for_wc4bp' );




// Apply redirect after user switch.
function sfwc_manage_redirect_after_account_switch_for_wc4bp () {
	
	// Remove default redirect from main plugin.
	remove_action( 'template_redirect', 'sfwc_redirect_to_dashboard_after_account_switch' );
	
	// Add custom redirect.
	add_action( 'template_redirect', 'sfwc_apply_redirect_after_account_switch_for_wc4bp' );
	function sfwc_apply_redirect_after_account_switch_for_wc4bp () {
		if ( isset( $_POST['sfwc_frontend_children'] ) ) {
			bp_core_redirect( esc_url( bp_core_get_user_domain( absint( $_POST['sfwc_frontend_children'] ) ) . wc4bp_Manager::get_shop_slug() . '/' ) );
			exit;
		}
	}
}
add_action( 'init', 'sfwc_manage_redirect_after_account_switch_for_wc4bp' );




// If Sub-User mode is enabled, don't show the "Filter Orders" form on WC orders page: mysite.com/members/user-name/shop/orders/.
function sfwc_dont_show_filter_orders_form_in_sub_user_mode() {
	
	// Get 'Options' settings.
    $sfwc_options = (array) get_option( 'sfwc_options' );
	
	// Get Subaccount Mode option value from Options settings.
	$sfwc_option_subaccount_mode = ( isset( $sfwc_options['sfwc_option_subaccount_mode'] ) ) ? $sfwc_options['sfwc_option_subaccount_mode'] : 'sub_user';
	
	if ( $sfwc_option_subaccount_mode == 'sub_user' ) {
	
		if ( bp_is_current_component( wc4bp_Manager::get_shop_slug() ) && bp_current_action() == 'orders' ) {
			remove_action( 'woocommerce_before_account_orders', 'sfwc_my_account_orders_add_select_subaccount_orders_form' );
		}
	}
}
add_action( 'bp_actions', 'sfwc_dont_show_filter_orders_form_in_sub_user_mode' );




// "Filter Orders" form not working in My Account > Subaccounts > Subaccount Orders (Sub-User mode enabled) since it's not working.
function sfwc_sub_user_mode_remove_filter_orders_form_for_wc4bp () {
	
	// Get 'Options' settings.
    $sfwc_options = (array) get_option( 'sfwc_options' );
	
	// Get Subaccount Mode option value from Options settings.
	$sfwc_option_subaccount_mode = ( isset( $sfwc_options['sfwc_option_subaccount_mode'] ) ) ? $sfwc_options['sfwc_option_subaccount_mode'] : 'sub_user';
	
	if ( $sfwc_option_subaccount_mode == 'sub_user' ) {
	
		#if ( bp_is_current_component( wc4bp_Manager::get_shop_slug() ) && bp_current_action() == 'extra-content' ) { // Not working.
		if ( is_wc_endpoint_url( 'subaccounts' ) ) {
			remove_action( 'woocommerce_before_account_orders', 'sfwc_my_account_orders_add_select_subaccount_orders_form' );
		}
	}
	
}
add_action( 'bp_actions', 'sfwc_sub_user_mode_remove_filter_orders_form_for_wc4bp' );