<?php
/**
* Media 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>
*/

function gridread_media_content_grid() {
    global $post; ?>
    <?php if ( has_post_thumbnail($post->ID) ) { ?>
    <div class="gridread-grid-post-thumbnail gridread-grid-post-block">
        <?php if ( gridread_get_option('remove_thumbnail_home_link') ) { ?>
            <?php the_post_thumbnail('gridread-360w-360h-image', array('class' => 'gridread-grid-post-thumbnail-img', 'title' => the_title_attribute('echo=0'))); ?>
        <?php } else { ?>
            <a href="<?php echo esc_url( get_permalink() ); ?>" class="gridread-grid-post-thumbnail-link" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_post_thumbnail('gridread-360w-360h-image', array('class' => 'gridread-grid-post-thumbnail-img', 'title' => the_title_attribute('echo=0'))); ?></a>
        <?php } ?>

        <?php if ( !(gridread_get_option('hide_thumbnail_home_data')) ) { ?>
        <?php if ( !(gridread_get_option('hide_post_title_home')) || gridread_get_option('show_post_categories_home') || gridread_is_grid_postmeta_active() ) { ?>
        <div class="gridread-grid-thumbnail-overlay gridread-clearfix">
        <div class="gridread-grid-thumbnail-overlay-inside gridread-clearfix">
        <div class="gridread-grid-post-details gridread-grid-post-block">
        <?php if ( gridread_get_option('show_post_categories_home') && has_category() ) { gridread_grid_cats(); } ?>

        <?php if ( !(gridread_get_option('hide_post_title_home')) ) { ?>
            <?php if ( gridread_get_option('remove_post_title_home_link') ) { ?>
                <?php the_title( sprintf( '<h3 class="gridread-grid-post-title">', esc_url( get_permalink() ) ), '</h3>' ); ?>
            <?php } else { ?>
                <?php the_title( sprintf( '<h3 class="gridread-grid-post-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h3>' ); ?>
            <?php } ?>
        <?php } ?>

        <?php gridread_grid_postmeta(); ?>
        </div>
        </div>
        </div>
        <?php } ?>
        <?php } ?>
    </div>
    <?php } else { ?>
    <div class="gridread-grid-post-thumbnail gridread-grid-post-thumbnail-default gridread-grid-post-block">
        <?php if ( gridread_get_option('remove_thumbnail_home_link') ) { ?>
            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/no-image-360-360.jpg' ); ?>" class="gridread-grid-post-thumbnail-img"/>
        <?php } else { ?>
            <a href="<?php echo esc_url( get_permalink() ); ?>" class="gridread-grid-post-thumbnail-link" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>"><img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/no-image-360-360.jpg' ); ?>" class="gridread-grid-post-thumbnail-img"/></a>
        <?php } ?>

        <?php if ( !(gridread_get_option('hide_thumbnail_home_data')) ) { ?>
        <?php if ( !(gridread_get_option('hide_post_title_home')) || gridread_get_option('show_post_categories_home') || gridread_is_grid_postmeta_active() ) { ?>
        <div class="gridread-grid-thumbnail-overlay gridread-clearfix">
        <div class="gridread-grid-thumbnail-overlay-inside gridread-clearfix">
        <div class="gridread-grid-post-details gridread-grid-post-block">
        <?php if ( gridread_get_option('show_post_categories_home') && has_category() ) { gridread_grid_cats(); } ?>

        <?php if ( !(gridread_get_option('hide_post_title_home')) ) { ?>
            <?php if ( gridread_get_option('remove_post_title_home_link') ) { ?>
                <?php the_title( sprintf( '<h3 class="gridread-grid-post-title">', esc_url( get_permalink() ) ), '</h3>' ); ?>
            <?php } else { ?>
                <?php the_title( sprintf( '<h3 class="gridread-grid-post-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h3>' ); ?>
            <?php } ?>
        <?php } ?>

        <?php gridread_grid_postmeta(); ?>
        </div>
        </div>
        </div>
        <?php } ?>
        <?php } ?>
    </div>
    <?php } ?>
<?php }


function gridread_media_content_single() {
    global $post;
    if ( has_post_thumbnail($post->ID) ) {
        if ( !(gridread_get_option('hide_thumbnail_post')) ) {
            if ( gridread_get_option('remove_thumbnail_post_link') ) { ?>
                <div class="gridread-post-thumbnail-single">
                <?php
                if ( is_page_template( array( 'template-full-width-post.php' ) ) ) {
                    the_post_thumbnail('gridread-1422w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0')));
                } else {
                    the_post_thumbnail('gridread-1074w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0')));
                }
                ?>
                </div>
            <?php } else { ?>
                <div class="gridread-post-thumbnail-single">
                <?php if ( is_page_template( array( 'template-full-width-post.php' ) ) ) { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="gridread-post-thumbnail-single-link"><?php the_post_thumbnail('gridread-1422w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } else { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="gridread-post-thumbnail-single-link"><?php the_post_thumbnail('gridread-1074w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } ?>
                </div>
    <?php   }
        }
    }
}


function gridread_media_content_single_location() {
    global $post;
    if( gridread_get_option('featured_media_under_post_title') ) {
        add_action('gridread_after_single_post_title', 'gridread_media_content_single', 10 );
    } else {
        add_action('gridread_before_single_post_title', 'gridread_media_content_single', 10 );
    }
}
add_action('template_redirect', 'gridread_media_content_single_location', 100 );


function gridread_media_content_page() {
    global $post; ?>
    <?php
    if ( has_post_thumbnail($post->ID) ) {
        if ( !(gridread_get_option('hide_thumbnail_page')) ) {
            if ( gridread_get_option('remove_thumbnail_page_link') ) { ?>
                <div class="gridread-post-thumbnail-single">
                <?php
                if ( is_page_template( array( 'template-full-width-page.php' ) ) ) {
                    the_post_thumbnail('gridread-1422w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0')));
                } else {
                    the_post_thumbnail('gridread-1074w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0')));
                }
                ?>
                </div>
            <?php } else { ?>
                <div class="gridread-post-thumbnail-single">
                <?php if ( is_page_template( array( 'template-full-width-page.php' ) ) ) { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="gridread-post-thumbnail-single-link"><?php the_post_thumbnail('gridread-1422w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } else { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="gridread-post-thumbnail-single-link"><?php the_post_thumbnail('gridread-1074w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } ?>
                </div>
    <?php   }
        }
    }
    ?>
<?php }


function gridread_media_content_page_location() {
    global $post;
    if( gridread_get_option('featured_media_under_page_title') ) {
        add_action('gridread_after_single_page_title', 'gridread_media_content_page', 10 );
    } else {
        add_action('gridread_before_single_page_title', 'gridread_media_content_page', 10 );
    }
}
add_action('template_redirect', 'gridread_media_content_page_location', 110 );


function gridread_media_content_nongrid() {
    global $post;
    if ( has_post_thumbnail($post->ID) ) {
        if ( !(gridread_get_option('hide_thumbnail_home')) ) {
            if ( gridread_get_option('remove_thumbnail_home_link') ) { ?>
                <div class="gridread-post-thumbnail-single">
                <?php
                if ( is_page_template( array( 'template-full-width-post.php' ) ) ) {
                    the_post_thumbnail('gridread-1422w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0')));
                } else {
                    the_post_thumbnail('gridread-1074w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0')));
                }
                ?>
                </div>
            <?php } else { ?>
                <div class="gridread-post-thumbnail-single">
                <?php if ( is_page_template( array( 'template-full-width-post.php' ) ) ) { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="gridread-post-thumbnail-single-link"><?php the_post_thumbnail('gridread-1422w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } else { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="gridread-post-thumbnail-single-link"><?php the_post_thumbnail('gridread-1074w-autoh-image', array('class' => 'gridread-post-thumbnail-single-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } ?>
                </div>
    <?php   }
        }
    }
}


function gridread_media_content_nongrid_location() {
    if( gridread_get_option('featured_nongrid_media_under_post_title') ) {
        add_action('gridread_after_nongrid_post_title', 'gridread_media_content_nongrid', 10 );
    } else {
        add_action('gridread_before_nongrid_post_title', 'gridread_media_content_nongrid', 10 );
    }
}
add_action('template_redirect', 'gridread_media_content_nongrid_location', 120 );