<?php

if (!function_exists('faced_construct_cat_section')) {
    /**
     * Display category section on homepage
     *
     * @since 1.0.0
     *
     */
    function faced_construct_cat_section()
    {
        global $faced_theme_options;
        if ((is_front_page()) && ($faced_theme_options['faced-enable-category-boxes'] == 1)) {
            /**
             * faced_single_cat_posts hook.
             *
             * @since 1.0.0
             *
             * @hooked faced_constuct_single_cat_posts - 10
             */
            do_action('faced_single_cat_posts');
        }
    }
}
add_action('faced_cat_section', 'faced_construct_cat_section', 10);


if (!function_exists('faced_constuct_single_cat_posts')) {
    /**
     * Display latest posts boxes of 3 different categories
     *
     * @since 1.0.0
     *
     */
    function faced_constuct_single_cat_posts()
    {
        global $faced_theme_options;
        $cat1 = absint($faced_theme_options['faced-single-cat-posts-select-1']);
        if (!empty($cat1)) {
?>
            <section class="promo-section sec-spacing">
                <div class="container">
                    <div class="row">
                        <?php
                        $faced_cat_post_args = array(
                            'category__in' => $cat1,
                            'post_type' => 'post',
                            'posts_per_page' => 3,
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => true
                        );
                        $faced_featured_query = new WP_Query($faced_cat_post_args);
                        if ($faced_featured_query->have_posts()) :

                            while ($faced_featured_query->have_posts()) : $faced_featured_query->the_post();
                        ?>
                                <div class="col-1-1 col-sm-1-2 col-md-1-3">
                                    <div class="card card-bg-image card-promo">
                                        <figure class="card_media">
                                            <a href="<?php the_permalink(); ?>">
                                                <?php
                                                if (has_post_thumbnail()) {
                                                    the_post_thumbnail('faced-medium');
                                                } else {
                                                ?>
                                                    <img src="<?php echo esc_url(get_template_directory_uri()) . '/candidthemes/assets/custom/img/faced-medium.jpg' ?>" alt="<?php the_title(); ?>">
                                                <?php
                                                }
                                                ?>
                                            </a>
                                        </figure>

                                        <article class="card_body">
                                            <h3 class="card_title">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php
                                                    $tags = get_the_tags();
                                                    if (!empty($tags)) {
                                                        $tag_name = $tags[0]->name;
                                                        echo esc_html($tag_name);
                                                    } else {
                                                        echo esc_html(get_cat_name($cat1));
                                                    }

                                                    ?>
                                                </a>
                                            </h3>
                                        </article>
                                    </div>
                                </div>
                        <?php
                            endwhile;
                        endif;
                        wp_reset_postdata();
                        ?>
                    </div>
                </div>
            </section>
            <?php
        }
    }
}
add_action('faced_single_cat_posts', 'faced_constuct_single_cat_posts', 10);



if (!function_exists('faced_posts_navigation')) {
    /**
     * Display pagination based on type seclected
     *
     * @since 1.0.0
     *
     */
    function faced_posts_navigation()
    {
        global $faced_theme_options;
        if ($faced_theme_options['faced-pagination-options'] == 'numeric') {
            the_posts_pagination();
        } elseif ($faced_theme_options['faced-pagination-options'] == 'ajax') {
            $page_number = get_query_var('paged');
            if ($page_number == 0) {
                $output_page = 2;
            } else {
                $output_page = $page_number + 1;
            }
            if (paginate_links()) {
                echo "<div class='ajax-pagination text-center'><div class='show-more' data-number='$output_page'><i class='fa fa-refresh'></i>" . __('Load More', 'faced') . "</div><div id='free-temp-post'></div></div>";
            }
        } else {
            the_posts_navigation();
        }
    }
}
add_action('faced_action_navigation', 'faced_posts_navigation', 10);


if (!function_exists('faced_related_post')) :
    /**
     * Display related posts from same category
     *
     * @param int $post_id
     * @return void
     *
     * @since 1.0.0
     *
     */
    function faced_related_post($post_id)
    {

        global $faced_theme_options;
        if ($faced_theme_options['faced-single-page-related-posts'] == 0) {
            return;
        }
        $categories = get_the_category($post_id);
        if ($categories) {
            $category_ids = array();
            $category = get_category($category_ids);
            $categories = get_the_category($post_id);
            foreach ($categories as $category) {
                $category_ids[] = $category->term_id;
            }
            $count = $category->category_count;
            if ($count > 1) { ?>
                <div class="related-post">
                    <?php
                    $faced_related_post_title = esc_html($faced_theme_options['faced-single-page-related-posts-title']);
                    if (!empty($faced_related_post_title)) :
                    ?>
                        <h2 class="post-title"><?php echo esc_html($faced_related_post_title); ?></h2>
                    <?php
                    endif;

                    $faced_cat_post_args = array(
                        'category__in' => $category_ids,
                        'post__not_in' => array($post_id),
                        'post_type' => 'post',
                        'posts_per_page' => 2,
                        'post_status' => 'publish',
                        'ignore_sticky_posts' => true
                    );
                    $faced_featured_query = new WP_Query($faced_cat_post_args);
                    ?>
                    <div class="row">
                        <?php
                        if ($faced_featured_query->have_posts()) :

                            while ($faced_featured_query->have_posts()) : $faced_featured_query->the_post();
                        ?>
                                <div class="col-1-1 col-sm-1-2 col-md-1-2">
                                    <div class="card card-blog-post card-full-width">
                                        <?php
                                        if (has_post_thumbnail()) :
                                        ?>
                                            <figure class="card_media">
                                                <a href="<?php the_permalink() ?>">
                                                    <?php the_post_thumbnail('faced-medium'); ?>
                                                </a>
                                            </figure>
                                        <?php
                                        endif;
                                        ?>
                                        <div class="card_body">
                                            <?php faced_list_category(); ?>
                                            <h4 class="card_title">
                                                <a href="<?php the_permalink() ?>">
                                                    <?php the_title(); ?>
                                                </a>
                                            </h4>
                                            <div class="entry-meta">
                                                <?php
                                                faced_posted_on();
                                                faced_posted_by();
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php
                            endwhile;
                            ?>
                    </div>

                <?php
                        endif;
                        wp_reset_postdata();
                ?>
                </div> <!-- .related-post -->
            <?php
            }
        }
    }
endif;
add_action('faced_related_posts', 'faced_related_post', 10, 1);


if (!function_exists('faced_constuct_carousel')) {
    /**
     * Add carousel on header
     *
     * @since 1.0.0
     */
    function faced_constuct_carousel()
    {

        if (is_front_page()) {
            global $faced_theme_options;
            if ($faced_theme_options['faced-enable-slider'] != 1)
                return false;
            $featured_cat = absint($faced_theme_options['faced-select-category']);
            $faced_slider_args = array();
            if (is_rtl()) {
                $faced_slider_args['rtl'] = true;
            }
            $faced_slider_args_encoded = wp_json_encode($faced_slider_args);
            $query_args = array(
                'post_type' => 'post',
                'ignore_sticky_posts' => true,
                'posts_per_page' => 6,
                'cat' => $featured_cat
            );

            $query = new WP_Query($query_args);
            if ($query->have_posts()) :
            ?>
                <section class="hero hero-slider-section">
                    <div class="container">
                        <!-- slick slider component start -->
                        <div class="hero_slick-slider" data-slick='<?php echo $faced_slider_args_encoded; ?>'>
                            <?php
                            $i = 1;
                            while ($query->have_posts()) :
                                $query->the_post();

                            ?>
                                <div class="card card-bg-image">
                                    <?php
                                    if (has_post_thumbnail()) {
                                    ?>
                                        <div class="post-thumb">
                                            <figure class="card_media">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php
                                                    $cropped_image = $faced_theme_options['faced-image-size-slider'];
                                                    if ($cropped_image == 'cropped-image') {
                                                        the_post_thumbnail('faced-large');
                                                    } else {
                                                        the_post_thumbnail();
                                                    }
                                                    ?>
                                                </a>
                                            </figure>
                                        </div>
                                    <?php
                                    } else {
                                    ?>
                                        <div class="post-thumb">
                                            <a href="<?php the_permalink(); ?>">

                                                <img src="<?php echo esc_url(get_template_directory_uri()) . '/candidthemes/assets/custom/img/faced-default.jpg' ?>" alt="<?php the_title(); ?>">

                                            </a>
                                        </div>
                                    <?php
                                    }
                                    ?>
                                    <article class="card_body">
                                        <?php
                                        faced_list_category();
                                        ?>

                                        <h3 class="card_title">
                                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        </h3>

                                        <div class="entry-meta">
                                            <?php
                                            faced_posted_on();
                                            faced_posted_by();
                                            ?>
                                        </div>
                                    </article>

                                </div>
                            <?php
                                $i++;

                            endwhile;
                            ?>
                        </div>
                    </div>
                </section><!-- .hero -->
            <?php
            endif;
            wp_reset_postdata();
        } //is_front_page
    }
}
add_action('faced_carousel', 'faced_constuct_carousel', 10);


if (!function_exists('faced_breadcrumb_options')) :
    /**
     * Functions to manage breadcrumbs
     */
    function faced_breadcrumb_options()
    {
        global $faced_theme_options;
        if (($faced_theme_options['faced-blog-site-breadcrumb'] == 1) && !is_front_page()) {
            $breadcrumb_from = $faced_theme_options['faced-breadcrumb-display-from-option'];

            if ((function_exists('yoast_breadcrumb')) && ($breadcrumb_from == 'yoast-breadcrumb')) {
            ?>
                <div class="faced-breadcrumb-wrapper">
                    <?php
                    yoast_breadcrumb();
                    ?>
                </div>
            <?php
            } elseif ((function_exists('rank_math_the_breadcrumbs')) && ($breadcrumb_from == 'rankmath-breadcrumb')) {
            ?>
                <div class="faced-breadcrumb-wrapper">
                    <?php
                    rank_math_the_breadcrumbs();
                    ?>
                </div>
            <?php
            } elseif ((function_exists('bcn_display')) && ($breadcrumb_from == 'breadcrumb-navxt')) {
            ?>
                <div class="faced-breadcrumb-wrapper">
                    <?php
                    bcn_display();
                    ?>
                </div>
            <?php
            } else {
            ?>
                <div class="faced-breadcrumb-wrapper">
                    <?php
                    faced_breadcrumbs();
                    ?>
                </div>
<?php
            }
        }
    }
endif;
add_action('faced_breadcrumb', 'faced_breadcrumb_options', 10);


/**
 * BreadCrumb Settings
 */
if (!function_exists('faced_breadcrumbs')) :
    function faced_breadcrumbs()
    {
        $breadcrumb_args = array(
            'container' => 'div',
            'show_browse' => false
        );
        global $faced_theme_options;

        $faced_you_are_here_text = esc_html($faced_theme_options['faced-breadcrumb-text']);


        if (!empty($faced_you_are_here_text)) {
            $faced_you_are_here_text = "<span class='breadcrumb'>" . $faced_you_are_here_text . "</span>";
        }
        echo "<div class='breadcrumbs init-animate clearfix'>" . $faced_you_are_here_text . "<div id='faced-breadcrumbs' class='clearfix'>";
        breadcrumb_trail($breadcrumb_args);
        echo "</div></div>";
    }
endif;
