<?php
/**
 * Sticky post navigation
 *
 * @package Multi
 */

?>
<?php

$sticky_post_nav  = 0;
$chosen_post_type = get_post_type();
switch ( $chosen_post_type ) {
	case 'post':
		$sticky_post_nav = get_theme_mod( 'multi_sticky_post_nav', MULTIMAGAZINE_STICKY_POST_NAV );
		break;
	case 'project':
		$sticky_post_nav = get_theme_mod( 'multi_sticky_project_nav', 0 );
		break;
	case 'page':
		$sticky_post_nav = get_theme_mod( 'multi_pages_show_sticky_page_nav', MULTIMAGAZINE_PAGES_SHOW_STICKY_PAGE_NAV );
		break;
	default:
		$sticky_post_nav = 0;
		break;
}

$sticky_post_nav = apply_filters( 'multimagazine_sticky_post_nav', $sticky_post_nav, $chosen_post_type );

if ( $sticky_post_nav ) {
	?>
	<nav class="sticky-post-nav" aria-label="<?php esc_attr_e( 'Post navigation', 'multimagazine' ); ?>">
	<?php
	$prev_post = get_adjacent_post( false, '', true );
	if ( empty( $prev_post ) ) {
		$prev_post = multimagazine_get_newest_post( $chosen_post_type );
	}
	$next_post = get_adjacent_post( false, '', false );
	if ( empty( $next_post ) ) {
		$next_post = multimagazine_get_oldest_post( $chosen_post_type );
	}

	if ( ! empty( $prev_post ) ) :
		$prev_id = $prev_post->ID;
		$classes = 'prev';
		if ( has_post_thumbnail( $prev_id ) ) {
			$classes .= ' has-image';
		}
		$prev_title = empty( $prev_post->post_title ) ? __( 'Untitled', 'multimagazine' ) : $prev_post->post_title;
		?>
		<a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>" class="<?php echo esc_attr( $classes ); ?>">
			<span class="link-base" data-label="<?php esc_attr_e( 'Previous', 'multimagazine' ); ?>"><i class="link-icon m-icon m-icon-long-arrow-left" aria-hidden="true"></i></span>
			<?php echo get_the_post_thumbnail( $prev_id, 'multimagazine-thumbnail-mini' ); ?><span class="title"><?php echo esc_html( $prev_title ); ?></span>
		</a>
		<?php
	endif;

	if ( ! empty( $next_post ) ) :
		$next_id = $next_post->ID;
		$classes = 'next';
		if ( has_post_thumbnail( $next_id ) ) {
			$classes .= ' has-image';
		}
		$next_title = empty( $next_post->post_title ) ? __( 'Untitled', 'multimagazine' ) : $next_post->post_title;
		?>
		<a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>" class="<?php echo esc_attr( $classes ); ?>">
			<?php echo get_the_post_thumbnail( $next_id, 'multimagazine-thumbnail-mini' ); ?><span class="title"><?php echo esc_html( $next_title ); ?></span>
			<span class="link-base" data-label="<?php esc_attr_e( 'Next', 'multimagazine' ); ?>"><i class="link-icon m-icon m-icon-long-arrow-right" aria-hidden="true"></i></span>
		</a>
		<?php
	endif;
	?>
	</nav>
	<?php
}
