<?php
/**
 * Custom template tags for this theme
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package CRT_Builder_With_Elementor
 */

if ( ! function_exists( 'crt_builder_with_elementor_posted_on' ) ) :
	/**
	 * Prints HTML with meta information for the current post-date/time.
	 */
	function crt_builder_with_elementor_posted_on() {
		if ( get_theme_mod( 'crt_builder_with_elementor_post_hide_date', false ) ) {
			return;
		}
		$crt_builder_with_elementor_time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
		if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
			$crt_builder_with_elementor_time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
		}

		$crt_builder_with_elementor_time_string = sprintf(
			$crt_builder_with_elementor_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() )
		);

		$posted_on = sprintf(
			/* translators: %s: post date. */
			esc_html_x( ' %s', 'post date','crt-builder-with-elementor' ),
			'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $crt_builder_with_elementor_time_string . '</a>'
		);

		echo '<span class="posted-on">' . $posted_on . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	}
endif;

if ( ! function_exists( 'crt_builder_with_elementor_posted_by' ) ) :
	/**
	 * Prints HTML with meta information for the current author.
	 */
	function crt_builder_with_elementor_posted_by($post) {
        $crt_builder_with_elementor_avatar = false;
        $author = '';
        $name = get_the_author_meta('display_name', $post->post_author);
        if($crt_builder_with_elementor_avatar) {
            $url = get_avatar($post->post_author);
            if($name) {
                $author = sprintf(
                    esc_html_x( 'By %s', '','crt-builder-with-elementor' ),'<a href="' . esc_url( get_author_posts_url( $post->post_author ) ) . '">' . esc_html( $name ) . '</a>'
                );
            }
        } else {
            if($name) {
                $author = sprintf(
                    esc_html_x( 'By %s', '','crt-builder-with-elementor' ),'<a href="' . esc_url( get_author_posts_url( $post->post_author ) ) . '">' . esc_html( $name ) . '</a>'
                );
            }
        }
		echo '<span class="entry-author">' . wp_kses_post( $author ) . '</span>';
	}
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_footer' ) ) :
	/**
	 * Prints HTML with meta information for the categories, tags and comments.
	 */
	function crt_builder_with_elementor_entry_footer() {
		// Hide category and tag text for pages.
		if ( 'post' === get_post_type() ) {
			$crt_builder_with_elementor_hide_category = get_theme_mod( 'crt_builder_with_elementor_post_hide_category', false );
			$crt_builder_with_elementor_hide_tag      = get_theme_mod( 'crt_builder_with_elementor_post_hide_tags', false );

			if ( ! $crt_builder_with_elementor_hide_category ) {
				/* translators: used between list items, there is a space after the comma */
				$crt_builder_with_elementor_categories_list = get_the_category_list( esc_html__( ', ','crt-builder-with-elementor' ) );
				if ( $crt_builder_with_elementor_categories_list ) {
					/* translators: 1: list of categories. */
					printf( '<span class="cat-links">' . esc_html__( '%1$s','crt-builder-with-elementor' ) . '</span>', $crt_builder_with_elementor_categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				}
			}
		}

		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>','crt-builder-with-elementor' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				wp_kses_post( get_the_title() )
			),
			'<span class="edit-link">',
			'</span>'
		);
	}
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_comment_count' ) ) :
    function crt_builder_with_elementor_entry_comment_count($post_id) {
        $comment_count = get_comment_count($post_id)['approved'];
        printf( esc_html__( '%1$s %2$s','crt-builder-with-elementor' ),'<i class="fa-regular fa-comment"></i>', absint( $comment_count ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML icon is static markup
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_read_time' ) ) :
    function crt_builder_with_elementor_entry_read_time($post_id) {
        $crt_builder_with_elementor_time_read = get_post_meta($post_id, 'crt_manage_post_metabox_time_read', true);
        $crt_builder_with_elementor_time_read = $crt_builder_with_elementor_time_read ? $crt_builder_with_elementor_time_read:'1';
        printf( esc_html__( '%1$s %2$s %3$s','crt-builder-with-elementor' ), '<i class="fa-solid fa-clock"></i>', esc_html( $crt_builder_with_elementor_time_read ), esc_html__( 'Min Read', 'crt-builder-with-elementor' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML icon is static markup
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_view_count' ) ) :
    function crt_builder_with_elementor_entry_view_count($post_id) {
        $crt_builder_with_elementor_view_count = get_post_meta($post_id, 'post_view_count', true);
        $crt_builder_with_elementor_view_count = $crt_builder_with_elementor_view_count ? $crt_builder_with_elementor_view_count:'1';
        if(!empty($crt_builder_with_elementor_view_count)) {
            printf( esc_html__( '%1$s %2$s ','crt-builder-with-elementor' ), esc_html__( 'View: ', 'crt-builder-with-elementor' ), esc_html( $crt_builder_with_elementor_view_count ) );
        }
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_date' ) ) :
    function crt_builder_with_elementor_entry_date($post) {
        $crt_builder_with_elementor_entry_date_format = get_theme_mod('crt_manage_entry_date_format', 'F d, Y');
        $date = wp_date( $crt_builder_with_elementor_entry_date_format, strtotime( $post->post_date ) );
        printf( esc_html__( '%1$s %2$s ','crt-builder-with-elementor' ), '', esc_html( $date ) );
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_single_footer' ) ) :
    /**
     * Prints HTML with meta information for the categories, tags and comments.
     */
    function crt_builder_with_elementor_entry_single_footer() {
        // Hide category and tag text for pages.
        if ( 'post' === get_post_type() ) {
            $crt_builder_with_elementor_hide_category = get_theme_mod( 'crt_builder_with_elementor_post_hide_category', false );
            $crt_builder_with_elementor_hide_tag      = get_theme_mod( 'crt_builder_with_elementor_post_hide_tags', false );

            if ( ! $crt_builder_with_elementor_hide_category ) {
                /* translators: used between list items, there is a space after the comma */
                $crt_builder_with_elementor_categories_list = get_the_category_list( esc_html__( '','crt-builder-with-elementor' ) );
                if ( $crt_builder_with_elementor_categories_list ) {
                    /* translators: 1: list of categories. */
                    printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s','crt-builder-with-elementor' ) . '</span>', $crt_builder_with_elementor_categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                }
            }

            if ( ! $crt_builder_with_elementor_hide_tag ) {
                /* translators: used between list items, there is a space after the comma */
                $crt_builder_with_elementor_tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator','crt-builder-with-elementor' ) );
                if ( $crt_builder_with_elementor_tags_list ) {
                    /* translators: 1: list of tags. */
                    printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s','crt-builder-with-elementor' ) . '</span>', $crt_builder_with_elementor_tags_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                }
            }
        }

        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>','crt-builder-with-elementor' ),
                    array(
                        'span' => array(
                            'class' => array(),
                        ),
                    )
                ),
                wp_kses_post( get_the_title() )
            ),
            '<span class="edit-link">',
            '</span>'
        );
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry_category' ) ) :
    function crt_builder_with_elementor_entry_category($post_id) {
        $crt_builder_with_elementor_categories_list = get_the_category_list( '<span class="px-2"> + </span>', 'multiple', $post_id );
        if ( $crt_builder_with_elementor_categories_list ) {
            printf( esc_html__( '%1$s','crt-builder-with-elementor' ), $crt_builder_with_elementor_categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_the_category_list() returns pre-escaped HTML links
        }
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_entry' ) ) :
    /**
     * Prints HTML with meta information for the categories
     */
	function crt_builder_with_elementor_entry_options( $post, $args = array('class' => 'mt-2', 'entry_date' => true, 'entry_cat' => true, 'entry_author' => true, 'entry_read_time' => true, 'entry_comment' => true, 'entry_view_count' => true, 'entry_date_order' => 1, 'entry_cat_order' => 2, 'entry_author_order' => 3, 'entry_read_time_order' => 4, 'entry_comment_order' => 5, 'entry_view_count_order' => 6)) {
        $post_id = $post->ID;
        ?>
        <div class="entry <?php echo esc_attr($args['class']); ?>">
            <?php if(isset($args['entry_date']) && $args['entry_date']): ?>
                <span class="entry__date <?php echo isset($args['entry_date_order']) ? 'order-' . esc_attr( $args['entry_date_order'] ) : ''; ?>"><?php crt_builder_with_elementor_entry_date($post); ?></span>
            <?php endif; ?>
            <?php if(isset($args['entry_cat']) && $args['entry_cat']): ?>
                <span class="entry__category <?php echo isset($args['entry_cat_order']) ? 'order-' . esc_attr( $args['entry_cat_order'] ) : ''; ?>"><?php crt_builder_with_elementor_entry_category($post_id) ?></span>
            <?php endif; ?>
            <?php if(isset($args['entry_author']) && $args['entry_author']): ?>
                <span class="entry__author <?php echo isset($args['entry_author_order']) ? 'order-' . esc_attr( $args['entry_author_order'] ) : ''; ?>"><?php crt_builder_with_elementor_posted_by($post) ?></span>
            <?php endif; ?>
            <?php if(isset($args['entry_read_time']) && $args['entry_read_time']): ?>
                <span class="entry__read-time <?php echo isset($args['entry_read_time_order']) ? 'order-' . esc_attr( $args['entry_read_time_order'] ) : ''; ?>"><?php crt_builder_with_elementor_entry_read_time($post_id) ?></span>
            <?php endif; ?>
            <?php if(isset($args['entry_comment']) && $args['entry_comment']): ?>
                <span class="entry__comment <?php echo isset($args['entry_comment_order']) ? 'order-' . esc_attr( $args['entry_comment_order'] ) : ''; ?>"><?php crt_builder_with_elementor_entry_comment_count($post_id) ?></span>
            <?php endif; ?>
            <?php if(isset($args['entry_view_count']) && $args['entry_view_count']): ?>
                <span class="entry__view-count <?php echo isset($args['entry_view_count_order']) ? 'order-' . esc_attr( $args['entry_view_count_order'] ) : ''; ?>"><?php crt_builder_with_elementor_entry_view_count($post_id) ?></span>
            <?php endif; ?>
        </div>
        <?php
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_button_view_more' ) ) :
    function crt_builder_with_elementor_button_view_more($class = 'mt-4') {
        $crt_builder_with_elementor_enable_view_more = get_theme_mod('crt_manage_general_post_enable_button', false);
        $crt_builder_with_elementor_button_text = get_theme_mod('crt_manage_general_post_text_button', __( 'Discover More','crt-builder-with-elementor' ));
        if($crt_builder_with_elementor_enable_view_more || $crt_builder_with_elementor_button_text):
        ?>
            <div class="<?php echo esc_attr($class) ?>">
                <a class="button-default" href="<?php echo esc_url( get_permalink() ); ?>" title="<?php echo esc_attr( get_the_title() ); ?>"><?php echo esc_html($crt_builder_with_elementor_button_text); ?></a>
            </div>
        <?php
        endif;
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_post_thumbnail' ) ) :
	/**
	 * Displays an optional post thumbnail.
	 *
	 * Wraps the post thumbnail in an anchor element on index views, or a div
	 * element when on single views.
	 */
	function crt_builder_with_elementor_post_thumbnail($crt_builder_with_elementor_thumb = '') {
		if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
			return;
		}
		if(is_page()):
            $crt_builder_with_elementor_thumbnail = get_theme_mod('crt_manage_page_thumbnail', 'outer-thumb');
            $crt_builder_with_elementor_thumbnail_size = get_theme_mod('crt_manage_page_thumbnail_size', 'ratio169');
        ?>
            <div class="post-thumbnail mb-4 post-thumbnail_<?php echo esc_attr($crt_builder_with_elementor_thumbnail); ?>">
                <?php $crt_builder_with_elementor_get_thumbnail_url = get_the_post_thumbnail_url( get_the_ID() ); ?>
                <figure class="ratio lazy <?php echo esc_attr($crt_builder_with_elementor_thumbnail_size); ?>" data-src="<?php echo esc_attr($crt_builder_with_elementor_get_thumbnail_url); ?>"></figure>
            </div><!-- .post-thumbnail -->
        <?php
		elseif ( is_singular() ) :
            $crt_builder_with_elementor_thumbnail = get_theme_mod('crt_manage_single_thumbnail', 'outer-thumb');
            $crt_builder_with_elementor_thumbnail_size = get_theme_mod('crt_manage_single_thumbnail_size', 'ratio169');
            ?>
			<div class="post-thumbnail mb-4 post-thumbnail_<?php echo esc_attr($crt_builder_with_elementor_thumbnail); ?>">
                <?php $crt_builder_with_elementor_get_thumbnail_url = get_the_post_thumbnail_url( get_the_ID() ); ?>
                <figure class="ratio lazy <?php echo esc_attr($crt_builder_with_elementor_thumbnail_size); ?>" data-src="<?php echo esc_attr($crt_builder_with_elementor_get_thumbnail_url); ?>"></figure>
			</div><!-- .post-thumbnail -->
			<?php
		endif; // End is_singular().
	}
endif;

if ( ! function_exists( 'crt_builder_with_elementor_post_thumb' ) ) :
    function crt_builder_with_elementor_post_thumb($class = 'ratio32', $size = 'crt-builder-with-elementor-image-medium') {
        $post = get_post();
        $post_id = $post->ID;
        $post_format = get_post_format($post) ? : 'standard';
        $crt_builder_with_elementor_get_thumbnail_url = get_the_post_thumbnail_url( $post_id );
        $crt_builder_with_elementor_galleries = get_post_meta($post_id, 'crt_manage_post_metabox_gallery');
        if(!empty($crt_builder_with_elementor_galleries)) {
            $crt_builder_with_elementor_galleries = array_merge($crt_builder_with_elementor_galleries[0], array($crt_builder_with_elementor_get_thumbnail_url));
        }
    ?>
            <?php if($post_format == 'gallery' && !empty($crt_builder_with_elementor_galleries)): ?>
                <div class="gallery-image">
                    <?php
                        foreach ($crt_builder_with_elementor_galleries as $image):
                        if( validate_file($image) ):
                    ?>
                        <div>
                            <figure class="<?php echo esc_attr($class) ?>" style="background-image: url(<?php echo esc_url($image); ?>)"></figure>
                        </div>
                    <?php endif; endforeach; ?>
                </div>
            <?php else: if($crt_builder_with_elementor_get_thumbnail_url): ?>
            <figure class="<?php echo esc_attr($class) ?>" data-src="<?php echo esc_url($crt_builder_with_elementor_get_thumbnail_url); ?>"></figure>
            <?php endif; endif; ?>
        <?php
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_is_landsape' ) ) :

    function crt_builder_with_elementor_is_landsape($file)
    {
        list($width, $height) = getimagesize($file);
        if ($width > $height) {
            return true;
        } else {
            return false;
        }
    }
endif;

if ( ! function_exists( 'wp_body_open' ) ) :
	/**
	 * Shim for sites older than 5.2.
	 *
	 * @link https://core.trac.wordpress.org/ticket/12563
	 */
	function wp_body_open() {
		do_action( 'wp_body_open' );
	}
endif;

if ( ! function_exists( 'crt_builder_with_elementor_heading' ) ) :
    /**
     * Prints HTML with heading information for the section
     */
    function crt_builder_with_elementor_heading( $crt_builder_with_elementor_section_title, $crt_builder_with_elementor_section_sub_title) {
        $crt_builder_with_elementor_heading_style = get_theme_mod('crt_manage_heading_style', 'center');
        $crt_builder_with_elementor_heading_line_position = get_theme_mod('crt_manage_heading_line_position', 'bottom');
        $crt_builder_with_elementor_heading_sub_enable = get_theme_mod('crt_manage_heading_sub_enable', true);
        ?>
        <div class="heading-default heading-default__<?php echo esc_attr($crt_builder_with_elementor_heading_style); ?> heading-default__line--<?php echo esc_attr($crt_builder_with_elementor_heading_line_position); ?>">
            <div class="heading-default__inner">
                <?php if(!empty($crt_builder_with_elementor_section_title)): ?>
                    <h2 class="heading-default__title"><?php echo esc_html($crt_builder_with_elementor_section_title); ?></h2>
                <?php endif; ?>
                <?php if($crt_builder_with_elementor_heading_sub_enable): ?>
                    <span class="heading-default__sub"><?php echo esc_html($crt_builder_with_elementor_section_sub_title); ?></span>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
endif;
