<?php
add_filter('wp_nav_menu_items', 'add_last_nav_item');
function add_last_nav_item($items) {
	$login_logout .= '<li id="menu-logout-item">
		[mepr-logout-link class="mp-login-logout"]
	</li>';	
	
	if( strpos($items, '<li id="menu-item-2518"') === 0 ) { // menu_p 
		if( is_user_logged_in() ) {
			$user = wp_get_current_user();
			$balance = mycred_return_users_balance();
			$last_name = get_user_meta( $user->ID, 'last_name', true );
			$first_name = get_user_meta( $user->ID, 'first_name', true );
			$avatar = '<li>
			<div id="menu-avatar">
				<img src="http://zinomedia.de/terminsnipe/wp-content/uploads/sites/6/2020/03/021-superhero.svg" alt="user-avatar"><strong>' . $first_name . ' ' . $last_name . '</strong>
			</div>
			<div id="menu-user-info"><a href="http://zinomedia.de/terminsnipe/dash/kaufen/"><img src="http://zinomedia.de/terminsnipe/wp-content/uploads/sites/6/2020/03/630865.svg" alt="fastcoins"> ' . $balance . ' FastCoins</a></div>
			</li>';
			
			$items = $avatar . $items;$login_logout;			
		}			
	}
	else if( strpos($items, '<li id="menu-item-982"') === 0 ) { // dash menu
		//$items .= $login_logout;
	}
	
	$items .= $login_logout;
	js_style_nav_login_logout();
	
	return $items;
}

function js_style_nav_login_logout() {
	$is_user_logged_in = is_user_logged_in();
	?>
		<script>
		// START JAVASCRIPT			
		document.addEventListener("DOMContentLoaded", function(event) {
			var is_user_logged_in = '<?php echo $is_user_logged_in ;?>';
			
			if (is_user_logged_in) {
				jQuery( ".mp-login-logout" ).each( function () {
					var nav_entry = jQuery(this);
					nav_entry[0].innerHTML = '<i class="fas fa-sign-out-alt"></i>Ausloggen';
				});				
			}
			else if (!is_user_logged_in) {
				jQuery( ".mp-login-logout" ).each( function () {
					var nav_entry = jQuery(this);
					nav_entry[0].innerHTML = '<i class="fas fa-sign-in-alt"></i>Einloggen';
				});
			}			
		});
		// END JAVASCRIPT
		</script>
	<?php
}