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

defined( 'WPINC' ) || exit;

/**
 * Single sections
 */
class Blocks_Single_Sections extends Blocks_Base {

	public function render() {
		?>
		<article class="<?php blocks_article_class(); ?>">
			<div id="content_box" >
				<?php
				if ( have_posts() ) :
					while ( have_posts() ) :
						the_post();
						?>
						<div id="post-<?php the_ID(); ?>" <?php post_class( 'g post' ); ?>>
							<?php
							if ( blocks_get_settings( 'mts_breadcrumb' ) ) {
								if ( function_exists( 'rank_math' ) && rank_math()->breadcrumbs ) {
									rank_math_the_breadcrumbs();
								} else {
									blocks_breadcrumbs();
								}
							}
							// Single post parts ordering.
							if ( ( null !== blocks_get_settings( 'mts_single_post_layout' ) ) && is_array( blocks_get_settings( 'mts_single_post_layout' ) ) && array_key_exists( 'enabled', blocks_get_settings( 'mts_single_post_layout' ) ) ) {
								$single_post_parts = blocks_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':
										blocks()->single_sections->single_post();
										break;

									// Post tags.
									case 'tags':
										?>
										<div class="tags-section">
											<?php
											blocks_post_tags( '<div class="tags">', ' ' );
											if ( blocks_get_settings( 'single_like_button' ) ) {
												?>
													<div class="single-like-button">
														<?php echo get_simple_likes_button( get_the_ID() ); ?>
													</div>
												<?php
											}
											$this->single_post_social_icons( 'below' );
											?>
										</div>
										<?php
										break;

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

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

									// Subscribe box.
									case 'subscribe':
										blocks()->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() {
		if ( 'mts_nosidebar' !== mts_custom_sidebar() ) {
			$sidebar = blocks_custom_sidebar_layout();
			$class   = 'sidebar-' . $sidebar;
		}
		//$sidebar = mts_custom_sidebar();
		?>
		<div class="single_post <?php echo $class; ?>">

			<?php blocks_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'     => '<i class="fa fa-chevron-right"></i>',
			'previouspagelink' => '<i class="fa fa-chevron-left"></i>',
			'pagelink'         => '%',
			'echo'             => 1,
		);
		wp_link_pages( $args );
	}

	public function single_author_section() {
		?>
		<div class="postauthor">
			<?php
			// Author gravatar.
			if ( function_exists( 'get_avatar' ) ) {
				echo get_avatar( get_the_author_meta( 'email' ), '94' ); // Gravatar size.
			}
			?>
			<h5 class="vcard author">
				<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>
			<?php
			if ( blocks_get_settings( 'authorbox_social_share' ) ) {
				$userid      = get_current_user_id();
				$facebook    = get_the_author_meta( 'facebook', $userid );
				$twitter     = get_the_author_meta( 'twitter', $userid );
				$instagram   = get_the_author_meta( 'instagram', $userid );
				$pinterest   = get_the_author_meta( 'pinterest', $userid );
				$stumbleupon = get_the_author_meta( 'stumbleupon', $userid );
				$linkedin    = get_the_author_meta( 'linkedin', $userid );

				if ( ! empty( $facebook ) || ! empty( $twitter ) || ! empty( $instagram ) || ! empty( $pinterest ) || ! empty( $stumbleupon ) || ! empty( $linkedin ) ) {
					echo '<div class="author-social clearfix">';
					if ( ! empty( $facebook ) ) {
						echo '<a href="' . $facebook . '" class="facebook"><i class="fa fa-facebook"></i></a>';
					}
					if ( ! empty( $twitter ) ) {
						echo '<a href="' . $twitter . '" class="twitter"><i class="fa fa-twitter"></i></a>';
					}
					if ( ! empty( $instagram ) ) {
						echo '<a href="' . $instagram . '" class="instagram"><i class="fa fa-instagram"></i></a>';
					}
					if ( ! empty( $pinterest ) ) {
						echo '<a href="' . $pinterest . '" class="pinterest"><i class="fa fa-pinterest"></i></a>';
					}
					if ( ! empty( $stumbleupon ) ) {
						echo '<a href="' . $stumbleupon . '" class="stumble"><i class="fa fa-stumbleupon"></i></a>';
					}
					if ( ! empty( $linkedin ) ) {
						echo '<a href="' . $linkedin . '" class="linkedin"><i class="fa fa-linkedin"></i></a>';
					}
					echo '</div>';
				}
			}
			?>
		</div>
		<?php
	}

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

	public function single_post_ads( $location ) {
		// Above Content Ad Code.
		if ( ! empty( blocks_get_settings( 'mts_posttop_adcode' ) ) && 'above' == $location ) {
			$toptime = ! empty( blocks_get_settings( 'mts_posttop_adcode_time' ) ) ? blocks_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( blocks_get_settings( 'mts_posttop_adcode' ) ); ?>
				</div>
				<?php
			}
		}
		// Below Content Ad Code.
		if ( ! empty( blocks_get_settings( 'mts_postend_adcode' ) ) && 'below' == $location ) {
			$endtime = ! empty( blocks_get_settings( 'mts_postend_adcode_time' ) ) ? blocks_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( blocks_get_settings( 'mts_postend_adcode' ) ); ?>
				</div>
				<?php
			}
		}
	}

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

}
