<?php 

if ( ! function_exists( 'gp_ranking' ) ) {

	function gp_ranking( $atts, $content = null ) {	
		
		extract( shortcode_atts( array(
			'widget_title' => '',
			'type' => 'hubs',
			'cats' => '',
			'orderby' => 'site_rating',
			'date_posted' => 'all',
			'date_modified' => 'all',
			'per_page' => '5',
			'offset' => '0',
			'featured_image' => 'enabled',
			'large_image_width' => '120',
			'large_image_height' => '120',
			'small_image_width' => '80',
			'small_image_height' => '80',
			'hard_crop' => true,
			'meta_hub_cats' => '',			
			'meta_hub_fields' => '',			
			'display_site_rating' => '',	
			'display_user_rating' => '',
			'classes' => '',
		), $atts ) );	
		
		global $gp, $post;

		// Detect shortcode
		$GLOBALS['gp_shortcode'] = 'ranking';
		
		// Review loop
		$GLOBALS['gp_review_loop'] = true;
				
		gp_shortcode_options( $atts );
		gp_query_variables();
				
		// Unique Name	
		STATIC $i = 0;
		$i++;
		$name = 'gp_ranking_wrapper_' . $i;
		
		// Type		
		if ( $type == 'both' ) {
			$type_hubs = 'hub-template.php';
			$type_reviews = 'review-template.php';
			$type = array( $type_hubs, $type_reviews );
			$type =	implode( ',', $type );
			$relation = 'OR';
		} elseif ( $type == 'hubs' ) {
			$type = 'hub-template.php';
			$relation = 'AND';
		} else {
			$type = 'review-template.php';
			$relation = 'AND';
		}
								
		$args = array(
			'post_status'     => 'publish',
			'post_type'       => 'page',
			'tax_query'       => array( 'relation' => 'OR', $GLOBALS['gp_hub_cats'] ),
			'orderby' 	      => $GLOBALS['gp_orderby_value'],
			'order' 	      => $GLOBALS['gp_order'],
			'meta_key' 		  => $GLOBALS['gp_meta_key'],
			'meta_query' 	  => array(
				'relation' 	  => $relation,		
				array(
					'key' 	  => '_wp_page_template',
					'value'   => $type,
					'compare' => '=='
				),
			),
			'offset'           => $GLOBALS['gp_offset'],
			'posts_per_page'   => $GLOBALS['gp_per_page'],
			'paged'            => 1,
			'date_query' 	 => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),
		);
		
		ob_start(); $gp_query = new wp_query( $args ); $gp_rank = 0; ?>

		<div id="<?php echo sanitize_html_class( $name ); ?>" class="ranking-wrapper ranking-shortcode <?php echo sanitize_html_class( $classes ); ?>">
				
			<?php if ( $gp_query->have_posts() ) : ?>

				<div class="inner-loop">
			
					<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); $gp_rank++; ?>

						<?php

						gp_get_hub_id( get_the_ID() );
		
						gp_ratings( $GLOBALS['gp_hub_id'] );

						// Ranking class
						if ( $gp_rank == 1 ) {
							$gp_ranking_class = 'top-ranked-item';
						} else {
							$gp_ranking_class = 'other-ranked-item';
						}
					
						?>
					
						<article <?php post_class( 'post-item ' . $GLOBALS['gp_rated_item_class'] . ' ' . $gp_ranking_class ); ?> style="background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ); ?>');">

							<?php if ( $widget_title && $gp_rank == 1 ) { ?>
								<div class="element-title">
									<h3><?php echo esc_attr( $widget_title ); ?></h3>
								</div>
							<?php } ?>

							<div class="ranking-number-overlay"><?php echo absint( $gp_rank ); ?></div>

							<div class="bg-overlay-dark"></div>
			
							<div class="foreground-overlay">
						
								<?php if ( has_post_thumbnail() && $featured_image == 'enabled' ) { ?>

									<div class="post-thumbnail featured-content image-align-left">

										<?php if ( $gp_rank == 1 ) { ?>
										
											<?php $image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $large_image_width, $large_image_height, $hard_crop, false, true ); ?>
											<?php if ( $gp['retina'] == 'gp-retina' ) {
												$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $large_image_width * 2, $large_image_height * 2, $hard_crop, true, true );
											} else {
												$retina = '';
											} ?>
									
										<?php } else { ?>
								
											<?php $image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $small_image_width, $small_image_height, $hard_crop, false, true ); ?>
											<?php if ( $gp['retina'] == 'gp-retina' ) {
												$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $small_image_width * 2, $small_image_height * 2, $hard_crop, true, true );
											} else {
												$retina = '';
											} ?>
									
										<?php } ?>	
						
										<a href="<?php the_permalink(); ?>">
											<img src="<?php echo esc_url( $image[0] ); ?>" data-rel="<?php echo esc_url( $retina ); ?>" width="<?php echo absint( $image[1] ); ?>" height="<?php echo absint( $image[2] ); ?>" alt="<?php if ( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) { echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); } else { the_title_attribute(); } ?>" class="wp-post-image" />
										</a>
							
									</div>
							
								<?php } ?>
								
								<div class="image-align-left">
								
									<?php if ( $GLOBALS['gp_site_rating_enabled'] == true OR $GLOBALS['gp_user_rating_enabled'] == true ) { ?>
										<div class="rating-wrapper">
											<?php if ( $GLOBALS['gp_site_rating_enabled'] == true ) { get_template_part( 'lib/sections/site', 'rating' ); } ?>
											<?php if ( $GLOBALS['gp_user_rating_enabled'] == true ) { get_template_part( 'lib/sections/user', 'rating' ); } ?>
										</div><!-- END RATING WRAPPER -->
									<?php } ?>
						
									<div class="loop-header">
										<h3 class="entry-title loop-title"><a href="<?php the_permalink(); ?>" title="<?php echo gp_prefix_hub_title( get_the_ID() ); ?>"><?php echo gp_prefix_hub_title( get_the_ID() ); ?></a> </h3>
									</div><!-- END ENTRY HEADER -->

									<?php if ( $meta_hub_cats == '1' && ( isset( $gp['hub_header_cats'] ) && ! empty( $gp['hub_header_cats'][0] ) ) ) { ?>

										<div class="hub-cats">

											<?php foreach( $gp['hub_header_cats'] as $gp_hub_cat ) {
												if ( has_term( $gp_hub_cat, 'gp_hubs', $GLOBALS['gp_hub_id'] ) ) {
													$term = get_term( $gp_hub_cat, 'gp_hubs' );
													$t_ID = $term->term_id;
													$term_data = get_option( "taxonomy_$t_ID" );											
													$term_link = get_term_link( $term, 'gp_hubs' );
													if ( ! $term_link OR is_wp_error( $term_link ) ) {
														continue;
													}
													?>
													<a href="<?php echo esc_url( $term_link ); ?>"><span style="background-color: <?php echo esc_attr( $term_data['color'] ); ?>"><?php echo esc_attr( $term->name ); ?></span></a>
												<?php } ?>
											<?php } ?>

										</div><!-- END HUB CATEGORIES -->

									<?php } ?>

									<?php if ( $meta_hub_fields == '1' && ( isset( $gp['hub_header_fields'] ) && ! empty( $gp['hub_header_fields'][0] ) ) ) { ?>

										<div class="entry-meta">
											<?php foreach( $gp['hub_header_fields'] as $gp_hub_field ) {
												$taxonomy = get_taxonomy( $gp_hub_field );
												if ( ! $taxonomy ) {
													continue;
												}
												$name = $taxonomy->labels->singular_name;
												$term_list = get_the_term_list( $GLOBALS['gp_hub_id'], $gp_hub_field, '<span>' . $name . ': ', ', ', '</span>' );		
												if ( ! $term_list OR is_wp_error( $term_list ) ) {
													continue;
												}	
												echo html_entity_decode( $term_list );
											} ?>
										</div><!-- END HUB FIELDS -->

									<?php } ?>
								
								</div>
														
							</div>
									
						</article><!-- END POST -->						
	
					<?php endwhile; ?>
			
				</div>

			<?php else : ?>

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

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

		<?php

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

	}

}

add_shortcode( 'ranking', 'gp_ranking' );
	
?>