<?php

/*--------------------blog test----------------------------------*/
	if( !function_exists( 'yolo_shortcode_blog' ) ) {

	function yolo_shortcode_blog( $atts, $content = null ) {
			// include_once('yolo-html-pagination.php');
		extract( shortcode_atts( array(
			'layout'            => 'list',
			'columns'           => '2',
			'categories'        => 'all',
			'filter'            => '',
			'orderby'           => 'latest',
			'posts_per_page'    => '6',
			'pagination'		=> 'page_num',
			'loadmore_text'		=>__('Load More', 'yolo-kidzone'),
			/*'post_count'     	=> '4',*/
			'excerpt_length'	=> '55',
			'hide_readmore'		=> '',
			'title'             => '',
			'sub_title'         => '',
			'hide_author'		=> '',
			'hide_date'			=> '',
			'hide_category'		=> '',
			'hide_comment'		=> '',
			'visibility'        => '',
			'class'             => '',
			'id'                => '',
			'custom_style'      => ''
			), $atts ) );
		
		if( is_front_page() || is_home()) {
			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : ( ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1 );
		} else {
			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
		}

		$order = 'DESC';
		switch ($orderby) {
			case 'latest':
				$orderby = 'post_date';
				$order = 'DESC';
				break;

			case 'oldest':
				$orderby = 'post_date';
				$order = 'ASC';
				break;

			case 'alphabet':
				$orderby = 'title';
				$order = 'ASC';
				break;

			case 'ralphabet':
				$orderby = 'title';
				$order = 'DESC';
				break;
		}
		$args = array(
			'orderby'         => "{$orderby}",
			'order'           => "{$order}",
			'post_type'       => "post",
/*			'taxonomy' => 'product_cat',*/
			'posts_per_page'  => "{$posts_per_page}",
			'paged'			  => $paged
			);
		if(!empty($categories) && $categories != 'all'){
			$args['cat'] = $categories;
		}
		$q = new WP_Query( $args );

		$class            = ( $class           != ''     ) ? 'yolo-blog-posts ' . esc_attr( $class ) : 'yolo-blog-posts';
		
		$id    = ( $id    != '' ) ? ' id="' . esc_attr( $id ) . '"' : '';
		$class = ( $class != '' ) ? ' class="' . esc_attr( $class ) . '"' : '';
		$custom_style = ( $custom_style != '' ) ? ' style="' . $custom_style . '"' : '';

		$html = array();
		$item_class='';
		$html[] ='<div '.$id.$class.$custom_style.'>';
		
		$html[] = (!empty($title) ? '<h2 class="title">'.$title.(!empty($sub_title) ? '<p class="small sub-title">'.$sub_title.'</p>' : '').'</h2>' : '');?>
<?php ob_start();?>
<div class="posts<?php echo (($pagination === 'loadmore') ? ' loadmore':''); ?><?php echo (($pagination === 'infinite_scroll') ? ' infinite-scroll':'') ?><?php echo (($layout === 'masonry') ? ' masonry':'') ?>" data-paginate="<?php echo esc_attr($pagination) ?>" data-layout="<?php echo esc_attr($layout);?>">
	<div class="posts-wrap<?php echo (($pagination === 'loadmore') ? ' loadmore-wrap':'') ?><?php echo (($pagination === 'infinite_scroll') ? ' infinite-scroll-wrap':'') ?><?php echo (($layout === 'masonry') ? ' masonry-wrap':'') ?> posts-layout-<?php echo esc_attr($layout)?><?php if( $layout == 'masonry') echo' row' ?> clear">
			<?php 
			$post_class = '';
			$masonry_grid_column = '';
			$grid_item = '';
			$grid_clear = '';
			$post_class .= (($pagination === 'infinite_scroll') ? ' infinite-scroll-item ':'');
			$post_class .= (($pagination === 'loadmore') ? ' loadmore-item ':'');	
			$masonry_grid_column .= (($layout === 'masonry'|| $layout === 'grid') ? ' columns-'.$columns : ''); 
			$grid_item .= (($layout === 'grid') ? ' grid-item' : '');
			?>		
			
			<?php wp_enqueue_script('yolo-loadmore');
			?>
			<?php $html[] = ob_get_clean();?>
			<?php $i = 0; $w = 0;$k=0;?>
			<?php	
			$prev_post_month = null;	
			if ( $q->have_posts() ) : 
				while ( $q->have_posts() ) : $q->the_post();
					global $post;$i++;
					?>
					<?php 
					$du = ($i % $columns);
					ob_start();
					$post_id = get_the_id();
					$post_type = get_post_type($post_id);
					$post_format = yolo_get_post_format($post_id, $post_type);
					$tags = get_the_tags($post_id);
					?>
					<?php 
					if( $layout === 'grid'){

						if(($du == 1) || ($du == $columns)){
							$grid_clear = ' yolo-clear-both';
						}else $grid_clear = '';
					}
					if( $layout == 'masonry' ) {
						// Categories class
						$cat_class = array();
						if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
							foreach ( (array) get_the_category($post->ID) as $cat ) {
								if ( empty($cat->slug ) )
									continue;
								$cat_class[] = 'mansonry-filter-' . sanitize_html_class($cat->slug, $cat->term_id);
							}
						}
						$masonry_size = yolo_get_post_meta($post_id, "_yolo_wp_post_masonry_{$post_format}_size", 'regular');
						$item_class = 'masonry-item '.$masonry_size.' '.implode(' ', $cat_class);
					}
					?>
					<?php 
					$post_timeline_class = '';
					if($layout == 'timeline'){

						$post_timestamp = strtotime( get_the_date() );
						$post_month = date( 'n', $post_timestamp );
						if($i % 2 == 0)
							$post_timeline_class =' timeline-align-right';
						else
							$post_timeline_class =' timeline-align-left';
						
						$time_arg['prev_post_month'] = $prev_post_month;
						$time_arg['post_month'] = $post_month;
							yolo_timeline_date($time_arg);
						
						$prev_post_month = $post_month;
					
					}?>
					<article id="post-<?php the_ID(); ?><?php ?>" <?php post_class($post_class.$item_class.$post_timeline_class.$masonry_grid_column.$grid_item.$grid_clear); ?>>
						<?php if($layout == 'timeline'): ?>
							<div class="timeline-badge">
								<span class= "moc-2"></span>
								<span class = "moc-1"></span>
							</div>
							<div class="timeline-arrow"></div>
						<?php endif; ?>
						<div class="content-wrap">
							
							<?php if( yolo_has_featured_content() ) : ?>
							<div class="content-featured">
								<?php
								switch ($post_format){
									case 'audio':
										yolo_featured_audio();
									break;
									case 'video':
										yolo_featured_video();
									break;
									case 'gallery':
										yolo_featured_gallery();
									break;
									case 'image':
										yolo_featured_image();
									break;
									case 'link':
									case 'quote':
									default:
										yolo_featured_default();
									break;
								}
								?>
								
							</div>
							<?php endif; ?>
							<?php if ($layout == 'list'):?>
								<?php if($post_format == 'link' || $post_format == 'quote' ):?>

										<?php else:?>
											<?php
												$time_date = get_the_date('d M');
											    $time_date = explode(' ', $time_date);
											    if(!$hide_date && is_singular()):?>
											        <div class = "date-style">
											        	<span class = "day">
											           		<?php echo esc_html($time_date[0]);?>
											            </span>
											            <span class = "month">
											            	<?php echo esc_html($time_date[1]);?>
											            </span>
											        </div>
											    <?php endif;?>
								<?php endif; ?>
							<?php endif;?>		
							<div class="<?php if($post_format=='quote') echo 'content-quote'; else echo 'content-info'?>">
								<?php if($post_format=='quote'):?>
									<?php
									$quote = '';
									$post_id = get_the_ID();
									$quote = yolo_get_post_meta($post_id , '_yolo_wp_post_quote', get_the_title( $post_id ) );
									$cite = yolo_get_post_meta($post_id , '_yolo_wp_post_quote_citation', '');
									?>
									<h2 class="content-title content-quote">
										<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr(sprintf(__('Permanent link to: "%s"', 'yolo-kidzone') , the_title_attribute('echo=0'))); ?>">
											<?php echo esc_html($quote); ?>
										</a>
									</h2>
									<cite class="content-sub-title content-cite"><i class="fa fa-quote-left"></i> <?php echo esc_html($cite); ?></cite>
								<?php else:?>
									<h2 class="content-title">
										<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permanent link to: "%s"', 'yolo-kidzone' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_title(); ?></a>
									</h2>
									<div class = "yolo-author">
										<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID')); ?>">
										<?php echo get_avatar( get_the_author_meta('user_email'),60 ) ; ?>
										</a>
									</div>
									<?php if ($layout == 'grid' || $layout == 'masonry'):?>
										<div class = "content-footer">
											<?php if($post_format == 'link' || $post_format == 'quote' ):?>

											<?php else:?>
												<?php yolo_content_meta(true); ?>
											<?php endif; ?>
											
										</div>
									<?php endif;?>
									<?php if($post_format == 'link'):?>
										<?php $link = yolo_get_post_meta( $post_id, '_yolo_wp_post_link',  '' ); ?>
										<?php if($link != '') : ?>
										<span class="content-sub-title content-link">
											<a href="<?php echo esc_url($link); ?>" title="<?php echo esc_attr( sprintf( __( 'Shared link from post: "%s"', 'yolo-kidzone' ), the_title_attribute( 'echo=0' ) ) ); ?>" target="_blank">
											<i class="fa fa-link"></i>
											<?php echo esc_html($link); ?>
											</a>
										</span>
										<?php endif; ?>
									<?php endif;?>
								<?php endif;?>
													
								<?php if($post_format == 'link' || $post_format == 'quote' ):?>
								<?php else:?>
								<div class="content-excerpt">
									<?php 
									$excerpt = $post->post_excerpt;
									if(empty($excerpt))
										$excerpt = $post->post_content;
									
									$excerpt = strip_shortcodes($excerpt);
									$excerpt = wp_trim_words($excerpt,$excerpt_length,'...');
									echo '<p>' . $excerpt . '</p>';
									?>
									<?php
									if($layout == 'timeline'){
										 if(empty($hide_readmore)) echo yolo_get_readmore_link();
									}
									?>
								</div>
								<?php if ($layout == 'grid' || $layout == 'masonry'):?>
								<div class ="yolo-last">
									<div class ="content-loadmore">
										<?php //if(empty($hide_readmore)) echo yolo_get_readmore_link();?>	
									</div>
									<ul class = "yolo-tag">
										<?php if($tags):
										foreach ($tags as $tag) {?>
											<li><a href = "<?php echo get_tag_link($tag->term_id);?>"><?php echo $tag->name;?></a></li>
										<?php }endif;?>
										
									</ul>
								</div>
								<?php endif;?>
								<?php endif;?>
								<?php if ($layout == 'list'):?>
								<div class = "content-footer">
									<?php if(empty($hide_readmore)) echo yolo_get_readmore_link();?>
									<ul class = "yolo-tag">
										<?php if($tags):
										foreach ($tags as $tag) {?>
											<li><a href = "<?php echo get_tag_link($tag->term_id);?>"><?php echo $tag->name;?></a></li>
										<?php }endif;?>
										
									</ul>
								</div>
							<?php endif;?>
							</div>
							<?php if ($layout == 'timeline'):?>
								<div class = "content-footer">
									<?php if($post_format == 'link' || $post_format == 'quote' ):?>
									<?php else:?>
										<?php yolo_content_meta(true); ?>
									<?php endif; ?>
								</div>
							<?php endif;?>
						</div>
					</article>
					
					<?php
 				$html[] = ob_get_clean();
				endwhile;
				endif;
		ob_start();
			 
			?>
		<div style = "clear:both;width:100%;"></div>
		<?php if($pagination === 'loadmore' && 1 < $q->max_num_pages ):?>
			<div class="loadmore-action">
				<div class="loadmore-loading"><div class="fade-loading"><i class="fa fa-refresh fa-spin"></i></div></div>
				<a href="#" class="btn-loadmore btn-primary"><?php echo esc_html($loadmore_text) ?></a>
			</div>
		<?php endif;?>
		<?php if($pagination != 'no') yolo_pagination_normal( array() ,$q );?>
	</div>
</div>	
	<?php $html[] = '</div><div style ="clear:both;"></div>';?>
		
		<?php $html[] = ob_get_clean();
		wp_reset_query();
		wp_reset_postdata();
	return implode( "\n", $html );
	}
}

add_shortcode( 'blog_post', 'yolo_shortcode_blog' );