<?php
/**
 * Genesis Framework.
 *
 * WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package Genesis\Loops
 * @author  StudioPress
 * @license GPL-2.0+
 * @link    http://my.studiopress.com/themes/genesis/
 */

add_action( 'genesis_loop', 'genesis_do_loop' );
/**
 * Attach a loop to the `genesis_loop` output hook so we can get some front-end output.
 *
 * @since 1.1.0
 *
 * @uses genesis_get_option()       Get theme setting value.
 * @uses genesis_get_custom_field() Get custom field value.
 * @uses genesis_custom_loop()      Do custom loop.
 * @uses genesis_standard_loop()    Do standard loop.
 */
function genesis_do_loop() {

	if ( is_page_template( 'page_blog.php' ) ) {
		$include = genesis_get_option( 'blog_cat' );
		$exclude = genesis_get_option( 'blog_cat_exclude' ) ? explode( ',', str_replace( ' ', '', genesis_get_option( 'blog_cat_exclude' ) ) ) : '';
		$paged   = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

		//* Easter Egg
		$query_args = wp_parse_args(
			genesis_get_custom_field( 'query_args' ),
			array(
				'cat'              => $include,
				'category__not_in' => $exclude,
				'showposts'        => genesis_get_option( 'blog_cat_num' ),
				'paged'            => $paged,
			)
		);

		genesis_custom_loop( $query_args );
	} else {
		genesis_standard_loop();
	}

}

/**
 * Standard loop, meant to be executed without modification in most circumstances where content needs to be displayed.
 *
 * It outputs basic wrapping HTML, but uses hooks to do most of its content output like title, content, post information
 * and comments.
 *
 * The action hooks called are:
 *
 *  - `genesis_before_entry`
 *  - `genesis_entry_header`
 *  - `genesis_before_entry_content`
 *  - `genesis_entry_content`
 *  - `genesis_after_entry_content`
 *  - `genesis_entry_footer`
 *  - `genesis_after_endwhile`
 *  - `genesis_loop_else` (only if no posts were found)
 *
 * @since 1.1.0
 *
 * @uses genesis_html5()       Check for HTML5 support.
 * @uses genesis_legacy_loop() XHTML loop.
 * @uses genesis_attr()        Contextual attributes.
 *
 * @return null Return early after legacy loop if not supporting HTML5.
 */
function genesis_standard_loop() {

	//* Use old loop hook structure if not supporting HTML5
	if ( ! genesis_html5() ) {
		genesis_legacy_loop();
		return;
	}

	if ( have_posts() ) : while ( have_posts() ) : the_post();

			do_action( 'genesis_before_entry' );

			printf( '<article %s>', genesis_attr( 'entry' ) );

				do_action( 'genesis_entry_header' );

				do_action( 'genesis_before_entry_content' );
				printf( '<div %s>', genesis_attr( 'entry-content' ) );
					do_action( 'genesis_entry_content' );
				echo '</div>'; //* end .entry-content
				do_action( 'genesis_after_entry_content' );

				do_action( 'genesis_entry_footer' );

			echo '</article>';

			do_action( 'genesis_after_entry' );

		endwhile; //* end of one post
		do_action( 'genesis_after_endwhile' );

	else : //* if no posts exist
		do_action( 'genesis_loop_else' );
	endif; //* end loop

}

/**
 * XHTML loop.
 *
 * This is called by {@link genesis_standard_loop()} if the child theme does not support HTML5.
 *
 * It is a standard loop, and is meant to be executed, without modification, in most circumstances where content needs
 * to be displayed.
 *
 * It outputs basic wrapping HTML, but uses hooks to do most of its content output like title, content, post information
 * and comments.
 *
 * The action hooks called are:
 *
 *  - `genesis_before_post`
 *  - `genesis_before_post_title`
 *  - `genesis_post_title`
 *  - `genesis_after_post_title`
 *  - `genesis_before_post_content`
 *  - `genesis_post_content`
 *  - `genesis_after_post_content`
 *  - `genesis_after_post`
 *  - `genesis_after_endwhile`
 *  - `genesis_loop_else` (only if no posts were found)
 *
 * @since 2.0.0
 *
 * @global integer $loop_counter Increments on each loop pass.
 */
function genesis_legacy_loop() {

	global $loop_counter;

	$loop_counter = 0;

	if ( have_posts() ) : while ( have_posts() ) : the_post();

		do_action( 'genesis_before_post' );

		printf( '<div class="%s">', join( ' ', get_post_class() ) );

			do_action( 'genesis_before_post_title' );
			do_action( 'genesis_post_title' );
			do_action( 'genesis_after_post_title' );

			do_action( 'genesis_before_post_content' );
			echo '<div class="entry-content">';
				do_action( 'genesis_post_content' );
			echo '</div>'; //* end .entry-content
			do_action( 'genesis_after_post_content' );

		echo '</div>'; //* end .entry

		do_action( 'genesis_after_post' );
		$loop_counter++;

	endwhile; //* end of one post
		do_action( 'genesis_after_endwhile' );

	else : //* if no posts exist
		do_action( 'genesis_loop_else' );
	endif; //* end loop

}

/**
 * Custom loop, meant to be executed when a custom query is needed.
 *
 * It accepts arguments in query_posts style format to modify the custom `WP_Query` object.
 *
 * It outputs basic wrapping HTML, but uses hooks to do most of its content output like title, content, post information,
 * and comments.
 *
 * The arguments can be passed in via the `genesis_custom_loop_args` filter.
 *
 * The action hooks called are the same as {@link genesis_standard_loop()}.
 *
 * @since 1.1.0
 *
 * @uses genesis_standard_loop()
 *
 * @global WP_Query $wp_query Query object.
 * @global integer  $more
 *
 * @param array $args Loop configuration.
 */
function genesis_custom_loop( $args = array() ) {

	global $wp_query, $more;

	$defaults = array(); //* For forward compatibility
	$args     = apply_filters( 'genesis_custom_loop_args', wp_parse_args( $args, $defaults ), $args, $defaults );

	$wp_query = new WP_Query( $args );

	//* Only set $more to 0 if we're on an archive
	$more = is_singular() ? $more : 0;

	genesis_standard_loop();

	//* Restore original query
	wp_reset_query();

}

/**
 * The grid loop - a specific implementation of a custom loop.
 *
 * Outputs markup compatible with a Feature + Grid style layout.
 * All normal loop hooks present, except for `genesis_post_content`.
 *
 * The arguments can be filtered by the `genesis_grid_loop_args` filter.
 *
 * @since 1.5.0
 *
 * @uses genesis_custom_loop()   Do custom loop.
 * @uses genesis_standard_loop() Do standard loop.
 * @uses genesis_reset_loop()    Restores all default post loop output by rehooking all default functions.
 *
 * @global array $_genesis_loop_args Associative array for grid loop configuration.
 *
 * @param array $args Associative array for grid loop configuration.
 */
function genesis_grid_loop( $args = array() ) {

	//* Global vars
	global $_genesis_loop_args;

	//* Parse args
	$args = apply_filters(
		'genesis_grid_loop_args',
		wp_parse_args(
			$args,
			array(
				'features'				=> 2,
				'features_on_all'		=> false,
				'feature_image_size'	=> 0,
				'feature_image_class'	=> 'alignleft',
				'feature_content_limit'	=> 0,
				'grid_image_size'		=> 'thumbnail',
				'grid_image_class'		=> 'alignleft',
				'grid_content_limit'	=> 0,
				'more'					=> __( 'Read more', 'genesis' ) . '&#x02026;',
			)
		)
	);

	//* If user chose more features than posts per page, adjust features
	if ( get_option( 'posts_per_page' ) < $args['features'] ) {
		$args['features'] = get_option( 'posts_per_page' );
	}

	//* What page are we on?
	$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

	//* Potentially remove features on page 2+
	if ( ! $args['features_on_all'] && $paged > 1 )
		$args['features'] = 0;

	//* Set global loop args
	$_genesis_loop_args = $args;

	//* Remove some unnecessary stuff from the grid loop
	remove_action( 'genesis_before_post_title', 'genesis_do_post_format_image' );
	remove_action( 'genesis_post_content', 'genesis_do_post_image' );
	remove_action( 'genesis_post_content', 'genesis_do_post_content' );
	remove_action( 'genesis_post_content', 'genesis_do_post_content_nav' );
	
	remove_action( 'genesis_entry_header', 'genesis_do_post_format_image', 4 );
	remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
	remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
	remove_action( 'genesis_entry_content', 'genesis_do_post_content_nav', 12 );
	remove_action( 'genesis_entry_content', 'genesis_do_post_permalink', 14 );


	//* Custom loop output
	add_filter( 'post_class', 'genesis_grid_loop_post_class' );
	add_action( 'genesis_post_content', 'genesis_grid_loop_content' );
	add_action( 'genesis_entry_content', 'genesis_grid_loop_content' );

	//* The loop
	genesis_standard_loop();

	//* Reset loops
	genesis_reset_loops();
	remove_filter( 'post_class', 'genesis_grid_loop_post_class' );
	remove_action( 'genesis_post_content', 'genesis_grid_loop_content' );
	remove_action( 'genesis_entry_content', 'genesis_grid_loop_content' );

}

/**
 * Filter the post classes to output custom classes for the feature and grid layout.
 *
 * Based on the grid loop args and the loop counter.
 *
 * Applies the `genesis_grid_loop_post_class` filter.
 *
 * @since 1.5.0
 *
 * @global array    $_genesis_loop_args Associative array for grid loop config.
 * @global WP_Query $wp_query           Query object.
 *
 * @param array $classes Existing post classes.
 *
 * @return array Amended post classes.
 */
function genesis_grid_loop_post_class( array $classes ) {

	global $_genesis_loop_args, $wp_query;

	$grid_classes = array();

	if ( $_genesis_loop_args['features'] && $wp_query->current_post < $_genesis_loop_args['features'] ) {
		$grid_classes[] = 'genesis-feature';
		$grid_classes[] = sprintf( 'genesis-feature-%s', $wp_query->current_post + 1 );
		$grid_classes[] = $wp_query->current_post&1 ? 'genesis-feature-even' : 'genesis-feature-odd';
	}
	elseif ( $_genesis_loop_args['features']&1 ) {
		$grid_classes[] = 'genesis-grid';
		$grid_classes[] = sprintf( 'genesis-grid-%s', $wp_query->current_post - $_genesis_loop_args['features'] + 1 );
		$grid_classes[] = $wp_query->current_post&1 ? 'genesis-grid-odd' : 'genesis-grid-even';
	}
	else {
		$grid_classes[] = 'genesis-grid';
		$grid_classes[] = sprintf( 'genesis-grid-%s', $wp_query->current_post - $_genesis_loop_args['features'] + 1 );
		$grid_classes[] = $wp_query->current_post&1 ? 'genesis-grid-even' : 'genesis-grid-odd';
	}

	return array_merge( $classes, apply_filters( 'genesis_grid_loop_post_class', $grid_classes ) );

}

/**
 * Output specially formatted content, based on the grid loop args.
 *
 * @since 1.5.0
 *
 * @uses genesis_get_image()  Return an image pulled from the media gallery.
 * @uses the_content_limit()  Echo the limited content.
 * @uses genesis_parse_attr() Return contextual attributes for an element.
 *
 * @global array $_genesis_loop_args Associative array for grid loop configuration.
 */
function genesis_grid_loop_content() {

	global $_genesis_loop_args;

	if ( in_array( 'genesis-feature', get_post_class() ) ) {

		if ( $_genesis_loop_args['feature_image_size'] ) {

			$image = genesis_get_image( array(
				'size'    => $_genesis_loop_args['feature_image_size'],
				'context' => 'grid-loop-featured',
				'attr'    => genesis_parse_attr( 'entry-image-grid-loop', array( 'class' => $_genesis_loop_args['feature_image_class'] ) ),
			) );

			printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $image );

		}

		if ( $_genesis_loop_args['feature_content_limit'] )
			the_content_limit( (int) $_genesis_loop_args['feature_content_limit'], esc_html( $_genesis_loop_args['more'] ) );
		else
			the_content( esc_html( $_genesis_loop_args['more'] ) );

	}

	else {

		if ( $_genesis_loop_args['grid_image_size'] ) {

			$image = genesis_get_image( array(
				'size'    => $_genesis_loop_args['grid_image_size'],
				'context' => 'grid-loop',
				'attr'    => genesis_parse_attr( 'entry-image-grid-loop', array( 'class' => $_genesis_loop_args['grid_image_class'] ) ),
			) );

			printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $image );

		}

		if ( $_genesis_loop_args['grid_content_limit'] ) {
			the_content_limit( (int) $_genesis_loop_args['grid_content_limit'], esc_html( $_genesis_loop_args['more'] ) );
		} else {
			the_excerpt();
			printf( '<a href="%s" class="more-link">%s</a>', get_permalink(), esc_html( $_genesis_loop_args['more'] ) );
		}

	}

}

add_action( 'genesis_after_post', 'genesis_add_id_to_global_exclude' );
add_action( 'genesis_after_entry', 'genesis_add_id_to_global_exclude' );
/**
 * Modify the global $_genesis_displayed_ids each time a loop iterates.
 *
 * Keep track of what posts have been shown on any given page by adding each ID to a global array, which can be used any
 * time by other loops to prevent posts from being displayed twice on a page.
 *
 * @since 2.0.0
 *
 * @global $_genesis_displayed_ids Array of displayed post IDs.
 */
function genesis_add_id_to_global_exclude() {

	global $_genesis_displayed_ids;

	$_genesis_displayed_ids[] = get_the_ID();

}
