<?php

/**
 * theme init class
 * */
if (!defined('ABSPATH')) {
	exit(); //exit if access directly
}

if (!class_exists('Nextpage_Init')) {

	class Nextpage_Init
	{

		private static $instance;

		public function __construct()
		{
			//theme setup
			add_action('after_setup_theme', array($this, 'theme_setup'));
			//widget init
			add_action('widgets_init', array($this, 'widgets_init'));
			//theme assets
			add_action('wp_enqueue_scripts', array($this, 'theme_assets'));
		}

		/**
		 * getInstance();
		 * @since 1.0.0
		 * */
		public static function getInstance()
		{
			if (null == self::$instance) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * theme setup
		 * @since 1.0.0
		 * */
		public function theme_setup()
		{
			/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on nextpage, use a find and replace
		 * to change 'nextpage' to the name of your theme in all the template files.
		 */
			load_theme_textdomain('nextpage', get_template_directory() . '/languages');

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

			/*
			 * Let WordPress manage the document title.
			 * By adding theme support, we declare that this theme does not use a
			 * hard-coded <title> tag in the document head, and expect WordPress to
			 * provide it for us.
			 */
			add_theme_support('title-tag');

			/*
			 * Enable support for Post Thumbnails on posts and pages.
			 *
			 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
			 */
			add_theme_support('post-thumbnails');

			/*
			* image size
			*/

			add_image_size('nextpage-blog-details', 1155, 592, true);

			add_image_size('nextpage-archive', 365, 365, true);

			// This theme uses wp_nav_menu() in one location.
			register_nav_menus(array(
				'main-menu' => esc_html__('Primary Menu', 'nextpage'),
			));
			// editor style
			add_editor_style();

			/*
			 * Switch default core markup for search form, comment form, and comments
			 * to output valid HTML5.
			 */
			add_theme_support('html5', array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
			));

			// Set up the WordPress core custom background feature.
			add_theme_support('custom-background', apply_filters('nextpage_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 support for core custom logo.
			 *
			 * @link https://codex.wordpress.org/Theme_Logo
			 */
			add_theme_support('custom-logo', array(
				'height'      => 150,
				'width'       => 300,
				'flex-width'  => true,
				'flex-height' => true,
			));

			// This variable is intended to be overruled from themes.
			// Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			$GLOBALS['content_width'] = apply_filters('nextpage_content_width', 640);

			//post formats
			add_theme_support('post-formats', array('image', 'video', 'quote', 'link', 'gallery'));

			//load theme dependency files
			self::include_files();
		}


		/**
		 * widgets_init
		 * @since 1.0.0
		 * */
		public function widgets_init()
		{

			register_sidebar(array(
				'name'          => esc_html__('Sidebar', 'nextpage'),
				'id'            => 'sidebar-1',
				'description'   => esc_html__('Add Sidebar widgets here.', 'nextpage'),
				'before_widget' => '<div id="%1$s" class="widget %2$s widget-border">',
				'after_widget'  => '</div>',
				'before_title'  => '<h5 class="widget-title">',
				'after_title'   => '</h5>',
			));

			register_sidebar(array(
				'name'          => esc_html__('Footer Widget', 'nextpage'),
				'id'            => 'footer-widget-1',
				'description'   => esc_html__('Add Footer  widgets here.', 'nextpage'),
				'before_widget' => '<div class="col-lg-3 col-md-6"><div id="%1$s" class="footer-widget widget %2$s">',
				'after_widget'  => '</div></div>',
				'before_title'  => '<h5 class="widget-title">',
				'after_title'   => '</h5>',
			));
		}

		/**
		 * include files
		 * @since 1.0.0
		 * */
		public function include_files()
		{

			$includes_files = array(
				array(
					'file-name' => 'breadcrumb',
					'file-path' => NEXTPAGE_INC
				),
				array(
					'file-name' => 'class-nextpage-excerpt',
					'file-path' => NEXTPAGE_INC
				),
				array(
					'file-name' => 'comments-modification',
					'file-path' => NEXTPAGE_INC
				),
				array(
					'file-name' => 'class-nextpage-general-hooks',
					'file-path' => NEXTPAGE_INC
				),
				array(
					'file-name' => 'class-nextpage-blog-hooks',
					'file-path' => NEXTPAGE_INC
				),
				array(
					'file-name' => 'customizer',
					'file-path' => NEXTPAGE_INC
				),
				array(
					'file-name' => 'theme-customizer',
					'file-path' => NEXTPAGE_THEME_OPTIONS
				),
				array(
					'file-name' => 'theme-options',
					'file-path' => NEXTPAGE_THEME_OPTIONS
				),
				array(
					'file-name' => 'theme-options-group',
					'file-path' => NEXTPAGE_THEME_OPTIONS
				),
				array(
					'file-name' => 'theme-metabox',
					'file-path' => NEXTPAGE_THEME_OPTIONS
				),
				array(
					'file-name' => 'activate',
					'file-path' => NEXTPAGE_INC . '/plugins/tgma'
				),
				array(
					'file-name' => 'theme-inline-styles',
					'file-path' => NEXTPAGE_THEME_STYLESHEETS
				),
				array(
					'file-name' => 'theme-customize-styles',
					'file-path' => NEXTPAGE_THEME_STYLESHEETS
				),

			);

			if (is_array($includes_files) && !empty($includes_files)) {
				foreach ($includes_files as $file) {
					if (file_exists($file['file-path'] . '/' . $file['file-name'] . '.php')) {
						require_once $file['file-path'] . '/' . $file['file-name'] . '.php';
					}
				}
			}
		}

		/**
		 * theme assets
		 * @since 1.0.0
		 * */
		public function theme_assets()
		{
			self::theme_css();
			self::theme_js();
		}

		/*
		*nextpage load font
		*/
		public static function nextpage_fonts_url()
		{
			$font_url = '';
			if ('off' !== esc_html_x('on', 'Google font: on or off', 'nextpage')) :
				$font_url = add_query_arg(
					array(
						'family' => urlencode('Rubik:400,400i,500,600,700,800display=swap"'),
					),
					"//fonts.googleapis.com/css"
				);
			endif;

			return apply_filters('nextpage_google_font_url', esc_url($font_url));
		}

		/**
		 * theme css
		 * @since 1.0.0
		 * */
		public function theme_css()
		{
			$ver          = NEXTPAGE_VERSION;
			$includes_css = array(
				array(
					'handle' => 'nextpage-font',
					'src'    => self::nextpage_fonts_url(),
					'deps'   => array(),
					'ver'    => $ver,
					'media'  => 'all'
				),
				array(
					'handle' => 'vendor',
					'src'    => NEXTPAGE_CSS . '/vendor.css',
					'deps'   => array(),
					'ver'    => $ver,
					'media'  => 'all'
				),
				array(
					'handle' => 'nextpage-main-style',
					'src'    => NEXTPAGE_CSS . '/style.css',
					'deps'   => array(),
					'ver'    => $ver,
					'media'  => 'all'
				),
				array(
					'handle' => 'nextpage-responsive',
					'src'    => NEXTPAGE_CSS . '/responsive.css',
					'deps'   => array(),
					'ver'    => $ver,
					'media'  => 'all'
				),
			);

			if (is_array($includes_css) && !empty($includes_css)) {
				foreach ($includes_css as $css) {
					call_user_func_array('wp_enqueue_style', $css);
				}
			}


			wp_enqueue_style('nextpage-style', get_stylesheet_uri());
		}

		/**
		 * theme js
		 * @since 1.0.0
		 * */
		public function theme_js()
		{
			$ver         = NEXTPAGE_VERSION;
			$includes_js = array(
				array(
					'handle'    => 'bootstrap',
					'src'       => NEXTPAGE_JS . '/bootstrap.js',
					'deps'      => array('jquery'),
					'ver'       => $ver,
				),
				array(
					'handle'    => 'navigation',
					'src'       => NEXTPAGE_JS . '/navigation.js',
					'deps'      => array('jquery'),
					'ver'       => 1.0,
				),
				array(
					'handle'    => 'nextpage-main-script',
					'src'       => NEXTPAGE_JS . '/main.js',
					'deps'      => array('jquery'),
					'ver'       => 1.0,
				),
			);


			if (is_array($includes_js) && !empty($includes_js)) {
				foreach ($includes_js as $js) {
					call_user_func_array('wp_enqueue_script', $js);
				}
			}


			if (is_singular() && comments_open() && get_option('thread_comments')) {
				wp_enqueue_script('comment-reply');
			}
		}
	} //end class

	Nextpage_Init::getInstance();
}
