<?php
// Once Click Demo Importer
function OCDI_Plugin_init()
{
  	if( !class_exists( 'OCDI_Plugin' ) ):
    	require_once plugin_dir_path(__FILE__) . '/importer/one-click-demo-import.php';
  	endif;
}
add_action( 'plugins_loaded', 'OCDI_Plugin_init' );

function ocdi_plugin_page_setup( $default_settings ) {
    $default_settings['parent_slug'] = 'imi-admin-welcome';
    $default_settings['page_title']  = esc_html__( 'One Click Demo Import' , 'blokco-core' );
    $default_settings['menu_title']  = esc_html__( 'Demo Importer' , 'blokco-core' );
    $default_settings['capability']  = 'import';
    $default_settings['menu_slug']   = 'imi-admin-demo-importer';
 
    return $default_settings;
}
add_filter( 'ocdi/plugin_page_setup', 'ocdi_plugin_page_setup' );


function ocdi_import_files() {
  return [
    [
      'import_file_name'             => 'ICO Advisor',
      'categories'                   => [ 'ICO', 'Advisor', 'Personal' ],
      'local_import_file'            => plugin_dir_path(__FILE__) . 'data/demo1/content.xml',
      'local_import_widget_file'     => plugin_dir_path(__FILE__) . 'data/demo1/widgets.json',
      'local_import_customizer_file' => '',
      'local_import_redux'           => [
        [
          'file_path'   => plugin_dir_path(__FILE__) . 'data/demo1/theme-options.json',
          'option_name' => 'blokco_options',
        ],
      ],
      'import_preview_image_url'     => plugin_dir_url(__FILE__) . 'data/demo1/screen-image.jpg',
      'preview_url'                  => 'https://demo1.imithemes.com/ico-advisor/',
    ],
    [
      'import_file_name'             => 'ICO Light',
      'categories'                   => [ 'light', 'ICO' ],
      'local_import_file'            => plugin_dir_path(__FILE__) . 'data/demo2/content.xml',
      'local_import_widget_file'     => plugin_dir_path(__FILE__) . 'data/demo2/widgets.json',
      'local_import_customizer_file' => '',
      'local_import_redux'           => [
        [
          'file_path'   => plugin_dir_path(__FILE__) . 'data/demo2/theme-options.json',
          'option_name' => 'blokco_options',
        ],
      ],
      'import_preview_image_url'     => plugin_dir_url(__FILE__) . 'data/demo2/screen-image.jpg',
      'preview_url'                  => 'https://demo1.imithemes.com/ico-landing1/',
    ],
    [
      'import_file_name'             => 'ICO Dark',
      'categories'                   => [ 'dark', 'ICO' ],
      'local_import_file'            => plugin_dir_path(__FILE__) . 'data/demo3/content.xml',
      'local_import_widget_file'     => plugin_dir_path(__FILE__) . 'data/demo3/widgets.json',
      'local_import_customizer_file' => '',
      'local_import_redux'           => [
        [
          'file_path'   => plugin_dir_path(__FILE__) . 'data/demo3/theme-options.json',
          'option_name' => 'blokco_options',
        ],
      ],
      'import_preview_image_url'     => plugin_dir_url(__FILE__) . 'data/demo3/screen-image.jpg',
      'preview_url'                  => 'https://demo1.imithemes.com/ico-landing1-dark/',
    ],
    [
      'import_file_name'             => 'Blockchain Consulting',
      'categories'                   => [ 'blockchain', 'consulting' ],
      'local_import_file'            => plugin_dir_path(__FILE__) . 'data/demo4/content.xml',
      'local_import_widget_file'     => plugin_dir_path(__FILE__) . 'data/demo4/widgets.json',
      'local_import_customizer_file' => '',
      'local_import_redux'           => [
        [
          'file_path'   => plugin_dir_path(__FILE__) . 'data/demo4/theme-options.json',
          'option_name' => 'blokco_options',
        ],
      ],
      'import_preview_image_url'     => plugin_dir_url(__FILE__) . 'data/demo4/screen-image.jpg',
      'preview_url'                  => 'https://demo1.imithemes.com/blokco/',
    ]
  ];
}
add_filter( 'ocdi/import_files', 'ocdi_import_files' );

function ocdi_register_plugins( $plugins ) {
 
  // List of plugins used by all theme demos.
  $theme_plugins = [
    [ 
      'name' 	=> esc_html__('Redux Framework', 'blokco-core'),
      'slug' 	=> 'redux-framework',
    ],
    [ 
      'name' 	=> esc_html__('Pojo Sidebars', 'blokco-core'),
      'slug' 	=> 'pojo-sidebars',
    ],
    [ 
      'name' 	=> esc_html__('Contact Form 7', 'blokco-core'),
      'slug' 	=> 'contact-form-7',
    ],
    [
      'name'     => 'Breadcrumb NavXT',
      'slug'     => 'breadcrumb-navxt',
    ],
    [
      'name'     => 'Eventer',
      'slug'     => 'eventer',
      'source'   => get_template_directory_uri() . '/framework/tgm/plugins/eventer.zip',
    ],
	[
      'name'     => 'Virtual Coins Widgets VC',
      'slug'     => 'virtual_coin_widgets_vc',
      'source'   => get_template_directory_uri() . '/framework/tgm/plugins/virtual_coin_widgets_vc.zip',
    ],
	[
      'name'     => 'Cryptocurrency Price Ticket Widget',
      'slug'     => 'cryptocurrency-price-ticker-widget',
    ],
    [
      'name'     => 'Revolution Slider',
      'slug'     => 'revslider',
      'source'   => get_template_directory_uri() . '/framework/tgm/plugins/revslider.zip',
    ],
	[
      'name'     => 'WP Bakery Page Builder',
      'slug'     => 'js_composer',
      'source'   => get_template_directory_uri() . '/framework/tgm/plugins/js_composer.zip',
    ],
    [ 
      'name' 	=> esc_html__('WooCommerce - excelling eCommerce', 'blokco-core'),
      'slug' 	=> 'woocommerce',
    ],
  ];
 
  // Check if user is on the theme recommeneded plugins step and a demo was selected.
  if (
    isset( $_GET['step'] ) &&
    $_GET['step'] === 'import' &&
    isset( $_GET['import'] )
  ) {
	// Adding one additional plugin for the first demo import ('import' number = 0).
    if ( $_GET['import'] === '0' ) {
 
      $theme_plugins = [
          [ 
            'name' 	=> esc_html__('Redux Framework', 'blokco-core'),
            'slug' 	=> 'redux-framework',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Contact Form 7', 'blokco-core'),
            'slug' 	=> 'contact-form-7',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Eventer', 'blokco-core'),
            'slug' 	=> 'eventer',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Virtual Coins Widgets VC', 'blokco-core'),
            'slug' 	=> 'virtual_coin_widgets_vc',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Pojo Sidebars', 'blokco-core'),
            'slug' 	=> 'pojo-sidebars',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Breadcrumb NavXT', 'blokco-core'),
            'slug' 	=> 'breadcrumb-navxt',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('WP Bakery Page Builder', 'blokco-core'),
            'slug' 	=> 'js_composer',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('WooCommerce', 'blokco-core'),
            'slug' 	=> 'woocommerce',
            'required'=> false,
            'preselected' => true,
          ],
	  ];
    }
 
    // List of all plugins only used by second demo import [overwrite the list] ('import' number = 1).
    if ( $_GET['import'] === '1' ) {
 
      $theme_plugins = [
          [ 
            'name' 	=> esc_html__('Redux Framework', 'blokco-core'),
            'slug' 	=> 'redux-framework',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Contact Form 7', 'blokco-core'),
            'slug' 	=> 'contact-form-7',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Pojo Sidebars', 'blokco-core'),
            'slug' 	=> 'pojo-sidebars',
            'required'=> false,
            'preselected' => true,
          ],
          [
            'name' 	=> esc_html__('WP Bakery Page Builder', 'blokco-core'),
            'slug' 	=> 'js_composer',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Breadcrumb NavXT', 'blokco-core'),
            'slug' 	=> 'breadcrumb-navxt',
            'required'=> false,
            'preselected' => true,
          ],
	  ];
    }
 
    // List of all plugins only used by second demo import [overwrite the list] ('import' number = 2).
    if ( $_GET['import'] === '2' ) {
 
      $theme_plugins = [
          [ 
            'name' 	=> esc_html__('Redux Framework', 'blokco-core'),
            'slug' 	=> 'redux-framework',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Contact Form 7', 'blokco-core'),
            'slug' 	=> 'contact-form-7',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Pojo Sidebars', 'blokco-core'),
            'slug' 	=> 'pojo-sidebars',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Breadcrumb NavXT', 'blokco-core'),
            'slug' 	=> 'breadcrumb-navxt',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('WP Bakery Page Builder', 'blokco-core'),
            'slug' 	=> 'js_composer',
            'required'=> true,
            'preselected' => true,
          ],
	  ];
    }
 
    // List of all plugins only used by second demo import [overwrite the list] ('import' number = 2).
    if ( $_GET['import'] === '3' ) {
 
      $theme_plugins = [
          [ 
            'name' 	=> esc_html__('Redux Framework', 'blokco-core'),
            'slug' 	=> 'redux-framework',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Contact Form 7', 'blokco-core'),
            'slug' 	=> 'contact-form-7',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Eventer', 'blokco-core'),
            'slug' 	=> 'eventer',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Virtual Coins Widgets VC', 'blokco-core'),
            'slug' 	=> 'virtual_coin_widgets_vc',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Pojo Sidebars', 'blokco-core'),
            'slug' 	=> 'pojo-sidebars',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Breadcrumb NavXT', 'blokco-core'),
            'slug' 	=> 'breadcrumb-navxt',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Cryptocurrency Price Ticket Widget', 'blokco-core'),
            'slug' 	=> 'cryptocurrency-price-ticker-widget',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('Revolution Slider', 'blokco-core'),
            'slug' 	=> 'revslider',
            'required'=> false,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('WP Bakery Page Builder', 'blokco-core'),
            'slug' 	=> 'js_composer',
            'required'=> true,
            'preselected' => true,
          ],
          [ 
            'name' 	=> esc_html__('WooCommerce', 'blokco-core'),
            'slug' 	=> 'woocommerce',
            'required'=> false,
            'preselected' => true,
          ],
	  ];
    }
  }
 
  return array_merge( $plugins, $theme_plugins );
}
add_filter( 'ocdi/register_plugins', 'ocdi_register_plugins' );

function ocdi_after_import( $selected_import ) {
 	if ( 'ICO Advisor' === $selected_import['import_file_name'] ) {
        $main = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
		$top_menu = get_term_by( 'name', 'Top Menu', 'nav_menu' );
		$footer_menu = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
		if ( isset( $main->term_id ) ) {
			set_theme_mod( 'nav_menu_locations', array(
				'primary-menu' => $main->term_id,
				'topbar-menu' => $top_menu->term_id,
				'footer-menu' => $footer_menu->term_id
			));
		}
		// Assign front page and posts page (blog page).
		$front_page_id = get_page_by_title( 'Home' );
		$blog_page_id  = get_page_by_title( 'News Medium Thumbnails' );

		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $front_page_id->ID );
		update_option( 'page_for_posts', $blog_page_id->ID );
		
		// Update Eventer plugin default color
		$eventer_data = get_option('eventer_options');
		$eventer_data['event_default_color'] = '#0DD5A6';
		$eventer_data['eventer_dequeue_carousel_scripts'] = 'on';
		update_option('eventer_options', $eventer_data);
    }
    elseif ( 'ICO Light' === $selected_import['import_file_name'] ) {
        $main = get_term_by( 'name', 'One Page Menu', 'nav_menu' );
		if ( isset( $main->term_id ) ) {
			set_theme_mod( 'nav_menu_locations', array(
				'primary-menu' => $main->term_id,
			));
		}
		// Assign front page and posts page (blog page).
		$front_page_id = get_page_by_title( 'Home' );
		$blog_page_id  = get_page_by_title( 'Blog' );

		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $front_page_id->ID );
		update_option( 'page_for_posts', $blog_page_id->ID );
		
    }
    elseif ( 'ICO Dark' === $selected_import['import_file_name'] ) {
        $main = get_term_by( 'name', 'One Page Menu', 'nav_menu' );
		if ( isset( $main->term_id ) ) {
			set_theme_mod( 'nav_menu_locations', array(
				'primary-menu' => $main->term_id,
			));
		}
		// Assign front page and posts page (blog page).
		$front_page_id = get_page_by_title( 'Home' );
		$blog_page_id  = get_page_by_title( 'Blog' );

		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $front_page_id->ID );
		update_option( 'page_for_posts', $blog_page_id->ID );
    }
    elseif ( 'Blockchain Consulting' === $selected_import['import_file_name'] ) {
        $main = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
		$top_menu = get_term_by( 'name', 'Top Menu', 'nav_menu' );
		$footer_menu = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
		if ( isset( $main->term_id ) ) {
			set_theme_mod( 'nav_menu_locations', array(
				'primary-menu' => $main->term_id,
				'topbar-menu' => $top_menu->term_id,
				'footer-menu' => $footer_menu->term_id
			));
		}
		// Assign front page and posts page (blog page).
		$front_page_id = get_page_by_title( 'Home' );
		$blog_page_id  = get_page_by_title( 'News Medium Thumbnails' );

		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $front_page_id->ID );
		update_option( 'page_for_posts', $blog_page_id->ID );
		
		// Update Eventer plugin default color
		$eventer_data = get_option('eventer_options');
		$eventer_data['event_default_color'] = '#0048a8';
		$eventer_data['eventer_dequeue_carousel_scripts'] = 'on';
		update_option('eventer_options', $eventer_data);
		
		if ( class_exists( 'RevSlider' ) ) {
            $slider_path = plugin_dir_path(__FILE__) . 'data/demo4/slider1.zip';

            if ( file_exists( $slider_path ) ) {
                $slider = new RevSlider();
                $slider->importSliderFromPost( true, true, $slider_path );
            }
      	}
    }
}
add_action( 'ocdi/after_import', 'ocdi_after_import' );