Support » Plugin: WP Statistics » URL tracking is severely broken in version 12

  • Resolved programmin

    (@programmin)


    Whenever site_url is longer than home_url, (if you have the uploads and wp folder in mysite/hp_wordpress, and home_url() is mysite for example, this code is clipping off the url incorrectly:

    	// This function gets the current page URI.
    	function wp_statistics_get_uri() {
    		// Get the site's path from the URL.
    		$site_uri = parse_url( site_url(), PHP_URL_PATH );
    	
    		// Get the current page URI.
    		$page_uri = $_SERVER["REQUEST_URI"];
    
    		// Strip the site's path from the URI.
    		$page_uri = substr( $page_uri, strlen( $site_uri ) );
    		
    		// If we're at the root (aka the URI is blank), let's make sure to indicate it.
    		if( $page_uri == '' ) { $page_uri = '/'; }
    		
    		return $page_uri;
    	}

    The previous 11 version had:

           // This function gets the current page URI.
    	function wp_statistics_get_uri() {
    		// Get the site's path from the URL.
    		$site_uri = parse_url( site_url(), PHP_URL_PATH );
    	
    		// Get the current page URI.
    		$page_uri = $_SERVER["REQUEST_URI"];
    
    		// Strip the site's path from the URI.
    		$page_uri = str_ireplace( $site_uri, '', $page_uri );
    		
    		// If we're at the root (aka the URI is blank), let's make sure to indicate it.
    		if( $page_uri == '' ) { $page_uri = '/'; }
    		
    		return $page_uri;
    	}

    and works correctly.

    Now there are many people with inconsistent data in the database as it clips the first characters of the name of the page!

    • This topic was modified 8 months, 4 weeks ago by  programmin.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Greg Ross

    (@gregross)

    The problem with the old code is that it could replace things in the URL other than the beginning, I’ll have to look at a third alternative to do what’s needed.

    Thanks for tracking this down.

    I have to wonder why it was looking at site_url in the first place instead of home_url, https://codex.wordpress.org/Function_Reference/home_url

    Site_url is wherever the wp files happen to live, home_url will be the root of the visible site pages.

    Plugin Contributor Greg Ross

    (@gregross)

    We could get either in to the function, try the following and see if it resolves the issue:

    
    	function wp_statistics_get_uri() {
    		// Get the site's path from the URL.
    		$site_uri = parse_url( site_url(), PHP_URL_PATH );
    		$site_uri_len = strlen( $site_uri );
    	
    		// Get the site's path from the URL.
    		$home_uri = parse_url( home_url(), PHP_URL_PATH );
    		$home_uri_len = strlen( $home_uri );
    
    		// Get the current page URI.
    		$page_uri = $_SERVER["REQUEST_URI"];
    
    		/*
    		 * We need to check which URI is longer in case one contains the other.
    		 *
    		 * For example home_uri might be "/site/wp" and site_uri might be "/site".
    		 *
    		 * In that case we want to check to see if the page_uri starts with "/site/wp" before
    		 * we check for "/site", but in the reverse case, we need to swap the order of the check.
    		 */
    		if( $site_uri_len > $home_uri_len ) {
    			if( substr( $page_uri, 0, $site_uri_len ) == $site_uri ) {
    				$page_uri = substr( $page_uri, $site_uri_len );
    			}
    			
    			if( substr( $page_uri, 0, $home_uri_len ) == $home_uri ) {
    				$page_uri = substr( $page_uri, $home_uri_len );
    			}
    		} else {
    			if( substr( $page_uri, 0, $home_uri_len ) == $home_uri ) {
    				$page_uri = substr( $page_uri, $home_uri_len );
    			}
    
    			if( substr( $page_uri, 0, $site_uri_len ) == $site_uri ) {
    				$page_uri = substr( $page_uri, $site_uri_len );
    			}
    		}
    		
    		// If we're at the root (aka the URI is blank), let's make sure to indicate it.
    		if( $page_uri == '' ) { $page_uri = '/'; }
    		
    		return $page_uri;
    	}
    

    I could check the latest version with that function replaced, but unless I am missing something there should not be a case where something in site_uri (the actual url of the /wp-content, uploads, /wp-admin/) would be something that would be recorded? All pages should contain home_url/nameofthepage or whatever permalink setting is set, right?

    Plugin Contributor Greg Ross

    (@gregross)

    We could get site_uri on the front page in some cases depending on how it’s being written, so better to be safe than sorry.

    Replacing that function, seems to make it better on an affected site.

    Plugin Contributor Greg Ross

    (@gregross)

    Thanks, I’ll do a 12.0.1 release tonight with the update.

    Plugin Contributor Greg Ross

    (@gregross)

    12.0.1 has been released with the fix.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘URL tracking is severely broken in version 12’ is closed to new replies.