<?php
/**
 * The template for displaying the header
 *
 * Displays all of the head element and everything up until the "site-content" div.
 *
 * @package WordPress
 * @subpackage GiveASAP
 * @since GiveASAP 1.0
 */

if ( ! defined( 'GASAP_URI' ) ) {
    define( 'GASAP_URI', plugin_dir_url( __FILE__ ) );
}

$subscriber        = giveasap_get_subscriber( absint( $_GET['giveasap_unsubscribe_id'] ) );
$giveaway          = $subscriber->get_giveaway();
$prize             = $giveaway->get_prize_settings();
$display           = $giveaway->get_display_settings();
$text              = $giveaway->get_text_settings();
$general           = $giveaway->get_general_settings();
$giveasap_settings = giveasap_get_settings();
$giveasap_front    = new GiveASAP_Front( $post );
$rules_button_text = isset( $giveasap_settings['rules_button'] ) ? $giveasap_settings['rules_button'] : __( 'Show Rules', 'giveasap' );
$ga_schedule       = $giveaway->get_schedule();
if ( empty( $prize['winners'] ) || '' === $prize['winners'] ) {
    $prize['winners'] = 1;
}

$giveasap_ended   = false;
$giveaway_started = false;

if ( $giveaway->has_started() ) {
    $giveaway_started = true;
}

if ( $giveaway->has_ended() ) {
    $giveasap_ended = true;
}

$template_name = $giveaway->get_template_name();
$template_file = $giveaway->get_template_file();

$twitter_image = false;
if ( isset( $display['twitter_image'] ) ) {
    $twitter_image = giveasap_get_image_url( $display['twitter_image'] );
}

$twitterAccount = '@wpgiveaways';
if ( isset( $display['twitter_account'] ) ) {
    $twitterAccount = $display['twitter_account'];
}

$facebookImage = false;
if ( isset( $display['facebook_image'] ) ) {
    $facebookImage = giveasap_get_image_url( $display['facebook_image'] );
}


$timezone       = get_option( 'gmt_offset' );
$timezone_short = giveasap_get_timezone_short( $timezone );
$style_href     = $giveaway->get_style_href();
$use_theme      = isset( $display['page_layout'] ) && '1' === $display['page_layout'] ? true : false;

$giveasap_front->enqueue();

if ( ! $use_theme ) {
    $giveasap_front->dequeue_for_single_page();
    setup_postdata( $post );

    $description    = get_the_excerpt();
    $description    = wp_strip_all_tags( $description );
    $giveaway_title = get_the_title();
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?> class="no-js">
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?php echo wp_title(); ?></title>
        <?php
        if ( function_exists( 'wp_site_icon' ) ) {
            wp_site_icon();
        }
        ?>
        <?php if ( $description ) { ?>
            <meta name="description" content="<?php echo esc_attr( $description ); ?>" />
        <?php } ?>
        <!-- Schema.org markup for Google+ -->
        <meta itemprop="name" content="<?php echo esc_attr( $giveaway_title ); ?>">
        <?php if ( $description ) { ?>
            <meta itemprop="description" content="<?php echo esc_attr( $description ); ?>">
        <?php } ?>

        <!-- Twitter Card data -->
        <meta name="twitter:card" content="summary_large_image">
        <meta name="twitter:site" content="<?php echo esc_attr( $twitterAccount ); ?>">
        <meta name="twitter:title" content="<?php echo esc_attr( $giveaway_title ); ?>">

        <?php if ( $description ) { ?>
            <meta name="twitter:description" content="<?php echo esc_attr( $description ); ?>">
        <?php } ?>

        <meta name="twitter:creator" content="<?php echo esc_attr( $twitterAccount ); ?>">

        <?php if ( $twitter_image ) { ?>

            <!-- Twitter summary card with large image must be at least 280x150px -->
            <meta name="twitter:image:src" content="<?php echo esc_attr( $twitter_image ); ?>">

        <?php } ?>

        <!-- Open Graph data -->
        <meta property="og:title" content="<?php echo esc_attr( $giveaway_title ); ?>" />
        <meta property="og:type" content="article" />
        <meta property="og:url" content="<?php the_permalink(); ?>" />
        <?php if ( $facebookImage ) { ?>
            <meta property="og:image" content="<?php echo esc_attr( $facebookImage ); ?>" />
        <?php } ?>
        <?php if ( $description ) { ?>
            <meta property="og:description" content="<?php echo esc_attr( $description ); ?>" />
        <?php } ?>
        <meta property="og:site_name" content="" />
        <?php

        do_action( 'sg_header' );

        if ( isset( $giveasap_settings['head_scripts'] ) && '' !== $giveasap_settings['head_scripts'] ) {
            echo wp_unslash( $giveasap_settings['head_scripts'] );
        }

        ?>
        <style>
            body, html {
                height:100%;
                max-height: 100%;
            }
        </style>
    </head>

    <body <?php body_class(); ?>>
    <?php
} else {
    $giveasap_front->set_giveaway( $giveaway );
    $giveasap_front->add_wp_header();
    get_header( 'giveaway' );
    setup_postdata( $post );
}
?>

    <div class="giveasap sg-step-1 template1 ">


        <div class="giveasap_box">
            <header class="giveasap_section shown">
                <h1><?php echo $giveaway_title; ?></h1>
            </header>
            <div class="giveasap_alert giveasap_ended" style="text-transform: none"><?php esc_html_e( 'You have been unsubscribed from emails for this Giveaway!', 'giveasap'); ?></div>

            <footer class="giveasap_section">

                <a class="giveasap_button giveasap_button-secondary" style="padding: 0;height: 40px;line-height: 40px;" href="<?php echo esc_url( $giveaway->get_main_page_link() ); ?>"><?php esc_html_e( 'Go to the Giveaway page', 'giveasap' ); ?></a>

            </footer>

        </div>

    </div>
    <script type="text/javascript">
        var sg = <?php echo json_encode( gasap()->get_localized_array() ); ?>
    </script>
<?php


if ( ! $use_theme ) {
    wp_reset_postdata();

    do_action( 'sg_footer' );
    ?>

    </body>
    </html>
    <?php
} else {
    wp_reset_postdata();
    get_footer( 'giveaway' );
}
