<?php
if (! function_exists("theme_advance_clear_cache")) {
	function theme_advance_clear_cache($option,$data) {
		if($data == true){
			if(WP_Filesystem(array('method'=>'direct'))){
				$whitelist = array(
					'index.html',
					'skin.css',
					'skin_\d+.css',
					'images\/index.html',
					'images',
					'backup',
				);
				global $wp_filesystem;
				$files = $wp_filesystem->dirlist(THEME_CACHE_DIR, true, false);
				$pattern = "/^".implode('|',$whitelist)."$/i";

				foreach($files as $filename => $fileinfo){
					if(!preg_match($pattern, $filename)){
						$wp_filesystem->delete(trailingslashit(THEME_CACHE_DIR).$filename, true);
					}
				}
				$files = $wp_filesystem->dirlist(THEME_CACHE_DIR.'/images', true, false);
				foreach($files as $filename => $fileinfo){
					if($filename != 'index.html'){
						$wp_filesystem->delete(THEME_CACHE_DIR.'/images/'.$filename, true);
					}
				}
			}
			$posts = get_posts( array( 
				'post_type'   => 'attachment', 
				'numberposts' => -1
			));
			foreach ( $posts as $post ) {
				$metadata = wp_get_attachment_metadata($post->ID);
				if(is_array($metadata)){
					unset($metadata['custom_sizes']);
				}
				wp_update_attachment_metadata($post->ID, $metadata);
			}
		}
		return false;
	}
}
if (! function_exists("theme_advance_reset_options_process")) {
	function theme_advance_reset_options_process($option,$data) {
		if(is_array($data)){
			foreach($data as $page){
				delete_option(THEME_SLUG . '_' . $page);
			}
		}
		return false;
	}
}
if (! function_exists("theme_advance_import_option")) {
	function theme_advance_import_option($value, $default) {
		$rows = isset($value['rows']) ? $value['rows'] : '5';
		echo '<textarea id="'.$value['id'].'" rows="' . $rows . '" name="' . $value['id'] . '" type="' . $value['type'] . '" class="code">';
		echo $default;
		echo '</textarea><br />';
		echo '</td></tr>';
	}
}
if (! function_exists("theme_advance_export_option")) {
	function theme_advance_export_option($value, $default) {
		global $theme_options;
		$rows = isset($value['rows']) ? $value['rows'] : '5';
		echo '<textarea id="'.$value['id'].'" rows="' . $rows . '" name="' . $value['id'] . '" type="' . $value['type'] . '" class="code">';
		echo base64_encode(serialize($theme_options));
		echo '</textarea><br />';
		echo '</td></tr>';
	}
}
if (! function_exists("theme_advance_export_process")) {
	function theme_advance_export_process($option,$data) {
		return '';
	}
}
if (! function_exists("theme_advance_import_process")) {
	function theme_advance_import_process($option,$data) {
		if($data != ''){
			
			$options_array = unserialize( base64_decode( $data ) );
			if(is_array($options_array)){
				foreach($options_array as $name => $options){
					update_option(THEME_SLUG . '_' . $name, $options);
				}
			}
		}
		return '';
	}
}
if (! function_exists("theme_update_theme_option")) {
	function theme_update_theme_option($value, $default) {
		require_once (THEME_ADMIN_FUNCTIONS . '/upgrade.php');

		$has_update =  upgradeHelper::check();
		$url = 'update-core.php?action='.THEME_SLUG.'_theme_update';
		$url = wp_nonce_url($url, 'upgrade-'.THEME_SLUG);
		$url = network_admin_url($url);
		$theme = THEME_SLUG;
		$package = upgradeHelper::getPackage();
		$updateInfo = upgradeHelper::getUpdateInfo();
		if($has_update){
			wp_print_scripts('jquery-download');
			global $wp_version;
			$referer = get_bloginfo( 'url' );
			if(theme_get_option('advanced','item_purchase_code')==''){
				$is_purchase_code_empty = 'true';
			}else{
				$is_purchase_code_empty = 'false';
			}
			
			echo <<<HTML
	<span id="update"></span>
	<a class="button-primary" id="upgrade_button" href="{$url}">Upgrade to version {$has_update}</a>
	<a class="button" href="#" id="nightly_build_download">Download nightly build</a>
	<a href="{$updateInfo}" target="_blank">View Changes</a>
	<script type="text/javascript">
		document.getElementById('upgrade_button').onclick = function(){
			if({$is_purchase_code_empty}){
				alert('Please enter your purchase code, then click "Save Changes" button.');
				return false;
			}else{
				return confirm("Are you sure you want to upgrade your {$theme} to version {$has_update}?\\nMake sure backup your files if you had made change on the theme files.");
			}
		};

		jQuery(document).ready(function(){
			jQuery('#nightly_build_download').click(function(){
				jQuery.download('{$package}','wp_version={$wp_version}&referer={$referer}','post');
				return false;
			})

		});
	</script>
HTML;
		} else {
			$url = admin_url( 'admin.php?page=theme_advanced&check=true#update');
			echo  <<<HTML
You are using the latest version. 
	<a class="button" href="{$url}">Check for updates</a>
	<span id="update"></span>
HTML;
		}
	}
}
$options = array(
	array(
		"name" => __("Advanced",THEME_ADMIN_DOMAIN),
		"type" => "title"
	),
	array(
		"name" => __("General",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("Html Title Format",THEME_ADMIN_DOMAIN),
			"id" => "title_format",
			"default" => false,
			"type" => "text",
			"size"=>100,
			"desc" => '<code>&%s%&</code> will be replaced with the site name.<br/> <code>&%p%&</code> will be replaced with the page name.',
		),
		array(
			"name" => __("404 Page Redirect",THEME_ADMIN_DOMAIN),
			"id" => "error_page",
			"page" => 0,
			"default" => 0,
			"chosen" => "true",
			"prompt" => __("None",THEME_ADMIN_DOMAIN),
			"type" => "select",
		),
		array(
			"name" => __("Clear Cache",THEME_ADMIN_DOMAIN),
			"id" => "clear_cache",
			"default" => false,
			"process" => "theme_advance_clear_cache",
			"type" => "toggle"
		),
		array(
			"name" => __("Shortcode Support in Comment",THEME_ADMIN_DOMAIN),
			"id" => "shortcode_comment",
			"default" => false,
			"type" => "toggle"
		),
		array(
			"name" => __("Show Symplex on Admin bar",THEME_ADMIN_DOMAIN),
			"id" => "admin_bar_menu",
			"default" => true,
			"type" => "toggle"
		),
		array(
			"name" => __("Show featured image on feeds",THEME_ADMIN_DOMAIN),
			"id" => "show_post_thumbnail_on_feed",
			"default" => false,
			"type" => "toggle"
		),
		array(
			"name" => __("Reset Theme Options",THEME_ADMIN_DOMAIN),
			"id" => "rest",
			"default" => array(),
			"desc" => __('If you want reset your theme options to defualt, please checked the items below.',THEME_ADMIN_DOMAIN),
			"options" => array(
				"general" => __('General',THEME_ADMIN_DOMAIN),
				"background" => __('Background',THEME_ADMIN_DOMAIN),
				"color" => __('Color',THEME_ADMIN_DOMAIN),
				"font" => __('Font',THEME_ADMIN_DOMAIN),
				"cufon" => __('Cufon',THEME_ADMIN_DOMAIN),
				"fontface" => __('Fontface',THEME_ADMIN_DOMAIN),
				"gfont" => __('Google Fonts',THEME_ADMIN_DOMAIN),
				"slideshow" => __('SlideShow',THEME_ADMIN_DOMAIN),
				"sidebar" => __('Sidebar',THEME_ADMIN_DOMAIN),
				"image" => __('Image',THEME_ADMIN_DOMAIN),
				"media" => __('Media',THEME_ADMIN_DOMAIN),
				"homepage" => __('Homepage',THEME_ADMIN_DOMAIN),
				"blog" => __('Blog',THEME_ADMIN_DOMAIN),
				"portfolio" => __('Portfolio',THEME_ADMIN_DOMAIN),
				"footer" => __('Footer',THEME_ADMIN_DOMAIN),
			),
			"process" => "theme_advance_reset_options_process",
			"type" => "checkboxes",
		),
	array(
		"type" => "end"
	),
	array(
		"name" => __("Import & Export",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => sprintf(__("Import %s Options Data",THEME_ADMIN_DOMAIN),THEME_NAME),
			"id" => "import",
			"desc" => __('To import the values of your theme options copy and paste what appears to be a random string of alpha numeric characters into this textarea and press the "Save Changes" button below.',THEME_ADMIN_DOMAIN),
			"function" => "theme_advance_import_option",
			"process" => "theme_advance_import_process",
			"type" => "custom",
			"default"=>''
		),
		array(
			"name" => sprintf(__("Export %s Options Data",THEME_ADMIN_DOMAIN),THEME_NAME),
			"id" => "export",
			"desc" => __("Export your saved Theme Options data by highlighting this text and doing a copy/paste into a blank .txt file.",THEME_ADMIN_DOMAIN),
			"function" => "theme_advance_export_option",
			"process" => "theme_advance_export_process",
			"type" => "custom",
			"default"=>''
		),
	array(
		"type" => "end"
	),
	array(
		"name" => __("Search",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("Exclude From Search",THEME_ADMIN_DOMAIN),
			"id" => "exclude_from_search",
			"default" => array(),
			"target" => 'public_post_types',
			"type" => "checkboxes",
		),
	array(
		"type" => "end"
	),
	array(
		"name" => __("Meta Box display Settings",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("Page General Options",THEME_ADMIN_DOMAIN),
			"id" => "page_general",
			"default" => array('post','page','portfolio'),
			"target" => 'post_types',
			"type" => "checkboxes",
		),
	array(
		"type" => "end"
	),
	array(
		"name" => __("JavaScript & CSS Optimizer",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("Combine Js",THEME_ADMIN_DOMAIN),
			"id" => "combine_js",
			"default" => false,
			"type" => "toggle"
		),
		array(
			"name" => __("Combine CSS",THEME_ADMIN_DOMAIN),
			"id" => "combine_css",
			"default" => false,
			"type" => "toggle"
		),
		array(
			"name" => __("Move Js To Bottom",THEME_ADMIN_DOMAIN),
			"id" => "move_bottom",
			"default" => false,
			"type" => "toggle"
		),
	array(
		"type" => "end"
	),

	array(
		"name" => __("Update Theme",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("Item Purchase Code",THEME_ADMIN_DOMAIN),
			"id" => "item_purchase_code",
			'desc' => 'It will use for verifying the purchase before update. (how to <a href="http://kaptinlin.com/support/page/get_code.html" target="_blank">get it</a>)',
			"default" => '',
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Enable Notification",THEME_ADMIN_DOMAIN),
			"id" => "update_notification",
			"default" => true,
			"type" => "toggle"
		),
		array(
			"name" => __("Update",THEME_ADMIN_DOMAIN),
			"id" => "updating_theme",
			"desc" =>  __("Updating Theme to the latest version.",THEME_ADMIN_DOMAIN),
			"default" => false,
			"function" => "theme_update_theme_option",
			//"process" => "theme_advance_updating_portfolio_more_process",
			"type" => "custom"
		),
		/*
		array(
			"name" => __("Restore",THEME_ADMIN_DOMAIN),
			"id" => "updating_theme",
			"desc" =>  __("Restore Theme to the state before updating.",THEME_ADMIN_DOMAIN),
			"default" => false,
			//"process" => "theme_advance_updating_portfolio_more_process",
			"type" => "toggle"
		),*/
	array(
		"type" => "end"
	),
	array(
		"name" => __("Featured Header Text",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("Category Archive Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "category_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Category Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "category_text",
			"default" => __('Category Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Tag Archive Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "tag_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Tag Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "tag_text",
			"default" => __('Tag Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Daily Archive Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "daily_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Daily Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "daily_text",
			"default" => __('Daily Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Monthly Archive Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "monthly_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Monthly Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "monthly_text",
			"default" => __('Monthly Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Yearly Archive Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "yearly_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Yearly Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "yearly_text",
			"default" => __('Yearly Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Author Archive Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "author_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Author Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "author_text",
			"default" => __('Author Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Blog Archives Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "blog_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Blog Archive Text",THEME_ADMIN_DOMAIN),
			"desc" => 'Default: <code>Blog Archives</code>',
			"id" => "blog_text",
			"default" => __('Blog Archives',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Taxonomy Archives Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "taxonomy_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Taxonomy Archive Text",THEME_ADMIN_DOMAIN),
			"id" => "taxonomy_text",
			"default" => __('Archives for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("404 Page Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "404_title",
			"default" => __('Oi! 404 ;)',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("404 Page Text",THEME_ADMIN_DOMAIN),
			"id" => "404_text",
			"default" => __("Ooops, we have an small error, we cant find this page anywhere, we swear, we looked and looked, nowhere to be found... Try using the search form or sitemap below.",THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
		array(
			"name" => __("Search Page Title",THEME_ADMIN_DOMAIN),
			"desc" => '',
			"id" => "search_title",
			"default" => __('Search',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		),
		array(
			"name" => __("Search Page Text",THEME_ADMIN_DOMAIN),
			"id" => "search_text",
			"default" => __('Search Results for:',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		),
	array(
		"type" => "end"
	),
);
if(function_exists('is_post_type_archive')){
	$archives = get_post_types(array(
		'public'   => true,
		'_builtin' => false,
		'show_ui'=> true,
	),'objects');
	if ($archives) {
		if(array_key_exists('portfolio',$archives)){
			unset($archives['portfolio']);
		}
		if(!empty($archives)){

			$options[] = array(
				"name" => "Custom Post Type Archive Featured Header Text",
				"type" => "start"
			);
			foreach ($archives  as $archive ) {
				if($archive->name != 'portfolio'){
					$options[] = array(
						"name" => sprintf(__("%s Archives Title",THEME_ADMIN_DOMAIN),$archive->name),
						"desc" => '',
						"id" => "archive_".$archive->name."_title",
						"default" => __('Archives','striking_front'),
						"size" => 50,
						"type" => "text"
					);
					$options[] = array(
						"name" => sprintf(__("%s Archives Text",THEME_ADMIN_DOMAIN),$archive->name),
						"desc" => 'Default: <code>Archives for: ‘%s’</code><br> <code>%s</code> will be replaced with the post type name.',
						"id" => "archive_".$archive->name."_text",
						"default" => __('Archives for: ‘%s’',THEME_FRONT_DOMAIN),
						'rows' => '2',
						"type" => "textarea"
					);
				}
			}
			$options[] = array(
				"type" => "end"
			);
		}
	}

}
$taxonomies=get_taxonomies(array(
	'public'   => true,
	'_builtin' => false,
	'show_ui'=> true,
),'objects'); 
if ($taxonomies && !empty($taxonomies)) {
	$options[] = array(
		"name" => "Custom Taxonomy Featured Header Text",
		"type" => "start"
	);
	foreach ($taxonomies  as $taxonomy ) {
		$options[] = array(
			"name" => sprintf(__("%s Archives Title",THEME_ADMIN_DOMAIN),$taxonomy->name),
			"desc" => '',
			"id" => "taxonomy_".$taxonomy->name."_title",
			"default" => __('Archives',THEME_FRONT_DOMAIN),
			"size" => 50,
			"type" => "text"
		);
		$options[] = array(
			"name" => sprintf(__("%s Archives Text",THEME_ADMIN_DOMAIN),$taxonomy->name),
			"desc" => 'Default: <code>Archives for: ??%s??</code><br> <code>%s</code> will be replaced with the taxonomy name.',
			"id" => "taxonomy_".$taxonomy->name."_text",
			"default" => __('Archives for: ??%s??',THEME_FRONT_DOMAIN),
			'rows' => '2',
			"type" => "textarea"
		);
	}
	$options[] = array(
		"type" => "end"
	);
}
return array(
	'auto' => true,
	'name' => 'advanced',
	'options' => $options
);
