--- G:/Websites/SOFTWARE/Downloaded Software/Domain Mapping/Originals/4423/classes/Domainmap/Module/Cdsso.php	Sat Sep 17 09:12:51 2016
+++ G:/Websites/SOFTWARE/Downloaded Software/Domain Mapping/Edits/4423/classes/Domainmap/Module/Cdsso.php	Sat Sep 17 09:10:45 2016
@@ -67,32 +67,35 @@
 	 *
 	 * @access public
 	 * @param Domainmap_Plugin $plugin The instance of the plugin class.
 	 */
 	public function __construct( Domainmap_Plugin $plugin ) {
 		parent::__construct( $plugin );
 
-		$this->_async =  $plugin->get_option("map_crossautologin_async");
+		$this->_async = $plugin->get_option( 'map_crossautologin_async' );
 
 		$this->_add_filter( 'login_redirect', 'set_interim_login', 10, 3 );
 		$this->_add_filter( 'login_message', 'get_login_message' );
 		$this->_add_filter( 'login_url', 'update_login_url', 10, 2 );
 		$this->_add_action( 'login_init', 'reauthenticate_user', 10 );
-		$this->_add_action('wp_head', 'add_auth_script', 0 );
+		$this->_add_action( 'wp_head', 'add_auth_script', 0 );
 
 		$this->_add_action( 'login_form_login', 'set_auth_script_for_login' );
 		$this->_add_action( 'login_footer', 'add_propagation_script' );
-		$this->_add_filter("logout_redirect", "logout_user_from_everywhere");
+		$this->_add_filter( 'logout_redirect', 'logout_user_from_everywhere' );
 
-		if( !$this->_async ){
+		if ( ! $this->_async ) {
 			$this->_add_action( 'plugins_loaded', 'authorize_user' );
 		}
 
-		add_filter('init', array( $this, "add_query_var_for_endpoint" ));
-		add_action('template_redirect', array( $this, 'dispatch_ajax_request' ));
+		add_action( 'init', array( $this, 'set_rewrite_endpoint' ), 0 );
+		add_filter( 'query_vars', array( $this, 'add_query_var_for_endpoint' ), 10, 1 );
+		add_action( 'shutdown', array( $this, '_flush_rewrite_rules' ), 900 );
+
+		add_action( 'template_redirect', array( $this, 'dispatch_ajax_request' ) );
 
 		$this->_add_ajax_action( self::ACTION_PROPAGATE_USER, 'propagate_user', true, true );
 	}
 
 	/**
 	 * Adds hook for login_head action if user tries to login.
 	 *
@@ -339,30 +342,38 @@
 	 *
 	 * @since 4.3.1
 	 * @param $vars
 	 *
 	 * @return array
 	 */
 	function add_query_var_for_endpoint($vars) {
-		add_rewrite_endpoint( self::SSO_ENDPOINT, EP_ALL );
 		$vars[] = self::SSO_ENDPOINT;
-		$this->_flush_rewrite_rules();
 		return $vars;
 	}
 
 	/**
+	 * Enqueues flush of rewrite rules on shutdown.
+	 *
+	 * @since 4.4.2.5
+	 */
+	function set_rewrite_endpoint() {
+		add_rewrite_endpoint( self::SSO_ENDPOINT, EP_ALL );
+	}
+
+	/**
 	 * Flushes rewrite rules if needed
 	 *
 	 * @since 4.3.1
 	 */
-	function _flush_rewrite_rules(){
-		$key =  domain_map::FLUSHED_REWRITE_RULES . get_current_blog_id();
-		if( !get_site_option( $key ) ){
+	function _flush_rewrite_rules() {
+		$rev = '1';
+		$key = $rev . '|' . domain_map::FLUSHED_REWRITE_RULES . get_current_blog_id();
+		if ( ! get_site_option( $key ) ) {
 			flush_rewrite_rules();
-			update_site_option( $key , true);
+			update_site_option( $key, true );
 		}
 	}
 
 	/**
 	 * Returns relevant endpoint url
 	 *
 	 * @since 4.3.1
@@ -370,36 +381,32 @@
 	 * @param null $domain
 	 *
 	 * @return string
 	 */
 	private function _get_sso_endpoint_url( $subsite = false, $domain = null){
 		global $wp_rewrite, $current_blog, $current_site;;
 
-
-
 		if( $subsite ){
 			$domain = is_null( $domain ) ? $current_blog->domain : $domain;
 			$subsite_scheme = self::utils()->get_mapped_domain_scheme( $domain );
 			$url  = trailingslashit( set_url_scheme( "http://" . $domain, $subsite_scheme ) );
 		}else{
 			$admin_scheme = self::utils()->get_admin_scheme() ? self::utils()->get_admin_scheme() : "http";
 			$url  = trailingslashit( set_url_scheme( network_home_url("/"), $admin_scheme ) );
 		}
 
 		return $wp_rewrite->using_permalinks() ? $url . self::SSO_ENDPOINT . "/" . time() . "/" : $url . "?" . self::SSO_ENDPOINT . "=" . time() ;
 	}
 
-
 	/**
 	 * Dispatches ajax request to the relevant methods
 	 *
 	 * @since 4.3.1
 	 */
 	function dispatch_ajax_request(){
-
 		global $wp_query;
 
 		if( !isset( $wp_query->query_vars[ self::SSO_ENDPOINT ] ) ) return;
 
 		define('DOING_AJAX', true);
 		header('Content-Type: text/html');
 		send_nosniff_header();
@@ -665,8 +672,8 @@
 		} else {
 			$redirect_to = admin_url();
 		}
 
 		return $redirect_to;
 	}
 
-}
\ No newline at end of file
+}
