<?php

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

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

// Inc folder directory
define('NEWSCOPE_INC_DIR', get_template_directory() . '/inc/');
if (function_exists('newscope_fs')) {
    $relative_path = str_replace(get_template_directory(), '', NEWSCOPE_INC_DIR);
    $relative_file_path = $relative_path . basename(__FILE__);
    newscope_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('newscope_fs')) {
        // Create a helper function for easy SDK access.
        function newscope_fs() {
            global $newscope_fs;

            if (!isset($newscope_fs)) {
                // Include Freemius SDK.
                require_once dirname(__FILE__) . '/fs/start.php';

                $newscope_fs = fs_dynamic_init(array(
                    'id'                  => '15880',
                    'slug'                => 'newscope',
                    'premium_slug'        => 'newscope-pro',
                    'type'                => 'theme',
                    'public_key'          => 'pk_79cf4dbf7d788c2de9b3eb14845f4',
                    'is_premium'          => false,
                    'is_premium_only'     => false,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'menu'                => array(
                        'slug'           => 'newscope_options',
                        'account'        => true,
                    ),
                ));
            }

            return $newscope_fs;
        }

        // Init Freemius.
        newscope_fs();
        // Signal that SDK was initiated.
        do_action('newscope_fs_loaded');
    }
}
// newscope Theme Setup Function
function newscope_setup() {
    load_theme_textdomain('newscope', 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(
        'primary'   => esc_html__('Main Menu', 'newscope'),
        'footer'    => esc_html__('Footer Menu', 'newscope'),
    ));
    // 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('newscope_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', 'newscope_setup');
function newscope_content_width() {
    $GLOBALS['content_width'] = apply_filters('newscope_content_width', 1170);
}

add_action('after_setup_theme', 'newscope_content_width', 0);
function newscope_widgets_init() {
    register_sidebar(array(
        'name'          => esc_html__('Sidebar', 'newscope'),
        'id'            => 'sidebar-1',
        'description'   => esc_html__('Add widgets here.', 'newscope'),
        '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', 'newscope'),
        'id'            => 'sidebar-page',
        'description'   => esc_html__('Add widgets here.', 'newscope'),
        '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', 'newscope'),
            'id'            => 'newscope-shop',
            'description'   => esc_html__('Add widgets here.', 'newscope'),
            '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', 'newscope'),
        'id'            => 'footer-1',
        'description'   => esc_html__('Add widgets here.', 'newscope'),
        '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', 'newscope'),
        'id'            => 'footer-2',
        'description'   => esc_html__('Add widgets here.', 'newscope'),
        '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', 'newscope'),
        'id'            => 'footer-3',
        'description'   => esc_html__('Add widgets here.', 'newscope'),
        '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', 'newscope'),
        'id'            => 'footer-4',
        'description'   => esc_html__('Add widgets here.', 'newscope'),
        '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', 'newscope_widgets_init');
/**
 * Enqueue scripts and styles.
 */
function newscope_scripts() {
    //Enqueue Style.
    wp_enqueue_style(
        'fontawesomeall-min',
        get_template_directory_uri() . '/assets/css/fontawesome-min.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'magnific-popup',
        get_template_directory_uri() . '/assets/css/magnific-popup-min.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'aos',
        get_template_directory_uri() . '/assets/css/aos.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'bootstrap',
        get_template_directory_uri() . '/assets/css/bootstrap-min.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    if (is_rtl()) {
        wp_enqueue_style(
            'bootstrap-rtl',
            get_template_directory_uri() . '/assets/css/bootstrap-rtl-min.css',
            array(),
            NEWSCOPE_VERSION,
            'all'
        );
    }
    wp_enqueue_style(
        'swiper',
        get_template_directory_uri() . '/assets/css/swiper-bundle-min.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'animate-min',
        get_template_directory_uri() . '/assets/css/animate-min.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'superfish',
        get_template_directory_uri() . '/assets/css/superfish.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );


    wp_enqueue_style(
        'slicknav-min',
        get_template_directory_uri() . '/assets/css/slicknav-min.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'newscope-typography',
        get_template_directory_uri() . '/assets/css/typography.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'newscope-theme-style',
        get_template_directory_uri() . '/assets/css/style.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'newscope-responsive',
        get_template_directory_uri() . '/assets/css/responsive.css',
        array(),
        NEWSCOPE_VERSION,
        'all'
    );
    wp_enqueue_style(
        'newscope-style',
        get_stylesheet_uri(),
        array(),
        NEWSCOPE_VERSION
    );
    wp_style_add_data('newscope-style', 'rtl', 'replace');


    //Enqueue scripts.
    wp_enqueue_script(
        'bootstrap-bundle-js',
        get_template_directory_uri() . '/assets/js/bootstrap-bundle-min.js',
        array('jquery'),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_script(
        'swiper',
        get_template_directory_uri() . '/assets/js/swiper-bundle.min.js',
        array(),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_script(
        'slicknav-min-js',
        get_template_directory_uri() . '/assets/js/jquery-slicknav-min.js',
        array(),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_script(
        'superfish-min',
        get_template_directory_uri() . '/assets/js/superfish.min.js',
        array(),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_script(
        'isotop-min',
        get_template_directory_uri() . '/assets/js/aos.js',
        array(),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_script(
        'magnific-popup',
        get_template_directory_uri() . '/assets/js/jquery.magnific-popup.min.js',
        array(),
        NEWSCOPE_VERSION,
        true
    );
    
    wp_enqueue_script(
        'newscope-navigation',
        get_template_directory_uri() . '/assets/js/navigation.js',
        array(),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_script(
        'newscope-main',
        get_template_directory_uri() . '/assets/js/main.js',
        array('jquery'),
        NEWSCOPE_VERSION,
        true
    );
    wp_enqueue_style(
        'newscope-fonts',
        newscope_fonts_url(),
        array(),
        null
    );
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}

add_action('wp_enqueue_scripts', 'newscope_scripts');
if (!function_exists('newscope_fonts_url')) {
    function newscope_fonts_url() {
        $fonts_url = '';
        $fonts = array();
        $subsets = 'latin,latin-ext';
        
        // Add first Google Font
        if ('off' !== _x('on', 'Cabin: on or off', 'newscope')) {
            $fonts[] = 'Cabin: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';
        }
        
        // Add second Google Font
        if ('off' !== _x('on', 'Playfair Display: on or off', 'newscope')) {
            $fonts[] = 'Playfair Display:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900';
        }
        
        $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 NEWSCOPE_INC_DIR . 'custom-header.php';
/**
 * Template Functions
 */
require NEWSCOPE_INC_DIR . 'template-functions.php';


/**
 * Customizer additions.
 */
require NEWSCOPE_INC_DIR . 'customizer.php';
/**
 * Kirki Customizer additions.
 */
require NEWSCOPE_INC_DIR . 'kirki/kirki.php';
require NEWSCOPE_INC_DIR . 'kirki-customize/kirki-customizer.php';
require NEWSCOPE_INC_DIR . 'kirki-customize/kirki-custom-functions.php';
require NEWSCOPE_INC_DIR . 'kirki-customize/theme-color.php';
/**
 * Load hooks ini.
 */
require NEWSCOPE_INC_DIR . 'hook/hook-ini.php';
/*
 * Comment Template
 */
require NEWSCOPE_INC_DIR . 'comment-template.php';
/*
 * Custom Metabox Options
 */
require NEWSCOPE_INC_DIR . 'custom-metabox.php';
/*
 * Widget Options
 */
require NEWSCOPE_INC_DIR . 'widgets/widgets.php';
/*
 * TGMPA Plugin
 */
require NEWSCOPE_INC_DIR . 'tgmpa/recommended-plugins.php';

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

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


// Hook the function early in the WordPress initialization process
add_action('init', 'newscope_check_web_stories_plugin_status', 1);

function newscope_check_web_stories_plugin_status() {
    // Ensure the `is_plugin_active` function is available
    if (!function_exists('is_plugin_active')) {
        include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    }

    // Check if the Web Stories plugin is active
    if (is_plugin_active('web-stories/web-stories.php')) {
        // Define the function if it doesn't already exist
        if (!function_exists('newscope_story_cat')) {
            function newscope_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;
            }
        }
    }
}

// Execute the function to ensure the function is defined
newscope_check_web_stories_plugin_status();