<?php
/**
 * File that holdes the front-end component of Author Bylines default template
 *
 * @package Author_Kit
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Label variables (template-specific).
$label_published_on      = __( 'Published on', 'author-kit' );
$label_in_category       = __( 'in', 'author-kit' );
$label_last_updated      = __( 'Last updated', 'author-kit' );
$label_no_comments       = __( 'No comments', 'author-kit' );
$label_one_comment       = __( '1 comment', 'author-kit' );
$label_multiple_comments = __( '% comments', 'author-kit' );

$active_byline_template = get_option( 'author_kit_custom_byline_template', 'byline-default' );

switch ( $active_byline_template ) {
	case 'template-elantra':
		$label_contributions        = get_theme_mod( 'author_kit_byline_contribution_from_label_elantra', __( 'Contributions from', 'author-kit' ) );
		$label_written_by           = get_theme_mod( 'author_kit_byline_written_by_label_elantra', __( 'Written By', 'author-kit' ) );
		$authorkit_byline_separator = get_theme_mod( 'author_kit_byline_separator_label_elantra', '|' );

		// Visibility flags.
		$hide_avatar       = get_theme_mod( 'author_kit_byline_hide_avatar_elantra', 0 );
		$hide_post_date    = get_theme_mod( 'author_kit_byline_hide_post_date_elantra', 0 );
		$hide_last_updated = get_theme_mod( 'author_kit_byline_hide_last_updated_date_elantra', 0 );
		$hide_categories   = get_theme_mod( 'author_kit_byline_hide_categories_elantra', 0 );
		$hide_comments     = get_theme_mod( 'author_kit_byline_hide_comments_elantra', 0 );
		break;

	case 'template-advo':
		$label_contributions        = get_theme_mod( 'author_kit_byline_contribution_from_label_advo', __( 'Other Writers', 'author-kit' ) );
		$label_written_by           = get_theme_mod( 'author_kit_byline_written_by_label_advo', __( 'Article By', 'author-kit' ) );
		$authorkit_byline_separator = get_theme_mod( 'author_kit_byline_separator_label_advo', '|' );

		// Visibility flags.
		$hide_avatar       = get_theme_mod( 'author_kit_byline_hide_avatar_advo', 0 );
		$hide_post_date    = get_theme_mod( 'author_kit_byline_hide_post_date_advo', 0 );
		$hide_last_updated = get_theme_mod( 'author_kit_byline_hide_last_updated_date_advo', 0 );
		$hide_categories   = get_theme_mod( 'author_kit_byline_hide_categories_advo', 1 );
		$hide_comments     = get_theme_mod( 'author_kit_byline_hide_comments_advo', 1 );
		break;

	case 'byline-default':
	default:
		// Labels.
		$label_contributions        = get_theme_mod( 'author_kit_byline_contribution_from_label', __( 'Co Authors', 'author-kit' ) );
		$label_written_by           = get_theme_mod( 'author_kit_byline_written_by_label', __( 'Published By', 'author-kit' ) );
		$authorkit_byline_separator = get_theme_mod( 'author_kit_byline_separator_label_default', '|' );


		// Visibility Flags (default values are provided as fallback).
		$hide_avatar       = get_theme_mod( 'author_kit_byline_hide_avatar_default', 0 );
		$hide_post_date    = get_theme_mod( 'author_kit_byline_hide_post_date_default', 0 );
		$hide_last_updated = get_theme_mod( 'author_kit_byline_hide_last_updated_date_default', 1 );
		$hide_categories   = get_theme_mod( 'author_kit_byline_hide_categories_default', 1 );
		$hide_comments     = get_theme_mod( 'author_kit_byline_hide_comments_default', 1 );
		break;
}



?>

<div class="author-kit-byline    author-kit-byline-container">
	<div class="author-kit-main-author-contributors">

		<?php if ( ! $hide_post_date && ! empty( $first_published_date ) ) : ?>
			<div>
				<span class="author-kit-published-date" itemprop="datePublished" content="<?php echo esc_attr( $first_published_date ); ?>">
					<i class="fa-regular fa-clock" style="margin-right: 3px;"></i>
					<?php echo esc_html( $label_published_on ); ?> <?php echo esc_html( $first_published_date ); ?>
				</span>
			</div>
			<span id="author-kit-published-date-separator" class="author-kit-separator"><?php echo esc_html( $authorkit_byline_separator ); ?></span>

		<?php endif; ?>


		<span class="author-kit-main-author-label">
			<?php echo esc_html( $label_written_by ); ?>
		</span>

		<?php if ( ! empty( $author_kit_name ) && ! empty( $author_id ) ) : ?>
			<div class="author-kit-main-author" itemprop="author" itemscope itemtype="http://schema.org/Person">
				<?php if ( ! $hide_avatar && ! empty( $avatar ) ) : ?>
                    <?php // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
					echo '<img src="' . esc_url( $avatar ) . '" alt="' . esc_attr( $avatar_alt_text ) . '" class="author-kit-author-avatar" itemprop="image">';
					?>
				<?php endif; ?>
				<a href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>" class="author-kit-byline-author-name" itemprop="name">
					<?php echo wp_kses_post( $author_kit_name ); ?>
				</a>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $coauthors ) && is_array( $coauthors ) ) : ?>
			<span class="author-kit-separator"><?php echo esc_html( $authorkit_byline_separator ); ?></span>
			<span class="author-kit-contributions-label"><?php echo esc_html( $label_contributions ); ?></span>

			<?php
			foreach ( $coauthors as $index => $coauthor_id ) :
				$coauthor_avatar = get_avatar_url( $coauthor_id );
				$coauthor_name   = author_kit_display_name( $coauthor_id );
				$coauthor_url    = get_author_posts_url( $coauthor_id );
				?>
				<div class="author-kit-coauthor" itemprop="contributor" itemscope itemtype="http://schema.org/Person">
					<?php if ( ! $hide_avatar && ! empty( $coauthor_avatar ) ) : ?>
                        <?php // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
						echo '<img src="' . esc_url( $coauthor_avatar ) . '" alt="' . esc_attr( $coauthor_name ) . '" class="author-kit-coauthor-avatar" itemprop="image">';
						?>
					<?php endif; ?>
					<?php if ( ! empty( $coauthor_name ) && ! empty( $coauthor_url ) ) : ?>
						<a href="<?php echo esc_url( $coauthor_url ); ?>" class="author-kit-coauthor-name" itemprop="name">
							<?php echo wp_kses_post( $coauthor_name ); ?>
						</a>
					<?php endif; ?>
					<?php if ( $index < count( $coauthors ) - 1 ) : ?>
						<span class="author-kit-comma-separator">,</span>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		<?php endif; ?>

		<?php if ( ! $hide_categories ) : ?>
			<span id="author-kit-categories-separator" class="author-kit-separator"><?php echo esc_html( $authorkit_byline_separator ); ?></span>
			<div id="author-kit-category-container">
				<?php $categories = get_the_category(); ?>
				<?php if ( ! empty( $categories ) ) : ?>
					<span class="author-kit-category-label">
						<i class="fas fa-tag" style="margin-right: 3px;"></i> <?php echo esc_html( $label_in_category ); ?>
					</span>
					<?php foreach ( $categories as $index => $category ) : ?>
						<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" class="author-kit-post-category">
							<?php echo esc_html( $category->name ); ?>
						</a>
						<?php if ( $index < count( $categories ) - 1 ) : ?>
							<span class="author-kit-comma-separator">,</span>
						<?php endif; ?>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<?php if ( ! $hide_last_updated && ! empty( $last_updated_date ) ) : ?>
			<span id="author-kit-last-updated-date-separator" class="author-kit-separator"><?php echo esc_html( $authorkit_byline_separator ); ?></span>
			<div>
				<span class="author-kit-last-updated" itemprop="dateModified" content="<?php echo esc_attr( $last_updated_date ); ?>">
					<i class="fa-regular fa-pen-to-square" style="margin-right: 3px;"></i>
					<?php echo esc_html( $label_last_updated ); ?> <?php echo esc_html( $last_updated_date ); ?>
				</span>
			</div>
		<?php endif; ?>

		<?php if ( ! $hide_comments ) : ?>
			<span id="author-kit-comment-separator" class="author-kit-separator"><?php echo esc_html( $authorkit_byline_separator ); ?></span>
			<div class="author-kit-comment-link">
				<a href="<?php echo esc_url( get_comments_link() ); ?>" class="author-kit-comments-section-link">
					<i class="fa-regular fa-comments" style="margin-right: 3px;"></i>
					<?php
					comments_number(
						esc_html( $label_no_comments ),
						esc_html( $label_one_comment ),
						esc_html( $label_multiple_comments )
					);
					?>
				</a>
			</div>
		<?php endif; ?>

	</div>
</div>
