<?php
/**
 * Basho class.
 *
 */

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

if ( ! class_exists( 'Basho' ) ) :

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

        /**
		 * 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;
			}

			/*
			 * Make theme available for translation.
			 * Translations can be filed in the /languages/ directory.
			 */

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

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

			// Loads wp-content/themes/basho/languages/basho/ru_RU.mo.
			load_theme_textdomain( 'basho', 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.
			$logo_width  = 170;
			$logo_height = 50;

			// If the 'basho_retina_logo' setting is active, double the recommended width and height.
			if ( get_theme_mod( 'basho_retina_logo', false ) ) {
				$logo_width  = floor( $logo_width * 2 );
				$logo_height = floor( $logo_height * 2 );
			}

			add_theme_support(
				'custom-logo',
				array(
					'height'      => $logo_height,
                	'width'       => $logo_width,
					'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(
                'main' 	    => __( 'Main Navigation', 'basho' ),
				'offcanvas' => __( 'Offcanvas Navigation', 'basho' ),
                'social' 	=> __( 'Social Links Menu', 'basho' ),
            );

            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', 'basho' ), $i ),
							'id'          => 'footer-sidebar-' . $i,
							'description' => wp_sprintf( '%s %d %s', __( 'Widgets in this area will be displayed in column number', 'basho' ), $i,  __( 'of the site footer.', 'basho' ) ),
						)
					)
				);
			}

		}

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

			global $basho_version;

            /**
			 * Styles.
			 */
            wp_enqueue_style( 'basho-style', get_stylesheet_uri(), array(), $basho_version );

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

            /**
			 * Fonts.
			 */
			wp_enqueue_style( 'basho-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.9' );
			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.9' );
			wp_script_add_data( 'uikit-icons-js', 'defer', true );

            // Enqueue theme script.
			wp_enqueue_script( 'basho-js', get_theme_file_uri( '/assets/js/basho' . $suffix . '.js' ), array( 'uikit-js' ), $basho_version, true );
			wp_script_add_data( 'basho-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 $basho_version;

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

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

		}

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

			$google_fonts = apply_filters(
				'basho_google_font_families', array(
					'open-sans' => 'Merriweather:400,400i,700,700i',
				)
			);

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

			$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 Basho();
