• Проблема : Перекидывает на главную страницу при переходе с линка Excel или word

    Контент нужно отображать зарегистрированным пользователям.

    Ситуация :
    Пользователь зарегистрирован на сайте, отдает линк кому то другому.
    Если линк скопировать и вставить в строку то все хорошо, откроется статья

    Если линк вставить в документ (Word/Ecxel/Outlook) то при переходе прокидывает на главную станицу портала. Не могу никак победить данное поведения

    Код ниже

     class Auntefication_extend {
    	
    
    	public function __construct() {
    		
    		add_action( 'wp', [ $this, 'wp_is_logged_in' ], 99 );
    		add_action( 'template_redirect', [ $this, 'redirect_user' ], 98 );
    
    	}
    
    	public function wp_is_logged_in( $wp ) {
    		 error_log( ' 1 cookie | hook wp : ' . ( isset( $_COOKIE['refer_uri'] ) ? $_COOKIE['refer_uri'] : 'not cookie' ) . ' -> ' . debug_backtrace()[0]['function'] );
    		if ( ! is_user_logged_in() ) {
    			// Hack
    			if ( false === array_key_exists( 'HTTP_COOKIE', $_SERVER ) ) {
    				return;
    			}
    			 error_log( 'User is not logged in | hook wp :' . debug_backtrace()[0]['function'] );
    			$this->remoove_header();
    //			 error_log( print_r( $_SERVER, true ) );
    			 error_log( '------------------------------' );
    			 error_log( print_r(debug_backtrace() , true));
    			if ( stripos( $wp->request, 'login' ) === false && mb_strlen( $wp->request ) >= 4 ) {
    				 error_log( 'redirect in hook wp : ' . home_url( '/login/?refer=' . $wp->request ) );
    				 error_log( print_r( 'request : ' . $wp->request, true ) );
    				 error_log( '>>>>> Set cookie : ' . $wp->request );
    				setcookie( 'refer_uri', $wp->request );
    //				 error_log( print_r( $res, true ) );
    				$this->remoove_header();
    				wp_safe_redirect( home_url( '/login/?refer=' . $wp->request ), 307 );
    				exit;
    			}
    			if ( ! empty( $_COOKIE['refer_uri'] ) ) {
    				 error_log( 'User is not logged in | Dont empty refer_uri :' );
    				 error_log( 'redirect in hook wp refer_uri : ' . home_url( '/login/?refer=' . $_COOKIE['refer_uri'] ) );
    				$this->remoove_header();
    				wp_safe_redirect( home_url( '/login/?refer=' . $_COOKIE['refer_uri'] ), 303 );
    				exit;
    			}
    			 error_log( 'User is not logged in | hook wp but not if input:' );
    		}
    		if ( is_user_logged_in() ) {
    			if ( is_page( 'login' ) ) {
    				$ref = isset( $_COOKIE['refer_uri'] ) ? $_COOKIE['refer_uri'] : '';
    				if ( ! empty( $ref ) ) {
    					$refer = $ref;
    				} elseif ( ! empty( $_GET ) && array_key_exists( 'refer', $_GET ) ) {
    					$refer = $_GET['refer'];
    				} else {
    					 error_log( 'I AM REDIRECT TO \/  wp : is_user_logged_in 89| is_page' );
    					$refer = '/';
    				}
    				 error_log( 'redirect in hook wp : is_user_logged_in | is_page' . home_url( $refer ) );
    				wp_safe_redirect( home_url( $refer ), 303, 'knowlage-base' );
    				exit;
    			}
    			if ( ! empty( $_COOKIE['refer_uri'] ) ) {
    				 error_log( 'hook wp is_user_logged and !empty refer uri' );
    
    				$ref = $_COOKIE['refer_uri'];
    				setcookie( 'refer_uri', null );
    				wp_safe_redirect( home_url( $ref ), 302, 'knowlage-base' );
    				exit;
    			};
    		}
    	}
    
    	/**
    	 * Remoove some headers
    	 */
    	private function remoove_header() {
    		header_remove( 'HTTP_X_FEATUREVERSION' );
    		header_remove( 'HTTP_X_MS_COOKIEURI_REQUESTED' );
    		header_remove( 'HTTP_X_OFFICE_MAJOR_VERSION' );
    		header_remove( 'HTTP_USER_AGENT' );
    		header_remove( 'HTTP_AUTHORIZATION' );
    	}
    
    	public function redirect_user() {
    		 error_log( ' 2 cookie | hook template_redirect : ' . ( isset( $_COOKIE['refer_uri'] ) ? $_COOKIE['refer_uri'] : 'not cookie' ) . '-> ' . debug_backtrace()[0]['function'] );
    //			error_log('redirect_user -> : '. $_SERVER['REQUEST_URI'] );
    		if ( ! is_user_logged_in() ) {
    			if ( ! is_page( 'login' ) ) {
    				 error_log( 'Set up redirect : | hook template_redirect | not loggined and not page login : REQUEST_URI->  ' . $_SERVER['REQUEST_URI'] . '-> ' . debug_backtrace()[1]['function'] );
    
    				$ref = isset( $_COOKIE['refer_uri'] ) ? $_COOKIE['refer_uri'] : '';
    				if ( ! empty( $ref ) ) {
    					$refer = $ref;
    				} elseif ( ! empty( $_GET ) && array_key_exists( 'refer', $_GET ) ) {
    					$refer = $_GET['refer'];
    				} else {
    					 error_log( 'I AM REDIRECT TO \/  template_redirect : is_user_logged_in 124| is_page' );
    //					 error_log( print_r( $_SERVER, true ) );
    					if ( mb_strlen( $_SERVER['REQUEST_URI'] ) >= 4 ) {
    						$refer = $_SERVER['REQUEST_URI'];
    					} else {
    						$refer = '/';
    					}
    				}
    				 error_log( 'string_to_referer : ' . home_url( '/login/?refer=' . $refer ) );
    				$return_url = home_url( '/login/?refer=' . $refer );
    				 error_log( 'redirect in hook template_redirect : !is_user_logged_in | !is_page : ' . $return_url );
    				$this->remoove_header();
    				wp_safe_redirect( $return_url, 303, 'knowlage-base' );
    				exit;
    			}
    		}
    
    		if ( is_user_logged_in() ) {
    			if ( ! empty( $_COOKIE['refer_uri'] ) ) {
    				 error_log( 'Set up redirect : | hook template_redirect | LOGGED and page LOGIN ' . $_SERVER['REQUEST_URI'] . '-> ' . debug_backtrace()[0]['function'] );
    				 error_log( 'redirect : ' . $_COOKIE['refer_uri'] );
    				$redir = $_COOKIE['refer_uri'];
    							$_COOKIE['refer_uri'] = null;
    				 error_log( 'redirect in hook template_redirect : is_user_logged_in | COOKIE ->refer_uri' . $redir );
    				wp_safe_redirect( $redir, 303, 'knowlage-base' );
    				exit;
    			}
    		}
    		 error_log( 'redirect not exeption : ' );
    //			error_log( print_r( $_SERVER, true ) );
    	}
    

    В лог пишет

    
    [21-Feb-2020 10:42:08 UTC]  1 cookie | hook wp : not cookie -> wp_is_logged_in
    [21-Feb-2020 10:42:08 UTC]  1 cookie | hook wp : not cookie -> wp_is_logged_in
    [21-Feb-2020 10:42:08 UTC]  2 cookie | hook template_redirect : not cookie-> redirect_user
    [21-Feb-2020 10:42:08 UTC] Set up redirect : | hook template_redirect | not loggined and not page login : REQUEST_URI->  /-> apply_filters
    [21-Feb-2020 10:42:08 UTC] I AM REDIRECT TO \/  template_redirect : is_user_logged_in 124| is_page
    [21-Feb-2020 10:42:08 UTC] string_to_referer : http://host:8082/login/?refer=/
    [21-Feb-2020 10:42:08 UTC] redirect in hook template_redirect : !is_user_logged_in | !is_page : http://host:8082/login/?refer=/
    [21-Feb-2020 10:42:08 UTC]  1 cookie | hook wp : not cookie -> wp_is_logged_in
    [21-Feb-2020 10:42:08 UTC]  2 cookie | hook template_redirect : not cookie-> redirect_user
    [21-Feb-2020 10:42:08 UTC] redirect not exeption : 
    [21-Feb-2020 10:42:08 UTC]  1 cookie | hook wp : not cookie -> wp_is_logged_in
    [21-Feb-2020 10:42:08 UTC]  2 cookie | hook template_redirect : not cookie-> redirect_user
    [21-Feb-2020 10:42:08 UTC] Set up redirect : | hook template_redirect | not loggined and not page login : REQUEST_URI->  /-> apply_filters
    [21-Feb-2020 10:42:08 UTC] I AM REDIRECT TO \/  template_redirect : is_user_logged_in 124| is_page
    [21-Feb-2020 10:42:08 UTC] string_to_referer : http://dev-easylearn.co.volia.com:8082/login/?refer=/
    [21-Feb-2020 10:42:08 UTC] redirect in hook template_redirect : !is_user_logged_in | !is_page : http://host:8082/login/?refer=/
    [21-Feb-2020 10:42:09 UTC]  1 cookie | hook wp : not cookie -> wp_is_logged_in
    [21-Feb-2020 10:42:09 UTC] redirect in hook wp : is_user_logged_in | is_page http://host:8082/
    [21-Feb-2020 10:42:09 UTC]  1 cookie | hook wp : not cookie -> wp_is_logged_in
    [21-Feb-2020 10:42:09 UTC]  2 cookie | hook template_redirect : not cookie-> redirect_user
    [21-Feb-2020 10:42:09 UTC] redirect not exeption :