<?php

global $post, $restorator_options;

$restorator_custom_divider 					= $restorator_options['custom_divider']['url'];
$restorator_custom_divider_width 			= isset( $restorator_options['custom_divider_width'] ) ? $restorator_options['custom_divider_width'] : '48';
$restorator_single_post_related_posts_title = isset( $restorator_options['single_post_related_posts_title'] ) ? $restorator_options['single_post_related_posts_title'] : esc_html__('Related Posts', 'restorator');
$restorator_blogsingle_layout 				= isset( $restorator_options['blogsingle_layout'] ) ? $restorator_options['blogsingle_layout'] : 'left-sidebar';
$restorator_post_excerpt_count 				= 155;
if ( $restorator_blogsingle_layout != 'no-sidebar') {
	$restorator_post_excerpt_count = 100;
}
?>

	<div id="related_posts_list">
		
		<?php if ( !empty( $restorator_single_post_related_posts_title ) ) { ?>
			<h4><?php echo esc_attr( $restorator_single_post_related_posts_title ); ?></h4>
			<div class="divider_active">
				<?php if( isset( $restorator_custom_divider ) && !empty( $restorator_custom_divider ) ){ ?>
					<img src="<?php echo esc_url( $restorator_custom_divider ); ?>" alt="" style="width:<?php echo esc_attr( $restorator_custom_divider_width ); ?>px !important;" />
				<?php } else { ?>
					<span class="bg_primary" style="width:<?php echo esc_attr( $restorator_custom_divider_width ); ?>px !important;"></span>
				<?php } ?>
			</div>
		<?php } ?>
		
		<div class="owl-carousel">

			<?php
				
				$categories = get_the_category($post->ID);
				
				if ($categories) {

					$category_ids = array();

					foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
					
					$args = array(
						'category__in'     => $category_ids,
						'post__not_in'     => array($post->ID),
						'ignore_sticky_posts' => 1,
						'orderby' => 'rand'
					);
					
						$my_query = new WP_Query($args);
						if( $my_query->have_posts() ) {
							while ($my_query->have_posts()) : $my_query->the_post();
								
								$restorator_featured_image_url = wp_get_attachment_url(get_post_thumbnail_id());
								$restorator_featured_image = '<img src="' . restorator_aq_resize($restorator_featured_image_url, 370, 250, true, true, true) . '" alt="' . get_the_title() . '" />';
								$text = wp_strip_all_tags(do_shortcode(get_the_content()));
								$restorator_post_excerpt = restorator_smarty_modifier_truncate($text, $restorator_post_excerpt_count);
								
								echo '
									<div class="item">
										<div class="posts_carousel_wrap clearfix">
											<div class="post_format_content">
												<div class="post_likes">';
													restorator_likes();
												echo '</div>
									';
												if( !empty( $restorator_featured_image_url ) ) {
													echo '<a href="' . get_permalink() . '">' . $restorator_featured_image . '</a>';
												}
										echo '
											</div>
											<div class="posts_carousel_content">
												<span class="post-meta-date">' . get_the_time('j F, Y') . '</span>
												<h5 class="post-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h5>
												<p>' . esc_html( $restorator_post_excerpt ) . '</p>
												' . restorator_comment_count() . '
											</div>
										</div>
									</div>
								';
								
							endwhile;
							wp_reset_postdata();
						}
					
				}
			?>

		</div>
	</div>

	<script type="text/javascript">
		jQuery(window).load(function() {
			jQuery('#related_posts_list .owl-carousel').owlCarousel({
				margin: 30,
				dots: false,
				nav: false,
				autoplay: true,
				autoplaySpeed: 1000,
				autoplayTimeout: 3000,
				navSpeed: 1000,
				autoplayHoverPause: true,
				responsive: {
					0: {items: 1},
					481: {items: 2},
					769: {items: 2},
					1025: {items: 3}
				}
			});
		});
	</script>