<?php



class td_update_to_newspaper6 {
    static $shortcode_map = array (
        'td_block10' => 'td_block_9',
        'td_block1' => 'td_block_1',
        'td_block2' => 'td_block_2',
        'td_block3' => 'td_block_3',
        'td_block4' => 'td_block_7',
        'td_block5' => 'td_block_8',
        'td_block6' => 'td_block_11',
        'td_block7' => 'td_block_5',
        'td_block8' => 'td_block_4',
        'td_block9' => 'td_block_12',

        'td_ad_box'             => 'td_block_ad_box',
        //'td_social' => '',
        'td_popular_categories' => 'td_block_popular_categories',
        'td_authors'            => 'td_block_authors',
        'td_text_with_title'    => 'td_block_text_with_title',
        'td_slide_big'          => 'td_block_big_grid_2',
        'td_slide'              => 'td_block_slide',
        'td_video_youtube'      => 'td_block_video_youtube',
        'td_video_vimeo'        => 'td_block_video_vimeo',
        'td_social_counter'     => 'td_block_social_counter'
    );

    static $td_options_to_be_imported = array (
        'tds_archive_page_layout',
        'tds_archive_sidebar_pos',
        'tds_author_page_layout',
        'tds_author_sidebar_pos',
        'tds_category_page_layout',
        'tds_category_sidebar_pos',
        'tds_tag_page_layout',
        'tds_tag_sidebar_pos',
        'tds_404_page_layout',
        'tds_logo_upload',
        'tds_logo_upload_r',
        'tds_favicon_upload',
        'tds_show_featured_image',
        'tds_featured_image_view_setting',   // de verificat
        'tds_show_tags',
        'tds_show_author_box',
        'tds_show_next_prev',
        'tds_similar_articles',
        'tds_similar_articles_type',   //verificat
        'tds_breadcrumbs_show',
        'tds_breadcrumbs_show_home',
        'tds_breadcrumbs_show_parent',
        'tds_breadcrumbs_show_article',
        'tds_snap_menu',  // de verificat
        'tds_stretch_background',
        'tds_header_style',  //verificat?
        'tds_inline_ad_paragraph',   //wtf ce ii
        'tds_footer_copyright',
        'tds_footer_copy_symbol',
        'tds_home_sidebar',
        'tds_home_page_layout',
        'tds_home_sidebar_pos',
        'tds_page_sidebar_pos',
        'tds_page_sidebar',
        'tds_archive_sidebar',
        'tds_author_sidebar',
        'tds_category_sidebar',
        'tds_tag_sidebar',
        'tds_search_sidebar_pos',
        'tds_search_sidebar',
        'tds_search_page_layout',
        'tds_attachment_sidebar_pos',
        'tds_attachment_sidebar',
        'tds_theme_color',
        'tds_logo_alt',
        'tds_logo_title',
        'td_analytics',
        'tds_woo_sidebar_pos',
        'tds_woo_sidebar',
        'tds_p_show_date',
        'tds_p_show_views',
        'tds_p_show_comments',
        'tds_top_menu',
        'tds_data_top_menu',
        'tds_data_time_format',
        'tds_p_enable_7_days_count',
        'tds_header_wrap_color',
        'tds_excerpts_type',
        'tds_bbpress_sidebar_pos',
        'tds_bbpress_sidebar',
        'tds_footer_color',
        'tds_footer_bottom_color',
        'tds_ios_icon_76',
        'tds_ios_icon_120',
        'tds_ios_icon_152',
        'tds_ios_icon_114',
        'tds_ios_icon_144',
        'tds_footer_text_color',
        'tds_footer_bottom_text_color',
        'td_ads',   // de verificat
        'category_options',  //de verificat
        'tds_inline_ad_align',
        'tds_disable_comments_sidewide',
        'tds_more_articles_on_post_pages_enable',
        'tds_more_articles_on_post_pages_distance_from_top',
        'tds_more_articles_on_post_pages_display',
        'tds_more_articles_on_post_pages_display_module',
        'tds_more_articles_on_post_pages_number',
        'tds_more_articles_on_post_pages_time_to_wait',
        'tds_disable_comments_pages',
        'tds_site_background_image',
        'tds_site_background_repeat',
        'tds_site_background_position_x',
        'tds_site_background_attachment',
        'tds_site_background_color',
        'td_fonts',   // de verificat
        'td_fonts_js_buffer',
        'td_fonts_css_buffer',
        'td_fonts_css_files',
        'tds_logo_menu_upload',
        'tds_logo_menu_upload_r',
        'tds_tweeter_username',
        'tds_footer',   // ???
        'tds_sub_footer',
        'td_default_site_post_template',
        'td_cake_status_time',
        'td_cake_status',
        'envato_key',
        'tds_ajax_post_view_count',
        'sidebars'
    );



    private static function replace_shortcodes($short_code_string) {
        return str_replace(array_keys(self::$shortcode_map ), array_values(self::$shortcode_map), $short_code_string);
    }


    /**
     * @param $sidebars_widgets string get_option('sidebars_widgets' )
     * @return array with new sidebars widgets
     */
    static function update_sidebar_widgets($sidebars_widgets) {
        if (is_array($sidebars_widgets)) {
            foreach ($sidebars_widgets as $sidebar_id => &$widgets) {
                if (is_array($widgets)) {
                    foreach ($widgets as &$widget) {
                        $widget = self::replace_shortcodes($widget);
                    }
                }
            }
        }
        return $sidebars_widgets;
    }


    /**
     * bring the new shortcodes in the theme
     * @param $content
     * @return mixed
     */
    static function update_content($content) {
        return self::replace_shortcodes($content);
    }


    static function update_theme_settings($old_theme_settings) {
        $settings_buffer = array();

        foreach (self::$td_options_to_be_imported as $option_id) {
            if (isset($old_theme_settings[$option_id]) and !empty($old_theme_settings[$option_id])) {
                $settings_buffer[$option_id] = $old_theme_settings[$option_id];
            }
        }
        td_global::$td_options = $settings_buffer;
        update_option(TD_THEME_OPTIONS_NAME, td_global::$td_options);
    }


    static function log($msg) {
        echo '> ' . $msg . '<br>';
    }
}







?>

<div class="wrap">

<div class="td-container-wrap">

    <div class="td-panel-main-header">
        <img src="<?php echo get_template_directory_uri() . '/includes/wp_booster/wp-admin/images/panel/panel-wrap/panel-logo.png'?>" alt=""/>
        <span class="td-panel-header-name"><?php echo TD_THEME_NAME . ' - Theme panel'; ?></span>
        <span class="td-panel-header-version">version: <?php echo TD_THEME_VERSION; ?></span>
    </div>


    <div id="td-container-left">
        <div id="td-container-right">
            <div id="td-col-left">
                <ul class="td-panel-menu">
                    <li class="td-welcome-menu">
                        <a data-td-is-back="yes" class="td-panel-menu-active" href="?page=td_theme_panel">
                            <span class="td-sp-nav-icon td-ico-export"></span>
                            Importing settings:
                            <span class="td-no-arrow"></span>
                        </a>
                    </li>

                    <li>
                        <a data-td-is-back="yes" href="?page=td_theme_welcome">
                            <span class="td-sp-nav-icon td-ico-back"></span>
                            Back
                            <span class="td-no-arrow"></span>
                        </a>
                    </li>
                </ul>
            </div>
            <div id="td-col-right" class="td-panel-content" style="min-height: 900px">

                <!-- Export theme settings -->
                <div id="td-panel-welcome" class="td-panel-active td-panel">

                    <?php echo td_panel_generator::box_start('Newspaper 4 - Newspaper 6 updater'); ?>

                    <div class="td-box-row">

                        Import script started: <br><br>

                        <?php
                        delete_option('wpb_js_column_css_classes');
                        delete_option('wpb_js_row_css_class');
                        delete_option('wpb_js_templates');



                        $old_newspaper_options = get_option('td_008');
                        if (isset($old_newspaper_options) and is_array($old_newspaper_options)) {
                            // import the widgets
                            td_update_to_newspaper6::log('Importing widgets');

                            // import the sidebar_widgets
                            $old_sidebars_widgets = get_option('sidebars_widgets');
                            $fixed_sidebars_widgets = td_update_to_newspaper6::update_sidebar_widgets($old_sidebars_widgets);
                            update_option('sidebars_widgets', $fixed_sidebars_widgets);


                            // update each widget
                            foreach (td_update_to_newspaper6::$shortcode_map as $old_shortcode => $new_shortcode) {
                                $db_get_widget_option = get_option('widget_' . $old_shortcode . '_widget');
                                if (!empty($db_get_widget_option)) {
                                    update_option('widget_' . $new_shortcode . '_widget', $db_get_widget_option);
                                }
                            }


                            // import the settings
                            td_update_to_newspaper6::log('Importing theme settings');
                            td_update_to_newspaper6::update_theme_settings($old_newspaper_options);


                            /*
                             *  - content
                             *  - post templates
                             *  - modules
                             */
                            td_update_to_newspaper6::log('Updating the content');
                            $args = array(
                                'post_type' => array('page'),
                                'posts_per_page' => '150'
                            );
                            $query = new WP_Query( $args );
                            if (!empty($query->posts)) {
                                foreach ($query->posts as $post) {
                                    // change the shortcodes
                                    $post->post_content = td_update_to_newspaper6::update_content($post->post_content);
                                    wp_update_post($post);

                                    // change the templates
                                    $td_cur_post_template = get_post_meta($post->ID, '_wp_page_template', true);
                                    if ($td_cur_post_template == 'page-homepage-bg-loop.php' or 'page-homepage-loop.php') {
                                        update_post_meta($post->ID, '_wp_page_template', 'page-pagebuilder-latest.php');
                                    } else {
                                        update_post_meta($post->ID, '_wp_page_template', '');
                                    }


                                }


                            }

                            delete_option('wpb_js_column_css_classes');
                            delete_option('wpb_js_row_css_class');
                            delete_option('wpb_js_templates');


                            td_update_to_newspaper6::log('Please regenerate your thumbnails!');
                        } else {
                            // td_008 - not found
                            td_update_to_newspaper6::log('Newspaper 4 settings not found in database! td_008 - not found');
                            td_update_to_newspaper6::log('Quiting!');
                        }


                        ?>


                        <div class="td-box-row-margin-bottom"></div>
                    </div>


                    <?php echo td_panel_generator::box_end();?>
                </div>


            </div>
        </div>
    </div>

    <div class="td-clear"></div>

</div>

<div class="td-clear"></div>
<br><br><br><br><br><br><br>


<?php

/*
die;
$sidebar_widgets_test = 'a:21:{s:19:"wp_inactive_widgets";a:3:{i:0;s:19:"td_block4_widget-11";i:1;s:18:"td_block3_widget-3";i:2;s:18:"td_social_widget-7";}s:10:"td-default";a:4:{i:0;s:26:"td_social_counter_widget-2";i:1;s:18:"td_ad_box_widget-5";i:2;s:18:"td_block2_widget-2";i:3;s:17:"td_slide_widget-2";}s:12:"td-top-right";a:1:{i:0;s:19:"login_form_widget-2";}s:11:"td-footer-1";a:2:{i:0;s:20:"footer_logo_widget-3";i:1;s:18:"td_social_widget-4";}s:11:"td-footer-2";a:1:{i:0;s:18:"td_block4_widget-8";}s:11:"td-footer-3";a:1:{i:0;s:30:"td_popular_categories_widget-2";}s:18:"td-default-widgets";a:13:{i:0;s:10:"archives-6";i:1;s:10:"archives-7";i:2;s:10:"calendar-3";i:3;s:12:"categories-5";i:4;s:11:"tag_cloud-5";i:5;s:17:"recent-comments-3";i:6;s:8:"search-7";i:7;s:14:"recent-posts-4";i:8;s:14:"recent-posts-3";i:9;s:12:"categories-4";i:10;s:6:"meta-9";i:11;s:12:"categories-3";i:12;s:10:"archives-5";}s:11:"td-homepage";a:1:{i:0;s:18:"td_block1_widget-2";}s:10:"td-authors";a:2:{i:0;s:19:"td_authors_widget-3";i:1;s:18:"td_block2_widget-3";}s:13:"td-homepage-2";a:2:{i:0;s:8:"search-8";i:1;s:18:"td_block2_widget-9";}s:11:"td-archives";a:3:{i:0;s:10:"archives-8";i:1;s:8:"search-9";i:2;s:18:"td_block4_widget-9";}s:7:"td-tags";a:2:{i:0;s:11:"tag_cloud-6";i:1;s:19:"td_block4_widget-10";}s:13:"td-attachment";a:1:{i:0;s:18:"td_block2_widget-5";}s:11:"td-category";a:1:{i:0;s:18:"td_block2_widget-6";}s:8:"td-video";a:1:{i:0;s:6:"text-2";}s:13:"td-shortcodes";a:1:{i:0;s:10:"nav_menu-2";}s:22:"td-woocommerce-sidebar";a:4:{i:0;s:25:"woocommerce_widget_cart-2";i:1;s:28:"woocommerce_product_search-2";i:2;s:26:"woocommerce_price_filter-2";i:3;s:32:"woocommerce_top_rated_products-2";}s:10:"td-modules";a:4:{i:0;s:6:"text-3";i:1;s:18:"td_ad_box_widget-6";i:2;s:26:"td_social_counter_widget-3";i:3;s:18:"td_block2_widget-7";}s:10:"td-reviews";a:2:{i:0;s:18:"td_block2_widget-8";i:1;s:18:"td_block3_widget-2";}s:20:"td-test-adsense-spot";a:1:{i:0;s:18:"td_ad_box_widget-7";}s:13:"array_version";i:3;}';
$testing_options_009 = '[encoded data, 39352 characters]';
$testing_options_009 = unserialize(base64_decode($testing_options_009));

foreach ($testing_options_009 as $key =>$value) {
if (isset(td_global::$td_options[$key])) {
echo "\n\n $key \n ---------------------- \n";
echo '009 -> ';
print_r($value);
echo "\n" . '011 -> ';
print_r(td_global::$td_options[$key]);


}
}
die;

$array_key_diff = array_diff_key($testing_options_009, td_global::$td_options);
echo "new keys in 009 -------------------------------------------\n\n\n\n\n";
print_r($array_key_diff);



$array_key_diff_2 = array_diff_key(td_global::$td_options, $testing_options_009);
echo "new keys in 011 -------------------------------------------\n\n\n\n";
print_r($array_key_diff_2);

die;
print_r(unserialize($sidebar_widgets_test));
print_r(td_update_to_newspaper6::update_sidebar_widgets(unserialize($sidebar_widgets_test)));



$newspaper_4_6_shortcode_mapp = array(
'td_block10' => 'td_block_10',
'td_block1' => 'td_block_1',
'td_block2' => 'td_block_2',
'td_block3' => 'td_block_3',
'td_block4' => 'td_block_4',
'td_block5' => 'td_block_5',
'td_block6' => 'td_block_6',
'td_block7' => 'td_block_7',
'td_block8' => 'td_block_8',
'td_block9' => 'td_block_9',

'td_ad_box'             => 'td_block_ad_box',
//'td_social' => '',
'td_popular_categories' => 'td_block_popular_categories',
'td_authors'            => 'td_block_authors',
'td_text_with_title'    => 'td_block_text_with_title',
'td_slide_big'          => 'td_block_big_grid_2',
'td_slide'              => 'td_block_slide',
'td_video_youtube'      => 'td_block_video_youtube',
'td_video_vimeo'        => 'td_block_video_vimeo',
'td_social_counter'     => 'td_block_social_counter',

);

*/
?>
