<?php
/**
 * Custom Functions
 * @package Wind Energy
 * @since 1.0.0
 */

if( !function_exists('wind_energy_site_logo') ):

    /**
     * Logo & Description
     */
    /**
     * Displays the site logo, either text or image.
     *
     * @param array $wind_energy_args Arguments for displaying the site logo either as an image or text.
     * @param boolean $wind_energy_echo Echo or return the HTML.
     *
     * @return string $wind_energy_html Compiled HTML based on our arguments.
     */
    function wind_energy_site_logo( $wind_energy_args = array(), $wind_energy_echo = true ){
        $wind_energy_logo = get_custom_logo();
        $wind_energy_site_title = get_bloginfo('name');
        $wind_energy_contents = '';
        $wind_energy_classname = '';
        $wind_energy_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(),
        );
        $wind_energy_args = wp_parse_args($wind_energy_args, $wind_energy_defaults);
        /**
         * Filters the arguments for `wind_energy_site_logo()`.
         *
         * @param array $wind_energy_args Parsed arguments.
         * @param array $wind_energy_defaults Function's default arguments.
         */
        $wind_energy_args = apply_filters('wind_energy_site_logo_args', $wind_energy_args, $wind_energy_defaults);
        
        $wind_energy_show_logo  = get_theme_mod('wind_energy_display_logo', false);
        $wind_energy_show_title = get_theme_mod('wind_energy_display_title', true);

        if ( has_custom_logo() && $wind_energy_show_logo ) {
            $wind_energy_contents .= sprintf($wind_energy_args['logo'], $wind_energy_logo, esc_html($wind_energy_site_title));
            $wind_energy_classname = $wind_energy_args['logo_class'];
        }

        if ( $wind_energy_show_title ) {
            $wind_energy_contents .= sprintf($wind_energy_args['title'], esc_url(get_home_url(null, '/')), esc_html($wind_energy_site_title));
            // If logo isn't shown, fallback to title class for wrapper.
            if ( !$wind_energy_show_logo ) {
                $wind_energy_classname = $wind_energy_args['title_class'];
            }
        }

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

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

    }

endif;

if( !function_exists('wind_energy_site_description') ):

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

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

endif;

if( !function_exists('wind_energy_posted_on') ):

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

        $wind_energy_default = wind_energy_get_default_theme_options();
        $wind_energy_post_date = absint( get_theme_mod( 'wind_energy_post_date',$wind_energy_default['wind_energy_post_date'] ) );

        if( $wind_energy_post_date ){

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

            $wind_energy_time_string = sprintf($wind_energy_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())
            );

            $wind_energy_year = get_the_date('Y');
            $wind_energy_month = get_the_date('m');
            $wind_energy_day = get_the_date('d');
            $wind_energy_link = get_day_link($wind_energy_year, $wind_energy_month, $wind_energy_day);

            $wind_energy_posted_on = '<a href="' . esc_url($wind_energy_link) . '" rel="bookmark">' . $wind_energy_time_string . '</a>';

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

            if( $wind_energy_icon ){

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

            }

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

        }

    }

endif;

if( !function_exists('wind_energy_posted_by') ) :

    /**
     * Prints HTML with meta information for the current author.
     */
    function wind_energy_posted_by( $wind_energy_icon = true, $wind_energy_animation_class = '' ){   

        $wind_energy_default = wind_energy_get_default_theme_options();
        $wind_energy_post_author = absint( get_theme_mod( 'wind_energy_post_author',$wind_energy_default['wind_energy_post_author'] ) );

        if( $wind_energy_post_author ){

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

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

            $wind_energy_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"> ' . $wind_energy_byline . '</span>'; // WPCS: XSS OK.
            echo '</div>';
            echo '</div>';

        }

    }

endif;


if( !function_exists('wind_energy_posted_by_avatar') ) :

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

        $wind_energy_default = wind_energy_get_default_theme_options();
        $wind_energy_post_author = absint( get_theme_mod( 'wind_energy_post_author',$wind_energy_default['wind_energy_post_author'] ) );

        if( $wind_energy_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">';

            $wind_energy_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"> ' . $wind_energy_byline . '</div>';

            if( $wind_energy_date ){
                wind_energy_posted_on($wind_energy_icon = false);
            }
            echo '</div>';

        }

    }

endif;

if( !function_exists('wind_energy_entry_footer') ):

    /**
     * Prints HTML with meta information for the categories, tags and comments.
     */
    function wind_energy_entry_footer( $wind_energy_cats = true, $wind_energy_tags = true, $wind_energy_edits = true){   

        $wind_energy_default = wind_energy_get_default_theme_options();
        $wind_energy_post_category = absint( get_theme_mod( 'wind_energy_post_category',$wind_energy_default['wind_energy_post_category'] ) );
        $wind_energy_post_tags = absint( get_theme_mod( 'wind_energy_post_tags',$wind_energy_default['wind_energy_post_tags'] ) );

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

            if( $wind_energy_cats && $wind_energy_post_category ){

                /* translators: used between list items, there is a space after the comma */
                $wind_energy_categories = get_the_category();
                if ($wind_energy_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( $wind_energy_categories as $wind_energy_category ){

                        $wind_energy_cat_name = $wind_energy_category->name;
                        $wind_energy_cat_slug = $wind_energy_category->slug;
                        $wind_energy_cat_url = get_category_link( $wind_energy_category->term_id );
                        ?>

                        <a class="twp_cat_<?php echo esc_attr( $wind_energy_cat_slug ); ?>" href="<?php echo esc_url( $wind_energy_cat_url ); ?>" rel="category tag"><?php echo esc_html( $wind_energy_cat_name ); ?></a>

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

            }

            if( $wind_energy_tags && $wind_energy_post_tags ){
                /* translators: used between list items, there is a space after the comma */
                $wind_energy_tags_list = get_the_tag_list('', esc_html_x(', ', 'list item separator', 'wind-energy'));
                if( $wind_energy_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($wind_energy_tags_list) . '</span>'; // WPCS: XSS OK.
                    echo '</div>';
                    echo '</div>';

                }

            }

            if( $wind_energy_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>', 'wind-energy'),
                            array(
                                'span' => array(
                                    'class' => array(),
                                ),
                            )
                        ),
                        get_the_title()
                    ),
                    '<span class="edit-link">',
                    '</span>'
                );
            }

        }
    }

endif;

if ( ! function_exists( 'wind_energy_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 wind_energy_post_thumbnail( $wind_energy_image_size = 'full' ) {

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

        // Fallback image path
        $wind_energy_default_image = get_template_directory_uri() . '/assets/images/slider1.png';

        // Image size → height class map
        $wind_energy_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',
        );

        $wind_energy_class = isset( $wind_energy_size_class_map[ $wind_energy_image_size ] )
            ? $wind_energy_size_class_map[ $wind_energy_image_size ]
            : 'data-bg-medium';

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

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

endif;

if( !function_exists('wind_energy_is_comment_by_post_author') ):

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

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

endif;

if( !function_exists('wind_energy_breadcrumb') ) :

    /**
     * Wind Energy Breadcrumb
     */
    function wind_energy_breadcrumb($wind_energy_comment = null){

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

    }

endif;