<?php

/**
 * newshit functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package newshit
 */

if ( !defined( 'NEWSHIT_VERSION' ) ) {
    $newshit_theme = wp_get_theme();
    define( 'NEWSHIT_VERSION', $newshit_theme->get( 'Version' ) );
}

// Inc folder directory
define( 'NEWSHIT_INC_DIR', get_template_directory() . '/inc/' );
if ( function_exists('newshit_fs')) {

    $relative_path = str_replace(get_template_directory(), '', NEWSHIT_INC_DIR);
    $relative_file_path = $relative_path . basename(__FILE__);
    newshit_fs()->set_basename(false, $relative_file_path);

} else {
	// DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE `function_exists` CALL ABOVE TO PROPERLY WORK.
    if ( ! function_exists( 'newshit_fs' ) ) {
        // Create a helper function for easy SDK access.
        function newshit_fs() {
            global $newshit_fs;
    
            if ( ! isset( $newshit_fs ) ) {
                // Include Freemius SDK.
                require_once dirname(__FILE__) . '/fs/start.php';
    
                $newshit_fs = fs_dynamic_init( array(
                    'id'                  => '12953',
                    'slug'                => 'newshit',
                    'premium_slug'        => 'newshit-pro',
                    'type'                => 'theme',
                    'public_key'          => 'pk_3e1096d1bed4f1c0b99e023dd6748',
                    'is_premium'          => false,
                    'is_premium_only'     => false,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'menu'                => array(
                        'slug'           => 'newshit_options',
                    ),
                ) );
            }
    
            return $newshit_fs;
        }
    
        // Init Freemius.
        newshit_fs();
        // Signal that SDK was initiated.
        do_action( 'newshit_fs_loaded' );
    }
}

// newshit Theme Setup Function
function newshit_setup()
{
    load_theme_textdomain( 'newshit', get_template_directory() . '/languages' );

    // Add default posts and comments RSS feed links to head.
    add_theme_support( 'automatic-feed-links' );

    // Title Tag Support
    add_theme_support( 'title-tag' );

    // Post Thumbnails Support
    add_theme_support( 'post-thumbnails' );

    // This theme uses wp_nav_menu() in one location.
    register_nav_menus( array(
        'headertop' => esc_html__( 'Header Top', 'newshit' ),
        'primary' => esc_html__( 'Primary Menu', 'newshit' ),
        'footer'  => esc_html__( 'Footer Menu', 'newshit' ),
    ) );

    // HTML5 Support
    add_theme_support( 'html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
        'style',
        'script'
    ) );

    // Post formats
    add_theme_support( 'post-formats', array(
        'aside',
        'gallery',
        'link',
        'image',
        'quote',
        'status',
        'video',
        'audio',
        'chat'
    ) );

    // Set up the WordPress core custom background feature.
    add_theme_support( 'custom-background', apply_filters( 'newshit_custom_background_args', array(
        'default-color' => 'ffffff',
        'default-image' => '',
    ) ) );

    // Add theme support for selective refresh for widgets.
    add_theme_support( 'customize-selective-refresh-widgets' );
    add_theme_support( 'custom-logo', array(
        'height'      => 250,
        'width'       => 250,
        'flex-width'  => true,
        'flex-height' => true,
    ) );

    add_theme_support( "responsive-embeds" );

    // Gutenberg
    add_theme_support( 'gutenberg', array(
        'wide-images' => true,
    ) );

    // Align wide
    add_theme_support( 'align-wide' );

    // Block style
    add_theme_support( 'wp-block-styles' );

    // Editor style
    add_theme_support( 'editor-styles' );

    // Editor style css
    add_editor_style( 'assets/css/theme-editor-style.css' );
}

add_action( 'after_setup_theme', 'newshit_setup' );

function newshit_content_width(){
    $GLOBALS['content_width'] = apply_filters( 'newshit_content_width', 1170 );
}

add_action( 'after_setup_theme', 'newshit_content_width', 0 );

function newshit_widgets_init(){
    register_sidebar( array(
        'name'          => esc_html__( 'Sidebar', 'newshit' ),
        'id'            => 'sidebar-1',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'Home Sidebar', 'newshit' ),
        'id'            => 'sidebar-home',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'Page Sidebar', 'newshit' ),
        'id'            => 'sidebar-page',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ) );

    if ( class_exists( 'WooCommerce' ) ) {
        register_sidebar( array(
            'name'          => esc_html__( 'Shop Sidebar', 'newshit' ),
            'id'            => 'newshit-shop',
            'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget'  => '</section>',
            'before_title'  => '<h2 class="widget-title">',
            'after_title'   => '</h2>',
        ) );
    }

    register_sidebar( array(
        'name'          => esc_html__( 'Footer One', 'newshit' ),
        'id'            => 'footer-1',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget footer-widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="widget-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'Footer Two', 'newshit' ),
        'id'            => 'footer-2',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget footer-widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="widget-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'Footer Three', 'newshit' ),
        'id'            => 'footer-3',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget footer-widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="widget-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'Footer Four', 'newshit' ),
        'id'            => 'footer-4',
        'description'   => esc_html__( 'Add widgets here.', 'newshit' ),
        'before_widget' => '<section id="%1$s" class="widget footer-widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="widget-title">',
        'after_title'   => '</h4>',
    ) );

}

add_action( 'widgets_init', 'newshit_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function newshit_scripts() {
	//Enqueue Style.
	wp_enqueue_style( 'bootstrap', get_template_directory_uri().'/assets/bootstrap/bootstrap-min.css', array(), NEWSHIT_VERSION, 'all' );
	if(is_rtl()){
		wp_enqueue_style( 'bootstrap-rtl', get_template_directory_uri().'/assets/bootstrap/bootstrap-rtl-min.css', array(), NEWSHIT_VERSION, 'all' );
	}
	wp_enqueue_style( 'fontawesomeall-min', get_template_directory_uri().'/assets/css/fontawesomeall-min.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'slicknav-min', get_template_directory_uri().'/assets/css/slicknav-min.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'superfish', get_template_directory_uri().'/assets/css/superfish.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'animate-min', get_template_directory_uri().'/assets/css/animate-min.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'slick', get_template_directory_uri().'/assets/slick/slick.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'magnific-popup', get_template_directory_uri().'/assets/css/magnific-popup.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'newshit-typography', get_template_directory_uri().'/assets/css/typography.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'newshit-theme', get_template_directory_uri().'/assets/scss/theme.css', array(), NEWSHIT_VERSION, 'all' );
	wp_enqueue_style( 'newshit-style', get_stylesheet_uri(), array(), NEWSHIT_VERSION );
	wp_style_add_data( 'newshit-style', 'rtl', 'replace' );

	//Enqueue scripts.
	wp_enqueue_script( 'bootstrap-bundle-js', get_template_directory_uri() . '/assets/bootstrap/bootstrap-bundle-min.js', array('jquery'), NEWSHIT_VERSION, true );
	wp_enqueue_script( 'slicknav-min-js', get_template_directory_uri() . '/assets/js/jquery-slicknav-min.js', array(), NEWSHIT_VERSION, true );
	wp_enqueue_script( 'superfish-min', get_template_directory_uri() . '/assets/js/superfish.min.js', array(), NEWSHIT_VERSION, true );
	wp_enqueue_script( 'slick-min', get_template_directory_uri() . '/assets/slick/slick-min.js', array(), NEWSHIT_VERSION, true );
	wp_enqueue_script( 'magnific-popup', get_template_directory_uri() . '/assets/js/jquery.magnific-popup.min.js', array(), NEWSHIT_VERSION, true );
    wp_enqueue_script( 'isotop-min', get_template_directory_uri() . '/assets/js/isotop-min.js', array(), NEWSHIT_VERSION, true );
	wp_enqueue_script( 'newshit-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), NEWSHIT_VERSION, true );
	wp_enqueue_script( 'acmeticker-min', get_template_directory_uri() . '/assets/js/acmeticker-min.js', array(), NEWSHIT_VERSION, true );
    wp_enqueue_script( 'newshit-main', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), NEWSHIT_VERSION, true );
	wp_enqueue_style( 'newshit-fonts', newshit_fonts_url(), array(), null );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'newshit_scripts' );

if ( !function_exists( 'newshit_fonts_url' ) ) {
    function newshit_fonts_url(){
        $fonts_url = '';
        $fonts = array();
        $subsets = 'latin,latin-ext';
        if ( 'off' !== _x( 'on', 'Jost: on or off', 'newshit' ) ) {
            $fonts[] = 'Jost:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600';
        }
        $query_args = array(
            'family' => urlencode( implode( '|', $fonts ) ),
            'subset' => urlencode( $subsets ),
        );
        if ( $fonts ) {
            $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
        }
        return esc_url_raw( $fonts_url );
    }

}

/**
 * Implement the Custom Header feature.
 */
require NEWSHIT_INC_DIR . 'custom-header.php';

/**
 * Template Functions
 */
require NEWSHIT_INC_DIR . 'template-functions.php';


/**
 * Customizer additions.
 */
require NEWSHIT_INC_DIR . 'customizer.php';

/**
 * Kirki Customizer additions.
 */
require NEWSHIT_INC_DIR . 'kirki/kirki.php';
require NEWSHIT_INC_DIR . 'kirki-customize/kirki-customizer.php';
require NEWSHIT_INC_DIR . 'kirki-customize/kirki-custom-functions.php';
require NEWSHIT_INC_DIR . 'kirki-customize/theme-color.php';

/*
 * Customizer Pro
 */
require_once get_template_directory() . '/customize-pro/class-customize.php';


/**
 * Load hooks ini.
 */
require NEWSHIT_INC_DIR . 'hook/hook-ini.php';

/*
 * Comment Template
 */
require NEWSHIT_INC_DIR . 'comment-template.php';

/*
 * Custom Metabox Options
 */
require NEWSHIT_INC_DIR . 'custom-metabox.php';

/*
 * Widget Options
 */
require NEWSHIT_INC_DIR . 'widgets/widgets.php';

/*
 * TGMPA Plugin
 */
require NEWSHIT_INC_DIR . 'tgmpa/recommended-plugins.php';

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
    require NEWSHIT_INC_DIR . 'jetpack.php';
}

/**
 * Load WooCommerce compatibility file.
 */
if ( class_exists( 'WooCommerce' ) ) {
    require get_template_directory() . '/inc/woocommerce.php';
}

function newshit__options_page() {
	add_menu_page(
		__( 'NewsHit Options', 'newshit' ),
		__( 'NewsHit Options', 'newshit' ),
		'manage_options',
		'newshit_options',
		'newshit_render__options_page',
		'dashicons-admin-generic',
		9
	);
}

add_action( 'admin_menu', 'newshit__options_page' );

function newshit_render__options_page() {}

if ( is_plugin_active( 'web-stories/web-stories.php' ) ) {

    function hewshit_story_cat() {
        $options = array();

        $terms = get_terms( array(
            'taxonomy' => 'web_story_category',
            'hide_empty' => false,
        ) );

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                $options[ $term->term_id ] = $term->name;
            }
        }

        return $options;
    };
}