<?php
/**
 * This function runs when WordPress completes its upgrade process
 * It iterates through each plugin updated to see if ours is included
 * @param $upgrader_object Array
 * @param $options Array
 */
function ds_switch_upgrade_completed( $upgrader_object, $options ) {
 // The path to our plugin's main file
 $ds_switch = plugin_basename( __FILE__ );

 // Check if migration flag is set
 if (et_get_option( 'ds-swtch-migrated' ) !== 'on') {

 // If an update has taken place and the updated type is plugins and the plugins element exists
 if( $options['action'] == 'update' && $options['type'] == 'plugin' && isset( $options['plugins'] ) ) {
  // Iterate through the plugins being updated and check if ours is there
  foreach( $options['plugins'] as $plugin ) {
   if( $plugin == $our_plugin ) {
    	// Check if the new plugin version is less than the rebuild version of 
    	if(DS_SWITCH_VERSION >= DS_SWITCH_MIN_MIGRATE_VERSION) {
    		// Call our function that migrates the data from the old version of switch
    		ds_divi_switch_update_et_options_custom();
		}
	   }
	  }
	 }
	}
}

add_action( 'upgrader_process_complete', 'wp_upe_upgrade_completed', 10, 2 );

function ds_divi_switch_update_et_options_custom() {

	$ds_options = array(
		array (
			"old" => "ds_left_flyin_menu_item",
			"new" => "swtch-header-1"),
		array (
			"old" => "ds_flat_menu_buttons",
			"new" => "swtch-header-2"),
		array (
			"old" => "ds_3d_menu_buttons",
			"new" => ""),
		array (
			"old" => "ds_app_style_menu",
			"new" => "swtch-header-2"),
		array (
			"old" => "ds_hamburger_menu",
			"new" => ""),
		array (
			"old" => "ds_menu_line",
			"new" => "swtch-header-1"),
		array (
			"old" => "ds_center_top_header",
			"new" => "swtch-header-5"),
		array (
			"old" => "ds_larger_info",
			"new" => ""),
		array (
			"old" => "ds_logo_hide",
			"new" => "logo_hide_after"),
		array (
			"old" => "ds_no_border_top",
			"new" => ""),
		array (
			"old" => "ds_fixed_mobile_header",
			"new" => ""),
		array (
			"old" => "ds_side_mobile_menu_light",
			"new" => ""),
		array (
			"old" => "ds_side_mobile_menu_dark",
			"new" => ""),
		array (
			"old" => "ds_ds_select_page_1",
			"new" => ""),
		array (
			"old" => "ds_ds_select_page_2",
			"new" => ""),
		array (
			"old" => "ds_replace_burger",
			"new" => ""),
		array (
			"old" => "ds_append_burger",
			"new" => ""),
		array (
			"old" => "ds_sub-collapse",
			"new" => ""),
		array (
			"old" => "ds_footer_bullets",
			"new" => ""),
		array (
			"old" => "ds_footer_hide_option",
			"new" => ""),
		array (
			"old" => "ds_footer_push",
			"new" => ""),
		array (
			"old" => "ds_sidebar_line",
			"new" => ""),
		array (
			"old" => "ds_loader_dark",
			"new" => ""),
		array (
			"old" => "ds_loader_light",
			"new" => ""),
		array (
			"old" => "ds_top_section_separators",
			"new" => ""),
		array (
			"old" => "ds_bottom_section_separators",
			"new" => ""),
		array (
			"old" => "ds_clean_comments",
			"new" => ""),
		array (
			"old" => "ds_square_buttons",
			"new" => ""),
		array (
			"old" => "ds_pretext_code",
			"new" => ""),
		array (
			"old" => "ds_style_cf7",
			"new" => ""),
		array (
			"old" => "ds_two-column-archive",
			"new" => ""),
		array (
			"old" => "ds_full-width-archive",
			"new" => ""),
		array (
			"old" => "ds_toggle_arrow_mod",
			"new" => ""),
		array (
			"old" => "ds_square_portfolio_images",
			"new" => ""),
		array (
			"old" => "ds_book_portfolio_images",
			"new" => ""),
		array (
			"old" => "ds_cinema_portfolio_images",
			"new" => ""),
		array (
			"old" => "ds_blurb_icon_grow",
			"new" => ""),
		array (
			"old" => "ds_blurb_icon_spin",
			"new" => ""),
		array (
			"old" => "ds_no_lightbox",
			"new" => ""),
		array (
			"old" => "ds_counter_hide",
			"new" => ""),
		array (
			"old" => "ds_overlay_zoom",
			"new" => ""),
		array (
			"old" => "ds_overlay_spin",
			"new" => ""),
		array (
			"old" => "ds_overlay_slide_up",
			"new" => ""),
		array (
			"old" => "ds_hover-animations",
			"new" => ""),
		array (
			"old" => "ds_slide_transition_left",
			"new" => ""),
		array (
			"old" => "ds_slide_transition_right",
			"new" => ""),
		array (
			"old" => "ds_slide_transition_flipY",
			"new" => ""),
		array (
			"old" => "ds_slide_transition_flipX",
			"new" => ""),
		array (
			"old" => "ds_slide_image_transition_left",
			"new" => ""),
		array (
			"old" => "ds_slide_image_transition_right",
			"new" => ""),
		array (
			"old" => "ds_slide_image_transition_flipY",
			"new" => ""),
		array (
			"old" => "ds_slide_image_transition_flipY",
			"new" => ""),
		array (
			"old" => "ds_down_arrow_section",
			"new" => ""),
		array (
			"old" => "ds_ds_instagram_filters",
			"new" => ""),
		array (
			"old" => "ds_full-width-square",
			"new" => ""),
		array (
			"old" => "ds_subscribe_field_reveal",
			"new" => ""),
		array (
			"old" => "ds_contact_field_reveal",
			"new" => ""),
		array (
			"old" => "ds_fade-blog-excerpt",
			"new" => ""),
		array (
			"old" => "ds_material-btt",
			"new" => ""),
		array (
			"old" => "ds_grow-rotate-excerpts",
			"new" => ""),
		array (
			"old" => "ds_lights-subs",
			"new" => ""),
		array (
			"old" => "ds_dark-subs",
			"new" => ""),
		array (
			"old" => "ds_dark-text-shadow",
			"new" => ""),
		array (
			"old" => "ds_light-text-shadow",
			"new" => ""),
	);
	
	// Update each of the options
	// We may need to check if old/existing field is set/has value to prevent conflict - will test once we have matching field IDs
	foreach ($ds_options as $ds_option) {
    	et_update_option( $ds_option['old'], $ds_option['new'] );
	}
	et_update_option( 'on' , 'ds-swtch-migrated' );

}
