<?php 
/**
 * @package 	WordPress
 * @subpackage 	Top Model
 * @version		1.1.6
 * 
 * Admin Panel Helper Functions
 * Created by CMSMasters
 * 
 */


function top_model_get_admin_page() {
	global $pagenow;
	
	$current_page = (isset($_GET['page'])) ? trim($_GET['page']) : '';
	
	if ($pagenow == 'options.php' && isset($_POST['_wp_http_referer'])) {
		$parts = explode('page=', $_POST['_wp_http_referer']);
		
		if (isset($parts[1])) {
			$page = $parts[1];
			$t = strpos($page, '&');
			
			if ($t !== false) {
				$page = substr($parts[1], 0, $t);
			}
			
			$current_page = trim($page);
		} else {
			$current_page = false;
		}
	}
	
	return $current_page;
}


function top_model_default_tab() {
	$current_page = top_model_get_admin_page();
	
	if ($current_page == 'cmsmasters-settings') {
		$default_tab = 'general';
	} elseif ($current_page == 'cmsmasters-settings-font') {
		$default_tab = 'content';
	} elseif ($current_page == 'cmsmasters-settings-color') {
		$default_tab = 'default';
	} elseif ($current_page == 'cmsmasters-settings-element') {
		$default_tab = 'sidebar';
	} elseif ($current_page == 'cmsmasters-settings-single') {
		$default_tab = 'post';
	} elseif ($current_page == 'cmsmasters-settings-demo') {
		$default_tab = 'import';
	} else {
		$default_tab = 'general';
	}
	
	return $default_tab;
}


function top_model_get_the_tab() {
	global $pagenow;
	
	$default_tab = top_model_default_tab();
	
	$current_tab = (isset($_GET['tab'])) ? $_GET['tab'] : $default_tab;
	
	if ($pagenow == 'options.php' && isset($_POST['_wp_http_referer'])) {
		$parts = explode('&tab=', $_POST['_wp_http_referer']);
		
		$partsNum = count($parts);
		
		if (isset($parts[1])) {
			$settings_updated = strpos($parts[1], '&');
			
			$tab_name = ($settings_updated !== false) ? substr($parts[1], 0, $settings_updated) : $parts[1];
			
			$current_tab = ($partsNum == 2) ? trim($tab_name) : $default_tab;
		}
	}
	
	return $current_tab;
}


function top_model_settings_page_header() {
    $settings_output = top_model_get_settings();
	$tabs = $settings_output['cmsmasters_page_tabs'];
	$current_tab = top_model_get_the_tab();
	
	echo '<div id="icon-options-general" class="icon32">' . 
		'<br />' . 
	'</div>' . 
	'<h2 style="padding-top:12px;">' . esc_html($settings_output['cmsmasters_page_title']) . '</h2>';
    
	if ($tabs != '') {
		$links = array();
		
		foreach ($tabs as $tab => $name) {
			$class = (($tab == $current_tab) ? 'nav-tab nav-tab-active' : 'nav-tab');
			
			$page = $_GET['page'];
			
			$links[] = '<a class="' . $class . '" href="?page=' . esc_attr($page) . '&tab=' . esc_attr($tab) . '"' . (($tab == 'recaptcha') ? ' style="background-color:#fdffc6;' . (($class == 'nav-tab nav-tab-active') ? ' border-bottom-color:#fdffc6;' : '') . '"' : '') . '>' . esc_html($name) . '</a>';
		}
		
		
		if (class_exists('Cmsmasters_Custom_Fonts') && $page == 'cmsmasters-settings-font') {
			$links[] = '<a class="' . $class . '" href="edit.php?post_type=cmsmasters_font">' . esc_html__('Custom Fonts', 'top-model') . '</a>';
		}
		
		
		echo '<h2 class="nav-tab-wrapper">';
		
		foreach ($links as $link) {
			echo wp_kses($link, 'post');
		}
		
		echo '</h2>';
	}
}

