<?php
/**
 * The template used for displaying gallery
 *
 * @package Signify Pro
 */
?>

<?php

$signify_type = get_theme_mod( 'signify_gallery_type', 'page' );
$sub_title = get_theme_mod( 'signify_gallery_sub_title' );

if ( 'page' === $signify_type && $signify_id = get_theme_mod( 'signify_gallery' ) ) {
	$args['page_id'] = absint( $signify_id );
} elseif ( 'post' === $signify_type && $signify_id = get_theme_mod( 'signify_gallery_post' ) ) {
	$args['p'] = absint( $signify_id );
} elseif ( 'category' === $signify_type && $signify_cat = get_theme_mod( 'signify_gallery_category' ) ) {
	$args['cat'] = absint( $signify_cat );
	$args['posts_per_page'] = 1;
}

// If $args is empty return false
if ( empty( $args ) ) {
	return;
}

// Create a new WP_Query using the argument previously created
$gallery_query = new WP_Query( $args );
if ( $gallery_query->have_posts() ) :
	while ( $gallery_query->have_posts() ) :
		$gallery_query->the_post();

		$classes = array();

		if ( ! get_theme_mod( 'signify_display_gallery_title' ) && ! $sub_title ) {
			$classes[] = 'no-section-heading';
		}
		$text   = get_theme_mod( 'signify_gallery_text' );
		if ( '' == $text ) {
			$classes[] = 'no-view-more-button';
		}
		?>
		<div id="gallery-section" class="gallery-section section <?php echo esc_attr( implode( ' ', $classes ) ); ?>">
			<div class="wrapper">
				<?php if ( get_theme_mod( 'signify_display_gallery_title' ) || $sub_title ) : ?>
				<div class="section-heading-wrapper featured-section-headline">
					<?php if ( $sub_title ) : ?>
						<div class="section-subtitle">
							<?php
							echo esc_html( $sub_title );
							?>
						</div><!-- .section-description-wrapper -->
					<?php endif; ?>
					<?php if ( get_theme_mod( 'signify_display_gallery_title' ) ) : ?>
						<header class="entry-header">
							<h2 class="entry-title section-title">
								<?php the_title(); ?>
							</h2>
						</header><!-- .entry-header -->
					<?php endif; ?>
				</div><!-- .section-heading-wrapper -->
				<?php endif; ?>
				<div class="section-content-wrapper gallery-content-wrapper">
					<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
						<div class="hentry-inner">
							<div class="entry-container">
								<div class="entry-content">
									<?php the_content(); ?>
								</div><!-- .entry-content -->

								<?php if ( get_edit_post_link() ) : ?>
									<footer class="entry-footer">
										<div class="entry-meta">
											<?php
												edit_post_link(
													sprintf(
														/* translators: %s: Name of current post */
														esc_html__( 'Edit %s', 'signify-pro' ),
														the_title( '<span class="screen-reader-text">"', '"</span>', false )
													),
													'<span class="edit-link">',
													'</span>'
												);
											?>
										</div>	<!-- .entry-meta -->
									</footer><!-- .entry-footer -->
								<?php endif; ?>
							</div><!-- .entry-container -->
						</div><!-- .hentry-inner -->
					</article>
				</div><!-- .section-content-wrapper -->
			</div><!-- .wrapper -->
			<?php
			$target = get_theme_mod( 'signify_gallery_target' ) ? '_blank': '_self';
			$signify_link   = get_theme_mod( 'signify_gallery_link', '#' );
			$text   = get_theme_mod( 'signify_gallery_text' );
			if ( $text ) :
			?>
				<p class="view-more">
					<a class="button" target="<?php echo $target; ?>" href="<?php echo esc_url( $signify_link ); ?>"><?php echo esc_html( $text ); ?></a>
				</p>
			<?php endif; ?>
		</div><!-- .section -->
		<?php
	endwhile;
	wp_reset_postdata();
endif;
