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

$sunflower_styled_layout = (bool) get_post_meta( $post->ID, '_sunflower_styled_layout', true ) ?? false;
if ( 'sunflower_event' === get_post_type() ) {
	$sunflower_styled_layout = false;
}

$sunflower_show_post_thumbnail = has_post_thumbnail() && ! get_post_meta( $post->ID, '_sunflower_hide_feature_image', true );
$sunflower_metadata            = $args['metadata'] ?? '';
$sunflower_class               = $args['class'] ?? '';

// Check if content is empty for layout purposes.
$sunflower_content_empty = empty( trim( wp_strip_all_tags( get_the_content() ) ) );
if ( $sunflower_content_empty ) {
	$sunflower_class .= ' content-empty';
}
?>

<article id="post-<?php the_ID(); ?>" <?php post_class( $sunflower_class ); ?>>
	<?php if ( ! $sunflower_styled_layout ) { ?>
	<header class="entry-header <?php echo ( $sunflower_show_post_thumbnail ) ? 'has-post-thumbnail' : 'has-no-post-thumbnail'; ?>">
		<div class="row position-relative">
			<div class="col-12">
		<?php
		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;
		?>
			</div>
		</div>
	</header><!-- .entry-header -->
		<?php
	}
	?>

	<?php
	if ( $sunflower_show_post_thumbnail ) {
		sunflower_post_thumbnail( $sunflower_styled_layout, true );
	}

	if ( 'post' === get_post_type() ) :
		?>
		<div class="entry-meta">
			<?php
			sunflower_posted_on();

			if ( sunflower_get_setting( 'sunflower_show_author' ) ) {
				sunflower_posted_by();
			}
			?>

			<?php
			$sunflower_roofline = get_post_meta( $post->ID, '_sunflower_roofline', true );
			if ( $sunflower_roofline ) {
				printf( '| <div class="sunflower_post_meta metainfo">%s</div>', esc_attr( $sunflower_roofline ) );
			}
			?>
		</div><!-- .entry-meta -->
		<?php
	endif;
	?>

	<div class="row">
	<?php
	if ( sunflower_get_setting( 'sunflower_sharer_mastodon' ) ) {
		?>
		<div class="modal fade" id="mastodonShareModal" tabindex="-1" role="dialog" aria-labelledby="mastodonShareModalLabel" aria-hidden="true">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
			<div class="modal-header">
				<h3 class="modal-title" id="mastodonShareModalLabel"><?php esc_html_e( 'Mastodon Instance address', 'sunflower' ); ?></h3>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<form>
				<div class="modal-body">
					<div class="form-group">
						<label for="msb-address"><?php esc_html_e( 'Enter your instance\'s address', 'sunflower' ); ?></label>
						<input type="text" class="form-control" id="msb-address" required="required" placeholder="https://gruene.social">
					</div>
					<div class="form-check">
						<input type="checkbox" class="form-check-input" id="msb-memorize-instance">
						<label class="form-check-label" for="msb-memorize-instance"><?php esc_html_e( 'Memorize my instance', 'sunflower' ); ?></label>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php esc_html_e( 'Close', 'sunflower' ); ?></button>
					<button id="msb-share" type="submit" class="btn btn-primary"><?php esc_html_e( 'Save and Share', 'sunflower' ); ?></button>
				</div>
			</form>
			</div>
		</div>
		</div>
		<?php } ?>
			<div class="entry-content">
				<?php
				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>', 'sunflower' ),
							array(
								'span' => array(
									'class' => array(),
								),
							)
						),
						wp_kses_post( get_the_title() )
					)
				);

				wp_link_pages(
					array(
						'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'sunflower' ),
						'after'  => '</div>',
					)
				);
				?>
			</div><!-- .entry-content -->

		<?php
		if ( $sunflower_metadata ) {
			printf(
				'<aside class="metabox small">%s</aside>',
				wp_kses_post( $sunflower_metadata )
			);
		}
		?>

	</div>

	<?php
		// Hook for adding content before the footer of a post.
		do_action( 'sunflower_content_before_footer', get_post() );
	?>

	<div class="row">
		<footer class="entry-footer mt-4">
			<?php sunflower_entry_footer( true ); ?>
		</footer><!-- .entry-footer -->
	</div>


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