<?php
/**
 * Importer plugin filter.
 *
 * @link https://wordpress.org/plugins/one-click-demo-import/
 *
 * @package Superfast
 */

/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'superfast_ocdi_import_files' ) ) :
	/**
	 * Set one click import demo data. Plugin require is. https://wordpress.org/plugins/one-click-demo-import/
	 *
	 * @since v.1.0.0
	 * @link https://wordpress.org/plugins/one-click-demo-import/faq/
	 *
	 * @return array
	 */
	function superfast_ocdi_import_files() {
		if ( class_exists( 'WooCommerce' ) ) {
			$arr = array(
				array(
					'import_file_name'             => 'Demo Import Default Layout',
					'local_import_file'            => trailingslashit( get_template_directory() ) . 'inc/demo-data/demo-content.xml',
					'local_import_widget_file'     => trailingslashit( get_template_directory() ) . 'inc/demo-data/widgets.json',
					'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'inc/demo-data/customizer.dat',
					'import_preview_image_url'     => 'https://demo.idtheme.com/img/superfast-default-min.jpg',
					'import_notice'                => __( 'Import demo from https://demo.idtheme.com/superfast/.', 'superfast' ),
				),
				array(
					'import_file_name'             => 'Demo Import Masonry Layout',
					'local_import_file'            => trailingslashit( get_template_directory() ) . 'inc/demo-data/demo-content.xml',
					'local_import_widget_file'     => trailingslashit( get_template_directory() ) . 'inc/demo-data/widgets-masonry.json',
					'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'inc/demo-data/customizer-masonry.dat',
					'import_preview_image_url'     => 'https://demo.idtheme.com/img/superfast-masonry-min.jpg',
					'import_notice'                => __( 'Import demo from https://demo.idtheme.com/superfast-masonry/.', 'superfast' ),
				),
				array(
					'import_file_name'             => 'Demo Import Woocommerce Layout',
					'local_import_file'            => trailingslashit( get_template_directory() ) . 'inc/demo-data/demo-content-woo.xml',
					'local_import_widget_file'     => trailingslashit( get_template_directory() ) . 'inc/demo-data/widgets-woo.json',
					'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'inc/demo-data/customizer-woo.dat',
					'import_preview_image_url'     => 'https://demo.idtheme.com/img/superfast-woo-min.jpg',
					'import_notice'                => __( 'Import demo from https://demo.idtheme.com/superfast-woo/.', 'superfast' ),
				),
			);
		} else {
			$arr = array(
				array(
					'import_file_name'             => 'Demo Import Default Layout',
					'local_import_file'            => trailingslashit( get_template_directory() ) . 'inc/demo-data/demo-content.xml',
					'local_import_widget_file'     => trailingslashit( get_template_directory() ) . 'inc/demo-data/widgets.json',
					'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'inc/demo-data/customizer.dat',
					'import_preview_image_url'     => 'https://demo.idtheme.com/img/superfast-default-min.jpg',
					'import_notice'                => __( 'Import demo from https://demo.idtheme.com/superfast/.', 'superfast' ),
				),
				array(
					'import_file_name'             => 'Demo Import Masonry Layout',
					'local_import_file'            => trailingslashit( get_template_directory() ) . 'inc/demo-data/demo-content.xml',
					'local_import_widget_file'     => trailingslashit( get_template_directory() ) . 'inc/demo-data/widgets-masonry.json',
					'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'inc/demo-data/customizer-masonry.dat',
					'import_preview_image_url'     => 'https://demo.idtheme.com/img/superfast-masonry-min.jpg',
					'import_notice'                => __( 'Import demo from https://demo.idtheme.com/superfast-masonry/.', 'superfast' ),
				),
			);
		}
		return $arr;
	}
endif;
add_filter( 'ocdi/import_files', 'superfast_ocdi_import_files' );

if ( ! function_exists( 'superfast_ocdi_before_import_import' ) ) :
	/**
	 * Filter before import content. https://ocdi.com/advanced-integration-guide/
	 *
	 * @since v.1.0.0
	 * @param array $selected_import selection importer data.
	 */
	function superfast_ocdi_before_import_import( $selected_import ) {

		// Find and delete the WP default 'Sample Page'.
		$default_page = get_page_by_title( 'Sample Page' );
		if ( ! empty( $default_page ) ) {
			wp_delete_post( $default_page->ID, $bypass_trash = true );
		}

		// Find and delete the WP default 'Hello world!' post.
		$default_post = get_posts( array( 'title' => 'Hello World!' ) );
		if ( ! empty( $default_post ) ) {
			wp_delete_post( $default_post[0]->ID, $bypass_trash = true );
		}

		/* Remove all widget active from sidebar */
		$null = null;
		update_option( 'sidebars_widgets', $null );
	}
endif;
add_action( 'ocdi/before_content_import', 'superfast_ocdi_before_import_import' );

if ( ! function_exists( 'superfast_ocdi_after_import' ) ) :
	/**
	 * Set action after import demo data. Plugin require is. https://wordpress.org/plugins/one-click-demo-import/
	 *
	 * @since v.1.0.0
	 * @link https://wordpress.org/plugins/one-click-demo-import/faq/
	 * @param array $selected_import selection importer data.
	 *
	 * @return void
	 */
	function superfast_ocdi_after_import( $selected_import ) {

		// Menus to Import and assign - you can remove or add as many as you want.
		$top_menu = get_term_by( 'name', 'Top menus', 'nav_menu' );

		set_theme_mod(
			'nav_menu_locations',
			array(
				'primary' => $top_menu->term_id,
			)
		);

		if ( 'Demo Import Default Layout' === $selected_import['import_file_name'] ) {
			// update option post per page.
			update_option( 'posts_per_page', 7 );
		}
		if ( 'Demo Import Masonry Layout' === $selected_import['import_file_name'] ) {
			// update option post per page.
			update_option( 'posts_per_page', 6 );
		}
		if ( 'Demo Import Woocommerce Layout' === $selected_import['import_file_name'] && class_exists( 'WooCommerce' ) ) {
			// update option post per page.
			update_option( 'posts_per_page', 6 );
			// get page name.
			$homepage = get_page_by_title( 'Shop' );
			if ( $homepage ) {
				update_option( 'page_on_front', $homepage->ID );
				update_option( 'show_on_front', 'page' );
			}
			// get page name.
			$blog = get_page_by_title( 'blog' );
			if ( $blog ) {
				update_option( 'page_for_posts', $blog->ID );
			}
		}

		if ( class_exists( 'Menu_Icons', false ) ) {
			$defaults = array(
				'global' => array(
					'icon_types' => array( 'image' ),
				),
			);

			update_option( 'menu-icons', $defaults );
		}

		if ( class_exists( 'Idblog_Core_Init', false ) ) {
			if ( 'Demo Import Default Layout' === $selected_import['import_file_name'] ) {
				$ads_options = array(
					'ads_topbanner'           => '<img src="https://demo.idtheme.com/img/old/idt-size-72090.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'ads_topbanner_aftermenu' => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-3.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'ads_after_betweenpost'   => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'ads_before_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-336280-2.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'ads_after_content'       => '<img src="https://demo.idtheme.com/img/old/idt-size-336280.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'ads_inside_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'ads_floatbanner_left'    => '',
					'ads_floatbanner_right'   => '',
					'ads_floatbanner_footer'  => '',
					'ads_footerbanner'        => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-2.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
				);

				$ampads_options = array(
					'amp_ads_topbanner'           => '<img src="https://demo.idtheme.com/img/old/idt-size-72090.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'amp_ads_topbanner_aftermenu' => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-3.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'amp_ads_after_betweenpost'   => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'amp_ads_before_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-336280-2.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'amp_ads_after_content'       => '<img src="https://demo.idtheme.com/img/old/idt-size-336280.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'amp_ads_inside_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
				);

				// Update entire array.
				update_option( 'idblg_ads', $ads_options );
				update_option( 'idblg_amp', $ampads_options );
			}
			if ( 'Demo Import Masonry Layout' === $selected_import['import_file_name'] ) {
				$ads_options = array(
					'ads_topbanner'           => '<img src="https://demo.idtheme.com/img/old/idt-size-72090.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'ads_topbanner_aftermenu' => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-3.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'ads_after_betweenpost'   => '',
					'ads_before_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-336280-2.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'ads_after_content'       => '<img src="https://demo.idtheme.com/img/old/idt-size-336280.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'ads_inside_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'ads_floatbanner_left'    => '<img src="https://demo.idtheme.com/img/old/idt-size-side-160600.jpg" alt="banner 160x600" title="banner 160x600" height="600" width="160" />',
					'ads_floatbanner_right'   => '<img src="https://demo.idtheme.com/img/old/idt-size-side-160600.jpg" alt="banner 160x600" title="banner 160x600" height="600" width="160" />',
					'ads_floatbanner_footer'  => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" /><img src="https://demo.idtheme.com/img/old/idt-size-468x60.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'ads_footerbanner'        => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-2.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
				);

				$ampads_options = array(
					'amp_ads_topbanner'           => '<img src="https://demo.idtheme.com/img/old/idt-size-72090.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'amp_ads_topbanner_aftermenu' => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-3.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'amp_ads_after_betweenpost'   => '',
					'amp_ads_before_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-336280-2.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'amp_ads_after_content'       => '<img src="https://demo.idtheme.com/img/old/idt-size-336280.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'amp_ads_inside_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
				);

				// Update entire array.
				update_option( 'idblg_ads', $ads_options );
				update_option( 'idblg_amp', $ampads_options );
			}
			if ( 'Demo Import Woocommerce Layout' === $selected_import['import_file_name'] ) {
				$ads_options = array(
					'ads_topbanner'           => '<img src="https://demo.idtheme.com/img/old/idt-size-72090.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'ads_topbanner_aftermenu' => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-3.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
					'ads_after_betweenpost'   => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'ads_before_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-336280-2.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'ads_after_content'       => '<img src="https://demo.idtheme.com/img/old/idt-size-336280.png" alt="banner 336x280" title="banner 336x280" height="280" width="336" />',
					'ads_inside_content'      => '<img src="https://demo.idtheme.com/img/old/idt-size-46860.jpg" alt="banner 468x60" title="banner 468x60" width="468" height="60" />',
					'ads_floatbanner_left'    => '',
					'ads_floatbanner_right'   => '',
					'ads_floatbanner_footer'  => '',
					'ads_footerbanner'        => '<img src="https://demo.idtheme.com/img/old/idt-size-72090-2.jpg" alt="banner 728x90" title="banner 728x90" height="90" width="728" />',
				);

				// Update entire array.
				update_option( 'idblg_ads', $ads_options );
			}
		}

	}
endif;
add_action( 'ocdi/after_import', 'superfast_ocdi_after_import' );

if ( ! function_exists( 'superfast_change_time_of_single_ajax_call' ) ) :
	/**
	 * Change ajax call timeout
	 *
	 * @link https://github.com/awesomemotive/one-click-demo-import/blob/master/docs/import-problems.md.
	 */
	function superfast_change_time_of_single_ajax_call() {
		return 60;
	}
endif;
add_action( 'ocdi/time_for_one_ajax_call', 'superfast_change_time_of_single_ajax_call' );

// disable generation of smaller images (thumbnails) during the content import.
add_filter( 'ocdi/regenerate_thumbnails_in_content_import', '__return_false' );

// disable the branding notice.
add_filter( 'ocdi/disable_pt_branding', '__return_true' );
