<?php
/**
 * The template to display single post
 *
 * @package HYGIA
 * @since HYGIA 1.0
 */

// Full post loading
$full_post_loading          = hygia_get_value_gp( 'action' ) == 'full_post_loading';

// Prev post loading
$prev_post_loading          = hygia_get_value_gp( 'action' ) == 'prev_post_loading';
$prev_post_loading_type     = hygia_get_theme_option( 'posts_navigation_scroll_which_block', 'article' );

// Position of the related posts
$hygia_related_position   = hygia_get_theme_option( 'related_position', 'below_content' );

// Type of the prev/next post navigation
$hygia_posts_navigation   = hygia_get_theme_option( 'posts_navigation' );
$hygia_prev_post          = false;
$hygia_prev_post_same_cat = (int)hygia_get_theme_option( 'posts_navigation_scroll_same_cat', 1 );

// Rewrite style of the single post if current post loading via AJAX and featured image and title is not in the content
if ( ( $full_post_loading 
		|| 
		( $prev_post_loading && 'article' == $prev_post_loading_type )
	) 
	&& 
	! in_array( hygia_get_theme_option( 'single_style' ), array( 'style-6' ) )
) {
	hygia_storage_set_array( 'options_meta', 'single_style', 'style-6' );
}

do_action( 'hygia_action_prev_post_loading', $prev_post_loading, $prev_post_loading_type );

get_header();

while ( have_posts() ) {

	the_post();

	// Type of the prev/next post navigation
	if ( 'scroll' == $hygia_posts_navigation ) {
		$hygia_prev_post = get_previous_post( $hygia_prev_post_same_cat );  // Get post from same category
		if ( ! $hygia_prev_post && $hygia_prev_post_same_cat ) {
			$hygia_prev_post = get_previous_post( false );                    // Get post from any category
		}
		if ( ! $hygia_prev_post ) {
			$hygia_posts_navigation = 'links';
		}
	}

	// Override some theme options to display featured image, title and post meta in the dynamic loaded posts
	if ( $full_post_loading || ( $prev_post_loading && $hygia_prev_post ) ) {
		hygia_sc_layouts_showed( 'featured', false );
		hygia_sc_layouts_showed( 'title', false );
		hygia_sc_layouts_showed( 'postmeta', false );
	}

	// If related posts should be inside the content
	if ( strpos( $hygia_related_position, 'inside' ) === 0 ) {
		ob_start();
	}

	// Display post's content
	get_template_part( apply_filters( 'hygia_filter_get_template_part', 'templates/content', 'single-' . hygia_get_theme_option( 'single_style' ) ), 'single-' . hygia_get_theme_option( 'single_style' ) );

	// If related posts should be inside the content
	if ( strpos( $hygia_related_position, 'inside' ) === 0 ) {
		$hygia_content = ob_get_contents();
		ob_end_clean();

		ob_start();
		do_action( 'hygia_action_related_posts' );
		$hygia_related_content = ob_get_contents();
		ob_end_clean();

		if ( ! empty( $hygia_related_content ) ) {
			$hygia_related_position_inside = max( 0, min( 9, hygia_get_theme_option( 'related_position_inside' ) ) );
			if ( 0 == $hygia_related_position_inside ) {
				$hygia_related_position_inside = mt_rand( 1, 9 );
			}

			$hygia_p_number         = 0;
			$hygia_related_inserted = false;
			$hygia_in_block         = false;
			$hygia_content_start    = strpos( $hygia_content, '<div class="post_content' );
			$hygia_content_end      = strrpos( $hygia_content, '</div>' );

			for ( $i = max( 0, $hygia_content_start ); $i < min( strlen( $hygia_content ) - 3, $hygia_content_end ); $i++ ) {
				if ( $hygia_content[ $i ] != '<' ) {
					continue;
				}
				if ( $hygia_in_block ) {
					if ( strtolower( substr( $hygia_content, $i + 1, 12 ) ) == '/blockquote>' ) {
						$hygia_in_block = false;
						$i += 12;
					}
					continue;
				} else if ( strtolower( substr( $hygia_content, $i + 1, 10 ) ) == 'blockquote' && in_array( $hygia_content[ $i + 11 ], array( '>', ' ' ) ) ) {
					$hygia_in_block = true;
					$i += 11;
					continue;
				} else if ( 'p' == $hygia_content[ $i + 1 ] && in_array( $hygia_content[ $i + 2 ], array( '>', ' ' ) ) ) {
					$hygia_p_number++;
					if ( $hygia_related_position_inside == $hygia_p_number ) {
						$hygia_related_inserted = true;
						$hygia_content = ( $i > 0 ? substr( $hygia_content, 0, $i ) : '' )
											. $hygia_related_content
											. substr( $hygia_content, $i );
					}
				}
			}
			if ( ! $hygia_related_inserted ) {
				if ( $hygia_content_end > 0 ) {
					$hygia_content = substr( $hygia_content, 0, $hygia_content_end ) . $hygia_related_content . substr( $hygia_content, $hygia_content_end );
				} else {
					$hygia_content .= $hygia_related_content;
				}
			}
		}

		hygia_show_layout( $hygia_content );
	}

	// Comments
	do_action( 'hygia_action_before_comments' );
	comments_template();
	do_action( 'hygia_action_after_comments' );

	// Related posts
	if ( 'below_content' == $hygia_related_position
		&& ( 'scroll' != $hygia_posts_navigation || (int)hygia_get_theme_option( 'posts_navigation_scroll_hide_related', 0 ) == 0 )
		&& ( ! $full_post_loading || (int)hygia_get_theme_option( 'open_full_post_hide_related', 1 ) == 0 )
	) {
		do_action( 'hygia_action_related_posts' );
	}

	// Post navigation: type 'scroll'
	if ( 'scroll' == $hygia_posts_navigation && ! $full_post_loading ) {
		?>
		<div class="nav-links-single-scroll"
			data-post-id="<?php echo esc_attr( get_the_ID( $hygia_prev_post ) ); ?>"
			data-post-link="<?php echo esc_attr( get_permalink( $hygia_prev_post ) ); ?>"
			data-post-title="<?php the_title_attribute( array( 'post' => $hygia_prev_post ) ); ?>"
			data-cur-post-link="<?php echo esc_attr( get_permalink() ); ?>"
			data-cur-post-title="<?php the_title_attribute(); ?>"
			<?php do_action( 'hygia_action_nav_links_single_scroll_data', $hygia_prev_post ); ?>
		></div>
		<?php
	}
}

get_footer();
