src/Import_Command.php
private function suggest_user( $author_user_login, $author_user_email = '' ) {

		if ( ! isset( $this->blog_users ) ) {
			$this->blog_users = get_users();
		}

		$shortest    = -1;
		$shortestavg = array();

		$threshold = floor( ( strlen( $author_user_login ) / 100 ) * 10 ); // 10 % of the strlen are valid
		$closest   = '';
		foreach ( $this->blog_users as $user ) {
			// Before we resort to an algorithm, let's try for an exact match
			if ( $author_user_email && $user->user_email === $author_user_email ) {
				return $user->user_login;
			}

			$levs        = array();
			$levs[]      = levenshtein( $author_user_login, $user->display_name );
			$levs[]      = levenshtein( $author_user_login, $user->user_login );
			$levs[]      = levenshtein( $author_user_login, $user->user_email );
			$email_parts = explode( '@', $user->user_email );
			$email_login = array_shift( $email_parts );
			$levs[]      = levenshtein( $author_user_login, $email_login );
			rsort( $levs );
			$lev = array_pop( $levs );
			if ( 0 === $lev ) {
				$closest  = $user->user_login;
				$shortest = 0;
				break;
			}

			if ( ( $lev <= $shortest || $shortest < 0 ) && $lev <= $threshold ) {
				$closest  = $user->user_login;
				$shortest = $lev;
			}
			$shortestavg[] = $lev;
		}
		// in case all usernames have a common pattern
		if ( $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) ) {
			return '';
		}

		return $closest;
	}
private function suggest_user( $author_user_login, $author_user_email = '' ) {

		if ( ! isset( $this->blog_users ) ) {
			$this->blog_users = get_users();
		}

		$shortest    = -1;
		$shortestavg = array();

		$threshold = floor( ( strlen( $author_user_login ) / 100 ) * 10 ); // 10 % of the strlen are valid
		$closest   = '';
		foreach ( $this->blog_users as $user ) {
			// Before we resort to an algorithm, let's try for an exact match
			if ( $author_user_email && $user->user_email === $author_user_email ) {
				return $user->user_login;
			}

			$levs        = array();
			$levs[]      = levenshtein( $author_user_login, $user->display_name );
			$levs[]      = levenshtein( $author_user_login, $user->user_login );
			$levs[]      = levenshtein( $author_user_login, $user->user_email );
			$email_parts = explode( '@', $user->user_email );
			$email_login = array_shift( $email_parts );
			$levs[]      = levenshtein( $author_user_login, $email_login );
			rsort( $levs );
			$lev = array_pop( $levs );
			if ( 0 === $lev ) {
				$closest  = $user->user_login;
				$shortest = 0;
				break;
			}

			if ( ( $lev <= $shortest || $shortest < 0 ) && $lev <= $threshold ) {
				$closest  = $user->user_login;
				$shortest = $lev;
			}
			$shortestavg[] = $lev;
		}
		// in case all usernames have a common pattern
		if ( $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) ) {
			return '';
		}

		return $closest;
	}
private function suggest_user ( $ author_user_login , $ author_user_email = '' ) { if ( ! isset ( $ this -> blog_users ) ) { $ this -> blog_users = get_users ( ) ; } $ shortest = - 1 ; $ shortestavg = array ( ) ; $ threshold = floor ( ( strlen ( $ author_user_login ) / 100 ) * 10 ) ; // 10 % of the strlen are valid $ closest = '' ; foreach ( $ this -> blog_users as $ user ) { // Before we resort to an algorithm, let's try for an exact match if ( $ author_user_email && $ user -> user_email === $ author_user_email ) { return $ user -> user_login ; } $ levs = array ( ) ; $ levs [ ] = levenshtein ( $ author_user_login , $ user -> display_name ) ; $ levs [ ] = levenshtein ( $ author_user_login , $ user -> user_login ) ; $ levs [ ] = levenshtein ( $ author_user_login , $ user -> user_email ) ; $ email_parts = explode ( '@' , $ user -> user_email ) ; $ email_login = array_shift ( $ email_parts ) ; $ levs [ ] = levenshtein ( $ author_user_login , $ email_login ) ; rsort ( $ levs ) ; $ lev = array_pop ( $ levs ) ; if ( 0 === $ lev ) { $ closest = $ user -> user_login ; $ shortest = 0 ; break ; } if ( ( $ lev <= $ shortest || $ shortest < 0 ) && $ lev <= $ threshold ) { $ closest = $ user -> user_login ; $ shortest = $ lev ; } $ shortestavg [ ] = $ lev ; } // in case all usernames have a common pattern if ( $ shortest > ( array_sum ( $ shortestavg ) / count ( $ shortestavg ) ) ) { return '' ; } return $ closest ; }
Suggests a blog user based on the levenshtein distance.
Suggests a blog user based on the levenshtein distance .