<?php

/**
 * Setup extra stuff for imported demo.
 * Menus, plugins settings, etc.
 */

if ( ! function_exists ( 'is_plugin_active' ) ) {
    require_once ( ABSPATH . 'wp-admin/includes/plugin.php' );
}

if ( !function_exists( 'shrk_wbc_set_plugins_settings' ) ) {
	/**
	 * Setting YITH Magnifier settings
	 */
	function shrk_wbc_set_plugins_settings( $demo_active_import, $demo_directory_path ) {
		// Yith Magnifier
		if ( is_plugin_active( 'yith-woocommerce-zoom-magnifier/init.php' ) ) {
			// Set image size			
			update_option( 'woocommerce_magnifier_image', array(
				'width' => '1200',
				'height' => '1200',
				'crop' => 'on'
				) 
			);

			// Set Zoom box position
			update_option( 'yith_wcmg_zoom_position', 'inside' );

			// Disable thumbnails slider
			update_option( 'yith_wcmg_enableslider', 'no' );
			update_option( 'yith_wcmg_slider_responsive', 'no' );
		}

		// Yith Quickview
		if ( is_plugin_active( 'yith-woocommerce-quick-view/init.php' ) ) {
			// Disable quickview in mobile
			update_option( 'yith-wcqv-enable-mobile', 'no' );

			// Disable lightbox
			update_option( 'yith-wcqv-enable-lightbox', 'no' );
		}
	}
	add_action( 'wbc_importer_after_content_import', 'shrk_wbc_set_plugins_settings', 10, 2 );
}

if ( !function_exists( 'shrk_wbc_set_menus') ) {
	/**
	 * Settings menus locations
	 */
	function shrk_wbc_set_menus( $demo_active_import, $demo_directory_path ) {
		$main_menu   = get_term_by( 'name', 'main_head', 'nav_menu' );
		$topbar_menu = get_term_by( 'name', 'topbar_menu', 'nav_menu' );

		if ( isset( $main_menu->term_id ) && isset( $topbar_menu->term_id ) ) {
			set_theme_mod( 'nav_menu_locations', array(
					'primary'    => $main_menu->term_id,
					'topbar-nav' => $topbar_menu->term_id
				)
			);
		} elseif ( isset( $main_menu->term_id ) ) {
			set_theme_mod( 'nav_menu_locations', array(
					'primary'    => $main_menu->term_id
				)
			);
		}
	}
	add_action( 'wbc_importer_after_content_import', 'shrk_wbc_set_menus', 10, 2 );
}

if ( !function_exists( 'shrk_wbc_set_menus_meta') ) {
	/**
	 * Setting menu item's meta
	 */
	function shrk_wbc_set_menus_meta( $menu_id, $item ) {
		foreach ( $item['postmeta'] as $meta ) {
			if ( $meta['key'] == '_shrk_menu_mmenu_active' && $menu_id ) {
				update_post_meta( $menu_id, '_shrk_menu_mmenu_active', sanitize_key( $meta['value'] ) );
			}

			if ( $meta['key'] == '_shrk_menu_mmenu_fullwidth' && $menu_id ) {
				update_post_meta( $menu_id, '_shrk_menu_mmenu_fullwidth', sanitize_key( $meta['value'] ) );
			}

			if ( $meta['key'] == '_shrk_menu_subtitle' && $menu_id ) {
				update_post_meta( $menu_id, '_shrk_menu_subtitle', sanitize_text_field( $meta['value'] ) );
			}
		}
	}
	add_action( 'importer_nav_update_menu_item', 'shrk_wbc_set_menus_meta', 10, 2 );
}

/**
 * Set / change theme options
 */
function shrk_wbc_redux_options_filter( $data ) {
	if ( isset( $data['site_logo']['url'] ) && strpos( $data['site_logo']['url'], 'uxshop-fashion' ) && isset( $data['footer_widgets_sections'] ) ) {
		// Importing fashion demo.
		$footer_section = shrk_wbc_get_post_by_slug( 'footer-4', 'pagesections' );
		
		if ( !empty( $footer_section ) ) {
			$data['footer_widgets_sections'] = array( strval( $footer_section ) );
		}
	}

	return $data;
}
add_filter( 'radium_theme_import_theme_options', 'shrk_wbc_redux_options_filter' );

/**
 * Import any Revolution slider sliders
 * Processes all .zip files present in demo_directory_path/revsliders
 */
function shrk_wbc_import_revsliders( $demo_active_import, $demo_directory_path ) {
	if ( is_plugin_active( 'revslider/revslider.php' ) && class_exists( 'RevSlider' ) ) {
	    $slider_files = glob( $demo_directory_path . 'revsliders/*.zip' );
	    $slider = new RevSlider();
		
		if ( $slider_files ) {
			foreach( $slider_files as $filepath ) {
				$slider->importSliderFromPost( true, true, $filepath );  
			}
		}
	}
}
add_action( 'wbc_importer_after_content_import', 'shrk_wbc_import_revsliders', 12, 2 );

/**
 * Uses get_page_by_path to return a post object
 * given the slug
 * 
 * @param  string $slug     The slug or path
 * @param  string $posttype The post type
 * @return int              The postID
 */
function shrk_wbc_get_post_by_slug( $slug, $posttype ) {
	$post = get_page_by_path( $slug, OBJECT, $posttype );
	return $post->ID;
}

if ( !function_exists( 'shrk_wbc_set_customizer_options') ) {
	function shrk_wbc_set_customizer_options( $demo_active_import, $demo_directory_path ) {
		if ( shrk_redux_importer_get_demo( $demo_active_import ) == 'fashion' ) {
			// Set custom cropping to 2:3 for fashion demo
			update_option( 'woocommerce_thumbnail_cropping', 'custom' );
			update_option( 'woocommerce_thumbnail_cropping_custom_width', '2' );
			update_option( 'woocommerce_thumbnail_cropping_custom_height', '3' );
		}
	}
	add_action( 'wbc_importer_after_theme_options_import', 'shrk_wbc_set_customizer_options', 10, 2 );
}

?>