<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: //codex.wordpress.org/Template_Hierarchy
 *
 * @package PANTRY
 * @since PANTRY 1.0
 */

$pantry_template = apply_filters( 'pantry_filter_get_template_part', pantry_blog_archive_get_template() );

if ( ! empty( $pantry_template ) && 'index' != $pantry_template ) {

	get_template_part( $pantry_template );

} else {

	pantry_storage_set( 'blog_archive', true );

	get_header();

	if ( have_posts() ) {

		// Query params
		$pantry_stickies   = is_home()
								|| ( in_array( pantry_get_theme_option( 'post_type' ), array( '', 'post' ) )
									&& (int) pantry_get_theme_option( 'parent_cat' ) == 0
									)
										? get_option( 'sticky_posts' )
										: false;
		$pantry_post_type  = pantry_get_theme_option( 'post_type' );
		$pantry_args       = array(
								'blog_style'     => pantry_get_theme_option( 'blog_style' ),
								'post_type'      => $pantry_post_type,
								'taxonomy'       => pantry_get_post_type_taxonomy( $pantry_post_type ),
								'parent_cat'     => pantry_get_theme_option( 'parent_cat' ),
								'posts_per_page' => pantry_get_theme_option( 'posts_per_page' ),
								'sticky'         => pantry_get_theme_option( 'sticky_style' ) == 'columns'
															&& is_array( $pantry_stickies )
															&& count( $pantry_stickies ) > 0
															&& get_query_var( 'paged' ) < 1
								);

		pantry_blog_archive_start();

		do_action( 'pantry_action_blog_archive_start' );

		if ( is_author() ) {
			do_action( 'pantry_action_before_page_author' );
			get_template_part( apply_filters( 'pantry_filter_get_template_part', 'templates/author-page' ) );
			do_action( 'pantry_action_after_page_author' );
		}

		if ( pantry_get_theme_option( 'show_filters' ) ) {
			do_action( 'pantry_action_before_page_filters' );
			pantry_show_filters( $pantry_args );
			do_action( 'pantry_action_after_page_filters' );
		} else {
			do_action( 'pantry_action_before_page_posts' );
			pantry_show_posts( array_merge( $pantry_args, array( 'cat' => $pantry_args['parent_cat'] ) ) );
			do_action( 'pantry_action_after_page_posts' );
		}

		do_action( 'pantry_action_blog_archive_end' );

		pantry_blog_archive_end();

	} else {

		if ( is_search() ) {
			get_template_part( apply_filters( 'pantry_filter_get_template_part', 'templates/content', 'none-search' ), 'none-search' );
		} else {
			get_template_part( apply_filters( 'pantry_filter_get_template_part', 'templates/content', 'none-archive' ), 'none-archive' );
		}
	}

	get_footer();
}
