<?php
/**
* Header Functions
*
* @package GridRead WordPress Theme
* @copyright Copyright (C) 2025 ThemesDNA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
* @author ThemesDNA <themesdna@gmail.com>
*/

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function gridread_pingback_header() {
    if ( is_singular() && pings_open() ) {
        echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
    }
}
add_action( 'wp_head', 'gridread_pingback_header' );

// Get custom-logo URL
function gridread_custom_logo() {
    if ( ! has_custom_logo() ) {return;}
    $gridread_custom_logo_id = get_theme_mod( 'custom_logo' );
    $gridread_logo = wp_get_attachment_image_src( $gridread_custom_logo_id , 'full' );
    $gridread_logo_src = $gridread_logo[0];
    return apply_filters( 'gridread_custom_logo', $gridread_logo_src );
}

// Site Title
function gridread_site_title() {
    if ( is_front_page() && is_home() ) { ?>
            <h1 class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_home() ) { ?>
            <h1 class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_singular() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_category() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_tag() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_author() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_archive() && !is_category() && !is_tag() && !is_author() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_search() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } elseif ( is_404() ) { ?>
            <p class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php } else { ?>
            <h1 class="gridread-site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
            <?php if ( !(gridread_get_option('hide_tagline')) ) { ?><p class="gridread-site-description"><?php bloginfo( 'description' ); ?></p><?php } ?>
    <?php }
}

function gridread_header_image_destination() {
    $url = home_url( '/' );
    if ( gridread_get_option('header_image_destination') ) {
        $url = gridread_get_option('header_image_destination');
    }
    return apply_filters( 'gridread_header_image_destination', $url );
}

function gridread_header_image_alt_text() {
    $header_image_alt_text = get_bloginfo( 'name' );

    if ( gridread_get_option('header_image_alt_text') ) {
        $header_image_alt_text = gridread_get_option('header_image_alt_text');
    }

    return apply_filters( 'gridread_header_image_alt_text', $header_image_alt_text );
}

function gridread_header_image_markup() {
    if ( get_header_image() ) {
        if ( gridread_get_option('remove_header_image_link') ) {
            the_header_image_tag( array( 'class' => 'gridread-header-img', 'alt' => esc_attr( gridread_header_image_alt_text() ) ) );
        } else { ?>
            <a href="<?php echo esc_url( gridread_header_image_destination() ); ?>" rel="home" class="gridread-header-img-link"><?php the_header_image_tag( array( 'class' => 'gridread-header-img', 'alt' => esc_attr( gridread_header_image_alt_text() ) ) ); ?></a>
        <?php }
    }
}

function gridread_header_image_details() {
    global $post;

    $header_image_custom_title = '';
    if ( gridread_get_option('header_image_custom_title') ) {
        $header_image_custom_title = gridread_get_option('header_image_custom_title');
    }

    $header_image_custom_description = '';
    if ( gridread_get_option('header_image_custom_description') ) {
        $header_image_custom_description = gridread_get_option('header_image_custom_description');
    }

    if ( !(gridread_get_option('hide_header_image_details')) ) {
        if ( !(gridread_get_option('hide_header_image_title')) || !(gridread_get_option('hide_header_image_description')) ) {
            if ( gridread_get_option('header_image_custom_text') ) {
                if ( $header_image_custom_title || $header_image_custom_description ) { ?>
                    <div class="gridread-header-image-info">
                    <div class="gridread-header-image-info-inside">
                        <?php if ( !(gridread_get_option('hide_header_image_title')) ) { ?><?php if ( $header_image_custom_title ) { ?><p class="gridread-header-image-site-title gridread-header-image-block"><?php echo wp_kses_post( force_balance_tags( do_shortcode($header_image_custom_title) ) ); ?></p><?php } ?><?php } ?>
                        <?php if ( !(gridread_get_option('hide_header_image_description')) ) { ?><?php if ( $header_image_custom_description ) { ?><p class="gridread-header-image-site-description gridread-header-image-block"><?php echo wp_kses_post( force_balance_tags( do_shortcode($header_image_custom_description) ) ); ?></p><?php } ?><?php } ?>
                    </div>
                    </div>
                <?php }
            } else { ?>
                <div class="gridread-header-image-info">
                <div class="gridread-header-image-info-inside">
                    <?php if ( !(gridread_get_option('hide_header_image_title')) ) { ?><p class="gridread-header-image-site-title gridread-header-image-block"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p><?php } ?>
                    <?php if ( !(gridread_get_option('hide_header_image_description')) ) { ?><p class="gridread-header-image-site-description gridread-header-image-block"><?php bloginfo( 'description' ); ?></p><?php } ?>
                </div>
                </div>
            <?php }
        }
    }
}

function gridread_header_image_wrapper() { ?>
    <div class="gridread-header-image gridread-clearfix">
    <?php gridread_header_image_markup(); ?>
    <?php gridread_header_image_details(); ?>
    </div>
    <?php
}

function gridread_header_image() {
    if ( gridread_get_option('hide_header_image') ) { return; }
    if ( get_header_image() ) {
        gridread_header_image_wrapper();
    }
}