<?php
/**
 * Manages custom functions and hooks for the entire theme.
 *
 * @package Blogbuster
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/*------------------------------ Header Section ------------------------------*/

    if ( ! function_exists( 'blogbuster_preloader_items' ) ) :

        /**
         * function to manage the requested preloader items
         * 
         * @since 1.0.0
         */
        function blogbuster_preloader_items() {
            $blogbuster_preloader_enable = blogbuster_get_customizer_option_value( 'blogbuster_preloader_enable' );

            if ( false === $blogbuster_preloader_enable ) {
                return;
            }

            $blogbuster_preloader_style = blogbuster_get_customizer_option_value( 'blogbuster_preloader_style' );

    ?>
            <div id="mt-preloader" class="preloader-background">
                <div class="preloader-wrapper">
                    <?php
                        switch ( $blogbuster_preloader_style ) {
                            case 'three_bounce':
                    ?>
                                <div class="mt-three-bounce">
                                    <div class="mt-child mt-bounce1"></div>
                                    <div class="mt-child mt-bounce2"></div>
                                    <div class="mt-child mt-bounce3"></div>
                                </div>
                    <?php
                                break;

                            case 'wave':
                    ?>
                                <div class="mt-wave">
                                    <div class="mt-rect mt-rect1"></div>
                                    <div class="mt-rect mt-rect2"></div>
                                    <div class="mt-rect mt-rect3"></div>
                                    <div class="mt-rect mt-rect4"></div>
                                    <div class="mt-rect mt-rect5"></div>
                                </div>
                    <?php
                                break;

                            case 'folding_cube':
                    ?>
                                <div class="mt-folding-cube">
                                    <div class="mt-cube1 mt-cube"></div>
                                    <div class="mt-cube2 mt-cube"></div>
                                    <div class="mt-cube4 mt-cube"></div>
                                    <div class="mt-cube3 mt-cube"></div>
                                </div>
                    <?php
                                break;

                                case 'ball':
                    ?>
                                <div class="mt-ball">
                                    <div></div>
                                </div>
                    <?php
                                break;
                            
                            default:
                    ?>
                                <div class="mt-three-bounce">
                                    <div class="mt-child mt-bounce1"></div>
                                    <div class="mt-child mt-bounce2"></div>
                                    <div class="mt-child mt-bounce3"></div>
                                </div>
                    <?php
                                break;
                        }
                    ?>
                </div><!-- .preloader-wrapper -->
            </div><!-- #mt-preloader -->
    <?php
        }

    endif;

    add_action( 'blogbuster_before_page', 'blogbuster_preloader_items', 5 );

    if ( ! function_exists( 'blogbuster_custom_header_html' ) ) :

        /**
         * function to get custom header markup
         * 
         * @since 1.0.0
         */
        function blogbuster_custom_header_html() {
            the_custom_header_markup();
        }

    endif;

    add_action( 'blogbuster_header_section', 'blogbuster_custom_header_html', 10 );

    if ( ! function_exists( 'blogbuster_main_header' ) ) :

        /**
         * function to display the main header section at header area.
         * 
         * @since 1.0.0
         */
        function blogbuster_main_header() {
            get_template_part( 'template-parts/header/main', 'header' );
        }

    endif;

    add_action( 'blogbuster_header_section', 'blogbuster_main_header', 20 );

/*------------------------------ Frontpage Section ------------------------------*/
    
    if ( ! function_exists( 'blogbuster_frontpage_featured' ) ) :

        /**
         * Displays the featured section on the front page.
         * 
         * @since 1.0.0
         */
        function blogbuster_frontpage_featured() {
            get_template_part( 'template-parts/frontpage/featured' );
        }

    endif;

    add_action( 'blogbuster_frontpage_section', 'blogbuster_frontpage_featured', 10 );

    if ( ! function_exists( 'blogbuster_frontpage_sticky_posts' ) ) :

        /**
         * Displays the sticky posts section on the front page.
         * 
         * @since 1.0.0
         */
        function blogbuster_frontpage_sticky_posts() {
            get_template_part( 'template-parts/frontpage/sticky', 'posts' );
        }

    endif;

    add_action( 'blogbuster_frontpage_section', 'blogbuster_frontpage_sticky_posts', 20 );

    if ( ! function_exists( 'blogbuster_exclude_sticky_posts_from_index' ) ) :

        /**
         * Removed the sticky post from index page archive list.
         * 
         * @since 1.0.0
         */
        function blogbuster_exclude_sticky_posts_from_index( $query ) {

            $blogbuster_front_sticky_post_enable = blogbuster_get_customizer_option_value( 'blogbuster_front_sticky_post_enable' );

            if ( true === $blogbuster_front_sticky_post_enable ) {

                if ( $query->is_main_query() && ( is_home() || is_front_page() ) ) {
                    $sticky_posts = get_option( 'sticky_posts' );
                    if ( !empty( $sticky_posts ) ) {
                        $query->set( 'post__not_in', $sticky_posts );
                    }
                }
            }
        }

    endif;

    add_action( 'pre_get_posts', 'blogbuster_exclude_sticky_posts_from_index' );

/*------------------------------ Innerpage Single Post -----------------------*/
    
    if ( ! function_exists( 'blogbuster_post_author_box' ) ) :

        /**
         * Displays the author box section on the single post page.
         * 
         * @since 1.0.0
         */
        function blogbuster_post_author_box() {
            get_template_part( 'template-parts/partials/post/author', 'box' );
        }

    endif;

    add_action( 'blogbuster_after_single_post_loop_content', 'blogbuster_post_author_box', 10 );

    if ( ! function_exists( 'blogbuster_related_posts_section' ) ) :

        /**
         * Displays the related posts section on the single post page.
         * 
         * @since 1.0.0
         */
        function blogbuster_related_posts_section() {
            get_template_part( 'template-parts/partials/post/related', 'posts' );
        }

    endif;

    add_action( 'blogbuster_after_single_post_loop_content', 'blogbuster_related_posts_section', 20 );

    if ( ! function_exists( 'blogbuster_archive_title_prefix' ) ) :
        
        /**
         * Archive title prefix
         *
         * @since 1.0.0
         */
        function blogbuster_archive_title_prefix( $title ) {

            $title_prefix_enable = blogbuster_get_customizer_option_value( 'blogbuster_archive_title_prefix_enable' );

            if ( false === $title_prefix_enable ) {
                return preg_replace( '/^\w+: /', '', $title );
            } else {
                return $title;
            }
            
        }

    endif;

    add_filter( 'get_the_archive_title', 'blogbuster_archive_title_prefix' );


/*------------------------------ Footer Section ------------------------------*/

    if ( ! function_exists( 'blogbuster_scroll_top_section' ) ) :

        /**
         * Displays the Scroll Top section in the footer.
         * 
         * @since 1.0.0
         */
        function blogbuster_scroll_top_section() {
            get_template_part( 'template-parts/partials/footer/scroll', 'top' );
        }

    endif;

    add_action( 'blogbuster_after_page', 'blogbuster_scroll_top_section', 10 );
    
    if ( ! function_exists( 'blogbuster_footer_background_animation' ) ) :

        /**
         * Manage the background animation div.
         * 
         * @since 1.0.0
         */
        function blogbuster_footer_background_animation() {
            $blogbuster_background_animation_enable = blogbuster_get_customizer_option_value( 'blogbuster_background_animation_enable' );
            if ( true !== $blogbuster_background_animation_enable ) {
                return;
            }
            echo '<div class="bubbles">
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                        <div class="bubble"></div>
                </div>';
        }

    endif;

    add_action( 'blogbuster_after_page', 'blogbuster_footer_background_animation', 20 );