<?php
/**
 * Custom Functions
 * @package Car Rental Service
 * @since 1.0.0
 */

if( !function_exists('car_rental_service_site_logo') ):

    /**
     * Logo & Description
     */
    /**
     * Displays the site logo, either text or image.
     *
     * @param array $car_rental_service_args Arguments for displaying the site logo either as an image or text.
     * @param boolean $car_rental_service_echo Echo or return the HTML.
     *
     * @return string $car_rental_service_html Compiled HTML based on our arguments.
     */
    function car_rental_service_site_logo( $car_rental_service_args = array(), $car_rental_service_echo = true ){
        $car_rental_service_logo = get_custom_logo();
        $car_rental_service_site_title = get_bloginfo('name');
        $car_rental_service_contents = '';
        $car_rental_service_classname = '';
        $car_rental_service_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(),
        );
        $car_rental_service_args = wp_parse_args($car_rental_service_args, $car_rental_service_defaults);
        /**
         * Filters the arguments for `car_rental_service_site_logo()`.
         *
         * @param array $car_rental_service_args Parsed arguments.
         * @param array $car_rental_service_defaults Function's default arguments.
         */
        $car_rental_service_args = apply_filters('car_rental_service_site_logo_args', $car_rental_service_args, $car_rental_service_defaults);
        
        $car_rental_service_show_logo  = get_theme_mod('car_rental_service_display_logo', false);
        $car_rental_service_show_title = get_theme_mod('car_rental_service_display_title', true);

        if ( has_custom_logo() && $car_rental_service_show_logo ) {
            $car_rental_service_contents .= sprintf($car_rental_service_args['logo'], $car_rental_service_logo, esc_html($car_rental_service_site_title));
            $car_rental_service_classname = $car_rental_service_args['logo_class'];
        }

        if ( $car_rental_service_show_title ) {
            $car_rental_service_contents .= sprintf($car_rental_service_args['title'], esc_url(get_home_url(null, '/')), esc_html($car_rental_service_site_title));
            // If logo isn't shown, fallback to title class for wrapper.
            if ( !$car_rental_service_show_logo ) {
                $car_rental_service_classname = $car_rental_service_args['title_class'];
            }
        }

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

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

    }

endif;

if( !function_exists('car_rental_service_site_description') ):

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

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

endif;

if( !function_exists('car_rental_service_posted_on') ):

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

        $car_rental_service_default = car_rental_service_get_default_theme_options();
        $car_rental_service_post_date = absint( get_theme_mod( 'car_rental_service_post_date',$car_rental_service_default['car_rental_service_post_date'] ) );

        if( $car_rental_service_post_date ){

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

            $car_rental_service_time_string = sprintf($car_rental_service_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())
            );

            $car_rental_service_year = get_the_date('Y');
            $car_rental_service_month = get_the_date('m');
            $car_rental_service_day = get_the_date('d');
            $car_rental_service_link = get_day_link($car_rental_service_year, $car_rental_service_month, $car_rental_service_day);

            $car_rental_service_posted_on = '<a href="' . esc_url($car_rental_service_link) . '" rel="bookmark">' . $car_rental_service_time_string . '</a>';

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

            if( $car_rental_service_icon ){

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

            }

            echo '<span class="posted-on">' . $car_rental_service_posted_on . '</span>'; // phpcs:ignore Standard.Category.SniffName.ErrorCode
            echo '</div>';
            echo '</div>';

        }

    }

endif;

if( !function_exists('car_rental_service_posted_by') ) :

    /**
     * Prints HTML with meta information for the current author.
     */
    function car_rental_service_posted_by( $car_rental_service_icon = true, $car_rental_service_animation_class = '' ){   

        $car_rental_service_default = car_rental_service_get_default_theme_options();
        $car_rental_service_post_author = absint( get_theme_mod( 'car_rental_service_post_author',$car_rental_service_default['car_rental_service_post_author'] ) );

        if( $car_rental_service_post_author ){

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

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

            $car_rental_service_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"> ' . $car_rental_service_byline . '</span>'; // phpcs:ignore Standard.Category.SniffName.ErrorCode
            echo '</div>';
            echo '</div>';

        }

    }

endif;


if( !function_exists('car_rental_service_posted_by_avatar') ) :

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

        $car_rental_service_default = car_rental_service_get_default_theme_options();
        $car_rental_service_post_author = absint( get_theme_mod( 'car_rental_service_post_author',$car_rental_service_default['car_rental_service_post_author'] ) );

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

            $car_rental_service_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"> ' . $car_rental_service_byline . '</div>';

            if( $car_rental_service_date ){
                car_rental_service_posted_on($car_rental_service_icon = false);
            }
            echo '</div>';

        }

    }

endif;

if( !function_exists('car_rental_service_entry_footer') ):

    /**
     * Prints HTML with meta information for the categories, tags and comments.
     */
    function car_rental_service_entry_footer( $car_rental_service_cats = true, $car_rental_service_tags = true, $car_rental_service_edits = true){   

        $car_rental_service_default = car_rental_service_get_default_theme_options();
        $car_rental_service_post_category = absint( get_theme_mod( 'car_rental_service_post_category',$car_rental_service_default['car_rental_service_post_category'] ) );
        $car_rental_service_post_tags = absint( get_theme_mod( 'car_rental_service_post_tags',$car_rental_service_default['car_rental_service_post_tags'] ) );

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

            if( $car_rental_service_cats && $car_rental_service_post_category ){

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

                        $car_rental_service_cat_name = $car_rental_service_category->name;
                        $car_rental_service_cat_slug = $car_rental_service_category->slug;
                        $car_rental_service_cat_url = get_category_link( $car_rental_service_category->term_id );
                        ?>

                        <a class="twp_cat_<?php echo esc_attr( $car_rental_service_cat_slug ); ?>" href="<?php echo esc_url( $car_rental_service_cat_url ); ?>" rel="category tag"><?php echo esc_html( $car_rental_service_cat_name ); ?></a>

                    <?php }
                    echo '</span>'; // phpcs:ignore Standard.Category.SniffName.ErrorCode
                    echo '</div>';
                    echo '</div>';
                }

            }

            if( $car_rental_service_tags && $car_rental_service_post_tags ){
                /* translators: used between list items, there is a space after the comma */
                $car_rental_service_tags_list = get_the_tag_list('', esc_html_x(', ', 'list item separator', 'car-rental-service'));
                if( $car_rental_service_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($car_rental_service_tags_list) . '</span>'; // phpcs:ignore Standard.Category.SniffName.ErrorCode
                    echo '</div>';
                    echo '</div>';

                }

            }

            if( $car_rental_service_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>', 'car-rental-service'),
                            array(
                                'span' => array(
                                    'class' => array(),
                                ),
                            )
                        ),
                        get_the_title()
                    ),
                    '<span class="edit-link">',
                    '</span>'
                );
            }

        }
    }

endif;

if ( ! function_exists( 'car_rental_service_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 car_rental_service_post_thumbnail( $car_rental_service_image_size = 'full' ) {

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

        // Fallback image path
        $car_rental_service_default_image = get_template_directory_uri() . '/assets/images/slide-bg.png';

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

        $car_rental_service_class = isset( $car_rental_service_size_class_map[ $car_rental_service_image_size ] )
            ? $car_rental_service_size_class_map[ $car_rental_service_image_size ]
            : 'data-bg-medium';

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

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

endif;

if( !function_exists('car_rental_service_is_comment_by_post_author') ):

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

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

endif;

if( !function_exists('car_rental_service_breadcrumb') ) :

    /**
     * Car Rental Service Breadcrumb
     */
    function car_rental_service_breadcrumb($car_rental_service_comment = null){

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

    }

endif;

if( ! function_exists( 'car_rental_service_page_banner_title' ) ) :

function car_rental_service_page_banner_title() {

    if ( function_exists( 'is_shop' ) && is_shop() ) {
        return woocommerce_page_title( false );

    } elseif ( is_singular() ) {
        return get_the_title();

    } elseif ( is_home() ) {
        $car_rental_service_posts_page_id = get_option( 'page_for_posts' );
        return $car_rental_service_posts_page_id ? get_the_title( $car_rental_service_posts_page_id ) : esc_html__( 'Blog', 'car-rental-service' );

    } elseif ( is_search() ) {
        return sprintf(
            esc_html__( 'Search Results for: %s', 'car-rental-service' ),
            get_search_query()
        );

    } elseif ( is_404() ) {
        return esc_html__( 'Page Not Found', 'car-rental-service' );

    } elseif ( is_tag() ) {
        return single_tag_title( 'Tag: ', false );

    } elseif ( is_category() ) {
        return single_cat_title( 'Category: ', false );

    } elseif ( is_archive() ) {
        return wp_strip_all_tags( get_the_archive_title() );
    }

    return get_the_title();
}

endif;