<?php 

if ( ! function_exists( 'gp_blog' ) ) {

	function gp_blog( $atts, $content = null ) {	
		
		extract( shortcode_atts( array(
			'widget_title' => '',
			'cats' => '', 
			'post_types' => 'post',
			'format' => 'blog-standard',
			'size' => 'blog-standard-size',
			'orderby' => 'newest',
			'date_posted' => 'all',
			'date_modified' => 'all',
			'filter' => 'disabled',
			'filter_cats' => '',
			'filter_date' => '',
			'filter_title' => '',					
			'filter_comment_count' => '',
			'filter_views' => '',
			'filter_followers' => '',
			'filter_site_rating' => '',
			'filter_user_rating' => '',
			'filter_hub_awards' => '',
			'filter_date_posted' => '',
			'filter_date_modified' => '',
			'filter_cats_id' => '',
			'per_page' => '12',
			'offset' => '0',
			'featured_image' => 'enabled',
			'image_width' => '140',
			'image_height' => '140',
			'hard_crop' => true,
			'image_alignment' => 'image-wrap-left',
			'title_position' => 'title-next-to-thumbnail',
			'content_display' => 'excerpt',
			'excerpt_length' => '160',
			'meta_author' => '',
			'meta_date' => '',
			'meta_comment_count' => '',
			'meta_views' => '',
			'meta_followers' => '',
			'meta_cats' => '',
			'meta_tags' => '',			
			'meta_hub_cats' => '',			
			'meta_hub_fields' => '',
			'meta_hub_award' => '',			
			'display_site_rating' => '',	
			'display_user_rating' => '',
			'read_more_link' => 'enabled',
			'page_numbers' => 'disabled',
			'see_all' => 'disabled',
			'see_all_link' => '',
			'see_all_text' => __( 'See All Items', 'gp_lang' ),
			'classes' => '',
		), $atts ) );
							
		global $gp;
		
		// Detect shortcode
		$GLOBALS['gp_shortcode'] = 'blog';

		gp_shortcode_options( $atts );
		gp_query_variables();
				
		// Unique Name	
		STATIC $i = 0;
		$i++;
		$name = 'gp_blog_wrapper_' . $i;
			
		$args = array(
			'post_status'    => 'publish',
			'post_type'      => explode( ',', $GLOBALS['gp_post_types'] ),
			'tax_query' 	 => array( 'relation' => 'OR', $GLOBALS['gp_hub_cats'], $GLOBALS['gp_video_cats'], $GLOBALS['gp_post_cats'], $GLOBALS['gp_portfolio_cats'], $GLOBALS['gp_slide_cats'] ),
			'orderby' 		 => $GLOBALS['gp_orderby_value'],
			'order' 		 => $GLOBALS['gp_order'],	
			'meta_key' 		 => $GLOBALS['gp_meta_key'],
			'posts_per_page' => $GLOBALS['gp_per_page'],
			'offset' 		 => $GLOBALS['gp_offset'],	
			'paged'          => $GLOBALS['gp_paged'],   
			'date_query' 	 => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),
		);

		ob_start(); $gp_query = new wp_query( $args ); ?>		

		<div id="<?php echo sanitize_html_class( $name ); ?>" class="blog-wrapper blog-shortcode <?php echo sanitize_html_class( $GLOBALS['gp_format'] ); ?> <?php echo sanitize_html_class( $size ); ?> <?php echo sanitize_html_class( $classes ); ?>"<?php echo gp_data_properties( 'blog' ); ?>>

			<?php if ( $widget_title OR $see_all == 'enabled' ) { ?>
				<div class="element-title">
					<?php if ( $widget_title ) { ?><h3><?php echo esc_attr( $widget_title ); ?></h3><?php } ?>
					<?php if ( $see_all == 'enabled' ) { ?>
						<div class="see-all-link"><a href="<?php echo esc_url( $see_all_link ); ?>"><?php echo esc_attr( $see_all_text ); ?><i></i></a></div>
					<?php } ?>
					<div class="element-title-line"></div>
				</div>
			<?php } ?>
			
			<?php get_template_part( 'lib/sections/filter' ); ?>
			
			<?php if ( $gp_query->have_posts() ) : ?>
				
				<div class="inner-loop <?php echo sanitize_html_class( $gp['ajax'] ); ?>">
		
					<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); ?>

						<?php if ( get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'review-template.php' OR get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'hub-template.php' ) { 
							get_template_part( 'review', 'loop' ); 
						} else {
							get_template_part( 'post', 'loop' ); 
						} ?>

					<?php endwhile; ?>
		
				</div>

				<?php if ( $page_numbers == 'enabled' ) { ?>
					<?php echo gp_pagination( $gp_query->max_num_pages ); ?>
				<?php } ?>

				<?php if ( $GLOBALS['gp_format'] == 'blog-masonry' && ( ! is_admin() OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) ) { ?>
		
					<script>
					jQuery( document ).ready( function( $ ) {

						'use strict';

						if ( $( '.blog-wrapper' ).hasClass( 'blog-masonry' ) ) {
	
							var container = $( '.blog-masonry .inner-loop' );
							var element = container;

							if ( container.find( 'img' ).length == 0 ) {
								element = $( '<img />' );
							}	

							imagesLoaded( element, function( instance ) {

								container.isotope({
									itemSelector: '.post-item',
									masonry: {
										columnWidth: container.find( '.post-item' )[0],
										gutter: 20
									}
								});

								container.animate( { 'opacity': 1 }, 1300 );
								$( 'ul.page-numbers' ).animate( { 'opacity': 1 }, 1300 );

							});
				
						}
	
					});	
					</script>	
		
				<?php } ?>
		
			<?php else : ?>

				<strong class="no-items-found"><?php _e( 'No items found.', 'gp_lang' ); ?></strong>

			<?php endif; wp_reset_postdata(); ?>
							
		</div><!-- END BLOG WRAPPER -->
					
		<?php

		$output_string = ob_get_contents();
		ob_end_clean(); 
		$GLOBALS['gp_shortcode'] = null;
		return $output_string;

	}

}

add_shortcode( 'blog', 'gp_blog' );
	
?>