<?php
/**
 * Blogs Shortcode
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */
if ( !defined( 'ABSPATH' ) ) exit;

// Blogs Shortcode
function aio_shortcode_blogs( $atts ) {

	extract( shortcode_atts( array(
		'id' 				=> '',
		'posts_per_page' 	=> '',
		'pagination'		=> false,
		'category'			=> '',
		'module' 			=> '',
		'column'			=> '',
		'ani'				=> '',
		'animation'			=> ''
	), $atts));

	if ( $posts_per_page == '') { $posts_per_page = '-1'; }
	if ( $column == '') { $column = 'col-sm-6 col-md-3'; }
	if ( $animation != '' ) { $animation = 'data-animation="' . $animation . '"'; $ani = 'fx-start'; }

	global $wp_query;
	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$wp_query = new WP_Query( array(
		'p'					=> esc_attr( $id ),
		'posts_per_page' 	=> esc_attr( $posts_per_page ),
		'post_type' 		=> 'post',
		'category_name'		=> esc_attr( $category ),
		'post_status'		=> 'publish',
		'orderby'			=> 'date',
		'order'				=> 'DESC',
		'paged'				=> $paged
	) );

	ob_start(); ?>

	<?php if ( $module == 'classic' ) : ?>
		
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
		<!-- begin #post-## -->
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

			<?php if ( has_post_thumbnail() ) : ?>
				<div class="blog-classic clearfix">
			<?php else : ?>
				<div class="blog-classic no-thumbnail clearfix">
			<?php endif; ?>
			
				<!-- begin .entry-header -->
				<header class="entry-header">

					<?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

					<?php if ( 'post' == get_post_type() ) : ?>
						<div class="entry-meta">
							<?php aio_posted_on_classic(); ?>
						</div>
					<?php endif; ?>

				</header>
				<!-- end .entry-header -->
			
				<!-- begin .entry-content -->
				<div class="entry-content">

					<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
						<div class="entry-thumbnail fx-hover">
							<div class="fx-hover-back"></div>
							<div class="fx-hover-icon">
								<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
								<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
							</div>
							<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 280, 260, true, true, true ) ); ?>" width="280" height="260" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
						</div>
						
						<div class="entry-calendar">
							<span class="meta-day"><?php echo get_the_date('d'); ?></span>
							<span class="meta-month"><?php echo get_the_date('M'); ?></span>
						</div>
					<?php endif; ?>

					<div class="entry-summary">
						<p><?php echo aio_excerpt(28); ?></p>
					</div>

					<?php
					wp_link_pages( array(
						'before' => '<div class="page-links">' . esc_html__( 'Pages:', PLUGIN_NAME ),
						'after'  => '</div>',
					));
					?>

				</div>
				<!-- end .entry-content -->
			
			</div>
			
			<div class="lines"></div>
		</article>
		<!-- end #post-## -->
		<?php endwhile; ?>
		
		<!-- begin .pagination -->
		<nav class="pagination">
			<?php
			echo paginate_links( array(
				'base'               => str_replace( PHP_INT_MAX, '%#%', esc_url( get_pagenum_link( PHP_INT_MAX ) ) ),
				'format'             => '?paged=%#%',
				'current'            => max( 1, get_query_var( 'paged' ) ),
				'total'              => $wp_query->max_num_pages,
				'type'               => 'list',
				'prev_text'	=> __( 'Prev', PLUGIN_NAME ),
				'next_text'	=> __( 'Next', PLUGIN_NAME ),
			));
			?>
		</nav>
		<!-- end .pagination -->
		
	<?php elseif ( $module == 'standard' ) : ?>
	
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
		<!-- begin #post-## -->
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<div class="blog-standard">

				<!-- begin .entry-header -->
				<header class="entry-header">
				
					<div class="entry-calendar">
						<span class="meta-day"><?php echo get_the_date('d'); ?></span>
						<span class="meta-month"><?php echo get_the_date('M Y'); ?></span>
					</div>

					<?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

					<?php if ( 'post' == get_post_type() ) : ?>
						<div class="entry-meta">
							<?php aio_posted_on(); ?>
						</div>
					<?php endif; ?>

				</header>
				<!-- end .entry-header -->
			
				<!-- begin .entry-content -->
				<div class="entry-content">

					<?php if (get_post_format() == 'gallery' ) : ?>
					
						<?php if ( count( vp_metabox( 'post.gallery' ) ) != 0) { ?>
							<div class="entry-gallery">
								<div class="slider-gallery">
									<?php $lists = vp_metabox( 'post.gallery' ); ?>
									<?php foreach ( $lists as $list ) { ?>
										<div class="slider-item">
											<img width="800" height="420" src="<?php echo esc_url( aio_resize( $list['gallery_picture'], 800, 420, true, true, true ) ); ?>" alt="<?php echo esc_attr( $list['gallery_caption'] ); ?>" />
										</div>
									<?php } ?>
								</div>
							</div>
						<?php } ?>
					
					<?php elseif (get_post_format() == 'quote' ) : ?>

						<div class="entry-quote fx-hover">
							<blockquote>
								<p><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'post.quote.0.quote_text' ) ); ?><i class="fa fa-quote-right"></i></p>
								<cite><?php echo wp_strip_all_tags( vp_metabox( 'post.quote.0.quote_author' ) ); ?></cite>
							</blockquote>
						</div>
					
					<?php elseif (get_post_format() == 'link' ) : ?>

						<div class="entry-link fx-hover">
							<a href="<?php echo esc_url( vp_metabox( 'post.link.0.link_url' ) ); ?>"><?php echo esc_url( vp_metabox( 'post.link.0.link_url' ) ); ?></a>
						</div>
					
					<?php elseif (get_post_format() == 'video' ) : ?>
					
						<div class="entry-video fx-hover">
							<?php echo vp_metabox( 'post.video.0.video_code' ); ?>
						</div>
						
					<?php elseif (get_post_format() == 'audio' ) : ?>
					
						<div class="entry-audio fx-hover">
							<?php echo vp_metabox( 'post.audio.0.audio_code' ); ?>
						</div>
					
					<?php else : ?>
					
						<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
							<div class="entry-picture fx-hover">
								<div class="fx-hover-back"></div>
								<div class="fx-hover-icon">
									<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
									<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
								</div>
								<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 800, 420, true, true, true ) ); ?>" width="800" height="420" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
							</div>
						<?php endif; ?>
						
					<?php endif; ?>

					<div class="entry-summary">
						<?php the_excerpt(); ?>
					</div>

					<?php
					wp_link_pages( array(
						'before' => '<div class="page-links">' . esc_html__( 'Pages:', PLUGIN_NAME ),
						'after'  => '</div>',
					));
					?>

				</div>
				<!-- end .entry-content -->
			
				<!-- begin .entry-footer -->
				<footer class="entry-footer">
					<?php aio_entry_footer(); ?>
				</footer>
				<!-- end .entry-footer -->
				
				<div class="lines"></div>
			
			</div>
		</article>
		<!-- end #post-## -->
		<?php endwhile; ?>
		
		<!-- begin .pagination -->
		<nav class="pagination">
			<?php
			echo paginate_links( array(
				'base'               => str_replace( PHP_INT_MAX, '%#%', esc_url( get_pagenum_link( PHP_INT_MAX ) ) ),
				'format'             => '?paged=%#%',
				'current'            => max( 1, get_query_var( 'paged' ) ),
				'total'              => $wp_query->max_num_pages,
				'type'               => 'list',
				'prev_text'	=> __( 'Prev', PLUGIN_NAME ),
				'next_text'	=> __( 'Next', PLUGIN_NAME ),
			));
			?>
		</nav>
		<!-- end .pagination -->
		
		
	<?php elseif ( $module == 'masonry' ) : ?>
	
		<!-- begin #post-## -->
		<div class="row">
		<div class="blog-mansonry">
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			<article class="blog-box column-equal <?php echo wp_strip_all_tags( $column ); ?> <?php echo wp_strip_all_tags( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
				<div class="inner-hover">
				<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) :  ?>
					<div class="thumbnail fx-hover">
						<div class="fx-hover-back"></div>
						<div class="fx-hover-icon">
							<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
							<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
						</div>
						<img src="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
					</div>
				<?php endif; ?>
				<h3><a href="<?php echo get_the_permalink(); ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
				<div class="blog-meta">
						<span class="blog-meta-author">By <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php echo get_the_author_meta( 'nickname' ); ?></a>, </span>
					<span class="blog-meta-date">On <?php echo esc_html( get_the_date() ); ?></span>
				</div>
				<p><?php echo aio_excerpt(14); ?></p>
				</div>
			</article>
		<?php endwhile; ?>
		</div>
		</div>
		
		<!-- begin .pagination -->
		<nav class="pagination">
			<?php
			echo paginate_links( array(
				'base'               => str_replace( PHP_INT_MAX, '%#%', esc_url( get_pagenum_link( PHP_INT_MAX ) ) ),
				'format'             => '?paged=%#%',
				'current'            => max( 1, get_query_var( 'paged' ) ),
				'total'              => $wp_query->max_num_pages,
				'type'               => 'list',
				'prev_text'	=> __( 'Prev', PLUGIN_NAME ),
				'next_text'	=> __( 'Next', PLUGIN_NAME ),
			));
			?>
		</nav>
		<!-- end .pagination -->
			
	<?php elseif ( $module == 'box' ) : ?>

		<div class="row row-equal fx">
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			<article class="blog-box column-equal <?php echo wp_strip_all_tags( $column ); ?> <?php echo wp_strip_all_tags( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
				<div class="inner-hover">
				<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) :  ?>
					<div class="thumbnail fx-hover">
						<div class="fx-hover-back"></div>
						<div class="fx-hover-icon">
							<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="gallery" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-search"></i></a>
							<a class="icon-medium icon-main" href="<?php echo get_the_permalink(); ?>"><i class="fa fa-link"></i></a>
						</div>
						<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 600, 340, true, true, true ) ); ?>" width="600" height="340" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
					</div>
				<?php endif; ?>
				<h3><a href="<?php echo get_the_permalink(); ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
				<div class="blog-meta">
						<span class="blog-meta-author">By <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php echo get_the_author_meta( 'nickname' ); ?></a>, </span>
					<span class="blog-meta-date">On <?php echo esc_html( get_the_date() ); ?></span>
				</div>
				<p><?php echo aio_excerpt(14); ?></p>
				</div>
			</article>
		<?php endwhile; ?>
		</div>
		
	<?php elseif ( $module == 'widget' ) : ?>
		
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) { ?>
				<article class="blog-widget">
					<div class="thumbnail">
						<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 400, 400, true, true, true ) ); ?>" width="400" height="400" alt="<?php echo strip_tags( get_the_title() ); ?>" />
					</div>
					<h3><a href="<?php echo get_the_permalink() ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
					<p><?php echo esc_html( get_the_date() ); ?></p>
				</article>
			<?php } else { ?>
				<article class="blog-widget blog-widget-no-thumb">
					<h3><a href="<?php echo get_the_permalink() ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
					<p><?php echo esc_html( get_the_date() ); ?></p>
				</article>
			<?php } ?>
		<?php endwhile; ?>
		
	<?php else : ?>
		
		<div class="row row-equal fx">
			<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			<article class="blog-grid column-equal <?php echo wp_strip_all_tags( $column ); ?> <?php echo wp_strip_all_tags( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
				<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) : ?>
					<div class="thumbnail fx-hover">
						<div class="fx-hover-back"></div>
						<div class="fx-hover-icon">
							<a class="icon-medium icon-main" href="<?php echo esc_url( aio_src( get_the_ID() ) ); ?>" data-lightbox="blog" data-title="<?php echo wp_strip_all_tags( get_the_title() ); ?>"><i class="fa fa-plus"></i></a>
						</div>
						<img src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 600, 340, true, true, true ) ); ?>" width="600" height="340" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
					</div>
				<?php endif; ?>
				<h3><a href="<?php echo get_the_permalink(); ?>"><?php echo wp_strip_all_tags( get_the_title() ); ?></a></h3>
				<p><?php echo aio_excerpt(15); ?></p>
				<a class="button-medium button-main" href="<?php echo get_the_permalink(); ?>"><?php _e( 'Read More', PLUGIN_NAME ); ?></a>
			</article>
			<?php endwhile; ?>
		</div>

	<?php endif; ?>

	<?php
	wp_reset_query();
	$html = ob_get_clean();
	return $html;
}
add_shortcode( 'shortcode_blogs', 'aio_shortcode_blogs' );

/*
 * EOF
 */
