<?php
    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;
    
    define('PIXE_REQUEST_DIR', 'https://account.pixebuilder.com/wp-content/uploads/pixedownload');
    define('PIXE_REQUEST_URL', 'https://account.pixebuilder.com/wp-json/pixebuilder/v1/request');
    define('PIXE_REQUEST_CLIENT_KEY', 'yE7Z_t7v5VdiCtKcAQO2uhdA9iRPxBy9WgV7QlxX2nA');

    // daily schedule to check data
    add_action('init', 'pixe_daily_schedule_request');
    function pixe_daily_schedule_request(){
        $live = true;

        if( $live ){
            $current_time = current_time('Y-m-d');
            $last_request = get_option('pixe-daily-schedule-time', '');
            if( $current_time == $last_request ) return;
            if( !is_admin() ) return;
        }
        
        // update plugin info
        pixe_update_plugin_info();

        // do daily schedule action
        do_action('pixe_daily_schedule');
        
        if( $live ){
            // update schedule time
            update_option('pixe-daily-schedule-time', $current_time);
        }
    }

    // retrieve plugin info from json file
    function pixe_get_plugin_info(){
        return get_option('pixe_plugin_info', array());
    }

    // refresh plugin info from server
    function pixe_update_plugin_info(){
        $response = wp_remote_get(PIXE_REQUEST_DIR . '/info.json');
        if( is_wp_error($response) ) return false;

        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);
        if( empty($data['version']) ) return;

        $data = map_deep($data, 'sanitize_text_field');
        update_option('pixe_plugin_info', $data, false);
    }

    // request pre built data
    function pixe_request($data){

        $timestamp = time();
        $domain = wp_parse_url(home_url(), PHP_URL_HOST);
        if( $domain == 'localhost' ){
            $domain .= wp_parse_url(home_url(), PHP_URL_PATH);
        }
        $license_key = apply_filters('pixe_license_key', 'FREE_LICENSE');
        $signature = hash_hmac('sha256', $license_key . '|' . $timestamp, PIXE_REQUEST_CLIENT_KEY);

        $body = array(
            'license_key' => $license_key,
            'timestamp'   => $timestamp,
            'signature'   => $signature,
            'domain'      => $domain,
            'data'        => $data
        );
        $args = array(
            'body'        => wp_json_encode($body),
            'headers'     => array(
                'Content-Type' => 'application/json',
            ),
            'timeout'     => 10,
            'redirection' => 5,
            'httpversion' => '1.1',
            'blocking'    => true,
        );

        $response = wp_remote_post(PIXE_REQUEST_URL, $args);

        // handle error
        if( is_wp_error($response) ){
            return $response; 
        }

        $code = wp_remote_retrieve_response_code($response);
        $body_raw = wp_remote_retrieve_body($response);
        $data = json_decode($body_raw, true);

        // handle error
        if( $code !== 200 ){
            $message = empty($data['message'])? 'Unknown Error : ' . $code: $data['message'];
            return new WP_Error('request_error', $message);
        }

        return $data;
    }

    // ajax copy template
    add_action('wp_ajax_pixe_copy_prebuilt_template', 'pixe_ajax_copy_prebuilt_template');
    function pixe_ajax_copy_prebuilt_template(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('edit_posts') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message')
            )));
        }

        // check data
        if( empty($_POST['id']) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => esc_html__('Invalid Data', 'pixebuilder'),
                'message'=> esc_html__('Please refresh the page and try again.' ,'pixebuilder')
            )));
        }

        $data = pixe_request(array(
            'id' => intval($_POST['id']),
            'type' => 'template',
            'key' => apply_filters('pixe_site_key', '')
        ));

        if( is_wp_error($data) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => esc_html__('Error', 'pixebuilder'),
                'message'=> $data->get_error_message()
            )));
        }else{

            // import menu
            if( !empty($data['menus']) ){
                foreach($data['menus'] as $menu){
                    pixe_import_menu($menu);
                }
            }

            // import additional templates
            $templates_data = array();
            if( !empty($data['templates']) ){
                $templates_data = pixe_import_templates($data['templates']);
            }

            // import main template
            $template_id = pixe_import_template($data);
            $data['id'] = $template_id;
            $template_data = pixe_get_template_list_item($data);

            die(json_encode(array(
                'status' => 'success',
                'template' => $template_data,
                'templates' => $templates_data
            )));
        }
    }

    // ajax get prebuilt content
    add_action('wp_ajax_pixe_get_prebuilt_content', 'pixe_ajax_get_prebuilt_content');
    function pixe_ajax_get_prebuilt_content(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check data
        if( empty($_POST['id']) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => esc_html__('Invalid Data', 'pixebuilder'),
                'message'=> esc_html__('Please refresh the page and try again.' ,'pixebuilder')
            )));
        }

        $data = pixe_request(array(
            'id' => intval($_POST['id']),
            'type' => 'prebuilt-content',
            'key' => apply_filters('pixe_site_key', '')
        ));

        if( is_wp_error($data) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => esc_html__('Error', 'pixebuilder'),
                'message'=> $data->get_error_message()
            )));
        }else{

            if( empty($data['content']) ){
                die(json_encode(array(
                    'status' => 'failed',
                    'head' => esc_html__('An Error Occurred', 'pixebuilder'),
                    'message'=> esc_html__('Content Not Found', 'pixebuilder')
                )));
            }

            // import template if necessary
            $templates_data = array();
            if( !empty($data['templates']) ){
                $templates_data = pixe_import_templates($data['templates']);
            }

            // content
            $content = pixe_update_prebuilt_attachment($data['content']);
            $content = pixe_update_elem_import_data($content);
            
            $ret = array(
                'status' => 'success',
                'type' => 'wrapper',
                'content' => $content
            );

            if( !empty($data['control']) ){
                $ret['control'] = $data['control'];
            }
            if( !empty($templates_data) ){
                $ret['templates'][] = $templates_data;
            }

            die(json_encode($ret));
        }
    }

    // find attachment and update id
    function pixe_upload_image($url){
        global $pixe_attachment_map;
        if( empty($pixe_attachment_map) ) $pixe_attachment_map = array();
        if( !empty($pixe_attachment_map[$url]) ) return $pixe_attachment_map[$url];

        // include necessary files
        require_once(ABSPATH . 'wp-admin/includes/file.php');
        require_once(ABSPATH . 'wp-admin/includes/media.php');
        require_once(ABSPATH . 'wp-admin/includes/image.php');

        // disable size generation
        add_filter('intermediate_image_sizes_advanced', '__return_empty_array');
        
        // prepare files
        $tmp = download_url($url);
        if( is_wp_error($tmp) ) return;
        $file_array = [
            'name' => basename(wp_parse_url($url, PHP_URL_PATH)),
            'tmp_name' => $tmp
        ];
        $attachment_id = media_handle_sideload($file_array);

        if( is_wp_error($attachment_id) ){
            @wp_delete_file($tmp);
            return;
        }

        $pixe_attachment_map[$url] = $attachment_id;

        // return original filter
        remove_filter('intermediate_image_sizes_advanced', '__return_empty_array');

        return $attachment_id;
    }
    function pixe_update_prebuilt_attachment($content){
        if( !is_array($content) ) return $content;

        if( !empty($content['type']) && $content['type'] == 'attachment' ){

            // uploads image to media
            if( !empty($content['url']) ){
                $attachment_id = pixe_upload_image($content['url']);
                if( !empty($attachment_id) ){
                    $attachment = wp_get_attachment_image_src($attachment_id, 'full');
                }
            }
            
            // replace attachment
            if( !empty($attachment) ){
                $content['id'] = $attachment_id;
                $content['url'] = $attachment[0];
                $content['width'] = $attachment[1];
                $content['height'] = $attachment[2];
            }else{
                unset($content['id']);
            }
        
        // recursive through all data
        }else{
            foreach($content as $key => $value){
                $content[$key] = pixe_update_prebuilt_attachment($value);
            }
        }
        return $content;
    }

    // update specific item data
    function pixe_update_elem_dependency($dependency){
        global $pixe_template_map;

        if( !empty($dependency['template']) ){
            $template = array();
            foreach($dependency['template'] as $template_id){
                if( !empty($pixe_template_map[$template_id]) ){
                    $template[] = $pixe_template_map[$template_id];
                }
            }
            $dependency['template'] = $template;
        }
        
        return $dependency;
    }
    function pixe_update_elem_import_data($content){
        $content = pixe_update_prebuilt_attachment($content);
        pixe_update_elem_data($content, 'post-list', ['layout', 'template'], 'template');
        pixe_update_elem_data($content, 'post-list', ['layout', 'include'], array());
        pixe_update_elem_data($content, 'icon', ['link', 'popup'], 'template');
        pixe_update_elem_data($content, 'hamburger-button', ['link', 'popup'], 'template');
        pixe_update_elem_data($content, 'wc-cart-icon', ['link', 'popup'], 'template');
        return $content;
    }
    function pixe_update_elem_data(&$content, $element, $keys, $data){
        if( !is_array($content) ) return;

        if( !empty($content['_type']) && $content['_type'] == 'element' && 
            !empty($content['_element']) && $content['_element'] == $element ){
            
            // get ref variable to target value
            $ref =& $content;
            foreach($keys as $key){
                if( empty($ref[$key]) ) $ref[$key] = array();
                $ref =& $ref[$key];
            }

            // replace data
            if( $data == 'template' ){
                global $pixe_template_map;
                if( !empty($ref) && !empty($pixe_template_map[$ref]) ){
                    $ref = $pixe_template_map[$ref];
                }
            }else{
                $ref = $data;
            }

        // recursive through all data
        }else{
            foreach($content as $key => $value){
                pixe_update_elem_data($content[$key], $element, $keys, $data);
            }
        }
    }

    // pixe import site
    function pixe_import_site($data, $options = array()){
        global $pixe_template_map, $pixe_post_map;

        $site_settings = $data['settings'];
        $term_slug = $data['slug'];
        $term_title = $data['title'];

        // controls
        if( in_array('controls', $options) && !empty($data['controls']) ){
            update_option('pixe_controls', $data['controls'], true);
        }

        // typo
        if( in_array('typo', $options) && !empty($data['typo']) ){
            update_option('pixe_typo', $data['typo'], true);
        }

        // color
        if( in_array('color', $options) && !empty($data['color']) ){
            $colors = get_option('pixe_colors', array());
            if( empty($colors) ){
                $colors = pixe_get_default_colors();
            }

            $colors['active-preset'] = $term_slug;
            $colors['preset'][$term_slug] = $data['color'];
            $colors['preset'][$term_slug]['label'] = $term_title;
            if( !in_array($term_slug, $colors['presetOrder']) ){
                $colors['presetOrder'][] = $term_slug;
            }
            
            update_option('pixe_colors', $colors);
        }

        
        if( in_array('template', $options) && !empty($data['templates']) ){

            // import template parts
            $subtypes = array('synced_block', 'post_template', 'taxonomy_template', 'popup', 'header', 'footer');
            foreach($subtypes as $subtype){
                foreach($data['templates'] as $template){
                    if( !empty($template['pixe_type']) && $template['pixe_type'] == 'template_part' && 
                        !empty($template['pixe_subtype']) && $template['pixe_subtype'] == $subtype ){
                        pixe_import_template($template);
                    }
                }
            }
        
            // import templates
            foreach($data['templates'] as $template){
                if( !empty($template['pixe_type']) && $template['pixe_type'] == 'template' ){
                    pixe_import_template($template);
                }
            }
        }

        // import post
        if( in_array('post', $options) ){

            // create category
            if( !empty($data['post-category']) ){
                foreach($data['post-category'] as $cat) {
                    $term = get_term_by('slug', $cat['slug'], 'category');

                    if( empty($term) ){
                        $result = wp_insert_term($cat['title'], 'category', array('slug' => $cat['slug']));
                    }
                }
            }

            // insert all posts
            if( !empty($data['posts']) ){
                foreach($data['posts'] as $page){
                    pixe_import_post($page, 'post');
                }
            }
        }

        // import pages
        if( in_array('page', $options) && !empty($data['pages']) ){
            foreach($data['pages'] as $page){
                pixe_import_post($page);
            }

            // assign single page template
            if( !empty($site_settings['single-custom-page-template']) ){
                if( !empty($pixe_template_map[$site_settings['single-custom-page-template']]) ){
                    pixe_set_template_area('page', $pixe_template_map[$site_settings['single-custom-page-template']]);
                }
            }

            // assign woocommerce pages
            if( !empty($site_settings['cart-page']) ){
                if( !empty($pixe_post_map[$site_settings['cart-page']]) ){
                    update_option('woocommerce_cart_page_id', $pixe_post_map[$site_settings['cart-page']]);
                }
            }
            if( !empty($site_settings['checkout-page']) ){
                if( !empty($pixe_post_map[$site_settings['checkout-page']]) ){
                    update_option('woocommerce_checkout_page_id', $pixe_post_map[$site_settings['checkout-page']]);
                }
            }
            if( !empty($site_settings['account-page']) ){
                if( !empty($pixe_post_map[$site_settings['account-page']]) ){
                    update_option('woocommerce_myaccount_page_id', $pixe_post_map[$site_settings['account-page']]);
                }
            }
            if( !empty($site_settings['purchase-summary-page']) ){
                if( !empty($pixe_post_map[$site_settings['purchase-summary-page']]) ){
                    update_option('woocommerce_order_summary_page_id', $pixe_post_map[$site_settings['purchase-summary-page']]);
                }
            }
        }

        // product
        if( in_array('product', $options) && !empty($data['products']) && function_exists('wc_get_product') ){
            foreach($data['products'] as $page){
                pixe_import_post($page, 'product');
            }
            
            // assign single product tempalte
            if( !empty($site_settings['single-product-template']) ){
                if( !empty($pixe_template_map[$site_settings['single-product-template']]) ){
                    pixe_set_template_area('single-product', $pixe_template_map[$site_settings['single-product-template']]);
                }
            }
        }

        // set as front page
        if( in_array('front-page', $options) && !empty($site_settings['home-page']) ){
            if( !empty($pixe_post_map[$site_settings['home-page']]) ){
                $front_page_id = $pixe_post_map[$site_settings['home-page']];
                update_option('show_on_front', 'page');
                update_option('page_on_front', $front_page_id);
            }
        }

        // menu
        if( in_array('menu', $options) && !empty($data['menus']) ){
            foreach($data['menus'] as $menu){
                pixe_import_menu($menu);
            }
        }
        
    }
    function pixe_import_templates($templates){
        $templates_data = array();

        if( !empty($templates) ){
            foreach($templates as $template){
                $template_id = pixe_import_template($template);

                $template['id'] = $template_id;
                $template_data = pixe_get_template_list_item($template);
                $template_data['pixe_type'] = empty($template['pixe_type'])? '': $template['pixe_type'];
                $template_data['pixe_subtype'] = empty($template['pixe_subtype'])? '': $template['pixe_subtype'];
                $templates_data[] = $template_data;
            }
        }

        return $template_data;
    }

    function pixe_import_template($template){
        global $pixe_template_map;
   
        $currentuser = wp_get_current_user();
        $currenttime = current_time('mysql', true);

        $template_id = pixe_set_template_data(array(
            'author' => $currentuser->ID,
            'date_utc' => $currenttime,
            'pixe_type' => $template['pixe_type'],
            'pixe_subtype' => $template['pixe_subtype'],
            'title' => $template['title']
        ));

        $template_settings = empty($template['settings'])? array(): json_decode($template['settings'], true);
        unset($template_settings['admin']);
        $template_settings = pixe_update_prebuilt_attachment($template_settings);
        
        // update parts id for template
        if( $template['pixe_type'] == 'template' && !empty($template_settings['template']) ){
            $parts = array('header', 'tablet-header', 'mobile-header', 'footer', 'tablet-footer', 'mobile-footer');
            foreach($parts as $part){
                if( !empty($template_settings['template'][$part]) ){
                    $old_template = $template_settings['template'][$part];
                    if( !empty($pixe_template_map[$old_template]) ){
                        $template_settings['template'][$part] = $pixe_template_map[$old_template];
                    }
                }
            }
        }

        $template_dependency = empty($template['dependency'])?  array(): json_decode($template['dependency'], true);
        $template_dependency = pixe_update_elem_dependency($template_dependency);

        $style = empty($template['style'])?  '': pixe_update_template_part_id($template['style'], $template['pixe_subtype'], $template['id'], $template_id);
        $content = empty($template['content'])?  '': pixe_update_template_part_id($template['content'], $template['pixe_subtype'], $template['id'], $template_id);
        $content = json_decode($content, true);
        $content = pixe_update_elem_import_data($content);

        pixe_set_template_data(array(
            'id' => $template_id,
            'settings' => $template_settings,
            'dependency' => $template_dependency,
            'content' => $content,
            'style' => $style
        ));

        // add to template map
        if( empty($pixe_template_map) ){
            $pixe_template_map = array();
        }
        if( !empty($template_id) ){
            $pixe_template_map[$template['id']] = $template_id;
        }

        return $template_id;
    }
    function pixe_import_menu($menu){
        global $pixe_menu_map, $pixe_menu_parent_backfill, $pixe_post_map;
        
        // initialize
        if( empty($pixe_menu_map) ){
            $pixe_menu_map = array();
        }
        if( empty($pixe_menu_parent_backfill) ){
            $pixe_menu_parent_backfill = array();
        }

        // import menu
        $menu_slug = $menu['slug'];
        $menu_name = $menu['title'];

        // if menu exists ignore
        $current_menu = get_term_by('slug', $menu_slug, 'nav_menu');
        if( !empty($current_menu) ){
            return;
        } 
        
        // create menu if not exists
        $menu_id = wp_create_nav_menu($menu_name);

        if( !is_wp_error($menu_id) ){
            wp_update_term($menu_id, 'nav_menu', array('slug' => $menu_slug));
            
            // import menu one by one
            if( !empty($menu['items']) ){
                foreach($menu['items'] as $menu_item){
                    $menu_object = array(
                        'menu-item-title'=> $menu_item['title'],
                        'menu-item-type'=> $menu_item['type'],
                        'menu-item-status' => 'publish',
                    );
                    switch($menu_item['type']){
                        case 'custom':
                            if( !empty($menu_item['url']) ){
                                $menu_object['menu-item-url'] = $menu_item['url'];
                            }
                            break;
                        case 'post_type':
                            if( !empty($menu_item['object_id']) && !empty($pixe_post_map[$menu_item['object_id']]) ){
                                $post_id = $pixe_post_map[$menu_item['object_id']];
                                $post_type = get_post_type($post_id);
                                $menu_object['menu-item-object'] = $post_type;
                                $menu_object['menu-item-object-id'] = $post_id;
                            }
                            break;
                        case 'taxonomy':
                            $menu_object['menu-item-object'] = $menu_item['object'];
                            $menu_object['menu-item-object-id'] = $menu_item['object_id']; // taxonomy id
                            break;
                    }

                    $menu_item_id = wp_update_nav_menu_item($menu_id, 0, $menu_object);
                    $pixe_menu_map[$menu_item['id']] = $menu_item_id;

                    // add backfill if has parent
                    if( !empty($menu_item['parent']) ){
                        $pixe_menu_parent_backfill[$menu_item_id] = $menu_item['parent'];
                    }
                }
            }
        }

    }
    function pixe_import_post($page, $post_type = 'page'){
        global $pixe_template_map, $pixe_post_map;

        $page_data = array(
            'post_title' => $page['title'],
            'post_content' => '',
            'post_status' => 'publish',
            'post_type' => $post_type,
            'post_author'  => 1,
        );
        $post_id = wp_insert_post($page_data);

        if( empty($post_id) || is_wp_error($post_id) ) return false;

        // set feature image
        if( !empty($page['featured-image']) ){
            $image_id = pixe_upload_image($page['featured-image']);
            if( !empty($image_id) ){
                set_post_thumbnail($post_id, $image_id);
            }
        }
        
        // set category of post
        $taxonomies = array('category');
        foreach($taxonomies as $taxonomy){
            if( !empty($page[$taxonomy]) ){
                $terms = get_terms(array('taxonomy' => $taxonomy, 'slug' => $page[$taxonomy], 'hide_empty' => false));
                $term_ids = wp_list_pluck($terms, 'term_id');
                wp_set_post_terms($post_id, $term_ids, $taxonomy, false);
            }
        }

        // update builder data
        if( !empty($page['content']) ){
            $content = pixe_update_elem_import_data($page['content']);
            update_post_meta($post_id, 'pixe-builder-main', $content);
            update_post_meta($post_id, 'use-pixe-builder', '1');
        }
        if( !empty($page['style']) ){
            update_post_meta($post_id, 'pixe-builder-styles', $page['style']);
        }
        if( !empty($page['dependency']) ){
            $dependency = pixe_update_elem_dependency($page['dependency']);
            update_post_meta($post_id, 'pixe-builder-dependency', $dependency);
        }

        if( $post_type == 'product' ){
            $product = wc_get_product($post_id);

            if( !$product ){
                $product = new WC_Product_Simple( $post_id );
            }
            if( !empty($page['description']) ){
                $product->set_description($page['description']);
            }
            if( !empty($page['short-description']) ){
                $product->set_short_description($page['short-description']);
            }
            if( !empty($page['sku']) ){
                $product->set_sku($page['sku']);
            }
            if( !empty($page['regular-price']) ){
                $product->set_regular_price($page['regular-price']);
            }
            if( !empty($page['sale-price']) ){
                $product->set_sale_price($page['sale-price']);
            }
            if( !empty($page['gallery']) ){
                $gallery_ids = array();
                foreach($page['gallery'] as $image_url){
                    $image_id = pixe_upload_image($image_url);
                    $gallery_ids[] = $image_id;
                }
                $product->set_gallery_image_ids($gallery_ids);
            }

            $product->save();
        }

        // replace template id in page settings
        $page_settings = empty($page['settings'])? array(): $page['settings'];
        if( !empty($page_settings['layout']['template']) ){
            $old_template = $page_settings['layout']['template'];
            if( !empty($pixe_template_map[$old_template]) ){
                $page_settings['layout']['template'] = $pixe_template_map[$old_template];
            }
            update_post_meta($post_id, 'pixe-builder-settings', $page_settings);
        }

        // update map data
        if( empty($pixe_post_map) ){
            $pixe_post_map = array();
        }
        if( !empty($post_id) ){
            $pixe_post_map[$page['id']] = $post_id;
        }
    }

    add_action('wp_ajax_pixe_import_site', 'pixe_ajax_import_site');
    function pixe_ajax_import_site(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message')
            )));
        }
        
        // check data
        if( empty($_POST['id']) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => esc_html__('Invalid Data', 'pixebuilder'),
                'message'=> esc_html__('Please refresh the page and try again.' ,'pixebuilder')
            )));
        }

        $data = pixe_request(array(
            'id' => intval($_POST['id']),
            'pb-type' => 'sites',
            'type' => 'prebuilt-content',
            'key' => apply_filters('pixe_site_key', '')
        ));

        if( is_wp_error($data) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => esc_html__('Error', 'pixebuilder'),
                'message'=> $data->get_error_message()
            )));
        }else{
            $options = empty($_POST['options'])? array(): map_deep(wp_unslash($_POST['options']), 'sanitize_text_field');
            $options[] = 'page';
            if( in_array('post', $options) ){
                $options[] = 'product';
            }
            pixe_import_site($data, $options);

            die(json_encode(array(
                'status' => 'success',
                'data' => $data,
                'options' => $options
            )));
        }
    }
