<?php


add_filter('ef3-theme-options-opt-name', 'haswell_set_demo_opt_name');
function haswell_set_demo_opt_name(){
    return 'smof_data';
}


add_filter('ef3-replace-theme-options', 'haswell_replace_theme_options');

function haswell_replace_theme_options(){
    return array(
        'dev_mode' => 0,
    );
}


add_filter('ef3-enable-create-demo', 'haswell_enable_create_demo');

function haswell_enable_create_demo(){
    return false;
}


add_action('ef3-import-start', 'haswell_move_trash', 1);

if(!function_exists('haswell_move_trash')){
    function haswell_move_trash(){        
        wp_trash_post(1);
        wp_trash_post(2);
        wp_trash_post(3);
        wp_trash_post(5);
        wp_trash_post(7);
        wp_trash_post(8);
        wp_trash_post(9);
        wp_trash_post(10);
        wp_trash_post(11);
    }
}


add_action('ef3-import-start','haswell_removed_default_wp_widgets', 10);

function haswell_removed_default_wp_widgets(){

    global $wp_registered_sidebars;
    $widgets = get_option('sidebars_widgets');
    foreach ($wp_registered_sidebars as $sidebar => $value) {
        unset($widgets[$sidebar]);
    }

    update_option('sidebars_widgets',$widgets);

    update_option('permalink_structure', '');
}


add_filter('ef3-replace-content', 'haswell_replace_content', 10 , 2);

function haswell_replace_content($replaces, $attachment){
    return array(
        '/tax_query:/' => 'remove_query:',
        '/categories:/' => 'remove_query:', 
        '/https:\/\/demo\.exptheme\.com\/haswelltravel/' => get_site_url(),
        '/https:\/\/demo\.exptheme\.com\/haswell/' => get_site_url()
    );
}


add_filter( 'wp_import_post_meta', 'haswell_replace_post_meta', 10, 3);

function haswell_replace_post_meta($post_metas, $post_id, $post){
    $newurl = get_site_url();
    if ( ! empty( $post_metas ) ) {
        foreach ( $post_metas as $k => $meta ) { 
            if ( !empty($meta['key']) ) {
                if ( $meta['key'] == '_edit_last' ) {
                    $post_metas[$k] = ['key' => $meta['key'], 'value' => $meta['value']];
                }else{
                    if ( !empty($meta['value']) ){
                        $value = maybe_unserialize( $meta['value'] );
                        if(!empty($value['url'])){
                            $value['url'] = str_replace('https://demo.exptheme.com/haswelltravel', $newurl, $value['url']);
                            $value['url'] = str_replace('https://demo.exptheme.com/haswell', $newurl, $value['url']);
                        }
                        if(!empty($value['thumbnail'])){
                            $value['thumbnail'] = str_replace('https://demo.exptheme.com/haswelltravel', $newurl, $value['thumbnail']);
                            $value['thumbnail'] = str_replace('https://demo.exptheme.com/haswell', $newurl, $value['thumbnail']);
                        }
                        $new_value = maybe_serialize( $value );
                        $post_metas[$k] = ['key' => $meta['key'], 'value' => $new_value];
                    } 
                }
            }
        }
    }
    return $post_metas;
}


add_filter( 'ef3-replace-file-theme-options', 'haswell_replace_theme_options_file',10,1 );

function haswell_replace_theme_options_file($data){
    $site_url = str_replace('/','\/', get_site_url());
    $data = str_replace('https:\/\/demo.exptheme.com\/haswelltravel', $site_url, $data);
    $data = str_replace('https:\/\/demo.exptheme.com\/haswell', $site_url, $data);
    return $data;
}


add_filter( 'ef3_widget_settings', 'haswell_replace_widget_setting',10,1 );

function haswell_replace_widget_setting($widget){
    if(!empty($widget->url)){ 
        $newurl = get_site_url();
        $widget->url = str_replace('https://demo.exptheme.com/haswelltravel', $newurl, $widget->url);
        $widget->url = str_replace('https://demo.exptheme.com/haswell', $newurl, $widget->url);
    }

    return $widget;
}


add_action('ef3-export-finish', 'haswell_export_extra_option',10,1);

function haswell_export_extra_option($folder_dir){
    global $wp_filesystem;
    $file = $folder_dir . 'extra-options.json';
    $options = [
        'date_format',
        'time_format',
        'default_category',
        'posts_per_page',
        'show_on_front',
        'page_on_front',
        'page_for_posts',
        'wp_user_roles'
    ];
    
    $file_contents = array();

    foreach ( $options as $option_name ) {
        $file_contents[ $option_name ] = get_option( $option_name );
    }

    if ( $file_contents !== false ) {
        $file_contents = json_encode( $file_contents );
        $wp_filesystem->put_contents( $file, $file_contents, FS_CHMOD_FILE );
    }
}


add_action('ef3-import-finish', 'haswell_import_extra_option',1,2);

function haswell_import_extra_option($id,$folder_dir){
    $file = $folder_dir . 'extra-options.json';
    if ( file_exists( $file ) && class_exists('EF3_Framework') ) {
        $file_contents = json_decode( file_ef3_get_contents( $file ), true );
        foreach ( $file_contents as $option_name => $option_values ) {
            update_option( $option_name, $option_values );
        }
    }
}


add_action('ef3-import-finish', 'haswell_set_options_page');
function haswell_set_options_page(){
    $pages = array(    
 
        'woocommerce_shop_page_id'      => 'Shop',
        'woocommerce_cart_page_id'      => 'Cart',
        'woocommerce_myaccount_page_id' => 'My Account',
        'page_on_front' => 'Corporate 1',
    );
    foreach ($pages as $key => $page){
        $page = get_page_by_title($page);
        if(!isset($page->ID))
            return ;
        update_option($key, $page->ID);
    }
}


add_action('ef3-import-finish', 'haswell_crop_images',99,2);

function haswell_crop_images($id,$folder) {
    $query = array(
        'post_type'      => 'attachment',
        'posts_per_page' => -1,
        'post_status'    => 'inherit',
    );
    $media = new WP_Query($query);
    if ($media->have_posts()) {
        foreach ($media->posts as $image) {
            if (strpos($image->post_mime_type, 'image/') !== false) {
                $image_path = get_attached_file($image->ID);
                $metadata = wp_generate_attachment_metadata($image->ID, $image_path);
                wp_update_attachment_metadata($image->ID, $metadata);
            }
        }
    }
}  