<?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 HYGIA
 * @since HYGIA 1.0
 */

$hygia_template = apply_filters( 'hygia_filter_get_template_part', hygia_blog_archive_get_template() );

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

	get_template_part( $hygia_template );

} else {

	hygia_storage_set( 'blog_archive', true );

	get_header();

	if ( have_posts() ) {

		// Query params
		$hygia_stickies   = is_home()
								|| ( in_array( hygia_get_theme_option( 'post_type' ), array( '', 'post' ) )
									&& (int) hygia_get_theme_option( 'parent_cat' ) == 0
									)
										? get_option( 'sticky_posts' )
										: false;
		$hygia_post_type  = hygia_get_theme_option( 'post_type' );
		$hygia_args       = array(
								'blog_style'     => hygia_get_theme_option( 'blog_style' ),
								'post_type'      => $hygia_post_type,
								'taxonomy'       => hygia_get_post_type_taxonomy( $hygia_post_type ),
								'parent_cat'     => hygia_get_theme_option( 'parent_cat' ),
								'posts_per_page' => hygia_get_theme_option( 'posts_per_page' ),
								'sticky'         => hygia_get_theme_option( 'sticky_style', 'inherit' ) == 'columns'
															&& is_array( $hygia_stickies )
															&& count( $hygia_stickies ) > 0
															&& get_query_var( 'paged' ) < 1
								);

		hygia_blog_archive_start();

		do_action( 'hygia_action_blog_archive_start' );

		if ( is_author() ) {
			do_action( 'hygia_action_before_page_author' );
			get_template_part( apply_filters( 'hygia_filter_get_template_part', 'templates/author-page' ) );
			do_action( 'hygia_action_after_page_author' );
		}

		if ( hygia_get_theme_option( 'show_filters', 0 ) ) {
			do_action( 'hygia_action_before_page_filters' );
			hygia_show_filters( $hygia_args );
			do_action( 'hygia_action_after_page_filters' );
		} else {
			do_action( 'hygia_action_before_page_posts' );
			hygia_show_posts( array_merge( $hygia_args, array( 'cat' => $hygia_args['parent_cat'] ) ) );
			do_action( 'hygia_action_after_page_posts' );
		}

		do_action( 'hygia_action_blog_archive_end' );

		hygia_blog_archive_end();

	} else {

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

	get_footer();
}
