<?php

class ESSBAdminControler {
	
	private static $instance = null;
	public static function get_instance() {
	
		if ( null == self::$instance ) {
			self::$instance = new self;
		}
	
		return self::$instance;
	
	} // end get_instance;
	
	function __construct() {
		global $pagenow;
		
		$deactivate_appscreo = essb_option_bool_value('deactivate_appscreo');
		
		//add_action ('init',array ($this, 'essb_settings_redirect' )  );
		add_action ( 'admin_menu', 	array ($this, 'register_menu' ) );
		add_action ( 'admin_enqueue_scripts', array ($this, 'register_admin_assets' ), 99 );	
		add_action ( 'wp_ajax_essb_process_search', array($this, 'essb_action_process_search' ));
		
		$hook = (defined ( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN) ? 'network_admin_menu' : 'admin_menu';
		add_action ( $hook, array ($this, 'handle_save_settings' ) );
		
		if (is_admin()) {
			add_action ( 'wp_ajax_essb_settings_save', array ($this, 'actions_download_settings' ) );
		}
		
		// admin meta boxes
		add_action('add_meta_boxes', array ($this, 'handle_essb_metabox' ) );
		add_action('save_post',  array ($this, 'handle_essb_save_metabox'));
		
		if (!$deactivate_appscreo) {
			add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
		}
		
		
		$dismiss_import = isset($_REQUEST['dismiss_import']) ? $_REQUEST['dismiss_import'] : '';
		if ($dismiss_import == 'true') {
			update_option('essb2_converted', 'true');
		}
		
		
		if (defined('ESSB_VERSION')) {
			add_action ( 'admin_notices', array ($this, 'add_notice_essb2_running' ) );
		}
		
		if (ESSB3_ADDONS_ACTIVE) {
			include_once(ESSB3_PLUGIN_ROOT . 'lib/admin/addons/essb-addons-helper4.php');
			ESSBAddonsHelper::get_instance();
		}
		
 		$easymode = isset($_REQUEST['easymode']) ? $_REQUEST['easymode'] : '';
 		if (!empty($easymode)) {
 			if ($easymode == "activate") {
 				update_option(ESSB3_EASYMODE_NAME, 'true');
 			}
 			if ($easymode == "deactivate") {
 				update_option(ESSB3_EASYMODE_NAME, 'false');
 			}
 			
 			$this->essb_settings_redirect_after_easymode();
 		}
 		
 		if(isset($pagenow) && $pagenow == 'plugins.php' && !ESSBActivationManager::isActivated()){
 			if (!ESSBActivationManager::isThemeIntegrated()) {
 				add_action('admin_notices', array($this, 'add_plugins_page_notices'));
 			}
 		}
 		else {
 			$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : '';
 			
 			if (strpos($page, 'essb_') === false) {
 				$this->setupReminder();
 			}
 			
 		}
	}
	
	function essb_action_process_search() {
		include_once(ESSB3_PLUGIN_ROOT . 'lib/admin/essb-admin-search.php');
	}
	
	function setupReminder() {
		if ( ESSBActivationManager::isDevelopment() ) {
			return;
		}
		
		if (ESSBActivationManager::isThemeIntegrated()) {
			return;
		}
		
		if ( ! ESSBActivationManager::isActivated() && ( empty( $_COOKIE['essbhideactivationmsg'] ) || version_compare( $_COOKIE['essbhideactivationmsg'],
				ESSB3_VERSION,
				'<' ) ) && ! (is_network_admin() )
		) {
			add_action( 'admin_notices',
					array( $this, 'adminNoticeLicenseActivation', ) );
		}
	}
	
	public function adminNoticeLicenseActivation() {
		update_option( 'essb_license_activation_notified', 'yes' );
		$redirect = admin_url( 'admin.php?page=essb_redirect_update&tab=update');
		?>
			<style>
				.vc_license-activation-notice {
					position: relative;
				}
			</style>
			<script type="text/javascript">
				(function ( $ ) {
					var setCookie = function ( c_name, value, exdays ) {
						var exdate = new Date();
						exdate.setDate( exdate.getDate() + exdays );
						var c_value = encodeURIComponent( value ) + ((null === exdays) ? "" : "; expires=" + exdate.toUTCString());
						document.cookie = c_name + "=" + c_value;
					};
					$( document ).on( 'click.essb-notice-dismiss',
						'.essb-notice-dismiss',
						function ( e ) {
							e.preventDefault();
							var $el = $( this ).closest(
								'#essb_license-activation-notice' );
							$el.fadeTo( 100, 0, function () {
								$el.slideUp( 100, function () {
									$el.remove();
								} );
							} );
							setCookie( 'essbhideactivationmsg',
								'<?php echo ESSB3_VERSION; ?>',
								30 );
						} );
				})( window.jQuery );
			</script>
			<?php
			echo '<div class="updated essb_license-activation-notice" id="essb_license-activation-notice" style="position: relative;"><p>' . sprintf( __( 'Hola! Would you like to receive automatic updates, access to extensions library and unlock ready made styles and premium support? Please <a href="%s">activate your copy</a> of Easy Social Share Buttons for WordPress.',
					'essb' ),
					wp_nonce_url( $redirect ) ) . '</p>' . '<button type="button" class="notice-dismiss essb-notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice.' ) . '</span></button></div>';
		}
		
	
	public function add_plugins_page_notices(){
		$plugins = get_plugins();
	
		foreach($plugins as $plugin_id => $plugin){
	
			$slug = dirname($plugin_id);
			if(empty($slug)) continue;
			if($slug !== 'easy-social-share-buttons3' && $slug !== 'easy-social-share-buttons3-ver4') continue;
							
			if(!ESSBActivationManager::isActivated()){ //activate for updates and support
				add_action( "after_plugin_row_" . $plugin_id, array($this, 'show_purchase_notice'), 10, 3);
			}
		}
	}
	
	public function show_purchase_notice(){
		$wp_list_table = _get_list_table('WP_Plugins_List_Table');
		$activate_url = admin_url('admin.php?page=essb_redirect_update&tab=update');
		
		$new_version_code = '';
		if (ESSBActivationManager::existNewVersion()) {
			$new_version_code = ' <b>New version '.ESSBActivationManager::getLatestVersion().' is available.</b>';
		}
		
		?>
	        <tr class="plugin-update-tr essb-plugin-update"><td colspan="<?php echo $wp_list_table->get_column_count(); ?>" class="plugin-update colspanchange">
	            <div class="update-message notice inline notice-warning notice-alt">
	            <p>Activate Easy Social Share Buttons for WordPress to <a href="<?php echo $activate_url; ?>"><b>unlock automatic plugin updates</b></a>. You can <a href="<?php echo $activate_url; ?>"><b>Enter an existing licence key</b></a> or <a href="http://codecanyon.net/item/easy-social-share-buttons-for-wordpress/6394476?ref=appscreo&license=regular&open_purchase_for_item_id=6394476&purchasable=source" target="_blank"><b>Purchase a licence key</b></a>. <?php echo $new_version_code; ?></p>
	            </div>
	        </tr>
	        <?php 
		}
	
	/**
	 * Add news dashboard widget
	 * 
	 * @since 3.6
	 */
	public function add_dashboard_widget() {
		// Create the widget
		wp_add_dashboard_widget( 'appscreo_news', apply_filters( 'appscreo_dashboard_widget_title', __( 'AppsCreo News', 'essb' ) ), array( $this, 'display_news_dashboard_widget' ) );
		
		// Make sure our widget is on top off all others
		global $wp_meta_boxes;
		
		// Get the regular dashboard widgets array
		$normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
		
		// Backup and delete our new dashboard widget from the end of the array
		$avada_widget_backup = array( 'appscreo_news' => $normal_dashboard['appscreo_news'] );
		unset( $normal_dashboard['appscreo_news'] );
		
		// Merge the two arrays together so our widget is at the beginning
		$sorted_dashboard = array_merge( $avada_widget_backup, $normal_dashboard );
		
		// Save the sorted array back into the original metaboxes
		$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
	}
	
	public function display_news_dashboard_widget() {
		$deactivate_appscreo = essb_option_bool_value('deactivate_appscreo');
		
		if (!$deactivate_appscreo) {
		// Create two feeds, the first being just a leading article with data and summary, the second being a normal news feed
			$feeds = array(
					'first' => array(
							'link'         => 'https://appscreo.com/',
							'url'          => 'https://appscreo.com/feed/',
							'title'        => __( 'AppsCreo News', 'essb' ),
							'items'        => 1,
							'show_summary' => 1,
							'show_author'  => 0,
							'show_date'    => 1,
					),
					'news' => array(
							'link'         => 'https://appscreo.com/',
							'url'          => 'https://appscreo.com/feed/',
							'title'        => __( 'AppsCreo News', 'essb' ),
							'items'        => 4,
							'show_summary' => 0,
							'show_author'  => 0,
							'show_date'    => 0,
					),
			);
			
			
			wp_dashboard_primary_output( 'appscreo_news', $feeds );
		}
		else {
			print '<div class="essb-admin-widget">';
			print '<h4><strong>AppsCreo News</strong></h4>';
			print '<a href="http://appscreo.com/" target="_blank">Read latest news, tips and tricks in our blog at http://appscreo.com</a>';
			print '</div>';
		}
		
		//wp_dashboard_cached_rss_widget( 'appscreo_news', 'wp_dashboard_primary_output', $feeds );
		
		print '<div class="essb-admin-widget">';
		print '<h4><strong>Subscribe to our mailing list and get interesting stuff and updates to your email inbox.</strong></h4>';
		print '<form action="//appscreo.us13.list-manage.com/subscribe/post?u=a1d01670c240536f6a70e7778&amp;id=c896311986" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>';
		print '<div class="input-text-wrap" id="title-wrap">';
		//print '<label class="screen-reader-text prompt" for="mce-EMAIL" id="title-prompt-text">Enter your email</label>';
		print '<input type="email" name="EMAIL" id="mce-EMAIL" autocomplete="off" placeholder="Enter your email" />';
		print '</div>';		
		print '<p><input type="submit" name="subscribe" id="mc-embedded-subscribe" class="button button-primary" value="Subscribe"></p>';
		print '</form>';
		print '</div>';
		
	}
	
	public function add_notice_new_addon() {
		if (ESSB3_ADDONS_ACTIVE && class_exists('ESSBAddonsHelper')) {
			
			if (class_exists('ESSBAdminActivate')) {
				ESSBAdminActivate::notice_new_addons();
			}
			
		}
		
	}
	
	public function add_notice_import_options() {
		?>
<div class="updated fade">
	<p style="padding: 5px 5px;">
		<strong><a
			href="http://codecanyon.net/item/easy-social-share-buttons-for-wordpress/6394476?ref=appscreo"
			target="_blank">Easy Social Share Buttons for WordPress</a></strong>
		found that you you have options set with previous version of product.
		</strong> <a
			href="<?php echo admin_url('admin.php?page=essb_redirect_advanced&tab=advanced&section=convert&subsection&dismiss_import=true');?>"
			class="button"
			style="float: right; margin-top: -8px; margin-left: 5px;">Dismiss</a><a
			href="<?php echo admin_url('admin.php?page=essb_redirect_advanced&tab=advanced&section=convert&subsection');?>"
			class="button" style="float: right; margin-top: -8px;">Click here to
			convert and import your previous options</a>
	</p>

</div>
<?php
	}
	
	public function add_notice_essb2_running() {
		?>
	<div class="updated fade">
		<p style="padding: 5px 5px;">
			<strong><a
				href="http://codecanyon.net/item/easy-social-share-buttons-for-wordpress/6394476?ref=appscreo"
				target="_blank">Easy Social Share Buttons for WordPress</a></strong>
			detects that older version of plugin is running. Please go to your Installed Plugins screen and deactivate old version of plugin (versions 2.x or 1.x) .
			</strong> <a
				href="<?php echo admin_url('plugins.php?s=easy+social+share+buttons');?>"
				class="button"
				style="float: right; margin-top: -8px; margin-left: 5px;">Click here to go to Installed Plugins list</a>
		</p>
	
	</div>
	<?php
		}
	
	public function handle_essb_metabox() {

		
		$display_in_types = essb_option_value('display_in_types');
		$turnoff_essb_optimize_box = essb_option_bool_value('turnoff_essb_optimize_box');
		$turnoff_essb_stats_box = essb_option_bool_value('turnoff_essb_stats_box');
		$turnoff_essb_advanced_box = essb_option_bool_value('turnoff_essb_advanced_box');
		
		$stats_are_activated =essb_option_bool_value('stats_active');
		if (!$stats_are_activated) {
			$turnoff_essb_stats_box = true;
		}
		
		if (!is_array($display_in_types)) {
			$display_in_types = array();
		}
		
		// get post types
		$pts	 = get_post_types( array('show_ui' => true, '_builtin' => true) );
		$cpts	 = get_post_types( array('show_ui' => true, '_builtin' => false) );
		foreach ( $pts as $pt ) {
			if ((defined('ESSB3_SSO_ACTIVE') && !$turnoff_essb_optimize_box) || in_array($pt, $display_in_types)) {
				add_meta_box('essb_metabox_optmize', __('Easy Social Share Buttons: Share Options', 'essb'), 'essb_register_settings_metabox_optimize', $pt, 'normal', 'high');				
			}
			
			//if (defined('ESSB3_SSO_ACTIVE') && !$turnoff_essb_optimize_box) {
			//	add_meta_box('essb_metabox_sso', __('Easy Social Share Buttons: Social Share Optimization', 'essb'), 'essb_register_settings_metabox_optimization', $pt, 'normal', 'high');				
			//}
			if (in_array($pt, $display_in_types)) {
				add_meta_box('essb_metabox', __('Easy Social Share Buttons', 'essb'), 'essb_register_settings_metabox_onoff', $pt, 'side', 'high');
				
				//if (!$turnoff_essb_optimize_box) {
				//	add_meta_box('essb_metabox_share', __('Easy Social Share Buttons: Share Customization', 'essb'), 'essb_register_settings_metabox_customization', $pt, 'normal', 'high');
				//}
				
				if (!$turnoff_essb_advanced_box) {
					add_meta_box('essb_metabox_visual', __('Easy Social Share Buttons: Visual Customization', 'essb'), 'essb_register_settings_metabox_visual', $pt, 'normal', 'high');
				}
				
				if (!$turnoff_essb_stats_box) {
					add_meta_box('essb_metabox_stats', __('Easy Social Share Buttons: Stats', 'essb'), 'essb_register_settings_metabox_stats', $pt, 'normal', 'core');
				}
				
				
			}
				
		}
		
		foreach ( $cpts as $cpt ) {
			
			if ((defined('ESSB3_SSO_ACTIVE') && !$turnoff_essb_optimize_box) || in_array($cpt, $display_in_types)) {
				add_meta_box('essb_metabox_optmize', __('Easy Social Share Buttons: Social Share Optimization', 'essb'), 'essb_register_settings_metabox_optimize', $cpt, 'normal', 'high');
			}
			
			//if (defined('ESSB3_SSO_ACTIVE') && !$turnoff_essb_optimize_box) {
			//	add_meta_box('essb_metabox_sso', __('Easy Social Share Buttons: Social Share Optimization', 'essb'), 'essb_register_settings_metabox_optimization', $cpt, 'normal', 'high');				
			//}
			if (in_array($cpt, $display_in_types)) {
				add_meta_box('essb_metabox', __('Easy Social Share Buttons', 'essb'), 'essb_register_settings_metabox_onoff', $cpt, 'side', 'high');
				
				//if (!$turnoff_essb_optimize_box) {
				//	add_meta_box('essb_metabox_share', __('Easy Social Share Buttons: Share Customization', 'essb'), 'essb_register_settings_metabox_customization', $cpt, 'normal', 'high');
				//}
				
				if (!$turnoff_essb_advanced_box) {
					add_meta_box('essb_metabox_visual', __('Easy Social Share Buttons: Visual Customization', 'essb'), 'essb_register_settings_metabox_visual', $cpt, 'normal', 'high');
				}

				if (!$turnoff_essb_stats_box) {
					add_meta_box('essb_metabox_stats', __('Easy Social Share Buttons: Stats', 'essb'), 'essb_register_settings_metabox_stats', $cpt, 'normal', 'core');
				}
				
			}
		}
	}
	
	public function handle_essb_save_metabox() {
		global $post, $post_id;
		
		if (! $post) {
			return $post_id;
		}
		
		if (! $post_id) {
			$post_id = $post->ID;
		}
		
		$essb_metabox = isset($_REQUEST['essb_metabox']) ? $_REQUEST['essb_metabox'] : array();
		
		$exist_metabox = isset($_REQUEST['essb_metabox']) ? true: false;
		
		if (!$exist_metabox && essb_option_bool_value('using_elementor')) {
			return;
		}
		
		$this->save_metabox_value ( $post_id, 'essb_off', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_button_style', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_template', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_counters', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_counter_pos', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_total_counter_pos', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_customizer', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_animations', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_optionsbp', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_content_position', $essb_metabox );
		foreach ( essb_available_button_positions() as $position => $name ) {
			$this->save_metabox_value ( $post_id, "essb_post_button_position_{$position}", $essb_metabox );
		}
		$this->save_metabox_value ( $post_id, 'essb_post_native', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_native_skin', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_share_message', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_share_url', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_share_image', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_share_text', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_pin_image', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_fb_url', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_plusone_url', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_twitter_hashtags', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_twitter_username', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_twitter_tweet', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_activate_ga_campaign_tracking', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_desc', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_author', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_title', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_image', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_video', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_video_w', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_video_h', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_url', $essb_metabox);
		
		$this->save_metabox_value ( $post_id, 'essb_post_twitter_desc', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_twitter_title', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_twitter_image', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_google_desc', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_google_title', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_google_image', $essb_metabox);
		$this->save_metabox_value ( $post_id, 'essb_activate_sharerecovery', $essb_metabox);

		$this->save_metabox_value ( $post_id, 'essb_post_og_image1', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_image2', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_image3', $essb_metabox );
		$this->save_metabox_value ( $post_id, 'essb_post_og_image4', $essb_metabox );
		
		
		// Twitter Custom Share Value
		$essb_pc_twitter = ESSBOptionValuesHelper::options_value($essb_metabox, 'essb_pc_twitter');
		if (!empty($essb_pc_twitter)) {
			$this->save_metabox_value_simple($post_id, 'essb_pc_twitter', $essb_pc_twitter);
		}
		
		// @since 3.4.1
		// apply on save clearing and caching of post meta values that will be used within plugin
		$post_image = has_post_thumbnail( $post_id ) ? wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' ) : '';
		$image = ($post_image != '') ? $post_image[0] : '';		
		$this->save_metabox_value_simple_with_clear($post_id, 'essb_cached_image', $image);
		
		// clear cached counters ttl
		delete_post_meta($post_id, 'essb_cache_timestamp');
	}
	
	public function save_metabox_value($post_id, $option, $valueContainer) {
		$value = ESSBOptionValuesHelper::options_value($valueContainer, $option);
		if (!empty($value)) {
			update_post_meta ( $post_id, $option, $value );
		}
		else {
			delete_post_meta ( $post_id, $option );
		}
	}
	
	public function save_metabox_value_simple($post_id, $option, $value) {
		update_post_meta ( $post_id, $option, $value );
	}
	
	public function save_metabox_value_simple_with_clear($post_id, $option, $value) {
		if (!empty($value)) {
			update_post_meta ( $post_id, $option, $value );
		}
		else {
			if (!essb_option_bool_value('using_elementor')) {
				delete_post_meta ( $post_id, $option );
			}
		}
	}
	
	public function register_menu() {
		global $essb_all_options, $essb_navigation_tabs, $essb_options;
		
		//$menu_pos = ESSBOptionValuesHelper::options_bool_value($essb_all_options, 'register_menu_under_settings');
		$menu_pos = false;
		$essb_access = ESSBOptionValuesHelper::options_value($essb_options, 'essb_access'); 
		if (empty($essb_access)) {
			$essb_access = "edit_pages";
		}
		
		if ($menu_pos) {
			add_options_page ( "Easy Social Share Buttons", "Easy Social Share Buttons", 'edit_pages', "essb_options", array ($this, 'essb_settings_load' ), ESSB3_PLUGIN_URL . '/assets/images/essb_16.png', 114 );
		}
		else {
			if (essb_show_welcome()) {
				add_menu_page ( "Easy Social Share Buttons", "Easy Social Share Buttons", $essb_access, "essb_options", array ($this, 'essb_settings_redirect_welcome' ) );
			}
			else {
				add_menu_page ( "Easy Social Share Buttons", "Easy Social Share Buttons", $essb_access, "essb_options", array ($this, 'essb_settings_load' ) );
			}
			
			$is_first = true;
			
			// @since 4.2 - introduce the new welcome screen
			if (essb_show_welcome()) {
				add_submenu_page( 'essb_options', __('Welcome', 'essb'), __('Welcome', 'essb'), $essb_access, 'essb_options', array ($this, 'essb_settings_redirect_welcome' ));
				$is_first = false;
			}
			
			foreach ( $essb_navigation_tabs as $name => $label ) {
				if ($is_first) {
					add_submenu_page( 'essb_options', $label, $label, $essb_access, 'essb_options', array ($this, 'essb_settings_load' ));
					$is_first = false;
				}
				else {
					add_submenu_page( 'essb_options', $label, $label, $essb_access, 'essb_redirect_'.$name, array ($this, 'essb_settings_redirect1' ));
				}
			}
			// submenu init
			//		add_submenu_page( 'se_settings', 'Settings', 'Settings', 'edit_pages', 'se_settings', array($this, 'es_settings_load'));
			
			add_submenu_page( 'essb_options', __('About', 'essb'), __('About', 'essb'), $essb_access, 'essb_about', array ($this, 'essb_settings_redirect_about' ));
			if (ESSB3_ADDONS_ACTIVE) {
				add_submenu_page( 'essb_options', __('Extensions', 'essb'), '<span style="color:#f39c12;">'.__('Extensions', 'essb').'</span>', $essb_access, 'essb_addons', array ($this, 'essb_settings_redirect_addons' ));
			}
		}
	}
	
	public function essb_settings_redirect1() {
		
		if ($this->require_easy_mode_screen()) {
			include (ESSB3_PLUGIN_ROOT . 'lib/admin/essb-choose-mode.php');
		}
		
		include (ESSB3_PLUGIN_ROOT . 'lib/admin/essb-settings42.php');
		
	}

	public function essb_settings_redirect_about() {
		include (ESSB3_PLUGIN_ROOT . 'lib/admin/welcome/essb-welcome.php');
	}
	
	public function essb_settings_redirect_addons() {
		include (ESSB3_PLUGIN_ROOT . 'lib/admin/addons/essb-addons.php');
	}
	
	public function essb_settings_redirect_welcome() {
		include (ESSB3_PLUGIN_ROOT . 'lib/admin/admin-options/essb-welcome.php');
	}
	
	public function essb_settings_redirect() {
		$requested = isset($_REQUEST['page']) ? $_REQUEST['page'] : "";
		
		if (strpos($requested, 'essb_redirect_') !== false) {
			$options_page = str_replace('essb_redirect_', '', $requested);
			//print $options_page;
			//print admin_url ( 'admin.php?page=essb_options&tab=' . $options_page );
			if ($options_page != '') {
				wp_redirect(admin_url ( 'admin.php?page=essb_options&tab=' . $options_page ));
			}
		}
	}

	public function essb_settings_redirect_after_easymode() {
		$requested = isset($_REQUEST['page']) ? $_REQUEST['page'] : "";
	
		if (strpos($requested, 'essb_redirect_') !== false) {
			$options_page = str_replace('essb_redirect_', '', $requested);
			//print $options_page;
			//print admin_url ( 'admin.php?page=essb_options&tab=' . $options_page );
			if ($options_page != '') {
				wp_redirect(admin_url ( 'admin.php?page=essb_options&tab=' . $options_page ));
			}
		}
		else if ($requested == "essb_options") {
			wp_redirect(admin_url ( 'admin.php?page=essb_options' ));
		}
	}
	
	
	public function essb_settings_load() {
	
		if ($this->require_easy_mode_screen()) {
			include (ESSB3_PLUGIN_ROOT . 'lib/admin/essb-choose-mode.php');
		}
		
		include (ESSB3_PLUGIN_ROOT . 'lib/admin/essb-settings42.php');
	}	
	
	public function require_easy_mode_screen() {
		
		if (!defined('ESSB3_LIGHTMODE')) {
			return false;
		}
		else {
			return true;
		}
	}
	
	public function register_admin_assets($hook) {	
		global $essb_admin_options;	
		
		$requested = isset($_REQUEST['page']) ? $_REQUEST['page'] : "";
		
		wp_register_style ( 'essb-admin-icon', ESSB3_PLUGIN_URL . '/assets/admin/easysocialshare.css', array (), ESSB3_VERSION );
		wp_enqueue_style ( 'essb-admin-icon' );

		// loading main plugin CSS to allow usage of extended controls
		wp_register_style ( 'essb-admin4', ESSB3_PLUGIN_URL . '/assets/admin/essb-admin42.css', array (), ESSB3_VERSION );
		wp_enqueue_style ( 'essb-admin4' );

		
		wp_enqueue_script ( 'essb-admin4', ESSB3_PLUGIN_URL . '/assets/admin/essb-admin42.js', array ('jquery' ), ESSB3_VERSION, true );
		
		$deactivate_fa = ESSBOptionValuesHelper::options_bool_value($essb_admin_options, 'deactivate_fa');
		// styles
		
		if (!$deactivate_fa) {
			wp_enqueue_style ( 'essb-fontawsome', ESSB3_PLUGIN_URL . '/assets/admin/font-awesome.min.css', array (), ESSB3_VERSION );
		}
		wp_enqueue_style ( 'essb-themifyicons', ESSB3_PLUGIN_URL . '/assets/admin/themify-icons.css', array (), ESSB3_VERSION );
		
		// register global admin assets only on required plugin settings pages
		if (strpos($requested, 'essb_') === false && strpos($requested, 'easy-social-metrics-lite') === false) {
			return;
		}
		
		wp_register_style ( 'essb-admin3-style', ESSB3_PLUGIN_URL . '/assets/css/easy-social-share-buttons.css', array (), ESSB3_VERSION );
		wp_enqueue_style ( 'essb-admin3-style' );
		
		

		wp_enqueue_script( 'jquery-ui-sortable' );
		wp_enqueue_style( 'wp-color-picker');
		wp_enqueue_script( 'wp-color-picker');
		
		wp_enqueue_script( 'wp-color-picker-alpha', ESSB3_PLUGIN_URL.'/assets/admin/wp-color-picker-alpha.js', array( 'wp-color-picker' ), ESSB3_VERSION );
		
		wp_register_style ( 'essb-datatable', ESSB3_PLUGIN_URL . '/assets/admin/datatable/jquery.dataTables.css', array (), ESSB3_VERSION );
		wp_enqueue_style ( 'essb-datatable' );
		wp_enqueue_script ( 'essb-datatable', ESSB3_PLUGIN_URL . '/assets/admin/datatable/jquery.dataTables.js', array ('jquery' ), ESSB3_VERSION, true );
		
		wp_enqueue_style ( 'essb-morris-styles', ESSB3_PLUGIN_URL.'/assets/admin/morris.min.css',array (), ESSB3_VERSION );
		
		wp_enqueue_script ( 'essb-morris', ESSB3_PLUGIN_URL . '/assets/admin/morris.min.js', array ('jquery' ), ESSB3_VERSION );
		wp_enqueue_script ( 'essb-raphael', ESSB3_PLUGIN_URL . '/assets/admin/raphael-min.js', array ('jquery' ), ESSB3_VERSION );
		
		//wp_enqueue_script ( 'essb-chartjs', ESSB3_PLUGIN_URL . '/assets/admin/chart.min.js', array ('jquery' ), ESSB3_VERSION );
	}
	
	public function handle_save_settings() {
		if (@$_POST && isset ( $_POST ['option_page'] )) {
			$changed = false;
			if ('essb_settings_group' == $this->getval($_POST, 'option_page' )) {
				$this->update_optons();
				$this->update_fanscounter_options();
				$this->update_wpml_translations();
				$this->update_additional_options();
				$this->restore_settings();
				$this->apply_readymade();
				//$this->apply_import();
				$changed = true;
				
				if (class_exists('ESSBDynamicCache')) {
					ESSBDynamicCache::flush();
				}
				
				if (class_exists('ESSBPrecompiledResources')) {
					ESSBPrecompiledResources::flush();
				}
				
				if (function_exists ( 'purge_essb_cache_static_cache' )) {
					purge_essb_cache_static_cache ();
				} 
			}
				
			if ($changed) {
				if (defined('ESSB3_SOCIALFANS_ACTIVE')) {
					if (class_exists('ESSBSocialFollowersCounter')) {
						essb_followers_counter()->settle_immediate_update();
						
						$current_options = get_option(ESSB3_OPTIONS_NAME);
						$fanscounter_clear_on_save = ESSBOptionValuesHelper::options_bool_value($current_options, 'fanscounter_clear_on_save');
						if ($fanscounter_clear_on_save) {
							essb_followers_counter()->clear_stored_values();
							//print "clear active";
						}
					}
				}
				
				$user_section = isset($_REQUEST['section']) ? $_REQUEST['section'] : '';
				$user_subsection = isset($_REQUEST['subsection']) ? $_REQUEST['subsection'] : '';
				
				//$goback = add_query_arg ( 'settings-updated', 'true', wp_get_referer () );
				$goback = esc_url_raw(add_query_arg(array('settings-updated' => 'true', 'section' => $user_section, 'subsection' => $user_subsection), wp_get_referer ()));
				//$goback = str_replace('#038;', '', $goback);
				wp_redirect ( $goback );
				//print  (int) $_SERVER['CONTENT_LENGTH'];
				
				die ();
			}
		}
		
		if (@$_REQUEST && isset($_REQUEST['import2x'])) {
			$this->apply_import();
			
			$user_section = isset($_REQUEST['section']) ? $_REQUEST['section'] : '';
			$user_subsection = isset($_REQUEST['subsection']) ? $_REQUEST['subsection'] : '';
			
			//$goback = add_query_arg ( 'settings-updated', 'true', wp_get_referer () );
			$goback = remove_query_arg('import2x');
			$goback = esc_url_raw(add_query_arg(array('settings-imported' => 'true', 'section' => $user_section, 'subsection' => $user_subsection), wp_get_referer ()));
			
			//$goback = str_replace('#038;', '', $goback);
			wp_redirect ( $goback );
			die ();
		}
		
		if (@$_REQUEST && isset($_REQUEST['ready_style'])) {
			$this->apply_readymade();
			
			$user_section = isset($_REQUEST['section']) ? $_REQUEST['section'] : '';
			$user_subsection = isset($_REQUEST['subsection']) ? $_REQUEST['subsection'] : '';
				
			//$goback = add_query_arg ( 'settings-updated', 'true', wp_get_referer () );
			$goback = remove_query_arg('ready_style');
			$goback = esc_url_raw(add_query_arg(array('settings-imported' => 'true', 'section' => $user_section, 'subsection' => $user_subsection), wp_get_referer ()));
				
			//$goback = str_replace('#038;', '', $goback);
			wp_redirect ( $goback );
			die ();
		}
	}
	
	public function apply_import() {
		$import = isset($_REQUEST['import2x']) ? $_REQUEST['import2x'] : '';
		
		if (!empty($import)) {
			if ($import == "post") {
				$this->import_post_settings();
			}
			if ($import == "settings") {
				$this->import_plugin_settings();
			}
			if ($import == "fans") {
				$this->import_fanscounter();
			}
			if ($import == "stats") {
				$this->import_stats_data();
			}
 		}
	}
	
	public function import_stats_data() {
		global $wpdb;
		
		$table_name = $wpdb->prefix . ESSB3_TRACKER_TABLE;
		$table_version_2x = $wpdb->prefix . 'essb_click_stats';
		
		$sql = "INSERT INTO ".$table_name.' (essb_date,essb_blog_id,essb_post_id,essb_service,essb_mobile) SELECT essb_date, essb_blog_id,essb_post_id,essb_service,"false" FROM '.$table_version_2x;
		
		$wpdb->query($sql);
	}
	
	public function import_fanscounter() {
		$previous_options = get_option('essb-fans-options');
		$essb_previous_supported_items = array ('facebook', 'twitter', 'google', 'youtube', 'vimeo', 'dribbble', 'github', 'envato', 'soundcloud', 'behance', 'delicious', 'instagram', 'pinterest', 'love', 'vk', 'rss', 'posts', 'comments', 'users', 'mailchimp', 'linkedin', 'tumblr', 'steam', 'flickr', 'total' );
		
		$defaults = ESSBSocialFollowersCounterHelper::options_structure();
		
		$facebook_id = $previous_options['social']['facebook']['id'];
		$facebook_token = $previous_options['social']['facebook']['token'];
		
		$defaults['facebook']['id'] = $facebook_id;
		$defaults['facebook']['access_token'] = $facebook_token;

		$twitter_id = $previous_options['social']['twitter']['id'];
		$twitter_key = $previous_options['social']['twitter']['key'];
		$twitter_secret = $previous_options['social']['twitter']['secret'];
		$twitter_token = $previous_options['social']['twitter']['token'];
		$twitter_tokensecret = $previous_options['social']['twitter']['tokensecret'];
		
		$defaults['twitter']['id'] = $twitter_id;
		$defaults['twitter']['consumer_key'] = $twitter_key;
		$defaults['twitter']['consumer_secret'] = $twitter_secret;
		$defaults['twitter']['access_token'] = $twitter_token;
		$defaults['twitter']['access_token_secret'] = $twitter_tokensecret;
		
		$google_id = $previous_options['social']['google']['id'];
		$google_type = $previous_options['social']['google']['type'];
		$google_api = $previous_options['social']['google']['api'];

		$defaults['google']['id'] = $google_id;
		$defaults['google']['api_key'] = $google_api;

		$defaults['youtube']['id'] = $previous_options['social']['youtube']['id'];
		$defaults['vimeo']['id'] = $previous_options['social']['vimeo']['id'];
		$defaults['pinterest']['id'] = $previous_options['social']['pinterest']['id'];
		$defaults['vk']['id'] = $previous_options['social']['vk']['id'];
		
		$defaults['instgram']['id'] = $previous_options['social']['instagram']['id'];
		$defaults['instgram']['api_key'] = $previous_options['social']['instagram']['api'];

		$defaults['mailchimp']['list_id'] = $previous_options['social']['mailchimp']['id'];
		$defaults['mailchimp']['api_key'] = $previous_options['social']['mailchimp']['api'];
		$defaults['mailchimp']['list_url'] = $previous_options['social']['mailchimp']['url'];
		
		$defaults['tumblr']['id'] = $previous_options['social']['tumblr']['id'];
		$defaults['tumblr']['api_key'] = $previous_options['social']['tumblr']['key'];
		$defaults['tumblr']['api_secret'] = $previous_options['social']['tumblr']['secret'];
		$defaults['tumblr']['access_token'] = $previous_options['social']['tumblr']['token'];
		$defaults['tumblr']['access_token_secret'] = $previous_options['social']['tumblr']['tokensecret'];
		
		$new_options = array();
		foreach ($defaults as $network => $options) {
			foreach ($options as $key => $value) {
				$settings_key = "essb3fans_".$network."_".$key;
				$new_options[$settings_key] = $value;
			}
		}
		
		update_option(ESSB3_OPTIONS_NAME_FANSCOUNTER, $new_options);
	}
	
	public function import_plugin_settings() {
		$previous_options = get_option('easy-social-share-buttons');
		
		if (!is_array($previous_options)) {
			return;
		}
		
		// basic change of settings;
		//activate_total_counter_text
		$activate_total_counter_text_value = ESSBOptionValuesHelper::options_value($previous_options, 'activate_total_counter_text_value');
		if (!empty($activate_total_counter_text_value)) {
			$previous_options['activate_total_counter_text'] = $activate_total_counter_text_value;
		}
		
		// button_style
		$hide_social_name = ESSBOptionValuesHelper::options_value($previous_options, 'hide_social_name'); // 1
		$force_hide_social_name = ESSBOptionValuesHelper::options_value($previous_options, 'force_hide_social_name'); // true
		$force_hide_icons = ESSBOptionValuesHelper::options_value($previous_options, 'force_hide_icons'); // true
		$force_hide_total_count = ESSBOptionValuesHelper::options_value($previous_options, 'force_hide_total_count'); // true
		
		$button_style = 'button';
		if ($hide_social_name == '1') {
			$button_style = 'icon_hover';
		}
		if ($force_hide_social_name == 'true') {
			$button_style = 'icon';
		}
		if ($force_hide_icons == 'true') {
			$button_style = 'button_name';
		}
		$previous_options ['button_style'] = $button_style;
		
		if ($force_hide_total_count == 'true') {
			$previous_options ['total_counter_pos'] = 'hidden';
		}
		
		$previous_options ['button_position'] = array();
		// display position
		$display_where = ESSBOptionValuesHelper::options_value ( $previous_options, 'display_where' );
		if ($display_where == "top" || $display_where == "bottom" || $display_where == "both" || $display_where == "float") {
			$previous_options['content_position'] = 'content_'.$display_where;
		} else if ($display_where == 'likeshare') {
			$previous_options['content_position'] = 'content_nativeshare';
		} else if ($display_where == 'sharelike') {
			$previous_options['content_position'] = 'content_sharenative';
		} else {
			
			if ($display_where == 'sidebar') {
				$previous_options['button_position'][] = 'sidebar';
			}
			if ($display_where == 'popup') {
				$previous_options['button_position'][] = 'popup';
			}
			if ($display_where == 'flyin') {
				$previous_options['button_position'][] = 'flyin';
			}
			if ($display_where == 'postfloat') {
				$previous_options['button_position'][] = 'postfloat';
			}
		}
		
		// active networks
		$networks = ESSBOptionValuesHelper::options_value ( $previous_options, 'networks' ); // array
		$user_networks = array ();
		if (is_array ( $networks )) {
			foreach ( $networks as $k => $v ) {
				$is_active = ($v [0] == 1) ? true : false;
				$network_name = $v [1];
				if (empty ( $network_name )) {
					$network_name = "-";
				}
				
				$network_option_value = "user_network_name_" . $k;
				
				if ($is_active) {
					$user_networks [] = $k;
				}
				$previous_options [$network_option_value] = $network_name;
			}
		}
		
		$previous_options ['networks'] = $user_networks;
		
		// for compatibility we activate native buttons
		$previous_options ['native_active'] = true;
		$previous_options ['fanscounter_active'] = true;
		
		update_option(ESSB3_OPTIONS_NAME, $previous_options);
		update_option('essb2_converted', 'true');
		
		$stats_active = ESSBOptionValuesHelper::options_value($previous_options, 'stats_active');
		if ($stats_active) {
			ESSBSocialShareAnalyticsBackEnd::install();
		}
	}
	
	public function import_post_settings() {
		global $wpdb;
		$post_types = array ("post", "page" );
		$querydata = new WP_Query ( array ('posts_per_page' => - 1, 'post_status' => 'publish', 'post_type' => $post_types ) );
		$translation_settings_map = array ();
		$translation_settings_map ['essb_position'] = 'complex';
		$translation_settings_map ['essb_theme'] = 'essb_post_template';
		$translation_settings_map ['essb_names'] = 'complex';
		$translation_settings_map ['essb_counter'] = 'essb_post_counters';
		$translation_settings_map ['essb_counter_pos'] = 'essb_post_counter_pos';
		$translation_settings_map ['essb_total_counter_pos'] = 'essb_post_total_counter_pos';
		$translation_settings_map ['essb_hidefb'] = 'complex';
		$translation_settings_map ['essb_hideplusone'] = 'complex';
		$translation_settings_map ['essb_hidevk'] = 'complex';
		$translation_settings_map ['essb_hidetwitter'] = 'complex';
		$translation_settings_map ['essb_hideyoutube'] = 'complex';
		$translation_settings_map ['essb_hidepinfollow'] = 'complex';
		$translation_settings_map ['essb_another_display_sidebar'] = 'essb_post_button_position_sidebar';
		$translation_settings_map ['essb_another_display_popup'] = 'essb_post_button_position_popup';
		$translation_settings_map ['essb_another_display_postfloat'] = 'essb_post_button_position_postfloat';
		$translation_settings_map ['essb_another_display_flyin'] = 'essb_post_button_position_flyin';
		$translation_settings_map ['essb_activate_customizer'] = 'essb_post_animations';
		$translation_settings_map ['essb_opt_by_bp'] = 'essb_post_optionsbp';
		$translation_settings_map ['essb_animation'] = 'essb_post_animations';
		$translation_settings_map ['essb_activate_nativeskinned'] = 'essb_post_native_skin';
		
		if ($querydata->have_posts ()) {
			while ( $querydata->have_posts () ) {
				$querydata->the_post ();
				global $post;
				$post_id = $post->ID;
				
				foreach ( $translation_settings_map as $old_key => $new_key ) {
					$value = get_post_meta ( $post_id, $old_key, true );
					
					if (empty ( $value )) {
						continue;
					}
					
					if ($old_key == "essb_theme") {
						$value = essb_template_folder($value);
					}
					
					if ($new_key != 'complex') {
						if ($value == '0') {
							$value = 'no';
						}
						if ($value == '1') {
							$value = 'yes';
						}
						
						
						
						$this->save_metabox_value_simple ( $post_id, $new_key, $value );
					} else {
						switch ($old_key) {
							case "essb_position" :
								break;
							case "essb_names" :
								$new_value = "";
								if ($value == "0") {
									$new_value = "button";
								} else {
									$new_value = "icon_hover";
								}
								$this->save_metabox_value_simple ( $post_id, 'essb_post_button_style', $new_value );
								break;
							case "essb_hidefb" :
							case "essb_hideplusone" :
							case "essb_hidevk" :
							case "essb_hidetwitter" :
							case "essb_hideyoutube" :
							case "essb_hidepinfollow" :
								if ($value == "1") {
									$this->save_metabox_value_simple ( $post_id, 'essb_post_native', 'no' );
								}
								break;
							case "essb_position" :
								if ($value == "top" || $value == "bottom" || $value == "both" || $value == "float") {
									$this->save_metabox_value_simple ( $post_id, 'essb_post_content_position', 'content_' . $value );
								} else if ($value == 'likeshare') {
									$this->save_metabox_value_simple ( $post_id, 'essb_post_content_position', 'content_nativeshare' );
								} else if ($value == 'sharelike') {
									$this->save_metabox_value_simple ( $post_id, 'essb_post_content_position', 'content_sharenative' );
								} else {
									$this->save_metabox_value_simple ( $post_id, 'essb_post_content_position', 'no' );
									
									if ($value == 'sidebar') {
										$this->save_metabox_value_simple ( $post_id, 'essb_post_button_position_sidebar', 'yes' );
									}
									if ($value == 'popup') {
										$this->save_metabox_value_simple ( $post_id, 'essb_post_button_position_popup', 'yes' );
									}
									if ($value == 'flyin') {
										$this->save_metabox_value_simple ( $post_id, 'essb_post_button_position_flyin', 'yes' );
									}
									if ($value == 'postfloat') {
										$this->save_metabox_value_simple ( $post_id, 'essb_post_button_position_postfloat', 'yes' );
									}
								}
								break;
						}
					}
				}
			}
		}
	}
	
	public function restore_settings() {
		//essb_backup[configuration]
		$result = false;
		
		$backup_element = isset($_REQUEST['essb_backup']) ? $_REQUEST['essb_backup'] : array();
		
		$backup_string = isset($backup_element['configuration1']) ? $backup_element['configuration1'] : '';
		if ($backup_string != '') {
			$backup_string = htmlspecialchars_decode ( $backup_string );
			$backup_string = stripslashes ( $backup_string );
				
			$imported_options = json_decode ( $backup_string, true );
			
			if (is_array($imported_options)) {
				$result = true;
				update_option(ESSB3_OPTIONS_NAME, $imported_options);
			}
		}
		
		if (isset($_FILES['essb_backup_file'])) {
			$import_file = $_FILES['essb_backup_file']['tmp_name'];
			if( !empty( $import_file ) ) {
			// Retrieve the settings from the file and convert the json object to an array.
				$settings = (array) json_decode( file_get_contents( $import_file ) );
				update_option( ESSB3_OPTIONS_NAME, $settings );
			}
		}
		
		$backup_element = isset($_REQUEST['essb_backup2']) ? $_REQUEST['essb_backup2'] : array();
		
		$backup_string = isset($backup_element['configuration1']) ? $backup_element['configuration1'] : '';
		if ($backup_string != '') {
			$backup_string = htmlspecialchars_decode ( $backup_string );
			$backup_string = stripslashes ( $backup_string );
		
			$imported_options = json_decode ( $backup_string, true );
				
			if (is_array($imported_options)) {
				$result = true;
				update_option(ESSB3_OPTIONS_NAME_FANSCOUNTER, $imported_options);
			}
		}
		
		return $result;
	}
	
	public function apply_readymade() {
		$ready_made_string = isset($_REQUEST['ready_style']) ? $_REQUEST['ready_style'] : '';

		if ($ready_made_string != '') {
			include_once(ESSB3_PLUGIN_ROOT . '/lib/admin/essb-readymade-styles.php');
			
			$exist_setting = isset($ready_made_styles[$ready_made_string]) ? $ready_made_styles[$ready_made_string] : '';
			
			if (!empty($exist_setting)) {
				$new_options = ESSB_Manager::convert_ready_made_option($exist_setting);
				update_option(ESSB3_OPTIONS_NAME, $new_options);
			}
			
		}
	}
	
	public function update_additional_options() {
		global $essb_navigation_tabs, $essb_sidebar_sections, $essb_section_options;
		
		$current_tab = isset($_REQUEST['tab']) ? $_REQUEST['tab'] : '';
		$user_options = array();
		$mapped_options = array();
		
		if ($current_tab == '') {
			return;
		}
		
		$options = $essb_section_options[$current_tab];
		
		foreach($options as $section => $fields) {
			$section_options = $fields;
		
			foreach ($section_options as $option) {
				$type = $option['type'];
				$id = isset($option['id']) ? $option['id'] : '';
		
				if ($id == '') {
					continue;
				}
		
				if (strpos($id, '|') === false) {
					continue;
				}
		
				$data_components = explode('|', $id);
				$settings_group = $data_components[0];
				$id = $data_components[1];
				
				if (!isset($user_options[$settings_group])) {
					$user_options[$settings_group] = isset($_REQUEST[$settings_group]) ? $_REQUEST[$settings_group] : array();
					$mapped_options[$settings_group] = array();
				}
		
				switch ($type) {
					case "checkbox_list_sortable":
						$option_value = isset($user_options[$settings_group][$id]) ? $user_options[$settings_group][$id] : '';
						$mapped_options[$settings_group][$id] = $option_value;
		
						$option_value = isset($user_options[$settings_group][$id.'_order']) ? $user_options[$settings_group][$id.'_order'] : '';
						$mapped_options[$settings_group][$id.'_order'] = $option_value;
						break;
					default:
						$option_value = isset($user_options[$settings_group][$id]) ? $user_options[$settings_group][$id] : '';
						$mapped_options[$settings_group][$id] = $option_value;
						break;
				}
			}
		}		
		
		foreach ($mapped_options as $key => $fields) {
			$settings_key = ESSBOptionsFramework::option_keys_to_settings($key);
			
			if ($settings_key != '') {
				$current_options = get_option($settings_key);
				if (!is_array($current_options)) {
					$current_options = array();
				}
				
				foreach ($fields as $field_key => $field_value) {
					$current_options[$field_key] = $field_value;
				}
				
				$current_options = $this->clean_blank_values($current_options);
				
				update_option($settings_key, $current_options);
			}
		}
		
		
	}
	
	public function update_fanscounter_options() {
		global $essb_navigation_tabs, $essb_sidebar_sections, $essb_section_options;
		
		$current_options = get_option(ESSB3_OPTIONS_NAME_FANSCOUNTER);
		if (!is_array($current_options)) {
			$current_options = array();
		}
		
		$current_tab = isset($_REQUEST['tab']) ? $_REQUEST['tab'] : '';
		$user_options = isset($_REQUEST['essb_options_fans']) ? $_REQUEST['essb_options_fans'] : array();
		
		if ($current_tab == '') {
			return;
		}
		
		$options = $essb_section_options[$current_tab];
		
		foreach($options as $section => $fields) {
			$section_options = $fields;
				
			foreach ($section_options as $option) {
				$type = $option['type'];
				$id = isset($option['id']) ? $option['id'] : '';
		
				if ($id == '') {
					continue;
				}
		
				if (strpos($id, 'essb3fans_') === false) {
					continue;
				}
				if (strpos($id, '|') !== false) {
					continue;
				}
						
		
				switch ($type) {
					case "checkbox_list_sortable":
						$option_value = isset($user_options[$id]) ? $user_options[$id] : '';
						$current_options[$id] = $option_value;
		
						$option_value = isset($user_options[$id.'_order']) ? $user_options[$id.'_order'] : '';
						$current_options[$id.'_order'] = $option_value;
						break;
					default:
						$option_value = isset($user_options[$id]) ? $user_options[$id] : '';
						$current_options[$id] = $option_value;
				
						break;
				}
			}
		}
		//print_r($current_options);
		update_option(ESSB3_OPTIONS_NAME_FANSCOUNTER, $current_options);
		
		// clear cached timeouts for social networks
		if (defined('ESSB3_SOCIALFANS_ACTIVE')) {
			if (class_exists('ESSBSocialFollowersCounter')) {
				essb_followers_counter()->settle_immediate_update();
			}
		}

	}

	public function update_wpml_translations() {
		global $essb_navigation_tabs, $essb_sidebar_sections, $essb_section_options;
	
		$current_options = get_option(ESSB3_WPML_OPTIONS_NAME);
		if (!is_array($current_options)) {
			$current_options = array();
		}
	
		$current_tab = isset($_REQUEST['tab']) ? $_REQUEST['tab'] : '';
		$user_options = isset($_REQUEST['essb_options']) ? $_REQUEST['essb_options'] : array();
	
		if ($current_tab == '') {
			return;
		}
	
		$options = $essb_section_options[$current_tab];
	
		foreach($options as $section => $fields) {
			$section_options = $fields;
	
			foreach ($section_options as $option) {
				$type = $option['type'];
				$id = isset($option['id']) ? $option['id'] : '';
	
				if ($id == '') {
					continue;
				}
	
				if (strpos($id, 'wpml_') === false) {
					continue;
				}
	
	
				switch ($type) {
					case "checkbox_list_sortable":
						$option_value = isset($user_options[$id]) ? $user_options[$id] : '';
						$current_options[$id] = $option_value;
	
						$option_value = isset($user_options[$id.'_order']) ? $user_options[$id.'_order'] : '';
						$current_options[$id.'_order'] = $option_value;
						break;
					default:
						$option_value = isset($user_options[$id]) ? $user_options[$id] : '';
						$current_options[$id] = $option_value;
	
						break;
				}
			}
		}
		//print_r($current_options);
		update_option(ESSB3_WPML_OPTIONS_NAME, $current_options);
	
	}
	
	
	public function update_optons() {
		global $essb_navigation_tabs, $essb_sidebar_sections, $essb_section_options;
		
		$current_options = get_option(ESSB3_OPTIONS_NAME);
		if (!is_array($current_options)) { $current_options = array(); }
		
		$current_tab = isset($_REQUEST['tab']) ? $_REQUEST['tab'] : '';
		$user_options = isset($_REQUEST['essb_options']) ? $_REQUEST['essb_options'] : array();
		
		$reset_settings = isset($_REQUEST['reset_settings']) ? $_REQUEST['reset_settings'] : '';
		
		//print_r($user_options);
		
		if ($current_tab == '') { return; }
		
		if ($current_tab == 'advanced') {
			$this->temporary_activate_post_type_settings();
		}
		
		// @since 4.1 change from social to where
		if ($current_tab == "where") {
			$this->temporary_activate_positions_by_posttypes();
		}
		
		$options = $essb_section_options[$current_tab];
		
		foreach($options as $section => $fields) {
			$section_options = $fields;
			
			foreach ($section_options as $option) {
				$type = $option['type'];
				$id = isset($option['id']) ? $option['id'] : '';
				
				if ($type == 'panel_start') {
					$element_options = isset($option['element_options']) ? $option['element_options']: array();
					$switch_id = isset($element_options['switch_id']) ? $element_options['switch_id'] : '';
					if ($switch_id != '') { $id = $switch_id; }
				}
				
				if ($id == '') { continue; }
				
				if (strpos($id, 'essb3fans_') !== false) { continue; }
				// handle separate WPML translations since version 4.1
				if (strpos($id, 'wpml_') !== false) {
					continue;
				}
				if (strpos($id, '|') !== false) {
					continue;
				}
				
				// custom ID parser for functions
				if ($id == 'essb3_options_template_select') {
					$id = 'style';
				}
				
				if ($id == 'essb3_network_selection') {
					$type = "network_select";
				}
				if ($id == "essb3_network_rename") {
					$type = "network_rename";
				}
				if ($id == "essb3_post_type_select") {
					$id = "display_in_types";
				}
				if ($id == "essb3_esml_post_type_select") {
					$id = "esml_monitor_types";
				}
				
				if ($id == 'essb3_network_selection' && defined('ESSB3_LIGHTMODE')) {
					$twitteruser =  isset($user_options['twitteruser']) ? $user_options['twitteruser'] : '';
					$current_options['twitteruser'] = $twitteruser;

					$twitterhashtags =  isset($user_options['twitterhashtags']) ? $user_options['twitterhashtags'] : '';
					$current_options['twitterhashtags'] = $twitterhashtags;
				}
				
				// quick setup options
				if ($id == "quick_setup_recommended") {
					$current_options['twitter_shareshort'] = 'true';
					$current_options['twitter_shareshort_service'] = 'wp';
					$current_options['twitter_message_optimize'] = 'true';
					$current_options['facebookadvanced'] = 'false';
					$current_options['buffer_twitter_user'] = 'true';
				}
				
				if ($id == "quick_setup_static") {
					$current_options['use_minified_css'] = 'true';
					$current_options['use_minified_js'] = 'true';
					$current_options['load_js_async'] = 'true';
					$current_options['load_css_footer'] = 'true';
				}
				
				if ($id == 'quick_setup_easy') {
					update_option(ESSB3_EASYMODE_NAME, 'true');
				}
				
				switch ($type) {
					case "network_rename":
						$option_value = isset($_REQUEST['essb_options_names']) ? $_REQUEST['essb_options_names'] : array();
						
						foreach ($option_value as $key => $value) {
							$network_option_value = "user_network_name_".$key;
							$current_options[$network_option_value] = $value;
						}
						
						break;
					case "network_select":
						$option_value = isset($user_options['networks']) ? $user_options['networks'] : array();
						$current_options['networks'] = $option_value;
						$option_value = isset($user_options['networks_order']) ? $user_options['networks_order'] : array();
						$current_options['networks_order'] = $option_value;
						
						// new network name handling
						$option_value = isset($_REQUEST['essb_options_names']) ? $_REQUEST['essb_options_names'] : array();
						
						foreach ($option_value as $key => $value) {
							$network_option_value = "user_network_name_".$key;
							$current_options[$network_option_value] = $value;
						}
						break;
					case "checkbox_list_sortable":
						$option_value = isset($user_options[$id]) ? $user_options[$id] : '';
						$current_options[$id] = $option_value;
						
						$option_value = isset($user_options[$id.'_order']) ? $user_options[$id.'_order'] : '';
						$current_options[$id.'_order'] = $option_value;
						break;
					default:
						$option_value = isset($user_options[$id]) ? $user_options[$id] : '';
						$current_options[$id] = $option_value;
						
						if ($id == "stats_active") {
							if ($option_value == "true") {
								ESSBSocialShareAnalyticsBackEnd::install();
							}
						}
						
						break;
				}
			}
		}
		
		$current_options = $this->clean_blank_values($current_options);
		
		// initially reset plugin settings to default one
		if ($reset_settings == 'true') {
			$current_options = array();
			
			$default_options = ' ';
				
			$options_base = ESSB_Manager::convert_ready_made_option($default_options);
			//print_r($options_base);
			if ($options_base) {
				$current_options = $options_base;
			}
		}
		update_option(ESSB3_OPTIONS_NAME, $current_options);
		
		$esml_active = ESSBOptionValuesHelper::options_bool_value($current_options, 'esml_active');
		if (!$esml_active) {
			delete_option ( "esml_version" );
			$this->removeAllQueuedMetricsUpdates();
		}
		
		//$purchase_code = ESSBOptionValuesHelper::options_value($current_options, 'purchase_code');
		//if ($purchase_code == '') {
		//	delete_transient('essb3-activate-notice');
		//}
	}
	
	function temporary_activate_positions_by_posttypes() {
		global $wp_post_types;
	
		/*$pts = get_post_types ( array ('show_ui' => true, '_builtin' => true ) );
		$cpts = get_post_types ( array ('show_ui' => true, '_builtin' => false ) );
		$first_post_type = "";
		$key = 1;
		foreach ( $pts as $pt ) {
			if (empty ( $first_post_type )) {
				$first_post_type = $pt;
				ESSBOptionsStructureHelper::menu_item ( 'display', 'positionspost', __ ( 'Display Positions by Post Type', 'essb' ), 'default', 'activate_first', 'positionspost-1' );
			}
			ESSBOptionsStructureHelper::submenu_item ( 'display', 'positionspost-' . $key, $wp_post_types [$pt]->label );
	
			ESSBOptionsStructureHelper::field_heading('display', 'positionspost-' . $key, 'heading1', __('Customize button positions for: '.$wp_post_types [$pt]->label, 'essb'));
			ESSBOptionsStructureHelper::field_image_radio('display', 'positionspost-' . $key, 'content_position_'.$pt, __('Primary content display position', 'essb'), __('Choose default method that will be used to render buttons inside content', 'essb'), essb_avaliable_content_positions());
			ESSBOptionsStructureHelper::field_image_checkbox('display', 'positionspost-' . $key, 'button_position_'.$pt, __('Additional button display positions', 'essb'), __('Choose additional display methods that can be used to display buttons.', 'essb'), essb_available_button_positions());
	
			$key ++;
		}
	
		foreach ( $cpts as $cpt ) {
			ESSBOptionsStructureHelper::submenu_item ( 'display', 'positionspost-' . $key, $wp_post_types [$cpt]->label );
	
			ESSBOptionsStructureHelper::field_heading('display', 'positionspost-' . $key, 'heading1', __('Customize button positions for: '.$wp_post_types [$cpt]->label, 'essb'));
			ESSBOptionsStructureHelper::field_image_radio('display', 'positionspost-' . $key, 'content_position_'.$cpt, __('Primary content display position', 'essb'), __('Choose default method that will be used to render buttons inside content', 'essb'), essb_avaliable_content_positions());
			ESSBOptionsStructureHelper::field_image_checkbox('display', 'positionspost-' . $key, 'button_position_'.$cpt, __('Additional button display positions', 'essb'), __('Choose additional display methods that can be used to display buttons.', 'essb'), essb_available_button_positions());
			$key ++;
		}*/
		ESSBOptionsStructureHelper::panel_start('where', 'display-2', __('I wish to have different button position for different post types', 'essb'), __('Activate this option if you wish to setup different positions for each post type.', 'essb'), 'fa21 fa fa-cogs', array("mode" => "switch", 'switch_id' => 'positions_by_pt', 'switch_on' => __('Yes', 'essb'), 'switch_off' => __('No', 'essb')));
		$pts = get_post_types ( array ('show_ui' => true, '_builtin' => true ) );
		$cpts = get_post_types ( array ('show_ui' => true, '_builtin' => false ) );
		$first_post_type = "";
		$key = 1;
		foreach ( $pts as $pt ) {
		
			ESSBOptionsStructureHelper::field_heading('where', 'display-2', 'heading5', __('Customize button positions for: '.$wp_post_types [$pt]->label, 'essb'));
			ESSBOptionsStructureHelper::structure_row_start('where', 'display-2');
			ESSBOptionsStructureHelper::structure_section_start('where', 'display-2', 'c6', __('Primary content display position', 'essb'), __('Choose default in content position that will be used for that post type', 'essb'));
			ESSBOptionsStructureHelper::field_select('where', 'display-2', 'content_position_'.$pt, '', '', essb_simplified_radio_check_list(essb_avaliable_content_positions(), true));
			ESSBOptionsStructureHelper::structure_section_end('where', 'display-2');
		
			ESSBOptionsStructureHelper::structure_section_start('where', 'display-2', 'c6', __('Additional button display positions', 'essb'), __('Choose additional site display position that will be used for that post type', 'essb'));
			ESSBOptionsStructureHelper::field_checkbox_list('where', 'display-2', 'button_position_'.$pt, '', '', essb_simplified_radio_check_list(essb_available_button_positions()));
			ESSBOptionsStructureHelper::structure_section_end('where', 'display-2');
			ESSBOptionsStructureHelper::structure_row_end('where', 'display-2');
		}
		
		foreach ( $cpts as $cpt ) {
		
			ESSBOptionsStructureHelper::field_heading('where', 'display-2', 'heading5', __('Customize button positions for: '.$wp_post_types [$pt]->label, 'essb'));
			ESSBOptionsStructureHelper::structure_row_start('where', 'display-2');
			ESSBOptionsStructureHelper::structure_section_start('where', 'display-2', 'c6', __('Primary content display position', 'essb'), __('Choose default in content position that will be used for that post type', 'essb'));
			ESSBOptionsStructureHelper::field_select('where', 'display-2', 'content_position_'.$cpt, '', '', essb_simplified_radio_check_list(essb_avaliable_content_positions(), true));
			ESSBOptionsStructureHelper::structure_section_end('where', 'display-2');
		
			ESSBOptionsStructureHelper::structure_section_start('where', 'display-2', 'c6', __('Additional button display positions', 'essb'), __('Choose additional site display position that will be used for that post type', 'essb'));
			ESSBOptionsStructureHelper::field_checkbox_list('where', 'display-2', 'button_position_'.$cpt, '', '', essb_simplified_radio_check_list(essb_available_button_positions()));
			ESSBOptionsStructureHelper::structure_section_end('where', 'display-2');
			ESSBOptionsStructureHelper::structure_row_end('where', 'display-2');
		
		}
		
		ESSBOptionsStructureHelper::panel_end('where', 'display-2');
	}
	
	
	function temporary_activate_post_type_settings() {
		global $wp_post_types;		
		
		$pts = get_post_types ( array ('show_ui' => true, '_builtin' => true ) );
		$cpts = get_post_types ( array ('show_ui' => true, '_builtin' => false ) );
		$first_post_type = "";
		$key = 1;
		foreach ( $pts as $pt ) {
			if (empty ( $first_post_type )) {
				$first_post_type = $pt;
				ESSBOptionsStructureHelper::menu_item ( 'advanced', 'advancedpost', __ ( 'Display Settings by Post Type', 'essb' ), 'default', 'activate_first', 'advancedpost-1' );
			}
			ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedpost-' . $key, $wp_post_types [$pt]->label );
		
			ESSBOptionsStructureHelper::field_heading('advanced', 'advancedpost-' . $key, 'heading1', __('Advanced settings for post type: '.$wp_post_types [$pt]->label, 'essb'));
			essb_prepare_location_advanced_customization ( 'advanced', 'advancedpost-' . $key, 'post-type-'.$pt, true );
			$key ++;
		}
		
		foreach ( $cpts as $cpt ) {
			ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedpost-' . $key, $wp_post_types [$cpt]->label );
			ESSBOptionsStructureHelper::field_heading('advanced', 'advancedpost-' . $key, 'heading1', __('Advanced settings for post type: '.$wp_post_types [$cpt]->label, 'essb'));
			essb_prepare_location_advanced_customization ( 'advanced', 'advancedpost-' . $key, 'post-type-'.$cpt, true );
			$key ++;
		}
		
		$key = 1;
		$cpt = 'woocommerce';
		$cpt_title = 'WooCommerce';
		ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedmodule-' . $key, $cpt_title );
		ESSBOptionsStructureHelper::field_heading ( 'advanced', 'advancedmodule-' . $key, 'heading1', __ ( 'Advanced settings for plugin: ' . $cpt_title, 'essb' ) );
		essb_prepare_location_advanced_customization ( 'advanced', 'advancedmodule-' . $key, 'post-type-' . $cpt, true );
		$key ++;
		
		$cpt = 'wpecommerce';
		$cpt_title = 'WP e-Commerce';
		ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedmodule-' . $key, $cpt_title );
		ESSBOptionsStructureHelper::field_heading ( 'advanced', 'advancedmodule-' . $key, 'heading1', __ ( 'Advanced settings for plugin: ' . $cpt_title, 'essb' ) );
		essb_prepare_location_advanced_customization ( 'advanced', 'advancedmodule-' . $key, 'post-type-' . $cpt, true );
		$key ++;
		
		$cpt = 'jigoshop';
		$cpt_title = 'JigoShop';
		ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedmodule-' . $key, $cpt_title );
		ESSBOptionsStructureHelper::field_heading ( 'advanced', 'advancedmodule-' . $key, 'heading1', __ ( 'Advanced settings for plugin: ' . $cpt_title, 'essb' ) );
		essb_prepare_location_advanced_customization ( 'advanced', 'advancedmodule-' . $key, 'post-type-' . $cpt, true );
		$key ++;
		
		$cpt = 'ithemes';
		$cpt_title = 'iThemes Exchange';
		ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedmodule-' . $key, $cpt_title );
		ESSBOptionsStructureHelper::field_heading ( 'advanced', 'advancedmodule-' . $key, 'heading1', __ ( 'Advanced settings for plugin: ' . $cpt_title, 'essb' ) );
		essb_prepare_location_advanced_customization ( 'advanced', 'advancedmodule-' . $key, 'post-type-' . $cpt, true );
		$key ++;
		
		$cpt = 'bbpress';
		$cpt_title = 'bbPress';
		ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedmodule-' . $key, $cpt_title );
		ESSBOptionsStructureHelper::field_heading ( 'advanced', 'advancedmodule-' . $key, 'heading1', __ ( 'Advanced settings for plugin: ' . $cpt_title, 'essb' ) );
		essb_prepare_location_advanced_customization ( 'advanced', 'advancedmodule-' . $key, 'post-type-' . $cpt, true );
		$key ++;
		
		$cpt = 'buddypress';
		$cpt_title = 'BuddyPress';
		ESSBOptionsStructureHelper::submenu_item ( 'advanced', 'advancedmodule-' . $key, $cpt_title );
		ESSBOptionsStructureHelper::field_heading ( 'advanced', 'advancedmodule-' . $key, 'heading1', __ ( 'Advanced settings for plugin: ' . $cpt_title, 'essb' ) );
		essb_prepare_location_advanced_customization ( 'advanced', 'advancedmodule-' . $key, 'post-type-' . $cpt, true );
		$key ++;
	}
	
	function clean_blank_values($object) {
		foreach ($object as $key => $value) {
			if (!is_array($value)) {
				$value = trim($value);
				
				if (empty($value)) {
					unset($object[$key]);
				}
			}
			else {
				if (count($value) == 0) {
					unset($object[$key]);
				}
			}
		}
		
		return $object;
	}
	
	function getval ($from, $what, $default=false) {
		if (is_object($from) && isset($from->$what)) return $from->$what;
		else if (is_array($from) && isset($from[$what])) return $from[$what];
		else return $default;
	}
	
	public function removeAllQueuedMetricsUpdates() {
		$crons = _get_cron_array();
		if ( !empty( $crons ) ) {
			foreach( $crons as $timestamp => $cron ) {
				// Remove single post updates
				if ( ! empty( $cron['easy_social_metrics_update_single_post'] ) )  {
					unset( $crons[$timestamp]['easy_social_metrics_update_single_post'] );
				}
	
				// Remove full post updates
				if ( ! empty( $cron['easy_social_metrics_lite_automatic_update'] ) )  {
					unset( $crons[$timestamp]['easy_social_metrics_lite_automatic_update'] );
				}
			}
			_set_cron_array( $crons );
				
			wp_clear_scheduled_hook('easy_social_metrics_update_single_post');
			wp_clear_scheduled_hook('easy_social_metrics_lite_automatic_update');
		}
	
		return;
	} // end removeAllQueuedUpdates()
	
	public function actions_download_settings() {
		global $essb_options;
		
		$backup_string = json_encode($essb_options);
		ignore_user_abort( true );
		nocache_headers();
		header('Content-disposition: attachment; filename=essb3-options-' . date( 'm-d-Y' ) . '.json');
		header('Content-type: application/json');
		header("Expires: 0" );
		echo $backup_string;
		exit;
	}
}

?>