/home/fdhrevqn/public_html/wp-content/themes/fox/inc/admin/import.php
<?php
class Fox_Import {

    /* Construct
    ----------------------------------- */
    function __construct() {
        
        $this->define_demos();
        
        add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 );
        
        /* One click import demo
         * @since 3.0
          */
        if ( ! defined( 'PT_OCDI_PATH' ) ) {
            
            define( 'PT_OCDI_PATH', get_template_directory() . '/inc/admin/import/demo-import/' );
            define( 'PT_OCDI_URL', get_template_directory_uri() . '/inc/admin/import/demo-import/' );
            
            add_filter( 'pt-ocdi/disable_pt_branding', '__return_true' );
            require_once get_parent_theme_file_path( '/inc/admin/import/demo-import/one-click-demo-import.php' );
        }
        
        /**
         * Demo Import
         *
         * @since 3.0
         */
        add_filter( 'pt-ocdi/import_files', array( $this, 'import_files' ) );
        
        // @since 4.2
        // to import sidebar
        // add_action( 'pt-ocdi/before_widgets_import', array( $this, 'before_widgets_import' ) );
        // add_action( 'pt-ocdi/before_content_import', array( $this, 'before_widgets_import' ) );
        
        add_action( 'pt-ocdi/after_import', array( $this, 'after_import_setup' ) );
        
        /**
         * Needed Script
         */
        add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
    
    }
    
    /* Enqueu scripts
    ----------------------------------- */
    function admin_enqueue_scripts( $hook ) {
        
        if ( 'fox-magazine_page_import-demo' === $hook ) {
            
            wp_enqueue_script( 'jquery-ui-dialog' );
            wp_enqueue_style( 'wp-jquery-ui-dialog' );
            wp_enqueue_script( 'fox-import', FOX_ADMIN_URL . 'js/import.js' , array( 'jquery', 'jquery-ui-dialog' ), FOX_VERSION );
            
            $import_js_data = array(
                'ajax_url'         => admin_url( 'admin-ajax.php' ),
                'ajax_nonce'       => wp_create_nonce( 'ocdi-ajax-verification' ),
                'dialog_options'   => [],
                'texts'            => array(
                    'missing_preview_image' => esc_html__( 'No preview image defined for this import.', 'wi' ),
                    'dialog_title'          => esc_html__( 'Are you sure?', 'wi' ),
                    'dialog_no'             => esc_html__( 'Cancel', 'wi' ),
                    'dialog_yes'            => esc_html__( 'Yes, import!', 'wi' ),
                    'dialog_uninstall'      => esc_html__( 'Uninstall', 'wi' ),
                    'uninstall_note'        => 'Uninstall will delete ONLY imported content, widgets and customizer. Your own posts, pages.. will NOT be deleted.',
                    'selected_import_title' => esc_html__( 'Selected demo import:', 'wi' ),
                    'import_settings'       => 'If you only import settings, all content and menus will NOT be imported.',
                ),
            );
            
            if ( ! is_wp_error( $this->demos ) ) {
                $import_js_data[ 'demos' ] = $this->demos;
            }
            
            wp_localize_script( 'fox-import', 'FOX_IMPORT', $import_js_data );
            
        }
        
    }
    
    /* List of demos
    ----------------------------------- */
    function define_demos() {
    
        $url = 'https://foxdata.withemes.com/wp-content/uploads/demos.txt';
        $url2 = 'https://thefox-demo.s3.amazonaws.com/demos.txt';
        
        $cache_time = DAY_IN_SECONDS;
        
        $key = 'fox_demos';
        $body = get_transient( $key );
        $json = [];

        if ( false === $body ) {

            $response = wp_remote_get( esc_url_raw( $url ) );
            if ( is_wp_error( $response ) ) {
                $response = wp_remote_get( esc_url_raw( $url2 ) );
            }
            if ( ! is_wp_error( $response ) ) {

                $body = wp_remote_retrieve_body( $response );
                $json = json_decode( $body, true );

                if ( ! empty( $json ) ) {
                    set_transient( $key , $body, $cache_time );
                }

                
            } else {
                
                $json = new WP_Error( 'remove_get_demos', sprintf( 'Can\'t retreive the demo list from %s.', $url ) );
                
            }
            
        } else {

            $json = json_decode( $body, true );

        }
        
        $this->demos = $json;
    
    }
    
    /* add an admin menu
     * @since 4.0
    ----------------------------------- */
    function admin_menu() {
    
        // add admin page to Appearance
        $hook = add_submenu_page(
            'fox',
            'Import Demo',
            'Import Demo',
            'manage_options',
            'import-demo',
            array( $this, 'create_admin_page')
        );
    
    }
    
    /* Options page callback
     * @since 1.0.0
    ----------------------------------- */
    public function create_admin_page() {
        ?>
     
<div class="wrap">

    <h1>Import Demo Data</h1>
    
    <hr>
    
    <?php 
        
        /* @todo
        $curr_demo = get_theme_mod( 'wi_demo' );
        if ( $curr_demo ) { ?>
    
    <div class="notice notice-info">
        <p>You're currently having <strong><?php echo ucfirst( $curr_demo ); ?> Demo</strong>. To install other demo, you SHOULD unstall this demo first. We strongly discourage installing another demo to overide existing one without uninstalling. <a href="#" class="button button-primary btn-uninstall" data-demo="<?php echo $curr_demo; ?>">Unstall it now >></a></p>
    </div>
            
        <?php } ?>
        */ ?>
    
    <div class="demo-browser">
        
        <div class="message"></div>
        
        <div class="loader loader-import">
            
            <div class="loader-header">
                
                <span class="fox-loading">
                    <i class="dashicons dashicons-update"></i>
                </span>
                <h3>Importing... Please don't leave this page</h3>
            </div>
            
            <div class="loader-msg">
                <p>The heaviest part of importing is Images. You can check importing progress in <a href="<?php echo admin_url( 'upload.php' ); ?>" target="_blank" title="Open in new tab"><strong>Dashboard > Media</strong></a> by checking whether all images have been downloaded or not.</p>
            </div>
            
        </div><!-- .loader -->
        
        <div class="loader loader-uninstall">
            
            <div class="loader-header">
                
                <span class="fox-loading">
                    <i class="dashicons dashicons-update"></i>
                </span>
                <h3>Uninstalling...</h3>
                
            </div>
            
        </div><!-- .loader -->
        
        <?php if ( is_wp_error( $this->demos ) ) { ?>
        
        <div class="message error">
        
            <p><?php echo $this->demos->get_error_message(); ?></p>
        
        </div>
        
        <?php } else { ?>
        
        <div class="demos-wrapper">
            
            <?php $demo = get_theme_mod( 'wi_demo' );
            if ( $demo ) {
                $has_demo = ' has-demo';
            } else {
                $has_demo = '';
            } ?>
            
            <div class="demos wp-clearfix <?php echo $has_demo; ?>">

                <?php 
                          foreach ( $this->demos as $id => $demodata ) : 
                    extract( wp_parse_args( $demodata, [
                        'name' => '',
                        'slug' => '',
                        'image' => '',
                        'preview' => '',
                    ] ) );

                if ( $slug == $demo ) {
                    $name .= ' - Installed';
                }

                ?>

                <div class="demo<?php if ( $slug == $demo ){ echo ' current'; }; ?>" data-demo="<?php echo esc_attr( $slug ); ?>">

                    <div class="demo-inner">

                        <figure class="demo-screenshot">

                            <img src="<?php echo esc_url( $image ); ?>">
                            <a class="wrap-link" target="_blank" href="<?php echo esc_url( $preview ); ?>"></a>

                            <div class="screenshot-loader">
                                <span class="fox-loading">
                                    <i class="dashicons dashicons-update"></i>
                                    <span class="loading-text">Importing...</span>
                                </span>
                            </div>

                            <span class="demo-name"><?php echo $name; ?></span>

                        </figure><!-- .demo-screenshot -->

                        <div class="demo-id-container">

                            <div class="demo-actions">
                                
                                <a class="fox-import-btn" data-import="full" data-slug="<?php echo esc_attr( $slug ); ?>" href="#" title="Import all posts, pages etc" style="text-decoration:none;text-transform:uppercase;">Import Full &rarr;</a>

                                <a class="fox-import-btn" data-import="settings" data-slug="<?php echo esc_attr( $slug ); ?>" href="#" title="Import Only Settings" style="text-decoration:none;color: #777;">Import style</a>

                                <a class="fox-import-btn" data-import="uninstall" data-slug="<?php echo esc_attr( $slug ); ?>" href="#" title="Uninstall">Uninstall</a>

                            </div><!-- .demo-actions -->

                        </div><!-- .demo-id-container -->

                    </div><!-- .demo-inner -->

                </div><!-- .demo -->

                <?php endforeach; ?>

            </div><!-- .demos -->
            
        </div><!-- .demos-wrapper -->
        
        <?php } ?>
        
    </div><!-- .demo-browser -->
    
    <div id="js-ocdi-modal-content"></div>

</div>

<?php
    }
    
    /**
     * [DEPRECATED] since 4.6
     * since we have change fox_sidebars to theme_mod so that we can
     
     * execute after import the content
     * before import the widgets
     * 
     *
     * @since 4.2
     *
    function before_widgets_import( $selected_import ) {
        
        $slug = isset( $selected_import[ 'slug' ] ) ? $selected_import[ 'slug' ] : '';
        
        if ( ! isset( $this->demos[ $slug ] ) ) return;
        
        $demodata = $this->demos[ $slug ];
        
        $custom_sidebars = isset( $demodata[ 'sidebars' ] ) ? $demodata[ 'sidebars' ] : [];
        $custom_sidebars = ( array ) $custom_sidebars;
        
        foreach ( $custom_sidebars as $sidebar => $sidebar_name ) {
        
            fox_add_sidebar([
                'slug' => $sidebar,
                'name' => $sidebar_name,
            ]);
        
        }
        
    }
    */
    
    /* Setup after importing process
     * @since 2.0
     * @improved since 4.0
    ----------------------------------- */
    function after_import_setup( $selected_import ) {
        
        $slug = isset( $selected_import[ 'slug' ] ) ? $selected_import[ 'slug' ] : '';
        
        if ( ! isset( $this->demos[ $slug ] ) ) return;
        
        $demodata = $this->demos[ $slug ];
        
        /**
         * 01 - assign menu
         */
        $nav_menu_locations = [];
        $positions = [
            'primary', 'footer', 'mobile', 'search-menu'
        ];
        
        // Assign menus to their locations.
        foreach ( $positions as $pos ) {
            
            if ( isset( $demodata[ $pos ] ) ) {
                $nav = get_term_by( 'name', $demodata[ $pos ], 'nav_menu' );
                if ( $nav ) {
                    $nav_menu_locations[ $pos ] = $nav->term_id;
                }
            }
            
        }

        if ( ! empty( $nav_menu_locations ) ) {
            set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
        }
        
        /**
         * 02 - assign home page, blog page
         */
        if ( isset( $demodata['homepage'] ) ) {
            
            $frontpage = get_page_by_title( $demodata['homepage'] );
            if ( $frontpage ) {
                update_option( 'show_on_front', 'page' );
                update_option( 'page_on_front', $frontpage->ID );
            }
            
        } else {
            
            update_option( 'show_on_front', 'posts' );
            
        }
        
        /**
         * 03 - set option for current demo
         */
        set_theme_mod( 'wi_demo', $slug );
        
    }
    
    /* Registers import files
     * @since 3.0
    ----------------------------------- */
    function import_files( $files ) {
        
        $files = array();
        
        if ( ! is_wp_error( $this->demos ) ) {
            
            foreach ( $this->demos as $id => $demodata ) {

                $files[ $id ] = [
                    'slug'             => $id,
                    'import_file_name' => $demodata[ 'name' ],
                    'import_file_url' => $demodata[ 'content' ],
                    'import_widget_file_url' => $demodata[ 'widgets' ],
                    'import_customizer_file_url' => $demodata[ 'customizer' ],
                    'import_preview_image_url'      => $demodata[ 'image' ],
                ];

            }
            
        }
        
        return $files;
    
    }

}
new Fox_Import();