<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package CRT_Builder_With_Elementor
 */
function crt_builder_with_elementor_body_classes( $classes ) {
    $classes[] = 'home_' . get_theme_mod('crt_manage_general_homepage_options', 'boxed');
    $classes[] = 'nav_' . get_theme_mod('crt_manage_header_nav_top_style', 'bg-color');
    if(is_front_page() && ! is_home()) {
        $classes[] = 'home-sections';
    }
    return $classes;
}
add_filter( 'body_class', 'crt_builder_with_elementor_body_classes' );

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function crt_builder_with_elementor_pingback_header() {
    if ( is_singular() && pings_open() ) {
        printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
    }
}
add_action( 'wp_head', 'crt_builder_with_elementor_pingback_header' );

if ( ! function_exists( 'crt_builder_with_elementor_excerpt_length' ) ) :
    /**
     * Excerpt length.
     */
    function crt_builder_with_elementor_excerpt_length( $length ) {
        if ( is_admin() ) {
            return $length;
        }

        return get_theme_mod( 'crt_builder_with_elementor_excerpt_length', 20 );
    }
endif;
add_filter( 'excerpt_length', 'crt_builder_with_elementor_excerpt_length', 999 );

if ( ! function_exists( 'crt_builder_with_elementor_excerpt_custom' ) ) :
    /**
     * Excerpt Custom length.
     */
    function crt_builder_with_elementor_excerpt_custom($limit, $post_id = null) {
        $excerpt = explode(' ', wp_trim_words(get_the_excerpt($post_id), $limit));
        if (count($excerpt) >= $limit) {
            array_pop($excerpt);
            $excerpt = implode(" ", $excerpt) . '...';
        } else {
            $excerpt = implode(" ", $excerpt);
        }
        $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
        return $excerpt;
    }
endif;

if ( ! function_exists( 'crt_builder_with_elementor_excerpt_more' ) ) :
    /**
     * Excerpt more.
     */
    function crt_builder_with_elementor_excerpt_more( $more ) {
        if ( is_admin() ) {
            return $more;
        }

        return '&hellip;';
    }
endif;
add_filter( 'excerpt_more', 'crt_builder_with_elementor_excerpt_more' );

/**
 * Breadcrumb.
 */
function crt_builder_with_elementor_breadcrumb( $args = array() ) {
    if ( ! get_theme_mod( 'crt_builder_with_elementor_enable_breadcrumb', true ) ) {
        return;
    }

    $args = array(
        'show_on_front' => false,
        'show_title'    => true,
        'show_browse'   => false,
    );
    crt_builder_with_elementor_breadcrumb_trail( $args );
}
add_action( 'crt_builder_with_elementor_breadcrumb', 'crt_builder_with_elementor_breadcrumb', 10 );

function crt_builder_with_elementor_archive_heading() {
    $crt_builder_with_elementor_archive_heading_align = get_theme_mod('crt_manage_archive_heading_align', 'center');
    ?>
    <section class="archive-header archive-header__<?php echo esc_attr($crt_builder_with_elementor_archive_heading_align); ?>">
        <div class="container">
            <div class="">
                <div class="row">
                    <div class="col-12 py-3 py-sm-5">
                    <?php if(is_author()): ?>
                        <div class="archive-heading-default">
                            <div class="archive-heading-default__inner">
                                <?php $crt_builder_with_elementor_avatar = get_avatar(get_the_author_meta('ID'), 80, $crt_builder_with_elementor_default_value = '', $alt = '', array('class' => 'mb-3 rounded-circle')); ?>
                                <?php echo $crt_builder_with_elementor_avatar; ?>
                                <h1 class="archive-heading-default__title"><?php echo esc_html(get_the_author()); ?></h1>
                                <?php the_archive_description( '<div class="archive-description">', '</div>' ); ?>
                                <?php echo crt_builder_with_elementor_social_author(); ?>
                            </div>
                        </div>
                    <?php elseif(is_home()): ?>
                        <?php
                            $queried_object = get_queried_object();
                            if($queried_object):
                        ?>
                        <div class="archive-heading-default">
                            <div class="archive-heading-default__inner">
                                <h1 class="archive-heading-default__title"><?php echo esc_html($queried_object->post_title); ?></h1>
                                <?php the_archive_description( '<div class="archive-description">', '</div>' ); ?>
                            </div>
                            <div class="breadcrumb-option">
                                <?php do_action('crt_builder_with_elementor_breadcrumb'); ?>
                            </div>
                        </div>
                        <?php endif; ?>
                    <?php else: ?>
                        <div class="archive-heading-default">
                            <div class="archive-heading-default__inner">
                                <?php the_archive_title( '<h1 class="archive-heading-default__title">', '</h1>' ); ?>
                                <?php the_archive_description( '<div class="archive-description">', '</div>' ); ?>
                            </div>
                            <div class="breadcrumb-option">
                                <?php do_action('crt_builder_with_elementor_breadcrumb'); ?>
                            </div>
                        </div>
                    <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <?php
}
add_action('crt_builder_with_elementor_archive_heading', 'crt_builder_with_elementor_archive_heading', 10);
/**
 * Layout.
 */
function crt_builder_with_elementor_archive_layout($crt_builder_with_elementor_position = '') {
    $crt_builder_with_elementor_col_one = '';
    $crt_builder_with_elementor_col_two = '';
    $crt_builder_with_elementor_category_layout = 'masonry-3-columns';
    $crt_builder_with_elementor_sidebar = 'right-sidebar';
    $crt_builder_with_elementor_sidebar_position = 'sidebar-1';

    if(crt_builder_with_elementor_is_woocommerce() && (is_product_category() || is_shop())) {
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_product_category_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_product_category_sidebar_position', 'sidebar-e-commerce' );
    } elseif (crt_builder_with_elementor_is_woocommerce() && is_product()) {
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_product_single_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_product_single_sidebar_position', 'sidebar-e-commerce' );
    } elseif(is_category() || is_home()) {
        $crt_builder_with_elementor_category_layout    = get_theme_mod( 'crt_manage_category_layout', 'grid-3-columns' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_category_sidebar', 'no-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_category_sidebar_position', 'sidebar-1' );
        $queried_object = get_queried_object();
        if(isset($queried_object->term_id)) {
            $crt_builder_with_elementor_term_layout = get_term_meta($queried_object->term_id, 'crt_manage_tax_layout');
            $crt_builder_with_elementor_term_sidebar = get_term_meta($queried_object->term_id, 'crt_manage_tax_sidebar');
            $crt_builder_with_elementor_term_sidebar_position = get_term_meta($queried_object->term_id, 'crt_manage_tax_sidebar_position');

            if(!empty($crt_builder_with_elementor_term_layout)) {
                $crt_builder_with_elementor_category_layout = $crt_builder_with_elementor_term_layout[0];
            }
            if(!empty($crt_builder_with_elementor_term_sidebar)) {
                $crt_builder_with_elementor_sidebar = $crt_builder_with_elementor_term_sidebar[0];
            }
            if(!empty($crt_builder_with_elementor_term_sidebar_position)) {
                $crt_builder_with_elementor_sidebar_position = $crt_builder_with_elementor_term_sidebar_position[0];
            }
        }
    } elseif (is_tag()) {
        $crt_builder_with_elementor_category_layout    = get_theme_mod( 'crt_manage_tag_layout', 'grid-3-columns' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_tag_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_tag_sidebar_position', 'sidebar-1' );
    } elseif (is_author()) {
        $crt_builder_with_elementor_category_layout    = get_theme_mod( 'crt_manage_author_layout', 'grid-3-columns' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_author_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_author_sidebar_position', 'sidebar-1' );
    } elseif (is_search()) {
        $crt_builder_with_elementor_category_layout    = get_theme_mod( 'crt_manage_search_layout', 'grid-3-columns' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_search_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_search_sidebar_position', 'sidebar-1' );
    } elseif (is_month()) {
        $crt_builder_with_elementor_category_layout    = get_theme_mod( 'crt_manage_date_month_year_layout', 'grid-3-columns' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_date_month_year_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_date_month_year_sidebar_position', 'sidebar-1' );
    } elseif (is_single()) {
        $crt_builder_with_elementor_category_layout             = get_theme_mod( 'crt_manage_single_related_layout','' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_single_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_single_sidebar_position', 'sidebar-1' );
        if(get_post_format() == 'aside') {
            $crt_builder_with_elementor_sidebar = 'right-sidebar';
        }
    } elseif (is_front_page() && ! is_home()) {
        $crt_builder_with_elementor_category_layout    = get_theme_mod( 'crt_manage_post_latest_layout', 'grid-3-columns' );
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_post_latest_sidebar', 'no-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_post_latest_sidebar_position', 'sidebar-1' );
    } elseif(is_page()) {
        $crt_builder_with_elementor_sidebar            = get_theme_mod( 'crt_manage_page_sidebar', 'right-sidebar' );
        $crt_builder_with_elementor_sidebar_position   = get_theme_mod( 'crt_manage_page_sidebar_position', 'sidebar-1' );
    }
    if($crt_builder_with_elementor_sidebar == 'right-sidebar') {
        $crt_builder_with_elementor_col_one = 'col-12 col-md-8 mb-3 mb-md-0 grid-wrap-small';
        $crt_builder_with_elementor_col_two = 'col-12 col-md-4 sidebar-fixed';
    } elseif($crt_builder_with_elementor_sidebar == 'left-sidebar') {
        $crt_builder_with_elementor_col_one = 'col-12 col-md-8 mb-3 mb-md-0 grid-wrap-small';
        $crt_builder_with_elementor_col_two = 'col-12 col-md-4 order-first sidebar-fixed';
    }  elseif($crt_builder_with_elementor_sidebar == 'no-sidebar') {
        $crt_builder_with_elementor_col_one = 'col-12';
        if(is_single() && !is_product()){
            $crt_builder_with_elementor_col_one = 'col-12 col-md-10 offset-0 offset-md-1';
        }
        $crt_builder_with_elementor_col_two = 'd-none';
    }

    if($crt_builder_with_elementor_category_layout == 'standard') {
        $crt_builder_with_elementor_category_layout = '';
    } elseif($crt_builder_with_elementor_category_layout == 'list-large-image') {
        $crt_builder_with_elementor_category_layout = 'large-image';
    } elseif($crt_builder_with_elementor_category_layout == 'grid-2-columns') {
        $crt_builder_with_elementor_category_layout = 'grid2';
    } elseif($crt_builder_with_elementor_category_layout == 'grid-3-columns') {
        $crt_builder_with_elementor_category_layout = 'grid3';
    } elseif($crt_builder_with_elementor_category_layout == 'grid-4-columns') {
        $crt_builder_with_elementor_category_layout = 'grid4';
    } elseif($crt_builder_with_elementor_category_layout == 'masonry-2-columns') {
        $crt_builder_with_elementor_category_layout = 'masonry2';
        crt_builder_with_elementor_css_width_grid($crt_builder_with_elementor_category_layout);
    } elseif($crt_builder_with_elementor_category_layout == 'masonry-3-columns') {
        $crt_builder_with_elementor_category_layout = 'masonry3';
        crt_builder_with_elementor_css_width_grid($crt_builder_with_elementor_category_layout);
    } elseif($crt_builder_with_elementor_category_layout == 'masonry-4-columns') {
        $crt_builder_with_elementor_category_layout = 'masonry4';
        if($crt_builder_with_elementor_sidebar != 'no-sidebar') {
            $crt_builder_with_elementor_category_layout = 'masonry3';
        }
        crt_builder_with_elementor_css_width_grid($crt_builder_with_elementor_category_layout);
    } elseif ($crt_builder_with_elementor_category_layout == 'list-odd') {
        $crt_builder_with_elementor_category_layout = 'odd';
    }

    return array(
        'col_one' => $crt_builder_with_elementor_col_one,
        'col_two' => $crt_builder_with_elementor_col_two,
        'layout' => $crt_builder_with_elementor_category_layout,
        'sidebar' => $crt_builder_with_elementor_sidebar_position,
    );
}

function crt_builder_with_elementor_layout_css() {
    function crt_builder_with_elementor_css_width_grid($crt_builder_with_elementor_grid) {
        $crt_builder_with_elementor_custom_css = '';
        if($crt_builder_with_elementor_grid == 'masonry2') {
            $crt_builder_with_elementor_custom_css .= 'body {--grid-width: 50%;}';
        }
        if($crt_builder_with_elementor_grid == 'masonry3') {
            $crt_builder_with_elementor_custom_css .= 'body {--grid-width: 33.33333%;}';
        }
        if($crt_builder_with_elementor_grid == 'masonry4') {
            $crt_builder_with_elementor_custom_css .= 'body {--grid-width: 25%;}';
        }
        if(!empty($crt_builder_with_elementor_custom_css)) {
            wp_register_style( 'crt-builder-with-elementor-style-layout', false );
            wp_enqueue_style( 'crt-builder-with-elementor-style-layout' );
            wp_add_inline_style( 'crt-builder-with-elementor-style-layout', $crt_builder_with_elementor_custom_css );
        }
    }
}

add_action( 'wp_enqueue_scripts', 'crt_builder_with_elementor_layout_css', 99 );


/**
 * Border Columns.
 */
function crt_builder_with_elementor_bor_col($crt_builder_with_elementor_layout) {
    ?>
    <?php if($crt_builder_with_elementor_layout == 'grid2' || $crt_builder_with_elementor_layout == 'masonry2'): ?>
        <div class="br-col br-col50 br-sm-col-none"></div>
    <?php elseif($crt_builder_with_elementor_layout == 'grid3' || $crt_builder_with_elementor_layout == 'masonry3'): ?>
        <div class="br-col br-col33 br-sm-col-none"></div>
        <div class="br-col br-col66 br-sm-col-none"></div>
    <?php elseif($crt_builder_with_elementor_layout == 'grid4' || $crt_builder_with_elementor_layout == 'masonry4'): ?>
        <div class="br-col br-col25 br-md-col50 br-sm-col-none"></div>
        <div class="br-col br-col50 br-md-col-none"></div>
        <div class="br-col br-col75 br-md-col-none"></div>
    <?php endif;
}

function crt_builder_with_elementor_social_author($author_id = '', $align = 'justify-content-center') {
    $crt_builder_with_elementor_user_socials = array(
        'crt_manage_user_social_facebook' => 'Facebook',
        'crt_manage_user_social_x-twitter' => 'Twitter',
        'crt_manage_user_social_google' => 'Google',
        'crt_manage_user_social_youtube' => 'Youtube',
        'crt_manage_user_social_email' => 'Email',
        'crt_manage_user_social_instagram' => 'Instagram',
        'crt_manage_user_social_pinterest' => 'Pinterest',
        'crt_manage_user_social_linkedin' => 'LinkedIn',
    );
    if(empty($author_id)) {
        $author_id = get_the_author_meta('ID');
    }
    if(!empty($author_id)) {
        $authors = array();
        foreach ($crt_builder_with_elementor_user_socials as $name => $value) {
            $url = get_user_meta($author_id, $name . '_url');
            if(isset($url[0]) && $url[0]) {
                $crt_builder_with_elementor_icon = explode('_', $name);
                $authors[] = array('url' => $url[0], 'icon' => $crt_builder_with_elementor_icon[4]);
            }
        }
        if(!empty($authors)) {
            $crt_builder_with_elementor_class_social_item = 'me-2 ms-2';
            if($align == 'justify-content-center') {
                $crt_builder_with_elementor_class_social_item = 'me-2 ms-2';
            } elseif($align == 'justify-content-start') {
                $crt_builder_with_elementor_class_social_item = 'me-3';
            } elseif($align == 'justify-content-end') {
                $crt_builder_with_elementor_class_social_item = 'ms-3';
            }
            ob_start();
            ?>
                <ul class="author-socials mt-3 d-flex m-0 p-0 <?php echo esc_attr($align); ?>">
                    <?php foreach ($authors as $crt_builder_with_elementor_author_item): if($crt_builder_with_elementor_author_item['icon'] == 'email') { $crt_builder_with_elementor_author_item['icon'] = 'envelope-o'; } ?>
                        <li class="<?php echo esc_attr($crt_builder_with_elementor_class_social_item); ?> list-unstyled"><a href="<?php echo esc_url($crt_builder_with_elementor_author_item['url']) ?>" target="_blank"><i class="fa-brands fa-<?php echo esc_attr($crt_builder_with_elementor_author_item['icon']) ?>"></i></a></li>
                    <?php endforeach; ?>
                </ul>
            <?php
            return ob_get_clean();
        }
    }
    return '';
}

/**
 * Pagination for archive.
 */
function crt_builder_with_elementor_render_posts_pagination() {
    $crt_builder_with_elementor_is_pagination_enabled = get_theme_mod( 'crt_builder_with_elementor_enable_pagination', true );
    if ( $crt_builder_with_elementor_is_pagination_enabled ) {
        $crt_builder_with_elementor_pagination_type = get_theme_mod( 'crt_builder_with_elementor_pagination_type', 'defaultc' );
        if ( 'default' === $crt_builder_with_elementor_pagination_type ) :
            the_posts_navigation();
        else :
            global $wp_query;
            $crt_builder_with_elementor_pagination = paginate_links( array(
                'current' => max( 1, get_query_var( 'paged' ) ),
                'total'      => $wp_query->max_num_pages,
                'prev_text'  => __( 'Previous' ,'crt-builder-with-elementor'  ),
                'next_text'  => __( 'Next' ,'crt-builder-with-elementor'  ),
                'type'       => 'list'
            ) );
            echo '<div class="pagination"><div class="pagination__inner">'. $crt_builder_with_elementor_pagination .'</div></div>';
        endif;
    }
}
add_action( 'crt_builder_with_elementor_posts_pagination', 'crt_builder_with_elementor_render_posts_pagination', 10 );


/**
 * Pagination for single post.
 */
function crt_builder_with_elementor_render_post_navigation() {
    $crt_builder_with_elementor_post_navigation_type = get_theme_mod( 'crt_manage_single_post_navigation_type', 'single-post-navigation-thumb' );
    if($crt_builder_with_elementor_post_navigation_type != 'single-post-navigation-thumb') {
        the_post_navigation(
            array(
                'prev_text' => '<span class="nav-title">%title</span><span class="nav-label">'.esc_html__( 'Prev Post','crt-builder-with-elementor' ).'</span>',
                'next_text' => '<span class="nav-title">%title</span><span class="nav-label">'.esc_html__( 'Next Post','crt-builder-with-elementor' ).'</span>',
            )
        );
    } else {
        $prev_post = get_previous_post();
        $next_post = get_next_post();
        $crt_builder_with_elementor_prev_thumbnail = '';
        $crt_builder_with_elementor_next_thumbnail = '';
        $crt_builder_with_elementor_prev_html = '';
        $crt_builder_with_elementor_next_html = '';
        if(isset($prev_post->ID)) {
            $crt_builder_with_elementor_prev_thumbnail = get_the_post_thumbnail_url( $prev_post->ID );
        }
        if(isset($next_post->ID)) {
            $crt_builder_with_elementor_next_thumbnail = get_the_post_thumbnail_url( $next_post->ID );
        }
        if(isset($prev_post->post_title)){
            $crt_builder_with_elementor_prev_html = '<div class="prev me-3 d-flex align-content-center"><figure class="me-3 ratio11 lazy position-relative" data-src="'.$crt_builder_with_elementor_prev_thumbnail.'"></figure><div class="d-flex flex-column justify-content-center"><span class="">'.esc_html__( 'Prev Post','crt-builder-with-elementor' ).'</span><h3>'.$prev_post->post_title.'</h3></div></div>';
        }
        if(isset($next_post->post_title)){
            $crt_builder_with_elementor_next_html = '<div class="next ms-3 d-flex align-content-center"><div class="d-flex flex-column justify-content-center align-items-end text-end"><span class="">'.esc_html__( 'Next Post','crt-builder-with-elementor' ).'</span><h3>'.$next_post->post_title.'</h3></div><figure class="ms-3 ratio11 lazy position-relative" data-src="'.$crt_builder_with_elementor_next_thumbnail.'"></figure></div>';
        }
    ?>
        <div class="d-flex post_navigation_thumb my-4">
            <div class="post_navigation_thumb__item w-50"><?php previous_post_link( '%link', $crt_builder_with_elementor_prev_html ); ?></div>
            <div class="post_navigation_thumb__item w-50"><?php next_post_link( '%link', $crt_builder_with_elementor_next_html ); ?></div>
        </div>
    <?php
    }
}
add_action( 'crt_builder_with_elementor_post_navigation', 'crt_builder_with_elementor_render_post_navigation' );


/**
 * Author for single post.
 */
function crt_builder_with_elementor_render_author_single($post) {
    $crt_builder_with_elementor_avatar = get_avatar($post->post_author, 300);
    $name = get_the_author_meta('display_name', $post->post_author );
    $description = get_the_author_meta( 'description', $post->post_author );
    $url = get_author_posts_url( $post->post_author );
    $crt_builder_with_elementor_author_social = crt_builder_with_elementor_social_author($post->post_author, 'justify-content-start');
    ?>
    <div class="single-author p-3 p-md-4 mt-5">
        <div class="row">
            <div class="col-5 col-md-3">
                <?php echo $crt_builder_with_elementor_avatar; ?>
            </div>
            <div class="col-7 col-md-8">
                <h4 class="single-author__name"><?php echo esc_html($name); ?></h4>
                <div class="single-author__description"><?php echo esc_html($description); ?></div>
                <div class="single-author__social"><?php echo $crt_builder_with_elementor_author_social; ?></div>
            </div>
        </div>
    </div>
    <?php
}
add_action( 'crt_builder_with_elementor_author', 'crt_builder_with_elementor_render_author_single' );


if ( ! function_exists( 'crt_builder_with_elementor_validate_excerpt_length' ) ) :
    function crt_builder_with_elementor_validate_excerpt_length( $crt_builder_with_elementor_validity, $value ) {
        $value = intval( $value );
        if ( empty( $value ) || ! is_numeric( $value ) ) {
            $crt_builder_with_elementor_validity->add( 'required', esc_html__( 'You must supply a valid number.','crt-builder-with-elementor' ) );
        } elseif ( $value < 1 ) {
            $crt_builder_with_elementor_validity->add( 'min_no_of_words', esc_html__( 'Minimum no of words is 1','crt-builder-with-elementor' ) );
        } elseif ( $value > 100 ) {
            $crt_builder_with_elementor_validity->add( 'max_no_of_words', esc_html__( 'Maximum no of words is 100','crt-builder-with-elementor' ) );
        }
        return $crt_builder_with_elementor_validity;
    }
endif;

/**
 * Adds footer copyright text.
 */
function crt_builder_with_elementor_output_footer_copyright_content() {
    $crt_builder_with_elementor_theme_data = wp_get_theme();
    $search     = array( '[the-year]', '[site-link]' );
    $replace    = array( date( 'Y' ), '<a href="' . esc_url( home_url( '/' ) ) . '">' . esc_attr( get_bloginfo( 'name', 'display' ) ) . '</a>' );
    /* translators: 1: Year, 2: Site Title with home URL. */
    $crt_builder_with_elementor_copyright_default = sprintf( esc_html_x( 'Copyright &copy; %1$s %2$s', '1: Year, 2: Site Title with home URL','crt-builder-with-elementor' ), '[the-year]', '[site-link]' );
    $crt_builder_with_elementor_copyright_text    = get_theme_mod( 'crt_builder_with_elementor_footer_copyright_text', $crt_builder_with_elementor_copyright_default );
    $crt_builder_with_elementor_copyright_text    = str_replace( $search, $replace, $crt_builder_with_elementor_copyright_text );
    $crt_builder_with_elementor_copyright_text   .= ' ' . esc_html( ' | ' . $crt_builder_with_elementor_theme_data->get( 'Name' ) ) . '&nbsp;' . esc_html__( 'by','crt-builder-with-elementor' ) . '&nbsp;<a target="_blank" href="' . esc_url( $crt_builder_with_elementor_theme_data->get( 'AuthorURI' ) ) . '">' . esc_html( ucwords( $crt_builder_with_elementor_theme_data->get( 'Author' ) ) ) . '</a>';
    /* translators: %s: WordPress.org URL */
    $crt_builder_with_elementor_copyright_text .= sprintf( esc_html__( ' | Powered by %s','crt-builder-with-elementor' ), '<a href="' . esc_url( __( 'https://wordpress.org/','crt-builder-with-elementor' ) ) . '" target="_blank">WordPress</a>. ' );
    ?>
    <div class="copyright">
        <span><?php echo wp_kses_post( $crt_builder_with_elementor_copyright_text ); ?></span>
    </div>
    <?php
}
add_action( 'crt_builder_with_elementor_footer_copyright', 'crt_builder_with_elementor_output_footer_copyright_content' );

/**
 * Check is json.
 */
function crt_builder_with_elementor_is_json($string) {
    json_decode(html_entity_decode($string), true);
    return json_last_error() === JSON_ERROR_NONE;
}

/**
 * Convert json to array.
 */
function crt_builder_with_elementor_json_to_array($data) {
    $array = array();
    $data = json_decode($data);
    if(empty($data)) {
        return $array;
    }
    foreach ($data as $key => $value) {
        $crt_builder_with_elementor_array_item = array();
        if(is_object($value)) {
            foreach ($value as $crt_builder_with_elementor_key_item =>  $crt_builder_with_elementor_value_item) {
                if(crt_builder_with_elementor_is_json($crt_builder_with_elementor_value_item)){
                    $crt_builder_with_elementor_array_item[$crt_builder_with_elementor_key_item] = json_decode(html_entity_decode($crt_builder_with_elementor_value_item), true);
                } else {
                    $crt_builder_with_elementor_array_item[$crt_builder_with_elementor_key_item] = $crt_builder_with_elementor_value_item;
                }
            }
            $array[$key] = is_object($value) ? (array) $crt_builder_with_elementor_array_item : $crt_builder_with_elementor_array_item;
        }
    }
    return $array;
}

add_action('crt_builder_with_elementor_header', 'crt_builder_with_elementor_header');
/**
 * Header Global.
 */
function crt_builder_with_elementor_header() {
    $header = get_theme_mod('crt_manage_header_type', 'v1');
    get_template_part( 'template-parts/header-'. $header );
}

/**
 * Single Header
 */
function crt_builder_with_elementor_single_header($args = array()) {
    $header = 'v3';
    get_template_part( 'template-parts/single/single-header-'. $header );
}

add_action( 'crt_builder_with_elementor_single_header', 'crt_builder_with_elementor_single_header', 10 );


/**
 * Custom Pagination.
 */
if ( ! function_exists( 'crt_builder_with_elementor_pagination_custom' ) ) :
    function crt_builder_with_elementor_pagination_custom( $paged = '', $crt_builder_with_elementor_max_page = '' ) {
        $crt_builder_with_elementor_big = 999999999;
        if( ! $paged ) {
            $paged = get_query_var('page');
        }

        if( ! $crt_builder_with_elementor_max_page ) {
            global $wp_query;
            $crt_builder_with_elementor_max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
        }

        $crt_builder_with_elementor_pagination = paginate_links( array(
            'base'       => str_replace( $crt_builder_with_elementor_big, '%#%', esc_url( get_pagenum_link( $crt_builder_with_elementor_big ) ) ),
            'format'     => '?paged=%#%',
            'current'    => max( 1, $paged ),
            'total'      => $crt_builder_with_elementor_max_page,
            'mid_size'   => 1,
            'prev_text'  => __( 'Previous' ,'crt-builder-with-elementor'  ),
            'next_text'  => __( 'Next' ,'crt-builder-with-elementor'  ),
            'type'       => 'list'
        ) );

        echo '<div class="pagination"><div class="pagination__inner">'. $crt_builder_with_elementor_pagination .'</div></div>';
    }
endif;

/**
 * Instagram Feed
 */
if ( ! function_exists( 'crt_builder_with_elementor_footer_instagram' ) ) :
    function crt_builder_with_elementor_footer_instagram() {
    $crt_builder_with_elementor_enable_instagram_feed = get_theme_mod('crt_manage_footer_instagram_feed_enable');
    $crt_builder_with_elementor_instagram_feed = get_theme_mod('crt_manage_footer_instagram_feed');
    $crt_builder_with_elementor_instagram_feed_fullwidth = get_theme_mod('crt_manage_footer_instagram_feed_enable_fullwidth', false);
?>
    <?php if($crt_builder_with_elementor_enable_instagram_feed): if(!$crt_builder_with_elementor_instagram_feed_fullwidth): ?><div class="container"><div class="row"><div class="col-12"><?php endif; ?>
    <?php echo do_shortcode($crt_builder_with_elementor_instagram_feed); ?>
    <?php if(!$crt_builder_with_elementor_instagram_feed_fullwidth): ?></div></div></div><?php endif; endif; ?>
<?php
}
endif;

/**
 * Get thumbnail size in custom repeater field
 */
if ( ! function_exists( 'crt_builder_with_elementor_gtsicrf' ) ) :
    function crt_builder_with_elementor_gtsicrf($post_id, $crt_builder_with_elementor_size_desktop, $crt_builder_with_elementor_size_mobile = '') {
        $crt_builder_with_elementor_thumbnail_size = '';
        $image_url = '';
        if(wp_is_mobile()) {
            if(!empty($crt_builder_with_elementor_size_mobile)) {
                $crt_builder_with_elementor_thumbnail_size = $crt_builder_with_elementor_size_mobile;
            } else {
                $crt_builder_with_elementor_thumbnail_size = 'crt-builder-with-elementor-image-small';
            }
        } else {
            if(!empty($crt_builder_with_elementor_size_desktop)) {
                $crt_builder_with_elementor_thumbnail_size = $crt_builder_with_elementor_size_desktop;
            } else {
                $crt_builder_with_elementor_thumbnail_size = 'crt-builder-with-elementor-image-medium';
            }
        }
        if($post_id['image_id']) {
            $image_url = wp_get_attachment_image_url( $post_id['image_id'], $crt_builder_with_elementor_thumbnail_size );
        } else {
            $image_url = $post_id['image_url'];
        }
        return $image_url;
    }
endif;