<?php

namespace core_next;
?>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="Referrer" content="origin">
    <meta name="applicable-device" content="pc,mobile" />
    <?php
    global $wp_query, $core_next_set, $paged;
    if (!empty($core_next_set['site_icon'])) {
        $extension = pathinfo($core_next_set['site_icon'], PATHINFO_EXTENSION);
        echo '<link rel="icon" href="' . $core_next_set['site_icon'] . '" type="image/' . $extension . '">';
    }
    if (is_single() || is_page()) {
        $title = get_the_title();

    } else if (is_category()) {
        $title = single_cat_title('', false);
    } else if (is_search()) {
        $query_terms = $wp_query->query_vars;
        $title = "搜索：" . $query_terms['s'];
    } else if (is_404()) {
        $title = '找不到资源';
    } else {
        $title = WordPress::getArchiveTitle();
    }
    $site_name = get_bloginfo('name');

    if (is_home()) {
        $sub_title = get_bloginfo('description');
        if ($sub_title == '') {
            $echo_title = "$site_name";
        } else {
            $echo_title = WordPress::handleTitle($site_name, $sub_title);
        }
    } else {
        if ($paged > 0) {
            $title .= " - 第{$paged}页";
        }
        if ($core_next_set['seo_title_type'] === 'site_title') {
            $echo_title = WordPress::handleTitle($site_name, $title);
        } else {
            $echo_title = WordPress::handleTitle($title, $site_name);
        }
        if (is_single() || is_page()) {
            $post_id = get_the_ID();
            $post_meta = Options::getPostMetaBoxOptions($post_id);
            if ($post_meta['custom_seo']) {
                $echo_title = $post_meta['seo_title'];
            }
        }
    }
    echo "<title>$echo_title</title>";
    //keyword
    if (is_home()) {
        $keywords = $core_next_set['seo_keywords'];
        $description = $core_next_set['seo_description'];
        Theme::echoKeywordsAndDescription($keywords, $description);
    }
    if ($core_next_set['open_auto_seo'] === true) {
        $keywords = '';
        $description = '';
        if (is_single() || is_page()) {

            $post_id = get_the_ID();
            $post_meta = Options::getPostMetaBoxOptions($post_id);
            if ($post_meta['custom_seo']) {
                $keywords = $post_meta['seo_keyword'];
                $description = $post_meta['seo_description'];
            } else {
                $post = get_post();
                $description = WordPress::getPostExcerpt($post);
                $keywords = '';
                $tags = get_the_tags();
                if ($tags) {
                    $tag_names = [];
                    foreach ($tags as $tag) {
                        $tag_names[] = $tag->name;
                    }
                    $keywords = implode(', ', $tag_names);
                }
            }
        } elseif (is_category()) {
            $description = category_description();
            $keywords = get_the_title();
        } elseif (is_tag()) {
            $description = tag_description();
            $keywords = single_tag_title('', false);
        } elseif (is_search()) {
            $query_terms = $wp_query->query_vars;
            $description = "搜索：" . $query_terms['s'];
            $keywords = $query_terms['s'];
        } else {
            $title = WordPress::getArchiveTitle();
            $keywords = $title;
            $description = $title;
        }
        if (!is_home()) {
            Theme::echoKeywordsAndDescription($keywords, $description);
        }
    }
    wp_head();
    ?>
</head>