<?php
/**
 * Template Name: Archive
 * The template for displaying archive pages
 *
 * Used to display archive-type pages if nothing more specific matches a query.
 * For example, puts together date-based pages if no date.php file exists.
 *
 * If you'd like to further customize these archive views, you may create a
 * new template file for each one. For example, tag.php (Tag archives),
 * category.php (Category archives), author.php (Author archives), etc.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage sunny-and-blue
 */
get_header();

echo '<article class="type-page">';
if (is_archive()) {
    if (have_posts()) {


//init
        $count = $theCatID = '';

// set to empty
        $count = $theCatID = '';
        if (is_category()) {
            $theCatID = get_term_by('slug', $wp_query->query_vars['category_name'], 'category');
            if (!empty($theCatID))
                $theCatID = $theCatID->term_id;
            if (isset($wp_query->query_vars['cat'])) $theCatID = (int)$wp_query->query_vars['cat'];
        }

        $count = $wp_query->found_posts;
        if (empty($count)) $count = 'No';

//Setup page title
        $title_string = '';

        if ($count > 0) {
            if (is_category()) { /* Category */
                $title_string = __('Category Archives:', 'sunny-and-blue') . ' ' . single_cat_title('', false);
            } elseif (is_tag()) { /* Tag */
                $title_string = __('Tagged Posts:', 'sunny-and-blue') . ' ' . single_tag_title('', false);
            } elseif (is_day()) {
                $title_string = __('Daily Archives:', 'sunny-and-blue') . ' ' . get_the_time('F jS, Y');
            } elseif (is_month()) {
                $title_string = __('Monthly Archives:', 'sunny-and-blue') . ' ' . get_the_time('F, Y');
            } elseif (is_year()) {
                $title_string = __('Yearly Archives:', 'sunny-and-blue') . ' ' . get_the_time('Y');
            } elseif (is_author()) {
                $title_string = __('Author Archives:', 'sunny-and-blue') . ' ' . '<a class="url fn n" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" title="' . esc_attr(get_the_author()) . '" rel="me">' . get_the_author() . '</a>';
                /* Since we called the_post() rewind the loop back to the beginning */
                rewind_posts();
            } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {
                $title_string = __('Archives', 'sunny-and-blue');
            } elseif (isset($wp_query->query_vars['taxonomy']) && taxonomy_exists($wp_query->query_vars['taxonomy'])) {
                $taxonomy_name = '';
                if (isset($wp_query->query_vars['chapters'])) {
                    $taxonomy_name = get_term_by('slug', $wp_query->query_vars['chapters'], 'chapters');
                    if (!is_wp_error($taxonomy_name) && !empty($taxonomy_name)) $title_string = $taxonomy_name->name;
                } elseif (isset($wp_query->query_vars['locations'])) {
                    $taxonomy_name = get_term_by('slug', $wp_query->query_vars['locations'], 'locations');
                    if (!is_wp_error($taxonomy_name) && !empty($taxonomy_name)) $title_string = $taxonomy_name->name;
                } elseif (term_exists($wp_query->query_vars['term'])) {
                    $title_string = __('Archive for', 'sunny-and-blue') . ' ' . $wp_query->query_vars['term'];
                } else {
                    $title_string = __('Archive for', 'sunny-and-blue') . ' ' . $wp_query->query_vars['taxonomy'];
                }
            } elseif ($wp_query->query_vars['post_type'] !== 'post') {
                $title_string = __('Archive for', 'sunny-and-blue') . ' ' . $wp_query->query_vars['post_type'];
            } else {
                $title_string = __('No Archive Found.', 'sunny-and-blue');
            }
        } else $title_string = __('No Archive Found.', 'sunny-and-blue');
        //End setup page title
        ?>


        <!-- Page Title -->
        <h1><?php echo $title_string; ?></h1>
        <!--Archives Begin-->
        <!--Page Description-->
        <?php if (isset($wp_query->query_vars['chapters']) || ($wp_query->query_vars['post_type'] == 'comic')) { ?>
            <h6 class="article-meta-extra"><?php printf(_n("%d comic.", "%d comics.", $count, 'sunny-and-blue'), $count); ?></h6>
        <?php } else { ?>
            <h6 class="article-meta-extra"><?php printf(_n("%d result.", "%d results.", $count, 'sunny-and-blue'), $count); ?></h6>
        <?php } ?>
        <!-- Start Displaying archives -->
        <?php

        while (have_posts()) : the_post();
            echo '<h2><a href="';
            echo the_permalink();
            echo '">';
            echo the_title();
            echo '</a>';
            echo "</h2>";
            echo '<h6 class="article-meta-extra">';
            echo "By ";
            the_author_posts_link();
            echo " on ";
            the_date(get_option('date_format'));
            echo " at ";
            the_time(get_option('time_format'));
            echo '</h6>';

            //If it's a comic
            if ($post->post_type == 'comic') {
                echo do_action('comic-post-info');
                get_the_post_thumbnail();
                if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
                    echo '<a href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr(get_post_field('post_title', get_the_ID())) . '">';
                    the_post_thumbnail('medium');
                    echo '</a><br/>';
                }
                //Display sharing only for comic, regular blog content has sharing already built in.
                if (function_exists('sharing_display')) {
                    $switched_status = get_post_meta($post->ID, 'sharing_disabled', false);
                    if (empty($switched_status)) echo sharing_display();
                }
            } // else regular blog post
            else {
                the_excerpt();
            }

            the_tags('Tags: ', ', ', '<br />');
        endwhile;

        ?>

        <!--Display pagination-->
        <?php the_posts_pagination(); ?>
        <!--Archives End-->
    <?php } else { ?>
        <h2 class="page-title"><?php echo $title_string; ?></h2>
        <?php printf(_n("%d result.", "%d results.", $count, 'sunny-and-blue'), $count); ?>
    <?php } ?>
    <!-- End if have posts -->
<?php } else { ?>
    <!-- If no type of archive page is being displayed, show regular archive from Comic Easel plugin -->
    <h1>Archive</h1>
    <?php echo do_shortcode("[comic-archive list=0 thumbnail=1]"); ?>
<?php } ?>
    <!--End if is archive -->
    </article>

<?php get_footer(); ?>