<?php
/**
 * Custom Functions
 * @package Digital Media
 * @since 1.0.0
 */

if( !function_exists('digital_media_site_logo') ):

    /**
     * Logo & Description
     */
    /**
     * Displays the site logo, either text or image.
     *
     * @param array $digital_media_args Arguments for displaying the site logo either as an image or text.
     * @param boolean $digital_media_echo Echo or return the HTML.
     *
     * @return string $digital_media_html Compiled HTML based on our arguments.
     */
    function digital_media_site_logo( $digital_media_args = array(), $digital_media_echo = true ){
        $digital_media_logo = get_custom_logo();
        $digital_media_site_title = get_bloginfo('name');
        $digital_media_contents = '';
        $digital_media_classname = '';
        $digital_media_defaults = array(
            'logo' => '%1$s<span class="screen-reader-text">%2$s</span>',
            'logo_class' => 'site-logo site-branding',
            'title' => '<a href="%1$s" class="custom-logo-name">%2$s</a>',
            'title_class' => 'site-title',
            'home_wrap' => '<h1 class="%1$s">%2$s</h1>',
            'single_wrap' => '<div class="%1$s">%2$s</div>',
            'condition' => (is_front_page() || is_home()) && !is_page(),
        );
        $digital_media_args = wp_parse_args($digital_media_args, $digital_media_defaults);
        /**
         * Filters the arguments for `digital_media_site_logo()`.
         *
         * @param array $digital_media_args Parsed arguments.
         * @param array $digital_media_defaults Function's default arguments.
         */
        $digital_media_args = apply_filters('digital_media_site_logo_args', $digital_media_args, $digital_media_defaults);
        
        $digital_media_show_logo  = get_theme_mod('digital_media_display_logo', false);
        $digital_media_show_title = get_theme_mod('digital_media_display_title', true);

        if ( has_custom_logo() && $digital_media_show_logo ) {
            $digital_media_contents .= sprintf($digital_media_args['logo'], $digital_media_logo, esc_html($digital_media_site_title));
            $digital_media_classname = $digital_media_args['logo_class'];
        }

        if ( $digital_media_show_title ) {
            $digital_media_contents .= sprintf($digital_media_args['title'], esc_url(get_home_url(null, '/')), esc_html($digital_media_site_title));
            // If logo isn't shown, fallback to title class for wrapper.
            if ( !$digital_media_show_logo ) {
                $digital_media_classname = $digital_media_args['title_class'];
            }
        }

        // If nothing is shown (logo or title both disabled), exit early
        if ( empty($digital_media_contents) ) {
            return;
        }

        $digital_media_wrap = $digital_media_args['condition'] ? 'home_wrap' : 'single_wrap';
        // $digital_media_wrap = 'home_wrap';
        $digital_media_html = sprintf($digital_media_args[$digital_media_wrap], $digital_media_classname, $digital_media_contents);
        /**
         * Filters the arguments for `digital_media_site_logo()`.
         *
         * @param string $digital_media_html Compiled html based on our arguments.
         * @param array $digital_media_args Parsed arguments.
         * @param string $digital_media_classname Class name based on current view, home or single.
         * @param string $digital_media_contents HTML for site title or logo.
         */
        $digital_media_html = apply_filters('digital_media_site_logo', $digital_media_html, $digital_media_args, $digital_media_classname, $digital_media_contents);
        if (!$digital_media_echo) {
            return $digital_media_html;
        }
        echo $digital_media_html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

    }

endif;

if( !function_exists('digital_media_site_description') ):

    /**
     * Displays the site description.
     *
     * @param boolean $digital_media_echo Echo or return the html.
     *
     * @return string $digital_media_html The HTML to display.
     */
    function digital_media_site_description($digital_media_echo = true){

        if ( get_theme_mod('digital_media_display_header_text', false) == true ) :
        $digital_media_description = get_bloginfo('description');
        if (!$digital_media_description) {
            return;
        }
        $digital_media_wrapper = '<div class="site-description"><span>%s</span></div><!-- .site-description -->';
        $digital_media_html = sprintf($digital_media_wrapper, esc_html($digital_media_description));
        /**
         * Filters the html for the site description.
         *
         * @param string $digital_media_html The HTML to display.
         * @param string $digital_media_description Site description via `bloginfo()`.
         * @param string $digital_media_wrapper The format used in case you want to reuse it in a `sprintf()`.
         * @since 1.0.0
         *
         */
        $digital_media_html = apply_filters('digital_media_site_description', $digital_media_html, $digital_media_description, $digital_media_wrapper);
        if (!$digital_media_echo) {
            return $digital_media_html;
        }
        echo $digital_media_html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        endif;
    }

endif;

if( !function_exists('digital_media_posted_on') ):

    /**
     * Prints HTML with meta information for the current post-date/time.
     */
    function digital_media_posted_on( $digital_media_icon = true, $animation_class = '' ){

        $digital_media_default = digital_media_get_default_theme_options();
        $digital_media_post_date = absint( get_theme_mod( 'digital_media_post_date',$digital_media_default['digital_media_post_date'] ) );

        if( $digital_media_post_date ){

            $digital_media_time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
            if (get_the_time('U') !== get_the_modified_time('U')) {
                $digital_media_time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
            }

            $digital_media_time_string = sprintf($digital_media_time_string,
                esc_attr(get_the_date(DATE_W3C)),
                esc_html(get_the_date()),
                esc_attr(get_the_modified_date(DATE_W3C)),
                esc_html(get_the_modified_date())
            );

            $digital_media_year = get_the_date('Y');
            $digital_media_month = get_the_date('m');
            $digital_media_day = get_the_date('d');
            $digital_media_link = get_day_link($digital_media_year, $digital_media_month, $digital_media_day);

            $digital_media_posted_on = '<a href="' . esc_url($digital_media_link) . '" rel="bookmark">' . $digital_media_time_string . '</a>';

            echo '<div class="entry-meta-item entry-meta-date">';
            echo '<div class="entry-meta-wrapper '.esc_attr( $animation_class ).'">';

            if( $digital_media_icon ){

                echo '<span class="entry-meta-icon calendar-icon"> ';
                digital_media_the_theme_svg('calendar');
                echo '</span>';

            }

            echo '<span class="posted-on">' . $digital_media_posted_on . '</span>'; // WPCS: XSS OK.
            echo '</div>';
            echo '</div>';

        }

    }

endif;

if( !function_exists('digital_media_posted_by') ) :

    /**
     * Prints HTML with meta information for the current author.
     */
    function digital_media_posted_by( $digital_media_icon = true, $animation_class = '' ){   

        $digital_media_default = digital_media_get_default_theme_options();
        $digital_media_post_author = absint( get_theme_mod( 'digital_media_post_author',$digital_media_default['digital_media_post_author'] ) );

        if( $digital_media_post_author ){

            echo '<div class="entry-meta-item entry-meta-author">';
            echo '<div class="entry-meta-wrapper '.esc_attr( $animation_class ).'">';

            if( $digital_media_icon ){
            
                echo '<span class="entry-meta-icon author-icon"> ';
                digital_media_the_theme_svg('user');
                echo '</span>';
                
            }

            $digital_media_byline = '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta('ID') ) ) . '">' . esc_html(get_the_author()) . '</a></span>';
            echo '<span class="byline"> ' . $digital_media_byline . '</span>'; // WPCS: XSS OK.
            echo '</div>';
            echo '</div>';

        }

    }

endif;


if( !function_exists('digital_media_posted_by_avatar') ) :

    /**
     * Prints HTML with meta information for the current author.
     */
    function digital_media_posted_by_avatar( $digital_media_date = false ){

        $digital_media_default = digital_media_get_default_theme_options();
        $digital_media_post_author = absint( get_theme_mod( 'digital_media_post_author',$digital_media_default['digital_media_post_author'] ) );

        if( $digital_media_post_author ){



            echo '<div class="entry-meta-left">';
            echo '<div class="entry-meta-item entry-meta-avatar">';
            echo wp_kses_post( get_avatar( get_the_author_meta( 'ID' ) ) );
            echo '</div>';
            echo '</div>';

            echo '<div class="entry-meta-right">';

            $digital_media_byline = '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta('ID') ) ) . '">' . esc_html(get_the_author()) . '</a></span>';

            echo '<div class="entry-meta-item entry-meta-byline"> ' . $digital_media_byline . '</div>';

            if( $digital_media_date ){
                digital_media_posted_on($digital_media_icon = false);
            }
            echo '</div>';

        }

    }

endif;

if( !function_exists('digital_media_entry_footer') ):

    /**
     * Prints HTML with meta information for the categories, tags and comments.
     */
    function digital_media_entry_footer( $digital_media_cats = true, $digital_media_tags = true, $digital_media_edits = true){   

        $digital_media_default = digital_media_get_default_theme_options();
        $digital_media_post_category = absint( get_theme_mod( 'digital_media_post_category',$digital_media_default['digital_media_post_category'] ) );
        $digital_media_post_tags = absint( get_theme_mod( 'digital_media_post_tags',$digital_media_default['digital_media_post_tags'] ) );

        // Hide category and tag text for pages.
        if ('post' === get_post_type()) {

            if( $digital_media_cats && $digital_media_post_category ){

                /* translators: used between list items, there is a space after the comma */
                $digital_media_categories = get_the_category();
                if ($digital_media_categories) {
                    echo '<div class="entry-meta-item entry-meta-categories">';
                    echo '<div class="entry-meta-wrapper">';
                
                    /* translators: 1: list of categories. */
                    echo '<span class="cat-links">';
                    foreach( $digital_media_categories as $digital_media_category ){

                        $digital_media_cat_name = $digital_media_category->name;
                        $digital_media_cat_slug = $digital_media_category->slug;
                        $digital_media_cat_url = get_category_link( $digital_media_category->term_id );
                        ?>

                        <a class="twp_cat_<?php echo esc_attr( $digital_media_cat_slug ); ?>" href="<?php echo esc_url( $digital_media_cat_url ); ?>" rel="category tag"><?php echo esc_html( $digital_media_cat_name ); ?></a>

                    <?php }
                    echo '</span>'; // WPCS: XSS OK.
                    echo '</div>';
                    echo '</div>';
                }

            }

            if( $digital_media_tags && $digital_media_post_tags ){
                /* translators: used between list items, there is a space after the comma */
                $digital_media_tags_list = get_the_tag_list('', esc_html_x(', ', 'list item separator', 'digital-media'));
                if( $digital_media_tags_list ){

                    echo '<div class="entry-meta-item entry-meta-tags">';
                    echo '<div class="entry-meta-wrapper">';

                    /* translators: 1: list of tags. */
                    echo '<span class="tags-links">';
                    echo wp_kses_post($digital_media_tags_list) . '</span>'; // WPCS: XSS OK.
                    echo '</div>';
                    echo '</div>';

                }

            }

            if( $digital_media_edits ){

                edit_post_link(
                    sprintf(
                        wp_kses(
                        /* translators: %s: Name of current post. Only visible to screen readers */
                            __('Edit <span class="screen-reader-text">%s</span>', 'digital-media'),
                            array(
                                'span' => array(
                                    'class' => array(),
                                ),
                            )
                        ),
                        get_the_title()
                    ),
                    '<span class="edit-link">',
                    '</span>'
                );
            }

        }
    }

endif;

if ( ! function_exists( 'digital_media_post_thumbnail' ) ) :

    /**
     * Displays an optional post thumbnail.
     *
     * Shows background style image with height class on archive/search/front,
     * and a normal inline image on single post/page views.
     */
    function digital_media_post_thumbnail( $digital_media_image_size = 'medium' ) {

        if ( post_password_required() || is_attachment() ) {
            return;
        }

        // Fallback image path
        $digital_media_default_image = get_template_directory_uri() . '/assets/images/default.png';

        // Image size → height class map
        $digital_media_size_class_map = array(
            'full'      => 'data-bg-large',
            'large'     => 'data-bg-big',
            'medium'    => 'data-bg-medium',
            'small'     => 'data-bg-small',
            'xsmall'    => 'data-bg-xsmall',
            'thumbnail' => 'data-bg-thumbnail',
        );

        $digital_media_class = isset( $digital_media_size_class_map[ $digital_media_image_size ] )
            ? $digital_media_size_class_map[ $digital_media_image_size ]
            : 'data-bg-medium';

        if ( is_singular() ) {
            the_post_thumbnail();
        } else {
            // 🔵 On archives → use background image style
            $digital_media_image = has_post_thumbnail()
                ? wp_get_attachment_image_src( get_post_thumbnail_id(), $digital_media_image_size )
                : array( $digital_media_default_image );

            $digital_media_bg_image = isset( $digital_media_image[0] ) ? $digital_media_image[0] : $digital_media_default_image;
            ?>
            <div class="post-thumbnail data-bg <?php echo esc_attr( $digital_media_class ); ?>"
                 data-background="<?php echo esc_url( $digital_media_bg_image ); ?>">
                <a href="<?php the_permalink(); ?>" class="theme-image-responsive" tabindex="0"></a>
            </div>
            <?php
        }
    }

endif;

if( !function_exists('digital_media_is_comment_by_post_author') ):

    /**
     * Comments
     */
    /**
     * Check if the specified comment is written by the author of the post commented on.
     *
     * @param object $digital_media_comment Comment data.
     *
     * @return bool
     */
    function digital_media_is_comment_by_post_author($digital_media_comment = null){

        if (is_object($digital_media_comment) && $digital_media_comment->user_id > 0) {
            $digital_media_user = get_userdata($digital_media_comment->user_id);
            $post = get_post($digital_media_comment->comment_post_ID);
            if (!empty($digital_media_user) && !empty($post)) {
                return $digital_media_comment->user_id === $post->post_author;
            }
        }
        return false;
    }

endif;

if( !function_exists('digital_media_breadcrumb') ) :

    /**
     * Digital Media Breadcrumb
     */
    function digital_media_breadcrumb($digital_media_comment = null){

        echo '<div class="entry-breadcrumb">';
        digital_media_breadcrumb_trail();
        echo '</div>';

    }

endif;