<?php
/**
 * Template part for displaying posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package ROI_Theme
 * @since 1.0.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

	<!-- Post Header -->
	<header class="entry-header">
		<?php
		// Display the post title
		if ( is_singular() ) :
			the_title( '<h1 class="entry-title">', '</h1>' );
		else :
			the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
		endif;

		// Display post meta information
		if ( 'post' === get_post_type() ) :
			?>
			<div class="entry-meta">
				<?php
				// Posted on date
				printf(
					'<span class="posted-on"><time class="entry-date published" datetime="%1$s">%2$s</time></span>',
					esc_attr( get_the_date( 'c' ) ),
					esc_html( get_the_date() )
				);

				// Posted by author
				printf(
					'<span class="byline"> %s <span class="author vcard"><a class="url fn n" href="%s">%s</a></span></span>',
					esc_html__( 'by', 'roi-theme' ),
					esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
					esc_html( get_the_author() )
				);

				// Comments link
				if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
					echo '<span class="comments-link">';
					comments_popup_link(
						esc_html__( 'Leave a comment', 'roi-theme' ),
						esc_html__( '1 Comment', 'roi-theme' ),
						esc_html__( '% Comments', 'roi-theme' )
					);
					echo '</span>';
				endif;
				?>
			</div><!-- .entry-meta -->
			<?php
		endif;
		?>
	</header><!-- .entry-header -->

	<!-- Featured Image -->
	<?php if ( has_post_thumbnail() ) : ?>
		<div class="post-thumbnail">
			<?php
			if ( is_singular() ) :
				the_post_thumbnail( 'roi-featured-large', array( 'alt' => the_title_attribute( array( 'echo' => false ) ) ) );
			else :
				?>
				<a href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
					<?php the_post_thumbnail( 'roi-featured-medium', array( 'alt' => the_title_attribute( array( 'echo' => false ) ) ) ); ?>
				</a>
				<?php
			endif;
			?>
		</div><!-- .post-thumbnail -->
	<?php endif; ?>

	<!-- Post Content -->
	<div class="entry-content">
		<?php
		// Display content or excerpt based on context
		if ( is_singular() ) :
			the_content(
				sprintf(
					wp_kses(
						/* translators: %s: Name of current post. Only visible to screen readers */
						__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'roi-theme' ),
						array(
							'span' => array(
								'class' => array(),
							),
						)
					),
					wp_kses_post( get_the_title() )
				)
			);

			// Display pagination for multi-page posts
			wp_link_pages(
				array(
					'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'roi-theme' ),
					'after'  => '</div>',
				)
			);
		else :
			the_excerpt();
		endif;
		?>
	</div><!-- .entry-content -->

	<!-- Post Footer -->
	<footer class="entry-footer">
		<?php
		// Display categories
		$categories_list = get_the_category_list( esc_html__( ', ', 'roi-theme' ) );
		if ( $categories_list ) :
			printf(
				'<span class="cat-links">%s %s</span>',
				esc_html__( 'Categories:', 'roi-theme' ),
				$categories_list // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			);
		endif;

		// Display tags
		$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'roi-theme' ) );
		if ( $tags_list ) :
			printf(
				'<span class="tags-links">%s %s</span>',
				esc_html__( 'Tags:', 'roi-theme' ),
				$tags_list // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			);
		endif;

		// Edit post link
		edit_post_link(
			sprintf(
				wp_kses(
					/* translators: %s: Name of current post. Only visible to screen readers */
					__( 'Edit <span class="screen-reader-text">%s</span>', 'roi-theme' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				wp_kses_post( get_the_title() )
			),
			'<span class="edit-link">',
			'</span>'
		);
		?>
	</footer><!-- .entry-footer -->

</article><!-- #post-<?php the_ID(); ?> -->
