WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Theme My Login loses the logout _wpnonce query string (3 posts)

  1. ReactorShop
    Member
    Posted 7 months ago #

    Hi Chouby,

    Let's move the Theme My Login business over here:

    As I mentioned in my previous post, this is what I did to fix the user links.

    function p4tml_my_login_domain() {
    	global $l10n;
    
    	$locale = get_locale();
    	$mofile = WP_PLUGIN_DIR . '/theme-my-login/language/' . 'theme-my-login-' . $locale . '.mo';
    	if( file_exists( $mofile ) )
    		load_textdomain( 'default', $mofile );
    }
    add_action( 'plugins_loaded', 'p4tml_my_login_domain', 9 ); //User links titles are read with a priority of 10 when plugins_loaded actions are applied.
    
    function p4tml_user_links( $user_links ) {
    	foreach( $user_links as $key => $user_link ) {
    		$user_links[$key]['title'] = __( $user_link['title'] );
    	}
    	return $user_links;
    }
    add_filter( 'tml_user_links', 'p4tml_user_links', 11 ); //Fixes user links titles translations before they're picked up by the theme.
    
    function p4tml_title( $title ) {
    	return __( $title );
    }
    add_filter( 'tml_title', 'p4tml_title', 11 ); //Fixes TML titles translations before they're picked up by the theme.

    And this adds the page's action meta needed in order for TML to recognize a translated page as its own:

    function p4tml_action_meta( $page_link, $post_id ) {
    	if( ! $page = get_post( $post_id ) )
    		return $page_link;
    	if( 'page' != $page->post_type )
    		return $page_link;
    
    	$original = pll_get_post( $page->ID, pll_default_language() );
    	if( $original == $page->ID )
    		return $page_link;
    
    	$tml_action = get_post_meta( $original, '_tml_action', true );
    
    	if( ! empty( $tml_action ) ) {
    		$check = get_post_meta( $page->ID, '_tml_action', true );
    		if( $tml_action != $check )
    			update_post_meta( $page->ID, '_tml_action', $tml_action );
    	}
    	return $page_link;
    }
    add_filter( 'page_link', 'p4tml_action_meta', 9, 2 );

    Now the translated Register, Lost Password, etc. pages can be recognized by TML.

    If a user wants to log out, the log-out page permalink requires a nonce query string:

    http://www.mysite.com/en/logout/?_wpnonce=8c9565c961

    This nonce query string is lost in polylang/frontend/frontend-links.php (near line 99)

    public function page_link($link, $id) { //$link contains the nonce query string.
    	if ($this->page_on_front && ($lang = $this->model->get_post_language($id)) && $id == $this->model->get_post($this->page_on_front, $lang))
    		return $lang->home_url;
    
    	return _get_page_link($id); //The nonce query string is not part of the id.
    }

    So, you get a pretty WordPress Failure Notice page, instead of a regular log out, which would return you to your current language home page, waiting for you to enter your username and password.

    This is what I did to fix it:

    public function page_link($link, $id) {
    	$query_string = '';
    	if( strpos( $link, '?' ) !== FALSE ) //Check if the link includes a query string.
    		$query_string = substr( $link, strpos( $link, '?' ), strlen( $link ) - strpos( $link, '?' ) ); //If it does, grab it!
    
    	if ($this->page_on_front && ($lang = $this->model->get_post_language($id)) && $id == $this->model->get_post($this->page_on_front, $lang))
    		return $lang->home_url . $query_string; //Append it.
    
    	return _get_page_link($id) . $query_string; //Append it.
    }

    This fixes the Theme My Login logout error and the WP e-Commerce's customer account tabs selection I mentioned in my previous post.

    After this, I convert the url from the default language to the current language:

    function p4we_translate_post_url($url, $leavename = false, $sample = false, $action = '' ) { //$action is set by TML
    	global $wp_rewrite;
    
    	if ( '' != $url && ( p4we_language_conditions_are_met() || ! empty( $action ) ) ) {
    		$query_string = '';
    		if( strpos( $url, '?' ) !== FALSE ) {
    			$query_string = substr( $url, strpos( $url, '?' ), strlen( $url ) - strpos( $url, '?' ) );
    			$url = str_replace( $query_string, "", $url );
    		}
    		$translated_page = p4we_translated_post_from_name ( basename ( $url ) ); //Query the cache/db using the page_name, use pll_get_post to get current language post and with this post id, get the post.
    		if (in_array ( $translated_page->post_type, array (
    				'page',
    				'wpsc-product'
    		) )) {
    			$draft_or_pending = in_array ( $translated_page->post_status, array (
    					'draft',
    					'pending',
    					'auto-draft'
    			) );
    
    			$url = $wp_rewrite->get_page_permastruct ();
    
    			if ( ! empty ( $url ) && ( ( isset( $translated_page->post_status ) && ! $draft_or_pending) || $sample)) {
    				if (! $leavename)
    					$url = str_replace ( '%pagename%', get_page_uri( $translated_page ), $url );
    
    				if ( pll_current_language() )
    					$url = home_url( pll_current_language () . '/' . $url );
    				elseif (isset ( $_COOKIE ['pll_language'] ))
    					$url = home_url ( $_COOKIE ['pll_language'] . '/' . $url );
    				else
    					$url = home_url ( $url );
    				$url = user_trailingslashit ( $url, 'page' );
    				if( ! empty( $query_string ) )
    					$url .= $query_string;
    			} else
    				$url = home_url ( '?page_id=' . $translated_page->ID );
    		}
    	}
    	return $url;
    }

    TODO: There's still a minor outstanding issue. If you add a page not originally included by Theme My Login to be shown to a user, you only get to enter its permalink in one language.

    A filter hook needs to be used in order to modify its permalink depending on the language.

    Well, I hope you approve this minor Polylang modification.

    If you require any additional information, please don't hesitate to ask.

    http://wordpress.org/plugins/polylang/

  2. ReactorShop
    Member
    Posted 7 months ago #

    I forgot something.

    The last filter in my previous post is hooked like this:

    function p4tml_page_link( $link, $action, $query ) {
    	if( ! empty( $action ) )
    		return p4we_translate_post_url( $link, FALSE, FALSE, $action );
    	else
    		return $link;
    }
    add_filter( 'tml_page_link', 'p4tml_page_link', 10, 3 );

    Sorry

  3. ReactorShop
    Member
    Posted 7 months ago #

    The nonce is passed with PLL 1.4dev18 without the need of modifications, so the situation originating this post is solved.

    From the TODO I listed above, in order to translate TML's custom user links permalinks, which I would suggest to enter them using the default language permalinks, I did this:

    function p4tml_user_links( $user_links ) {
    	foreach( $user_links as $key => $user_link ) {
    		$user_links[$key]['title'] = __( $user_link['title'] );
    		$user_links[$key]['url'] = p4we_translate_post_url( $user_link['url'] );
    	}
    	return $user_links;
    }
    add_filter( 'tml_user_links', 'p4tml_user_links', 11 );

    The p4we_language_conditions_are_met() function indicated inside the p4we_translate_post_url() function, only checks if the current language is different than the default language.

    You have to make sure that the translated custom user link title exists inside the 'default' translation domain, or conditionally select a different domain which will contain it, or use pll__ to maintain it inside the Strings Translation admin page.

    As always, thank you very much for your support, Chouby.

    Have a nice weekend!

Reply

You must log in to post.

About this Plugin

About this Topic