<?php
/*
 * Get Tenant Id by tenant name
 * @param $tenant_name
 */
defined( 'ABSPATH') OR die('Access denied!' );
if( ! function_exists('m365_get_tenant_id_by_tenant_name') ) {
    function m365_get_tenant_id_by_tenant_name( $tenant_name ) {
        $output = wp_remote_request( "https://login.windows.net/".$tenant_name.".onmicrosoft.com/.well-known/openid-configuration");
		$output = json_decode( $output['body'] );	
		if( isset( $output->authorization_endpoint ) )
		{
			$output = explode("/", $output->authorization_endpoint );
			$output = $output[3];
			update_option('m365_tenant_id',$output);		
			return $output;
		}
		else
		{
			$output = "";
			return $output;
		}
    }
}
?>