<?php
/**
 * Template for Single posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package stoni
 * @since 1.0.0
 * @version 1.0.0
 */

use WglAddons\Templates\WglBlogHero;

get_header();
the_post();

$sb = Stoni_Theme_Helper::render_sidebars('single');
$column = $sb['column'];
$row_class = $sb['row_class'];
$container_class = $sb['container_class'];
$layout = $sb['layout'];

$single_type = Stoni_Theme_Helper::get_option('single_type_layout');
if (empty($single_type)) {
	$single_type = 2;
}

if (class_exists( 'RWMB_Loader' )) {
	$mb_type = rwmb_meta('mb_post_layout_conditional');
	if(!empty($mb_type) && $mb_type != 'default' ){
		$single_type = rwmb_meta('mb_single_type_layout');
	}
}


$row_class .= ' single_type-'.$single_type;

if ($single_type === '3') {
	echo '<div class="post_featured_bg">';
		get_template_part('templates/post/single/post', $single_type.'_image');
	echo '</div>';
}
?>

<div class="wgl-container<?php echo apply_filters('stoni_container_class', $container_class); ?>">
        <div class="row<?php echo apply_filters('stoni_row_class', $row_class); ?>">
			<div id='main-content' class="wgl_col-<?php echo apply_filters('stoni_column_class', $column); ?>">
				<?php
					get_template_part('templates/post/single/post', $single_type);


				$previousPost = get_adjacent_post(false, '', true);
				$nextPost  = get_adjacent_post(false, '', false);

				if ($nextPost || $previousPost):
					?>
					<div class="stoni-post-navigation">
						<?php
						if(is_a( $previousPost, 'WP_Post' )){
							$image_prev_url = wp_get_attachment_image_src(get_post_thumbnail_id($previousPost->ID), 'thumbnail');

							$img_prev_html = '';
							$class_image_prev = ' no_image';
							$img_prev_html .= "<span class='image_prev". esc_attr($class_image_prev)."'>";

							$img_prev_html .= "<span class='no_image_post'></span>";

							$img_prev_html .= "</span>";

							echo '<div class="prev-link_wrapper">';
								echo '<div class="info_prev-link_wrapper"><a href="' . esc_url(get_permalink($previousPost->ID)) . '" title="' . esc_attr($previousPost->post_title) . '">'.$img_prev_html.'<span class="prev-link-info_wrapper"><span class="prev_title">'.esc_html($previousPost->post_title) .'</span></span></a></div>';
							echo '</div>';
						}
						if(is_a( $nextPost, 'WP_Post' )) {
							$image_next_url = wp_get_attachment_image_src(get_post_thumbnail_id($nextPost->ID), 'thumbnail');

							$img_next_html = '';
							$class_image_next = ' no_image';
							$img_next_html .= "<span class='image_next".esc_attr($class_image_next)."'>";

							$img_next_html .= "<span class='no_image_post'></span>";

							$img_next_html .= "</span>";
							echo '<div class="next-link_wrapper">';
							echo '<div class="info_next-link_wrapper"><a href="' . esc_url(get_permalink($nextPost->ID)) . '" title="' . esc_attr($nextPost->post_title) . '"><span class="next-link-info_wrapper"><span class="next_title">'.esc_html($nextPost->post_title) .'</span></span>'.$img_next_html.'</a></div>';
							echo '</div>';
						}
						?>
					</div>
					<?php
				endif;

				$show_post_related = Stoni_Theme_Helper::get_option('single_related_posts');

				if (class_exists( 'RWMB_Loader' )) {
					$mb_blog_show_r = rwmb_meta('mb_blog_show_r');
					if(!empty($mb_blog_show_r) && $mb_blog_show_r != 'default' ){
						$show_post_related = $mb_blog_show_r === 'off' ? null : $mb_blog_show_r;
					}
				}

				if (
                    (bool) $show_post_related
                    && class_exists('Stoni_Core')
                    && class_exists('\Elementor\Plugin')
                ) {

					$mb_blog_carousel_r = $mb_blog_column_r = $mb_blog_number_r = $mb_blog_title_r ='';
					$mb_blog_cat_r = array();

					$mb_blog_carousel_r = Stoni_Theme_Helper::options_compare('blog_carousel_r', 'mb_blog_show_r', 'custom');
					$mb_blog_title_r = Stoni_Theme_Helper::options_compare('blog_title_r', 'mb_blog_show_r', 'custom');

					$cats = Stoni_Theme_Helper::get_option('blog_cat_r');
					if (!empty($cats)) {
						$mb_blog_cat_r[] = implode(",", $cats);
					}

					if (
                        class_exists('RWMB_Loader')
                        && get_queried_object_id() !== 0
                        && rwmb_meta('mb_blog_show_r') == 'custom'
                    ) {
						$mb_blog_cat_r = get_post_meta(get_the_id(), 'mb_blog_cat_r');
					}

					$mb_blog_column_r = Stoni_Theme_Helper::options_compare('blog_column_r', 'mb_blog_show_r', 'custom');
					$mb_blog_number_r = Stoni_Theme_Helper::options_compare('blog_number_r', 'mb_blog_show_r', 'custom');

					echo '<div class="single related_posts">';

						// Related Posts
						// Get Cats_Slug
						$categories = $post_category_compile = '';
						if (get_the_category()) $categories = get_the_category();

						if ($categories) {
							$post_categ = '';
							foreach ($categories as $category) {
								$post_categ = $post_categ . $category->slug . ',';
							}
							$post_category_compile .= '' . trim($post_categ, ',') . '';

							if(!empty($mb_blog_cat_r[0])){
								$categories = get_categories( array( 'include' => $mb_blog_cat_r[0]  ) );
								$post_categ = $post_category_compile = '';
								foreach ($categories as $category) {
									$post_categ = $post_categ . $category->slug . ',';
								}
								$post_category_compile .= '' . trim($post_categ, ',') . '';
							}

							$mb_blog_cat_r = $post_category_compile;
						}
						echo '<div class="stoni_module_title"><h4>'.(!empty($mb_blog_title_r) ? esc_html($mb_blog_title_r) : esc_html__('Related Posts', 'stoni')) .' </h4></div>';

						$related_posts_atts = [
						    'blog_navigation' => 'none',
						    'use_navigation' => null,
						    'blog_layout' => !empty($mb_blog_carousel_r) ? 'carousel' : 'grid',
						    'hide_share' => true,
						    'hide_content' => null,
						    'hide_likes' => true,
						    'meta_author' => true,
						    'meta_comments' => true,
						    'read_more_hide' => null,
						    'read_more_text' => esc_html__('Read More', 'stoni'),
						    'heading_tag' => 'h4',
						    'content_letter_count' => 130,
						    'crop_square_img' => 1,
						    'items_load' => 4,
						    'name_load_more' => esc_html__('Load More', 'stoni'),
						    'blog_columns' => !empty($mb_blog_column_r) ? $mb_blog_column_r : (($layout == "none") ? "4" : "6"),
						    'autoplay' => null,
						    'autoplay_speed' => 3000,
						    'use_pagination' => null,
						    'pag_type' => 'circle',
						    'pag_offset' => '',
						    'custom_resp' => true,
						    'resp_medium' => null,
						    'pag_color' => null,
						    'custom_pag_color' => null,
						    'resp_tablets_slides' => null,
						    'resp_tablets' => null,
						    'resp_medium_slides' => null,
						    'resp_mobile' => '600',
                            'resp_mobile_slides' => '1',
                            // Query
						    'number_of_posts' => (int) $mb_blog_number_r,
						    'categories' => $mb_blog_cat_r,
						    'order_by' => 'rand',
                        ];

        				echo (new WglBlogHero())->render($related_posts_atts);

					echo '</div>';
                }

				if (comments_open() || get_comments_number()) {
                    ?>
					<div class="row">
						<div class="wgl_col-12">
							<?php comments_template(); ?>
						</div>
					</div>
                    <?php
                }

			echo '</div>';

			echo (!empty($sb['content']) ) ? $sb['content'] : '';

		echo '</div>';

echo '</div>';

get_footer();
