<?php

/**
 * Setup theme class.
 *
 * @package Kubelio\Core
 * @since 1.0.0
 */

namespace Kubelio\Core;

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

/**
 * Setup Class
 */
class Setup {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action('after_setup_theme', array($this, 'theme_support'));
		add_action('widgets_init', array($this, 'register_sidebars'));
		add_action('wp_head', array($this, 'theme_color'));
		add_action('tgmpa_register', array($this, 'required_plugins'));
	}

	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 */
	public function theme_support() {
		// Load theme translations. Hosted themes are auto-loaded since WP 4.6,
		// but this keeps translations working off wp.org and silences Theme Check.
		load_theme_textdomain('kubelio', get_template_directory() . '/languages');

		add_theme_support('responsive-embeds');
		add_theme_support('post-thumbnails');
		add_theme_support('title-tag');
		add_theme_support('automatic-feed-links');
		add_theme_support('align-wide');
		add_theme_support('customize-selective-refresh-widgets');

		// Add html5 support.
		add_theme_support('html5', [
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
		]);

		// Register theme menu.
		register_nav_menus(
			array(
				'primary' => esc_html__('Primary Menu', 'kubelio'),
				'footer'  => esc_html__('Footer Menu', 'kubelio'),
				'mobile'  => esc_html__('Mobile Menu', 'kubelio'),
			)
		);

		// Add custom image size.
		add_image_size('featured_medium', 420, 999, false);

		// Hook after setup theme.
		do_action('kubelio_after_setup_theme');
	}

	/**
	 * Register widget areas (sidebars)
	 */
	public function register_sidebars() {
		register_sidebar(
			array(
				'name'          => esc_html__('Sidebar', 'kubelio'),
				'id'            => 'kubelio_sidebar',
				'before_widget' => '<div id="%1$s" class="rt-widget is-widget-aside %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<div class="rt-widget__heading mb-40"><h3 class="rt-widget__title has-large-font-size">',
				'after_title'   => '</h3></div>',
			)
		);

		for ($i = 1; $i <= 6; $i++) {
			register_sidebar(
				array(
					/* translators: %d is the footer column number. */
					'name'          => sprintf(esc_html__('Footer Column %d', 'kubelio'), $i),
					'id'            => 'kubelio_footer_' . $i,
					'before_widget' => '<div id="%1$s" class="rt-widget is-widget-footer %2$s">',
					'after_widget'  => '</div>',
					'before_title'  => '<div class="rt-widget__header mb-40"><h3 class="rt-widget__title has-large-font-size">',
					'after_title'   => '</h3></div>',
				)
			);
		}
	}

	/**
	 * Add theme color brand on search board mobile browser
	 */
	public function theme_color() {
		$color = get_theme_mod('color_browser');

		if (! empty($color)) {
			echo '<meta name="theme-color" content="' . esc_attr($color) . '">';
		}
	}

	/**
	 * Register the required plugins for this theme.
	 */
	public function required_plugins() {

		$plugins = array(
			array(
				'name'     => 'Kubelio Companion',
				'slug'     => 'kubelio-companion',
				'required' => false,
			),
			array(
				'name'     => 'WooCommerce',
				'slug'     => 'woocommerce',
				'required' => false,
			),
			array(
				'name'     => 'Contact Form 7',
				'slug'     => 'contact-form-7',
				'required' => false,
			),

		);

		$config = array(
			'id'           => 'kubelio_tgmpa', // Unique ID for hashing notices for multiple instances of TGMPA.
			'default_path' => '', // Default absolute path to bundled plugins.
			'menu'         => 'tgmpa-install-plugins', // Menu slug.
			'parent_slug'  => 'kubelio-panel', // Parent menu slug.
			'capability'   => 'edit_theme_options', // Capability needed to view plugin install page, should be a capability associated with the parent menu used.
			'has_notices'  => true, // Show admin notices or not.
			'dismissable'  => true, // If false, a user cannot dismiss the nag message.
			'dismiss_msg'  => true, // If 'dismissable' is false, this message will be output at top of nag.
			'is_automatic' => true, // Automatically activate plugins after installation or not.
			'message'      => '', // Message to output right before the plugins table.
		);

		tgmpa($plugins, $config);
	}
}
