<?php
/**
 * Sidebars
 *
 * @package Risen
 */

defined( 'WPINC' ) || exit;

/**
 * Single sections
 */
class Risen_Single_Sections extends Risen_Base {

	public function render() {
	?>
	<article class="<?php risen_article_class(); ?>">
		<div id="content_box" >
			<?php
			if ( have_posts() ) :
				while ( have_posts() ) :
					the_post();
					$img_size = risen_get_settings( 'featured_image_size' );
					?>
					<div id="post-<?php the_ID(); ?>" <?php post_class( 'g post' . isset( $img_size ) ? 'header-style-' . $img_size : '' ); ?>>
						<?php
						if ( risen_get_settings( 'mts_breadcrumb' ) ) {
							if ( function_exists( 'rank_math' ) && rank_math()->breadcrumbs ) {
								rank_math_the_breadcrumbs();
							} else {
								risen_breadcrumbs();
							}
						}
						// Single post parts ordering.
						if ( ( null !== risen_get_settings( 'mts_single_post_layout' ) ) && is_array( risen_get_settings( 'mts_single_post_layout' ) ) && array_key_exists( 'enabled', risen_get_settings( 'mts_single_post_layout' ) ) ) {
							$single_post_parts = risen_get_settings( 'mts_single_post_layout' )['enabled'];
						} else {
							$single_post_parts = array(
								'content'   => 'content',
								'related'   => 'related',
								'author'    => 'author',
								'subscribe' => 'subscribe',
							);
						}

						foreach ( $single_post_parts as $part => $label ) {
							switch ( $part ) {
								// Content area.
								case 'content':
									risen()->single_sections->single_post();
									break;

								// Post tags.
								case 'tags':
									risen_post_tags( '<div class="tags"><span class="tagtext">' . esc_html__( 'Tags ', 'risen' ) . '<span class="bull">&bull;</span>' . '</span>', '' );
									break;

								// Related Posts.
								case 'related':
									if ( 'default' === risen_get_settings( 'related_posts_position' ) ) {
										risen_related_posts();
									}
									break;

								// Author box.
								case 'author':
									risen()->single_sections->single_author_section();
									break;

								// Subscribe box.
								case 'subscribe':
									risen()->single_sections->single_subscribe_box();
									break;
							}
						}
						?>
					</div><!--.g post-->
				<?php
				// Comment area.
				comments_template( '', true );

				endwhile;
			endif; /* end loop */
			?>
		</div>
	</article>
	<?php
	}

	public function single_post() {
		?>
		<div class="single_post">

			<?php risen_action( 'single_post_header' ); ?>

			<div class="post-single-content box mark-links entry-content">

				<?php
				$this->single_post_ads( 'above' );

				$this->single_post_social_icons( 'above' );
				?>

				<div class="thecontent">
					<?php the_content(); ?>
				</div>

				<?php
				$this->single_post_pagination();

				$this->single_post_ads( 'below' );

				$this->single_post_social_icons( 'below' );
				?>

			</div><!--.post-single-content-->
		</div><!--.single_post-->
		<?php
	}

	public function single_post_pagination() {
		// Single Post Pagination.
		$args = array(
			'before'           => '<div class="pagination">',
			'after'            => '</div>',
			'link_before'      => '<span class="current"><span class="currenttext">',
			'link_after'       => '</span></span>',
			'next_or_number'   => 'next_and_number',
			'nextpagelink'     => __( 'Next', 'risen' ),
			'previouspagelink' => __( 'Previous', 'risen' ),
			'pagelink'         => '%',
			'echo'             => 1,
		);
		wp_link_pages( $args );
	}

	public function single_author_section() {
	?>
		<div class="postauthor">

			<?php
			// Author box title.
			if ( ! empty( risen_get_settings( 'author_box_title' ) ) ) {
				printf( '<h4>%s</h4>', risen_get_settings( 'author_box_title' ) );
			}
			// Author gravatar.
			if ( function_exists( 'get_avatar' ) ) {
				echo get_avatar( get_the_author_meta( 'email' ), '100' ); // Gravatar size.
			}
			?>
			<h5 class="vcard author">
				<span class="post-by"><?php _e( 'Post By ', 'risen' ); ?></span>
				<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" class="fn">
					<?php the_author_meta( 'display_name' ); ?>
				</a>
			</h5>
			<p><?php the_author_meta( 'description' ); ?></p>
		</div>
	<?php
	}

	public function single_subscribe_box() {
		if ( is_active_sidebar( 'single-subscribe' ) && risen_get_settings( 'single_subscribe_box' ) ) {
		?>
			<div class="single-subscribe clear">
				<?php dynamic_sidebar( 'single-subscribe' ); ?>
			</div>
		<?php
		}
	}

	public function single_post_ads( $location ) {
		// Above Content Ad Code.
		if ( ! empty( risen_get_settings( 'mts_posttop_adcode' ) ) && 'above' == $location ) {
			$toptime = ! empty( risen_get_settings( 'mts_posttop_adcode_time' ) ) ? risen_get_settings( 'mts_posttop_adcode_time' ) : '0';
			if ( strcmp( date( 'Y-m-d', strtotime( -$toptime . ' day' ) ), get_the_time( 'Y-m-d' ) ) >= 0 ) {
				?>
				<div class="topad">
					<?php echo do_shortcode( risen_get_settings( 'mts_posttop_adcode' ) ); ?>
				</div>
				<?php
			}
		}
		// Below Content Ad Code.
		if ( ! empty( risen_get_settings( 'mts_postend_adcode' ) ) && 'below' == $location ) {
			$endtime = ! empty( risen_get_settings( 'mts_postend_adcode_time' ) ) ? risen_get_settings( 'mts_postend_adcode_time' ) : '0';
			if ( strcmp( date( 'Y-m-d', strtotime( -$endtime . ' day' ) ), get_the_time( 'Y-m-d' ) ) >= 0 ) {
				?>
				<div class="bottomad">
					<?php echo do_shortcode( risen_get_settings( 'mts_postend_adcode' ) ); ?>
				</div>
				<?php
			}
		}
	}

	public function single_post_social_icons( $location ) {
		// Social Share icons above content.
		if ( null !== risen_get_settings( 'mts_social_button_position' ) && 'top' === risen_get_settings( 'mts_social_button_position' ) && 'above' == $location ) {
			risen_social_buttons();
		}
		// Social Share icons below content and floating.
		if ( ( null !== risen_get_settings( 'mts_social_button_position' ) ) && 'top' !== risen_get_settings( 'mts_social_button_position' ) && 'below' == $location ) {
			risen_social_buttons();
		}
	}

}
