<?php defined('ABSPATH') OR die('Direct Access Restricted!');
function o365_register_deactivation_hook_check_multisite($sitewide,$args = array())
{
	$method = 'o365_register_deactivation_hook';
	if ( is_multisite() && $sitewide )
	{
        global $wpdb, $blog_id;
        $dbquery = 'SELECT blog_id FROM '.$wpdb->blogs;
        $ids = $wpdb->get_col( $dbquery );
        foreach ( $ids as $id )
		{
            switch_to_blog( $id );
            call_user_func_array( $method, array( $args ) );
        }
        switch_to_blog( $blog_id );
    }
	else call_user_func_array( $method, array( $args ) );
}
function o365_register_deactivation_hook(){
	if ( ! function_exists( 'is_plugin_active' ) )
	{
		require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
	}
	$delete_option = o365_get_setting('delete_settings');
	if( $delete_option == "yes")
    {
		global $wpdb;	
		$tables = array();
		foreach( $tables as $table ){	
			$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}$table;");	
		}
		// Value saved in options table	
		$options = array(	
			'365_plugin_top_slug',	
			'o365_settings'
		);		
	   foreach( $options as $op ){
			delete_option( $op );
		}
	}
	$dependent = 'o365-user-auth/o365-user-auth.php';	
	if( is_plugin_active($dependent) ){	
	  add_action('update_option_active_plugins','o365_user_auth_update_option_active_plugins_func');	
	}
	$dependent = 'o365-powerbi/o365-powerbi.php';	
	if( is_plugin_active($dependent) ){	
	  add_action('update_option_active_plugins','o365_powerbi_update_option_active_plugins_func');	
	}
	$dependent = 'o365-powerapps/o365-powerapps.php';
	if ( !is_plugin_active( $dependent ))
	{
		$dependent = 'o365-powerapps/o365-powerapps.php';	
		if( is_plugin_active($dependent) ){
		  add_action('update_option_active_plugins','o365_powerapps_update_option_active_plugins_func');	
		}
	}
	$dependent = 'o365-wp-sp-display/o365-wp-sp-display.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_sp_display_update_option_active_plugins_func');	
	}
	$dependent = 'o365-wp-crm-display/o365-wp-crm-display.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_crm_update_option_active_plugins_func');	
	}
	$dependent = 'o365-wp-ms-booking/o365-wp-ms-booking.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_ms_booking_update_option_active_plugins_func');
	}
	$dependent = 'o365-wp-onedrive-display/o365-wp-onedrive-display.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_onedrive_update_option_active_plugins_func');
	}
	$dependent = 'o365-wp-outlook-display/o365-wp-outlook-display.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_outlook_update_option_active_plugins_func');
	}
	$dependent = 'o365-wp-sp-calendar-display/o365-wp-sp-calendar-display.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_sp_calendar_update_option_active_plugins_func');
	}
	$dependent = 'o365-wp-sp-video-display/o365-wp-sp-video-display.php';
	if( is_plugin_active($dependent) ){
	  add_action('update_option_active_plugins','o365_sp_video_update_option_active_plugins_func');
	}
}
function o365_user_auth_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-user-auth/o365-user-auth.php' );
}
function o365_powerapps_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-powerapps/o365-powerapps.php' );
}
function o365_powerbi_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-powerbi/o365-powerbi.php' );
}
function o365_sp_display_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-sp-display/o365-wp-sp-display.php' );
}
function o365_sp_video_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-sp-video-display/o365-wp-sp-video-display.php' );
}
function o365_sp_calendar_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-sp-calendar-display/o365-wp-sp-calendar-display.php' );
}
function o365_outlook_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-outlook-display/o365-wp-outlook-display.php' );	
}
function o365_onedrive_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-onedrive-display/o365-wp-onedrive-display.php' );
}
function o365_ms_booking_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-ms-booking/o365-wp-ms-booking.php' );
}
function o365_crm_update_option_active_plugins_func()
{
	deactivate_plugins( 'o365-wp-crm-display/o365-wp-crm-display.php' );
}