<?php
add_action( 'init', 'complete_convert_redux' );
function complete_convert_redux() {

	
if(isset($_POST['convert']) && check_admin_referer( 'complete_convert', 'complete_convert' ) ) {
	$picart = get_option('complete');
	$active_widgets = get_option( 'sidebars_widgets' );
	$home_blocks = $picart['home_sort_id'];

if ($home_blocks):

foreach ($home_blocks as $key=>$value) {

    switch($key) {
	//About Section
    case 'about':
		if(!empty($home_blocks['about'])){
				//ABOUT SECTION--------------------------------------------
				$active_widgets[ 'front_sidebar' ][] = 'complete_front_about-1';
				// The latest 15 questions from WordPress Stack Exchange.
				$about_content[ 1 ] = array (
					'title' => $picart['about_header_id'],
					'subtitle' => $picart['about_preheader_id'],
					'content' => $picart['about_content_id'],
					'divider' => $picart['divider_icon'],
					'title_color' => $picart['about_header_color'],
					'content_color' => $picart['about_text_color'],
					'content_bg' => $picart['about_bg_color'],
				);
				update_option( 'widget_complete_front_about', $about_content );
		}
	
	break;
    case 'blocks':
		if(!empty($home_blocks['blocks'])){
				//BLOCKS SECTION--------------------------------------------
				$active_widgets[ 'front_sidebar' ][] = 'complete_front_blocks-1';
				// The latest 15 questions from WordPress Stack Exchange.
				$blocks_content[ 1 ] = array (
					'block1title' => $picart['block1_text_id'],
					'block1img' => $picart['block1_image']['url'],
					'block1content' =>  $picart['block1_textarea_id'],
					'block2title' => $picart['block2_text_id'],
					'block2img' => $picart['block2_image']['url'],
					'block2content' =>  $picart['block2_textarea_id'],
					'block3title' => $picart['block3_text_id'],
					'block3img' => $picart['block3_image']['url'],
					'block3content' => $picart['block3_textarea_id'],
					'block4title' => $picart['block4_text_id'],
					'block4img' => $picart['block4_image']['url'],
					'block4content' => $picart['block4_textarea_id'],
					'block5title' => $picart['block5_text_id'],
					'block5img' => $picart['block5_image']['url'],
					'block5content' => $picart['block5_textarea_id'],
					'block6title' => $picart['block6_text_id'],
					'block6img' => $picart['block6_image']['url'],
					'block6content' => $picart['block6_textarea_id'],
					
					'blockstitlecolor' => $picart['blocktitle_color_id'],
					'blockstxtcolor' => $picart['blocktxt_color_id'],
					'blocksbgcolor' => $picart['midrow_color_id'],
				);
				update_option( 'widget_complete_front_blocks', $blocks_content );
				
		}
	break;
	
	
	case 'welcome-text':
		if(!empty($home_blocks['welcome-text'])){
				//WELCOME TEXT SECTION--------------------------------------------
				$active_widgets[ 'front_sidebar' ][] = 'complete_front_text-1';
				// The latest 15 questions from WordPress Stack Exchange.
				$text_content[ 1 ] = array (
					'title' => __('Welcome Text','picart'),
					'content' => $picart['welcm_textarea_id'],
					'padtopbottom' => '2',
					'paddingside' => '2',
					'parallax' => '',
					'content_color' => $picart['welcometxt_color_id'],
					'content_bg' => $picart['welcome_color_id'],
					'content_bgimg' => $picart['welcome_bg_image']['url'],
				);
				//Updated Below --With Newsletter Widget
				if(empty($home_blocks['newsletter'])){
					update_option( 'widget_complete_front_text', $text_content );	
				}
				
		}
	break;
	
	
	case 'posts':
		if(!empty($home_blocks['posts'])){
				//POSTS SECTION--------------------------------------------
				$active_widgets[ 'front_sidebar' ][] = 'complete_front_posts-1';
				// The latest 15 questions from WordPress Stack Exchange.
				$posts_content[ 1 ] = array (
					'title' => $picart['posts_title_id'],
					'subtitle' => $picart['posts_subtitle_id'],
					'layout' => $picart['front_layout_id'],
					'type' => $picart['n_posts_type_id'],
					'count' => $picart['n_posts_field_id'],
					'category' => $picart['posts_cat_id'],
					'divider' => $picart['divider_icon'],
					'navigation' => $picart['navigation_type'],
					'postbgcolor' => $picart['frontposts_color_id'],
					'titlecolor' => $picart['frontposts_title_color'],
					'secbgcolor' => $picart['frontposts_bg_color'],
				);
				update_option( 'widget_complete_front_posts', $posts_content );
		}
	break;
	
	
	
    } //end of SWITCH

} //end of FOREACH

endif;
		
		//Assign Widgets to frontpage sidebar
		update_option( 'sidebars_widgets', $active_widgets );
		//Update convert =1  and Nivo/Accordion Slides
		$picart['converted'] = '1';
		update_option( 'complete', $picart );
	
		//After Conversion Redirect to Customizer
        $redirect = admin_url('/customize.php'); 
		wp_redirect( $redirect);
	}
}