<?php

/**
 * Theme Demo Import.
 *
 * @package Blockst
 */
if ( !defined( 'ABSPATH' ) ) {
    exit( 'Direct script access denied.' );
}
/*
* Demo Import
*/
function blockst_ocdi_import_files() {
    $url = 'https://blockst.deothemes.com';
    return array(array(
        'import_file_name'           => 'Home',
        'import_file_url'            => $url . '/import/01/demo-content.xml',
        'import_widget_file_url'     => $url . '/import/01/widgets.wie',
        'import_customizer_file_url' => $url . '/import/01/customizer.dat',
        'import_preview_image_url'   => $url . '/import/01/preview.jpg',
        'preview_url'                => $url,
    ), array(
        'import_file_name'           => 'Online Store',
        'import_file_url'            => $url . '/import/02/demo-content.xml',
        'import_widget_file_url'     => $url . '/import/02/widgets.wie',
        'import_customizer_file_url' => $url . '/import/02/customizer.dat',
        'import_preview_image_url'   => $url . '/import/02/preview.jpg',
        'preview_url'                => $url . '/online-store',
    ));
}

add_filter( 'pt-ocdi/import_files', 'blockst_ocdi_import_files' );
/**
* Assign menus and front page after demo import
*
* @param array $selected_import array with demo import data
*/
function blockst_ocdi_after_import(  $selected_import  ) {
    global $wp_rewrite;
    // Assign menus to their locations (only the menus that were actually imported).
    $primary_menu = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
    $footer_menu = get_term_by( 'name', 'Footer Bottom Menu', 'nav_menu' );
    $menu_locations = array();
    if ( $primary_menu ) {
        $menu_locations['primary-menu'] = $primary_menu->term_id;
    }
    if ( $footer_menu ) {
        $menu_locations['footer-bottom-menu'] = $footer_menu->term_id;
    }
    set_theme_mod( 'nav_menu_locations', $menu_locations );
    // Delete WooCommerce duplicates
    $pages2 = array(
        'cart',
        'checkout',
        'my-account',
        'wishlist'
    );
    foreach ( $pages2 as $page2 ) {
        $page = get_page_by_path( $page2 . '-2' );
        if ( $page ) {
            wp_delete_post( $page->ID, true );
        }
    }
    $shop2 = get_page_by_path( 'shop-2' );
    if ( $shop2 ) {
        $shop1 = get_page_by_path( 'shop' );
        if ( $shop1 ) {
            wp_delete_post( $shop1->ID, true );
        }
        wp_update_post( [
            'post_name' => 'shop',
            'ID'        => $shop2->ID,
        ] );
    }
    // Assign WooCommerce pages (the Home demo has no shop pages, so skip the missing ones)
    $woo_pages = array(
        'woocommerce_shop_page_id'      => get_page_by_path( 'shop' ),
        'woocommerce_cart_page_id'      => get_page_by_path( 'cart' ),
        'woocommerce_checkout_page_id'  => get_page_by_path( 'checkout' ),
        'woocommerce_myaccount_page_id' => get_page_by_path( 'my-account' ),
    );
    foreach ( $woo_pages as $option_name => $woo_page ) {
        if ( $woo_page ) {
            update_option( $option_name, $woo_page->ID );
        }
    }
    // Assign front page based on demo import
    switch ( $selected_import ) {
        case 1:
            $front_page = blockst_get_page_by_title( 'Home Online Store' );
            break;
        default:
            $front_page = blockst_get_page_by_title( 'Home' );
            break;
    }
    $blog_page = blockst_get_page_by_title( 'Blog' );
    if ( $front_page ) {
        update_option( 'show_on_front', 'page' );
        update_option( 'page_on_front', $front_page->ID );
    }
    if ( $blog_page ) {
        update_option( 'page_for_posts', $blog_page->ID );
    }
    // Omit installing WooCommerce pages
    delete_option( '_wc_needs_pages' );
    delete_transient( '_wc_activation_redirect' );
    // Set permalinks and flush rewrite rules
    $wp_rewrite->set_permalink_structure( '/%postname%/' );
    update_option( "rewrite_rules", FALSE );
    $wp_rewrite->flush_rules( true );
}

add_action( 'pt-ocdi/after_import', 'blockst_ocdi_after_import' );