<?php

/**
 * Class TX_Widget_Importer_Service
 */

if ( ! class_exists( 'TX_Widget_Importer_Service' ) ) {
	class TX_Widget_Importer_Service {
		/**
		 * Remove all widgets
		 */
		public static function remove_all() {
			self::remove_inactive_widgets();
			self::remove_active_widgets();
		}

		/**
		 * Remove active widgets.
		 */
		private static function remove_active_widgets() {
			$sidebars_widgets = wp_get_sidebars_widgets();

			foreach ( $sidebars_widgets as $sidebar_key => $sidebars_widget ) {
				if ( $sidebar_key === 'wp_inactive_widgets' ) {
					continue;
				}

				foreach ( $sidebars_widget as $key => $widget_id ) {
					$pieces       = explode( '-', $widget_id );
					$multi_number = array_pop( $pieces );
					$id_base      = implode( '-', $pieces );
					$widget       = get_option( 'widget_' . $id_base );
					unset( $widget[ $multi_number ] );
					update_option( 'widget_' . $id_base, $widget );
					unset( $sidebars_widgets[ $sidebar_key ][ $key ] );
				}
			}

			wp_set_sidebars_widgets( $sidebars_widgets );
		}

		/**
		 * Remove inactive widgets.
		 */
		private static function remove_inactive_widgets() {
			$sidebars_widgets = wp_get_sidebars_widgets();

			if ( ! isset( $sidebars_widgets['wp_inactive_widgets'] ) || ! is_array( $sidebars_widgets['wp_inactive_widgets'] ) ) {
				return;
			}

			foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) {
				$pieces       = explode( '-', $widget_id );
				$multi_number = array_pop( $pieces );
				$id_base      = implode( '-', $pieces );
				$widget       = get_option( 'widget_' . $id_base );
				unset( $widget[ $multi_number ] );
				update_option( 'widget_' . $id_base, $widget );
				unset( $sidebars_widgets['wp_inactive_widgets'][ $key ] );
			}

			wp_set_sidebars_widgets( $sidebars_widgets );
		}

		/**
		 * Import widgets.
		 *
		 * @param $json_data
		 * @param $clear_current
		 *
		 */
		public static function import( $json_data, $clear_current = true ) {
			global $wp_registered_sidebars;

			//	Clear current widgets.
			if ( $clear_current ) {
				self::clear_widgets();
			}

			$sidebars_data  = $json_data[0];
			$widget_data    = $json_data[1];
			$new_widgets    = array();
			$map_widgets_id = array();


			foreach ( $sidebars_data as $import_sidebar => $import_widgets ) {
				foreach ( $import_widgets as $import_widget ) {
					//if the sidebar exists
					if ( isset( $wp_registered_sidebars[ $import_sidebar ] ) ) {
						$title               = trim( substr( $import_widget, 0, strrpos( $import_widget, '-' ) ) );
						$index               = trim( substr( $import_widget, strrpos( $import_widget, '-' ) + 1 ) );
						$current_widget_data = get_option( 'widget_' . $title );
						$new_widget_name     = self::get_new_widget_name( $title, $index );
						$new_index           = trim( substr( $new_widget_name, strrpos( $new_widget_name, '-' ) + 1 ) );
						if ( ! empty( $new_widgets[ $title ] ) && is_array( $new_widgets[ $title ] ) ) {
							while ( array_key_exists( $new_index, $new_widgets[ $title ] ) ) {
								$new_index ++;
							}
						}
						$current_sidebars[ $import_sidebar ][]   = $title . '-' . $new_index;
						$map_widgets_id[ $title . '-' . $index ] = $title . '-' . $new_index;
						if ( array_key_exists( $title, $new_widgets ) ) {
							$new_widgets[ $title ][ $new_index ] = $widget_data[ $title ][ $index ];
							$multiwidget                         = $new_widgets[ $title ]['_multiwidget'];
							unset( $new_widgets[ $title ]['_multiwidget'] );
							$new_widgets[ $title ]['_multiwidget'] = isset( $multiwidget ) ? $multiwidget : 0;
						} else {
							$current_widget_data[ $new_index ] = $widget_data[ $title ][ $index ];
							$current_multiwidget               = $current_widget_data['_multiwidget'];
							$new_multiwidget                   = isset( $widget_data[ $title ]['_multiwidget'] ) ? $widget_data[ $title ]['_multiwidget'] : false;
							$multiwidget                       = ( $current_multiwidget != $new_multiwidget ) ? $current_multiwidget : 1;
							unset( $current_widget_data['_multiwidget'] );
							$current_widget_data['_multiwidget'] = isset( $multiwidget ) ? $multiwidget : 0;
							$new_widgets[ $title ]               = $current_widget_data;
						}
					}
				}
			}

			// Import widget.
			if ( isset( $new_widgets ) && isset( $current_sidebars ) ) {
				update_option( 'sidebars_widgets', $current_sidebars );
				foreach ( $new_widgets as $title => $content ) {
					update_option( 'widget_' . $title, $content );
				}
			}

			//self::mapping_nav_menu();
		}

		/**
		 * Remap nav menu
		 */
		private static function mapping_nav_menu() {
			$nav_menus = get_option( 'widget_nav_menu' );

			if ( empty( $nav_menus ) ) {
				return;
			}

			$tx_wp_import = TX_WP_Import_Service::instance( false );
			$terms          = $tx_wp_import->processed_terms;

			if ( empty( $terms ) ) {
				return;
			}

			foreach ( $nav_menus as $key => $nav_menu ) {
				$nav_menu_id = ! empty( $nav_menu['nav_menu'] ) ? $nav_menu['nav_menu'] : false;

				if ( ! $nav_menu_id ) {
					continue;
				}

				if ( empty( $terms[ $nav_menu_id ] ) ) {
					continue;
				}

				$new_nav_menu_id = $terms[ $nav_menu_id ];
				$nav_menu_obj    = wp_get_nav_menu_object( $new_nav_menu_id );

				if ( ! $nav_menu_obj ) {
					continue;
				}

				$nav_menu['nav_menu'] = $new_nav_menu_id;
				$nav_menus[ $key ]    = $nav_menu;
			}

			update_option( 'widget_nav_menu', $nav_menus );
		}



		/**
		 *
		 * @param string $widget_name
		 * @param string $widget_index
		 *
		 * @return string
		 */
		function get_new_widget_name( $widget_name, $widget_index ) {
			$current_sidebars = get_option( 'sidebars_widgets' );
			$all_widget_array = array();
			foreach ( $current_sidebars as $sidebar => $widgets ) {
				if ( ! empty( $widgets ) && is_array( $widgets ) && $sidebar != 'wp_inactive_widgets' ) {
					foreach ( $widgets as $widget ) {
						$all_widget_array[] = $widget;
					}
				}
			}
			while ( in_array( $widget_name . '-' . $widget_index, $all_widget_array ) ) {
				$widget_index ++;
			}
			$new_widget_name = $widget_name . '-' . $widget_index;

			return $new_widget_name;
		}


		/**
		 * Clear all widgets before import.
		 */
		function clear_widgets() {
			$sidebars = wp_get_sidebars_widgets();
			$inactive = isset( $sidebars['wp_inactive_widgets'] ) ? $sidebars['wp_inactive_widgets'] : array();
			unset( $sidebars['wp_inactive_widgets'] );
			foreach ( $sidebars as $sidebar => $widgets ) {
				$inactive             = array_merge( $inactive, $widgets );
				$sidebars[ $sidebar ] = array();
			}
			$sidebars['wp_inactive_widgets'] = $inactive;
			wp_set_sidebars_widgets( $sidebars );
		}

	}
}
