<?php
/* ==========================================================
Blog Post
=========================================================== */
if ( !function_exists('confin_blogs_function')) {
  function confin_blogs_function( $atts, $content = NULL ) {
  	extract( $atts );
		$uniqid = uniqid( '-', false);
		$inline_style = '';

		if ( $title_color || $title_size ) {
			$inline_style .= '.recent-blog-section'.$uniqid.'.recent-blog-section .blog-grids .details h3 a {';
			$inline_style .= ( $title_color ) ? 'color: '.$title_color.';' : '';
      $inline_style .= ( $title_size ) ? 'font-size:'.confin_core_check_px($title_size) .';' : '';
			$inline_style .= '} ';
		}
		if ( $date_color ) {
			$inline_style .= '.recent-blog-section'.$uniqid.'.recent-blog-section .blog-grids .date p {';
			$inline_style .= ( $date_color ) ? 'color: '.$date_color.';' : '';
			$inline_style .= '} ';
		}
		if ( $date_bg  ) {
			$inline_style .= '.recent-blog-section'.$uniqid.'.recent-blog-section .blog-grids .date {';
			$inline_style .= ( $date_bg ) ? 'background-color: '.$date_bg.';' : '';
			$inline_style .= '} ';
		}
		if ( $meta_color  ) {
			$inline_style .= '.recent-blog-section'.$uniqid.'.recent-blog-section .meta ul li {';
			$inline_style .= ( $date_color ) ? 'color: '.$date_color.';' : '';
			$inline_style .= '} ';
		}
		add_inline_style( $inline_style );
		$styled_class = ' recent-blog-section'.$uniqid;

  	$blog_order = $blog_order ? $blog_order : 'DESC';
  	$blog_orderby = $blog_orderby ? $blog_orderby : 'none';

		// Columns

  	$aparticular_item = explode(',', $particular_item);
    $perticular_items = array();
    foreach ( $aparticular_item as $item ) {
      $perticular_items[] = substr($item, 0, strpos($item, ":"));;
    }
    $perticular_items = ($particular_item) ? $perticular_items : '';
	  	global $paged;
	    if( get_query_var( 'paged' ) )
	      $my_page = get_query_var( 'paged' );
	    else {
	      if( get_query_var( 'page' ) )
	        $my_page = get_query_var( 'page' );
	      else
	        $my_page = 1;
	      set_query_var( 'paged', $my_page );
	      $paged = $my_page;
	    }
    $args = array(
    	'paged' => $paged,
      'post_type' => 'post',
      'posts_per_page' => (int)$blog_limit,
      'orderby' => $blog_orderby,
      'order' => $blog_order,
      'cat' => $perticular_items,
			'ignore_sticky_posts' => true,
    );

    $query_blog = new WP_Query( $args );
    ob_start();
    $post_count = 0;
    if ( $query_blog->have_posts() ) : ?>
			<div class="latest-blog-section <?php echo esc_attr( $class . $styled_class ); ?>">
    		<div class="news-grids clearfix">
					<?php
					while ( $query_blog->have_posts() ) :
						$query_blog-> the_post();
						$confin_large_image =  wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
						$post_count++;
						$post_options = get_post_meta( get_the_ID(), 'post_type_metabox', true );
						$grid_image = isset( $post_options['grid_image'] ) ? $post_options['grid_image'] : '';
						$image_url = $grid_image ? wp_get_attachment_url( $grid_image ) : $confin_large_image[0];
            			$image_alt = get_post_meta( $grid_image , '_wp_attachment_image_alt', true); ?>
				   		<div class="grid">
				   			<?php if($image_url){ ?>
						    <div class="entry-media">
						        <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>">
						    </div>
							<?php } ?>
						    <div class="details">
						        <ul class="entry-meta">
						        	 <?php if ( $blog_date ) { ?>
						            <li><i class="ti-time"></i><?php echo get_the_date(); ?></li>
												<?php } ?>
												<li><i class="ti-comment-alt"></i>
											  <?php
													comments_popup_link( esc_html__( '0 Comments', 'confin-core' ), esc_html__( '1 Comment', 'confin-core' ), esc_html__( '% Comments', 'confin-core' ), '', esc_html__( 'Comments Off', 'confin-core' ) );
									      ?>
						            </li>
						        </ul>
						      <h5><a href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a></h5>
						    </div>
						</div>
				<?php
					endwhile; wp_reset_postdata(); ?>
				</div>
				<?php
				if( $pagination ){
					echo '<nav class="pagination_area"><div class="pagination">';
							$big = 999999999;
							echo paginate_links( array(
                'base'      => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                'format'    => '?paged=%#%',
                'total'     => $query_blog->max_num_pages,
                'show_all'  => false,
                'current'   => max( 1, $my_page ),
								'prev_text'    => '<div class="fa fa-angle-left"></div>',
								'next_text'    => '<div class="fa fa-angle-right"></div>',
                'mid_size'  => 1,
                'type'      => 'list'
              ) );
	        echo '</div></div>';
				}
				?>
			</div>
			<!--End blog section-->
    <?php
  	endif;
    return ob_get_clean();
  }
}
add_shortcode( 'blog_section', 'confin_blogs_function' );