<?php 

    /**
     * newscope Site Branding Hook 
     */
    if( ! function_exists( 'newscope_site_branding' ) ) :
        /**
         * Site Branding
         *
         * @since 1.0.0 
         */
        function newscope_site_branding(){
            get_template_part( 'inc/header/header-'.''. get_theme_mod( 'select_header', 'one' ) .'' );
        } 
    endif;
    add_action( 'newscope_action_before_header', 'newscope_site_branding', 10 );


    
    /**
     * newscope Site Logo Hook 
     */
    if( ! function_exists( 'newscope_site_logo_hook' ) ) :
        /**
         * Site Logo
         *
         * @since 1.0.0 
         */
        function newscope_site_logo_hook(){
            if( has_custom_logo() ){
                the_custom_logo();
            } else {
                ?>
                <h2 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h2>
                <?php 
                $newscope_description = get_bloginfo( 'description', 'display' );
                if ( $newscope_description || is_customize_preview() ) :
                    ?>
                        <p class="site-description"><?php echo $newscope_description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
                    <?php
                endif; 
            }
        } 
    endif;
    add_action( 'newscope_action_site_logo', 'newscope_site_logo_hook', 10 );


    /**
     * newscope Footer Contents Hook 
     */
    if ( ! function_exists( 'newscope_footer_contents' ) ) :

        /**
         *  Footer Content
         *
         * @since 1.0.0
         */
        function newscope_footer_contents() {
            get_template_part( 'inc/footer/footer-'.''. get_theme_mod( 'select_footer', 'one' ) .'' );
        }
    endif;
    add_action( 'newscope_action_before_footer', 'newscope_footer_contents', 10 );


    /**
     * newscope breadcrumb Hook 
     */
    if ( ! function_exists( 'newscope_breadcrumb_hook' ) ) :

        /**
         * breadcrumb
         *
         * @since 1.0.0
         */
        function newscope_breadcrumb_hook() {  ?>
            <div class="page-title-wrapper">
                <div class="container">
                    <div class="page-title-inner">
                        <?php newscope_breadcrumb(); ?>
                    </div>
                </div>
            </div>
        <?php  }
    endif;
    add_action( 'newscope_action_breadcrumb', 'newscope_breadcrumb_hook' );
    
    /**
     * newscope Author Section Hook 
     */
    if ( ! function_exists( 'newscope_author_section_hook' ) ) :

        /**
         * breadcrumb
         *
         * @since 1.0.0
         */
        function newscope_author_section_hook() {  ?>
            <div class="author-info-wrapper">
                <div class="author-info-box">
                    <div class="author-image">
                        <?php echo get_avatar( get_the_author_meta( 'ID' )); ?>
                    </div>
                    <div class="author-info">
                        <h2><?php the_author_posts_link(); ?></h2>
                        <p><?php the_author_meta('description'); ?></p>
                        <ul>
                            <?php
                           
                                $newscope_user_cm = wp_get_user_contact_methods();
                                foreach ($newscope_user_cm as $newscope_ucm_key => $newscope_ucm_value) :
                                    $newscope_cm_link = get_user_meta(get_the_author_meta('ID'), $newscope_ucm_key, true);
                                    ?>
                                    <?php if($newscope_cm_link) : ?>
                                        <li>
                                            <a data-toggle="tooltip" data-placement="top" data-original-title="<?php echo esc_attr($newscope_ucm_key) ?>" href="<?php echo esc_url($newscope_cm_link); ?>"><span class="fab fa-<?php echo esc_attr($newscope_ucm_key) ?>"></span>
                                            </a>
                                        </li>
                                    <?php endif; ?>
                                    <?php
                                endforeach;
                                 
                            ?>
                        </ul>
                    </div>
                </div>
            </div>
        <?php  }
    endif;
    add_action( 'newscope_action_author_section', 'newscope_author_section_hook' );

    /**
    * newscope Navication Hook 
    */
    if ( ! function_exists( 'newscope_navication_hook' ) ) :

        /**
        * Navication
        *
        * @since 1.0.0
        */
        function newscope_navication_hook() {  ?>
            <div class="header-menu-wrapper" id="<?php echo esc_attr( true === get_theme_mod( 'sticky_header_enable', false ) ? 'sticky-header' : 'sticky-header-no' ); ?>">
                <div class="container-xxl">
                    <nav class="navbar navbar-expand-lg navbar-light main-navigation" id="site-navigation">
                        <div class="navbar-collapse nav-menu">
                            <?php
                                wp_nav_menu(
                                array(
                                    'container' 		  => false,
                                    'theme_location' 	=> 'primary',
                                    'menu_id'        	=> 'primary-menu',
                                    'menu_class'      => 'sf-menu',
                                    'fallback_cb'     => 'newscope_primary_navigation_fallback',
                                )
                                );
                            ?>
                        </div>

                        <div class="menu-right">
                            <?php if( get_theme_mod( 'header_dark_mode_enalbe', true ) ) : ?>
                                <div class="switch-dark-lite">
                                    <label class="switch" for="darkswitch">
                                    <input type="checkbox" id="darkswitch" class="theme-dark-lite">
                                    <span class="slider"></span>
                                    <input type="hidden" id="hiddenThemeMode" name="theme_mode" value="<?php echo esc_attr( get_theme_mod('theme_mode') ); ?>">
                                    </label>
                                </div>
                            <?php endif; ?>
                            <?php if( get_theme_mod( 'header_search_enable', true ) ) : ?>
                            <div class="header-search-box search-open">
                                <button><i class="fas fa-search"></i></button>
                            </div>
                            <?php endif; ?>
                        </div>
                        <div id="mobile"></div>
                    </nav>
                </div>
            </div>
            <?php if( get_theme_mod( 'header_search_enable', true ) ) : ?>
            <div class="header-search-popup">
                <div class="header-search-popup-content">
                    <form method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                        <span class="screen-reader-text"><?php esc_html_e( 'Search for:', 'newscope' ) ?></span>
                        <input type="search" class="search-field" placeholder="<?php esc_attr_e( 'Search ', 'newscope' ) ?>" value="<?php echo esc_attr(get_search_query()) ?>" name="s" title="<?php esc_attr_e( 'Search for:', 'newscope' ) ?>" />
                        <button type="submit" class="search-submit"><span class="fas fa-search"></span></button>
                    </form>
                </div>
                <div class="header-search-overlay search-open">
                    <div class="closes-button">
                    <button><i class="fas fa-times"></i></button>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        <?php  }
    endif;
    add_action( 'newscope_action_navication_menu', 'newscope_navication_hook' );



    /**
     * BRAKING NEWS HOOK
     */

    if ( ! function_exists( 'newscope_braking_news_hook' ) ) :

        /**
         * BRAKING NEWS
         *
         * @since 1.0.0
         */
        function newscope_braking_news_hook() {  

            if( 'one' === get_theme_mod('sticker_filter_select','one' ) && !empty( get_theme_mod( 'sticker_filter_cat', '' ) )){
                $p = new \WP_Query( array( 
                    'posts_per_page' => get_theme_mod( 'news_sticker_dispaly', 30 ),
                    'post_type' => 'post',
                    'orderby'        => get_theme_mod( 'news_sticker_postby' ),
                    'order'          => get_theme_mod( 'news_sticker_order' ),
                    'tax_query'      => array(
                        array(
                            'taxonomy' => 'category',
                            'terms'    => get_theme_mod( 'sticker_filter_cat' ),
                            'field' => 'term_id',
                        )
                    )
                ));
            }elseif( 'two' === get_theme_mod('sticker_filter_select' ) && !empty( get_theme_mod( 'sticker_filter_tags', '' ) )){
                $p = new \WP_Query( array( 
                    'posts_per_page' => get_theme_mod( 'news_sticker_dispaly', 30 ),
                    'post_type' => 'post',
                    'orderby'        => get_theme_mod( 'news_sticker_postby' ),
                    'order'          => get_theme_mod( 'news_sticker_order' ),
                    'tax_query'      => array(
                        array(
                            'taxonomy' => 'post_tag',
                            'terms'    => get_theme_mod( 'sticker_filter_tags' ),
                            'field' => 'term_id',
                        )
                    )
                ));
            }else{
                $p = new \WP_Query( array( 
                    'posts_per_page' => get_theme_mod( 'news_sticker_dispaly', 30 ),
                    'post_type' => 'post',
                    'orderby'   => get_theme_mod( 'news_sticker_postby' ),
                    'order'     => get_theme_mod( 'news_sticker_order' ),
                ));
            }
        ?>
            <div class="news-tag v9"><a href="#"><?php echo esc_html( get_theme_mod( 'braking_news', 'Braking News' ) ); ?></a></div>
            <div class="marquee">
                <div class="marquee__item">
                    <?php while ( $p->have_posts() ): $p->the_post(); ?>
                    <div class="top-marque-wrapper">
                        <h4 class="list-title"><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php echo newscope_words_limit( get_the_title(), get_theme_mod( 'news_sticker_title_lanth', 5 ) ); ?></a></h4>
                        <div class="list-icon"><i class="fas fa-star"></i></div>
                    </div>
                    <?php endwhile; wp_reset_query(); ?>
                </div>
            </div>
        <?php  }
    endif;
    add_action( 'newscope_action_braking_news', 'newscope_braking_news_hook' );


    /**
     * newscope Top Header Hook 
     */
    if ( ! function_exists( 'newscope_header_top_hook' ) ) :

        /**
         * Top Header
         *
         * @since 1.0.0
         */
        function newscope_header_top_hook() {  ?>
             <div class="top-header">
                <?php if( true === get_theme_mod( 'enable_news_sticker', true )) : ?>
                <div class="top-news-left-content">
                    <?php
                    /**
                    * Hook - newscope_action_braking_news.
                    */
                        do_action( 'newscope_action_braking_news' );
                    ?>
                </div>
                <?php endif; ?>
                <div class="top-news-right-content">
                    <?php if ( true === get_theme_mod( 'header_top_social', true ) ) : ?>
                        <ul class="news-social-links">
                            <?php
                            $social_icons = get_theme_mod('header_social_icons', array());
                            if (empty($social_icons)) {
                                $social_icons = array(
                                    array(
                                        'name'   => esc_html__('Facebook', 'newscope'),
                                        'icon'   => 'fab fa-facebook-f',
                                        'link'   => '#',
                                        'target' => '_self',
                                        'color'  => '#175BEB',
                                    ),
                                    array(
                                        'name'   => esc_html__('Twitter', 'newscope'),
                                        'icon'   => 'fab fa-twitter',
                                        'link'   => '#',
                                        'target' => '_self',
                                        'color'  => '#1DA1F2',
                                    ),
                                    array(
                                        'name'   => esc_html__('Linkedin', 'newscope'),
                                        'icon'   => 'fab fa-linkedin-in',
                                        'link'   => '#',
                                        'target' => '_self',
                                        'color'  => '#0072B1',
                                    ),
                                    array(
                                        'name'   => esc_html__('Instagram', 'newscope'),
                                        'icon'   => 'fab fa-instagram',
                                        'link'   => '#',
                                        'target' => '_self',
                                        'color'  => '#d62976',
                                    ),
                                );
                            }
                            foreach ( $social_icons as $social ) : ?>
                                <li>
                                    <a href="<?php echo esc_url($social['link']); ?>" class="link-icon" title="<?php echo esc_attr($social['name']); ?>" target="<?php echo esc_attr($social['target']); ?>" style="background-color:<?php echo esc_attr($social['color']); ?>"><i class="<?php echo esc_attr($social['icon']); ?>"></i></a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                    <div class="all-news-btn">
                        <?php
                        if( has_nav_menu( 'language-menu' )){
                            wp_nav_menu(
                                array(
                                    'container' 		  => false,
                                    'theme_location' 	=> 'language-menu',
                                    'menu_id'        	=> 'language-menu',
                                    'menu_class'      => 'dropdown-content',
                                )
                            );
                        }?>
                        <?php if( get_theme_mod( 'header_notification', true ) ) : ?>
                            <a href="<?php echo esc_url( get_theme_mod( 'header_notification_link' ) ); ?>" target="<?php echo esc_attr( get_theme_mod( 'header_notification_open_new' ) === true ? '_blank' : '_self' ); ?>" class="news-btn v1"><?php echo esc_html( get_theme_mod( 'header_notification_text', __('Subscribe Now','newscope') ) ); ?></a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php  }
    endif;
    add_action( 'newscope_action_top_header', 'newscope_header_top_hook' );


    /**
     * newscope Top Header Hook 
     */
    if ( ! function_exists( 'newscope_category_image_hook' ) ) :

        /**
         * Category with Image
         *
         * @since 1.0.0
         */
        function newscope_category_image_hook() {  ?>
       
       <div class="header-center-wrapper">
            <div class="update-news">
                <div class="swiper update-post-swiper-slider">
                    <div class="swiper-wrapper">
                    <?php 
                        // Fetch all categories
                        $categories = get_categories();

                        foreach ($categories as $category) {
                            // Fetch the latest post from each category
                            $args = array(
                                'category__in' => array($category->term_id),
                                'posts_per_page' => 1,
                            );
                            $query = new WP_Query($args);

                            if ($query->have_posts()) :
                                while ($query->have_posts()) : $query->the_post();

                                    $category_image = get_category_image($category->term_id);
                                    $category_name = $category->name;
                                    $category_link = get_category_link($category->term_id);
                                    ?>
                                    <div class="swiper-slide">
                                        <div class="update-news-card">
                                        <?php if ($category_image) : ?>
                                            <img src="<?php echo esc_url($category_image); ?>" alt="<?php echo esc_attr($category_name); ?>">
                                        <?php else : ?>
                                            <img src="<?php echo esc_url(get_template_directory_uri() . '/assets/image/update-news/news-img-default.png'); ?>" alt="default-img">
                                        <?php endif; ?>
                                            <div class="card-content">
                                            <a href="<?php echo esc_url($category_link); ?>"><?php echo esc_html($category_name); ?></a>
                                            </div>
                                        </div>
                                    </div>
                                    <?php
                                endwhile;
                                wp_reset_postdata();
                            else :
                                echo '<p>'.esc_html__('No posts found in this category.','newscope').'</p>';
                            endif;
                        }
                    ?>
                    </div>
                </div>
            </div>
        </div>
        <?php  }
    endif;
    add_action( 'newscope_action_cat_with_image', 'newscope_category_image_hook' );


    /**
* Front Banner Section Hook 
*/
if ( ! function_exists( 'front_slider_section_hook' ) ) :

    /**
     * breadcrumb
     *
     * @since 1.0.0
     */
    function front_slider_section_hook() {  ?>
    <?php if( 'one' === get_theme_mod( 'select_news_main_section', 'one' ) ) : ?>
        <div class="hero-slider-wrapper">
            <div class="swiper hero-swiper-slider">
                <div class="swiper-wrapper">
                    <?php do_action( 'newscope_news_main_slider_loop_action' ); ?>
                </div>
                <div class="swiper-button-next"><?php echo esc_html__('Next','newscope'); ?></div>
                <div class="swiper-button-prev"><?php echo esc_html__( 'Prev','newscope' ); ?></div>
                <div class="swiper-pagination"></div>
            </div>
        </div>
    <?php endif; ?>
    <?php  }
endif;
add_action( 'front_slider_section_action', 'front_slider_section_hook' );




/**
* Front Social Link Section Hook 
*/
if ( ! function_exists( 'front_social_item_section_hook' ) ) :

    /**
     * Social Item
     *
     * @since 1.0.0
     */
    function front_social_item_section_hook() {  ?>
    <?php if( true === get_theme_mod( 'social_media_section', true ) ) : ?>
        <div class="subscribe-logo-wrapper">
            <ul>
                <?php
                $social_icons = get_theme_mod('social_media_section_icons', array());
                if (empty($social_icons)) {
                    $social_icons = array(
                        array(
                            'name'   => esc_html__('Facebook', 'newscope'),
                            'icon'   => 'fab fa-facebook-f',
                            'link'   => '#',
                            'target' => '_self',
                        ),
                        array(
                            'name'   => esc_html__('Twitter', 'newscope'),
                            'icon'   => 'fab fa-twitter',
                            'link'   => '#',
                            'target' => '_self',
                        ),
                        array(
                            'name'   => esc_html__('Linkedin', 'newscope'),
                            'icon'   => 'fab fa-linkedin-in',
                            'link'   => '#',
                            'target' => '_self',
                        ),
                        array(
                            'name'   => esc_html__('Instagram', 'newscope'),
                            'icon'   => 'fab fa-instagram',
                            'link'   => '#',
                            'target' => '_self',
                        ),
                    );
                }
                foreach ( $social_icons as $social ) : ?>
                    <li>
                        <a href="<?php echo esc_url($social['link']); ?>" data-aos="fade-right" data-aos-duration="800" data-aos-delay="100" class="link-icon" title="<?php echo esc_attr($social['name']); ?>" target="<?php echo esc_attr($social['target']); ?>">
                        <div class="social-icon">
                            <i class="<?php echo esc_attr($social['icon']); ?>"></i>
                        </div>
                        <?php if( !empty( $social['name'] ) ) : ?>
                        <h4 class="social-title"><?php echo esc_html($social['name']); ?></h4>
                        <?php endif; ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
    <?php  }
endif;
add_action( 'front_social_item_section_action', 'front_social_item_section_hook' );


/**
* Front Instagram Post Section Hook 
*/
if ( ! function_exists( 'front_instagram_post_section_hook' ) ) :

    /**
     * Instagram Post
     *
     * @since 1.0.0
     */
    function front_instagram_post_section_hook() {  ?>
    <?php if( true === get_theme_mod( 'instagram_post_enable' ) ) : ?>
        <div class="subscribe-to-instagram-slider-wrapper">
            <div class="swiper subscribe-to-instagram-slider">
                <div class="swiper-wrapper">
                    <?php 
                    $instagram_post = get_theme_mod('instagram_post_items');
                    foreach( $instagram_post as $item  ) :
                    ?>
                    <div class="swiper-slide">
                        <div class="instagram-card-wrapper">
                            <div class="instagram-img">
                                <img src="<?php echo esc_url( $item['link_image'] ); ?>" >
                                <div class="instagram-icon">
                                    <a href="<?php echo esc_url( $item['link_url'] ); ?>" target="<?php echo esc_attr( $item['link_target'] ); ?>">
                                        <i class="fab fa-instagram-square"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    <?php  }
endif;
add_action( 'front_instagram_post_section_action', 'front_instagram_post_section_hook' );

// demo import 
function newscope_ocdi_import_files() {
    $url = 'https://demo.themeuniver.com/demo-data/newscope/';
    $demo = 'https://newscope.themeuniver.com/';
    return array(
        array(
            'import_file_name'  => esc_html__('Default Demo','newscope' ),
            'categories'        => [ 'Default' ],
            'import_file_url' =>  $url . 'default/demo.xml',
            'import_widget_file_url'  => $url . 'default/widget.wie',
            'import_customizer_file_url'  => $url . 'default/customizer.dat',
            'preview_url'   => $demo,
        ),
    );
}
add_filter( 'ocdi/import_files', 'newscope_ocdi_import_files' );

function ocdi_after_import_setup() {
    // Assign menus to their locations.
    $main_menu = get_term_by( 'name', 'Main Menu', 'nav_menu' );
    $footer_menu = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
    set_theme_mod( 'nav_menu_locations', array(
            'primary' => $main_menu->term_id, // replace 'main-menu' here with the menu location identifier from register_nav_menu() function
            'footer' => $footer_menu->term_id, // replace 'main-menu' here with the menu location identifier from register_nav_menu() function
        )
    );
}
add_action( 'ocdi/after_import', 'ocdi_after_import_setup' );