<?php
/**
 * Eisai class.
 *
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Eisai' ) ) :

	/**
	 * The main Eisai theme class.
	 */
	class Eisai {

        /**
		 * Construct class.
		 */
        public function __construct() {
            add_action( 'after_setup_theme', 			array( $this, 'setup' ) );
			add_action( 'init', 						array( $this, 'menus' ) );
			add_action( 'widgets_init', 				array( $this, 'sidebars' ) );
            add_action( 'wp_enqueue_scripts', 			array( $this, 'scripts' ), 10 );
			add_action( 'enqueue_block_editor_assets', 	array( $this, 'blocks' ), 1, 1 );
        }

        /**
		 * Sets up theme defaults, registers support for various WordPress features.
		 */
        public function setup() {

			// Custom background color.
			add_theme_support( 'custom-background', array( 'default-color' => 'ffffff', ) );

			// Set content-width.
			global $content_width;
			if ( ! isset( $content_width ) ) {
				$content_width = 580;
			}

			/*
			 * Load Localisation files.
			 *
			 * Note: the first loaded translation file overrides
             * any following ones if the same translation is present.
			 */

			// Loads wp-content/languages/themes/eisai-ru_RU.mo.
			load_theme_textdomain( 'eisai', trailingslashit( WP_LANG_DIR ) . 'themes/' );

			// Loads wp-content/themes/eisai-child/languages/eisai/ru-RU.mo.
			load_theme_textdomain( 'eisai', get_stylesheet_directory() . '/languages' );

			// Loads wp-content/themes/eisai/languages/eisai/ru_RU.mo.
			load_theme_textdomain( 'eisai', 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.
            add_theme_support( 'title-tag' );

			// Output valid HTML5 markup for search form, comment form, comments, etc.
			add_theme_support(
				'html5',
				array(
					'search-form',
					'comment-form',
					'comment-list',
					'gallery',
					'caption',
					'script',
					'style',
				)
			);

            // Enable support for Post Thumbnails on posts and pages.
            add_theme_support( 'post-thumbnails' );

			// Custom logo.
			add_theme_support(
				'custom-logo',
				array(
					'height'      => 120,
					'width'       => 80,
					'flex-height' => true,
					'flex-width'  => true,
				)
			);

			// Add support for full and wide align images.
			add_theme_support( 'align-wide' );

			// Add theme support for selective refresh for widgets.
			add_theme_support( 'customize-selective-refresh-widgets' );

			// Add support for responsive embedded content.
			add_theme_support( 'responsive-embeds' );

			// Adds `async` and `defer` support for scripts registered or enqueued by the theme.
			add_filter( 'script_loader_tag', array( $this, 'filter_script_loader_tag' ), 10, 2 );

        }

		/**
		 * Register navigation menus.
		 */
        public function menus() {

			$locations = array(
                'primary' 	=> __( 'Primary Navigation', 'eisai' ),
				'navbar'	=> __( 'Navbar Navigation', 'eisai' ),
                'social' 	=> __( 'Social Links Menu', 'eisai' ),
            );

            register_nav_menus( $locations );

		}

		/**
		 * Register theme sidebars.
		 */
		function sidebars() {

			// Arguments used in all register_sidebar() calls.
			$sidebar_args = array(
				'before_title'  => '<h2 class="widget-title">',
				'after_title'   => '</h2>',
				'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
				'after_widget'  => '</div></div>',
			);

			// Register 4 sidebars.
			for ( $i = 1; $i <= 4; $i++ ) {
				register_sidebar(
					array_merge(
						$sidebar_args,
						array(
							'name'        => wp_sprintf( '%s %d', __( 'Footer', 'eisai' ), $i ),
							'id'          => 'footer-sidebar-' . $i,
							'description' => wp_sprintf( '%s %d %s', __( 'Widgets in this area will be displayed in column number', 'eisai' ), $i,  __( 'of the site footer.', 'eisai' ) ),
						)
					)
				);
			}

		}

        /**
		 * Enqueue scripts and styles.
		 */
		public function scripts() {

			global $eisai_version;

            /**
			 * Styles.
			 */
            wp_enqueue_style( 'eisai-style', get_stylesheet_uri(), array(), $eisai_version );

			/**
			 * Print CSS..
			 */
			wp_enqueue_style( 'eisai-print-style', get_template_directory_uri() . '/print.css', array(), $eisai_version, 'print' );

            /**
			 * Fonts.
			 */
			wp_enqueue_style( 'eisai-fonts', $this->google_fonts(), array(), null );

			/**
			 * Scripts.
			 */
			$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

            // Load the html5 shiv.
        	wp_enqueue_script( 'html5', get_theme_file_uri( '/assets/js/html5' . $suffix . '.js' ) );
        	wp_script_add_data( 'html5', 'conditional', 'lt IE 9' );
			wp_script_add_data( 'html5', 'defer', true );

            // Enqueue Uikit script.
            wp_enqueue_script( 'uikit-js', get_theme_file_uri( '/assets/js/uikit' . $suffix . '.js' ), array(), '3.5.6' );
			wp_script_add_data( 'uikit-js', 'defer', true );
			wp_enqueue_script( 'uikit-icons-js', get_theme_file_uri( '/assets/js/uikit-icons' . $suffix . '.js' ), array( 'uikit-js' ), '3.5.6' );
			wp_script_add_data( 'uikit-icons-js', 'defer', true );

            // Enqueue theme script.
			wp_enqueue_script( 'eisai-js', get_theme_file_uri( '/assets/js/eisai' . $suffix . '.js' ), array( 'uikit-js' ), $eisai_version, true );
			wp_script_add_data( 'eisai-js', 'defer', true );

            // Comment reply script.
            if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        		wp_enqueue_script( 'comment-reply' );
        	}

		}

		/**
		 * Enqueue supplemental block editor styles.
		 */
		public function blocks() {

			global $eisai_version;

			/**
			 * Fonts.
			 */
			wp_enqueue_style( 'eisai-fonts', $this->google_fonts(), array(), null );

			/**
			 * Block editor styles.
			 */
			wp_enqueue_style( 'eisai-block-editor-style', get_theme_file_uri( '/assets/css/editor-style-block.css' ), array(), $eisai_version );

		}

        /**
		 * Load Google Fonts.
		 */
        public function google_fonts() {

			$google_fonts = apply_filters(
				'eisai_google_font_families', array(
					'pt-sans' => 'PT+Sans:400,400i,700,700i',
				)
			);

			$query_args = array(
				'family' => implode( '|', $google_fonts ),
				'subset' => rawurlencode( 'latin,cyrillic' ),
			);

			$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );

			return $fonts_url;

        }

		/**
		 * Adds async/defer attributes to enqueued / registered scripts.
		 *
		 * If #12009 lands in WordPress, this function can no-op since it would be handled in core.
		 *
		 * @link https://core.trac.wordpress.org/ticket/12009
		 *
		 * @param string $tag    The script tag.
		 * @param string $handle The script handle.
		 * @return string Script HTML string.
		 */
        public function filter_script_loader_tag( $tag, $handle ) {

			foreach ( [ 'async', 'defer' ] as $attr ) {
				if ( ! wp_scripts()->get_data( $handle, $attr ) ) {
					continue;
				}
				// Prevent adding attribute when already added in #12009.
				if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) {
					$tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 );
				}
				// Only allow async or defer, not both.
				break;
			}

			return $tag;

        }

    }
endif;

return new Eisai();
