<?php
/**
 * The Main Addon
 *
 * @package Hestia
 */

/**
 * Class Hestia_Main_Addon
 */
class Hestia_Main_Addon extends Hestia_Abstract_Main {
	/**
	 * Initialize the main addon.
	 */
	public function init() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'customizer_enqueue' ) );
		add_action( 'customize_preview_init', array( $this, 'enqueue_customizer_script' ) );
		add_action( 'hestia_about_page_array', array( $this, 'filter_about_page_array_pro' ) );
		add_action( 'widgets_init', array( $this, 'initialize_widgets' ) );
		add_action( 'after_switch_theme', array( $this, 'migrate_lite_options' ) );
		add_filter( 'hestia_filter_onboarding_data', array( $this, 'add_remote_demos' ) );
		add_filter( 'hestia_editor_color_palette', array( $this, 'filter_editor_colors_palette' ) );

		add_action( 'themeisle_ob_after_customizer_import', array( $this, 'migrate_big_title_to_slider' ) );
	}

	/**
	 * Migrate big title to slider when importing lite demo to pro.
	 */
	public function migrate_big_title_to_slider() {
		$big_title_content = array(
			'title'       => get_theme_mod( 'hestia_big_title_title' ),
			'text'        => get_theme_mod( 'hestia_big_title_text' ),
			'button_text' => get_theme_mod( 'hestia_big_title_button_text' ),
			'button_link' => get_theme_mod( 'hestia_big_title_button_link' ),
			'background'  => get_theme_mod( 'hestia_big_title_background' ),
		);

		$data = array();

		foreach ( $big_title_content as $index => $value ) {
			if ( empty( $value ) ) {
				continue;
			}
			$data[ $index ] = $value;
		}
		if ( empty( $data ) ) {
			return;
		}

		$slider_setup = array(
			array(
				'id'        => 'customizer_repeater_56d7ea7f40a56',
				'title'     => $data['title'],
				'subtitle'  => $data['text'],
				'text'      => $data['button_text'],
				'link'      => $data['button_link'],
				'image_url' => $data['background'],
			),
		);

		set_theme_mod( 'hestia_slider_content', json_encode( $slider_setup ) );
	}

	/**
	 * Add remote import demos.
	 *
	 * @param array $demos the array of demos defined in Hestia_Public.
	 *
	 * @return array
	 */
	public function add_remote_demos( $demos ) {
		if ( ! isset( $demos['remote'] ) ) {
			$demos['remote'] = array();
		}
		if ( isset( $demos['upsell'] ) ) {
			$demos['upsell'] = array();
		}

		$remote_demos = array(
			'hestia-lawyers'     => array(
				'url'        => 'https://demo.themeisle.com/hestia-lawyers/',
				'screenshot' => 'https://demo.themeisle.com/hestia-pro-demo-content/wp-content/uploads/sites/105/2018/08/hestia-lawyers-demo-screenshot.png',
				'title'      => 'Lawyers Demo',
			),
			'hestia-travel'      => array(
				'url'        => 'https://demo.themeisle.com/hestia-travel/',
				'screenshot' => 'https://demo.themeisle.com/hestia-pro-demo-content/wp-content/uploads/sites/105/2018/08/hestia-travel-demo-screenshot.png',
				'title'      => 'Travel Agency Demo',
			),
			'hestia-coffee-shop' => array(
				'url'        => 'https://demo.themeisle.com/hestia-coffee-shop/',
				'screenshot' => 'https://demo.themeisle.com/hestia-pro-demo-content/wp-content/uploads/sites/105/2018/08/hestia-coffee-shop-demo-screenshot.png',
				'title'      => 'Coffee Shop Demo',
			),
			'hestia-gym'         => array(
				'url'        => 'https://demo.themeisle.com/hestia-gym/',
				'screenshot' => 'https://demo.themeisle.com/hestia-pro-demo-content/wp-content/uploads/sites/105/2018/08/hestia-gym-demo-screenshot.png',
				'title'      => 'Gym Demo',
			),
		);

		$addon_demos = array_merge( $remote_demos, $demos['remote'] );

		$demos['remote'] = $addon_demos;

		return $demos;
	}

	/**
	 * Customizer enqueue scripts.
	 */
	public function customizer_enqueue() {

		wp_enqueue_script( 'hestia_scripts_customizer_pro', HESTIA_ADDONS_URI . 'assets/js/scripts-customizer-pro.js', array( 'jquery' ), HESTIA_VENDOR_VERSION, true );
		$control_settings = array(
			'sections_container' => '#accordion-panel-hestia_frontpage_sections > ul, #sub-accordion-panel-hestia_frontpage_sections',
			'blocked_items'      => '#accordion-section-hestia_slider, #accordion-section-hestia_info_jetpack, #accordion-section-hestia_info_woocommerce, #accordion-section-sidebar-widgets-sidebar-big-title',
		);
		wp_localize_script( 'hestia_scripts_customizer_pro', 'shortcode_settings', $control_settings );
	}

	/**
	 * Handle enqueue.
	 */
	public function enqueue() {
		if ( ! is_front_page() ) {
			return;
		}

		if ( get_option( 'show_on_front' ) !== 'page' ) {
			return;
		}

		// Scroll animations enqueue
		$enable_animations = apply_filters( 'hestia_enable_animations', true );
		if ( $enable_animations ) {
			wp_enqueue_script( 'animate-on-scroll', get_template_directory_uri() . '/inc/addons/assets/js/aos.min.js', HESTIA_VENDOR_VERSION, true );
		}
	}

	/**
	 * Enqueue Customizer Script.
	 */
	public function enqueue_customizer_script() {
		wp_enqueue_script(
			'hestia-pro-customizer-preview',
			get_template_directory_uri() . '/inc/addons/assets/js/customizer.js',
			array(
				'jquery',
			),
			HESTIA_VERSION,
			true
		);
	}

	/**
	 * Function to filter the about page settings
	 *
	 * @param array $config The about page config array.
	 *
	 * @return array
	 */
	public function filter_about_page_array_pro( $config ) {

		unset( $config['custom_tabs']['free_pro'] );
		$config['getting_started']['content'][1]['text']                                = esc_html__( 'Need more details? Please check our full documentation for detailed information on how to use Hestia Pro.', 'hestia-pro' );
		$config['getting_started']['content'][1]['button']['link']                      = 'http://docs.themeisle.com/article/532-hestia-pro-documentation';
		$config['recommended_actions']['plugins']['themeisle-companion']['description'] = __( 'Extend your theme functionality with various modules like Social Media Share Buttons & Icons, custom menu-icons, one click import page templates, page builder addons and free stock featured images.', 'hestia-pro' );
		$config['support']['content'][0]['text']                                        = esc_html__( 'We want to make sure you have the best experience using Hestia Pro and that is why we gathered here all the necessary information for you. We hope you will enjoy using Hestia Pro, as much as we enjoy creating great products.', 'hestia-pro' );
		$config['support']['content'][1]['button']['link']                              = 'http://docs.themeisle.com/article/532-hestia-pro-documentation';

		$old_config['menu_name'] = apply_filters( 'hestia_about_page_filter', __( 'About Hestia Pro', 'hestia-pro' ), 'pro_menu_name' );
		$old_config['page_name'] = apply_filters( 'hestia_about_page_filter', __( 'About Hestia Pro', 'hestia-pro' ), 'pro_page_name' );
		/* translators: s - theme name */
		$old_config['welcome_title']   = apply_filters( 'hestia_about_page_filter', sprintf( __( 'Welcome to %s! - Version ', 'hestia-pro' ), 'Hestia Pro' ), 'pro_welcome_title' );
		$old_config['welcome_content'] = apply_filters( 'hestia_about_page_filter', esc_html__( 'Hestia Pro is a modern WordPress theme for professionals. It fits creative business, small businesses (restaurants, wedding planners, sport/medical shops), startups, corporate businesses, online agencies and firms, portfolios, ecommerce (WooCommerce), and freelancers. It has a multipurpose one-page design, widgetized footer, blog/news page and a clean look, is compatible with: Flat Parallax Slider, Photo Gallery, Travel Map and Elementor Page Builder . The theme is responsive, WPML, Retina ready, SEO friendly, and uses Material Kit for design.', 'hestia-pro' ), 'pro_welcome_content' );

		return $config;
	}

	/**
	 * Register widgets for the theme.
	 *
	 * @since    Hestia 1.0
	 * @modified 1.1.40
	 */
	public function initialize_widgets() {
		/**
		 * Array of all the main sidebars registered in the theme
		 */
		$sidebars_array = array(
			'blog-subscribe-widgets' => esc_html__( 'Blog Subscribe Section', 'hestia-pro' ),
		);

		foreach ( $sidebars_array as $sidebar_id => $sidebar_name ) {
			$sidebar_settings = array(
				'name'          => $sidebar_name,
				'id'            => $sidebar_id,
				'before_widget' => '<div id="%1$s" class="widget %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<h5>',
				'after_title'   => '</h5>',
			);
			if ( $sidebar_id === 'blog-subscribe-widgets' ) {
				$sidebar_settings['before_widget'] = '';
				$sidebar_settings['after_widget']  = '';
			}

			register_sidebar( $sidebar_settings );
		}
	}

	/**
	 * Import lite options.
	 */
	public function migrate_lite_options() {
		$old_theme = strtolower( get_option( 'theme_switched' ) );
		if ( 'hestia' !== $old_theme ) {
			return;
		}

		/* import Hestia options */
		$hestia_mods = get_option( 'theme_mods_hestia' );

		if ( ! empty( $hestia_mods ) ) {

			foreach ( $hestia_mods as $hestia_mod_k => $hestia_mod_v ) {
				set_theme_mod( $hestia_mod_k, $hestia_mod_v );
			}
		}
	}

	/**
	 * Add more colors in the Gutenberg palette.
	 *
	 * @param array $colors The colors theme-support array.
	 *
	 * @return array
	 */
	function filter_editor_colors_palette( $colors ) {

		$new_arr = array(
			3  => array(
				'name'  => esc_html__( 'Secondary', 'hestia-pro' ),
				'slug'  => 'secondary',
				'color' => get_theme_mod( 'secondary_color', '#2d3359' ),
			),
			6  => array(
				'name'  => esc_html__( 'Body', 'hestia-pro' ),
				'slug'  => 'body-color',
				'color' => get_theme_mod( 'body_color', '#999999' ),
			),
			7  => array(
				'name'  => esc_html__( 'Header Overlay', 'hestia-pro' ),
				'slug'  => 'header-overlay-color',
				'color' => get_theme_mod( 'header_overlay_color', 'rgba(0,0,0,0.5)' ),
			),
			8  => array(
				'name'  => esc_html__( 'Header Text', 'hestia-pro' ),
				'slug'  => 'header-text-color',
				'color' => get_theme_mod( 'header_text_color', '#fffffe' ),
			),
			9  => array(
				'name'  => esc_html__( 'Navbar Background', 'hestia-pro' ),
				'slug'  => 'navbar-background',
				'color' => get_theme_mod( 'navbar_background_color', '#fffffd' ),
			),
			10 => array(
				'name'  => esc_html__( 'Navbar Text', 'hestia-pro' ),
				'slug'  => 'navbar-text-color',
				'color' => get_theme_mod( 'navbar_text_color', '#555555' ),
			),
			11 => array(
				'name'  => esc_html__( 'Navbar Text Hover', 'hestia-pro' ),
				'slug'  => 'navbar-text-color-hover',
				'color' => get_theme_mod( 'navbar_text_color_hover', '#e91e63' ),
			),
			12 => array(
				'name'  => esc_html__( 'Transparent Navbar Text Color', 'hestia-pro' ),
				'slug'  => 'navbar-transparent-text-color',
				'color' => get_theme_mod( 'navbar_transparent_text_color', '#fffffc' ),
			),
		);

		$colors = array_merge( $colors + $new_arr );
		ksort( $colors );

		return $colors;
	}
}
