<?php
/*
/*-----------------------------------------------*/
/* KENTOOZ FRAMEWORK FOR FREE THEME
/* Website: kentooz.com
/* The Author: Gian Mokhammad Ramadhan 
/* Social network :twitter.com/g14nnakal facebook.com/gianmr
/* Version :1.0
/*-----------------------------------------------*/

// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

/*******************************************
# Register CSS on hook system ~ header
# Add googlefont and custom css @ kasep theme v.1.01
*******************************************/
function ktz_register_css() {
	if( !is_admin() ) {
		if (get_theme_option('ktz_ffh') != '') {
		$getheadinggfont = '';
		if( get_theme_option('ktz_ffh') ){
			$getheadinggfont = get_theme_option('ktz_ffh');
			$getheadinggfont = str_replace(' ', '+', $getheadinggfont);
		}
		wp_register_style( 'googlefont-css', 'http://fonts.googleapis.com/css?family='. $getheadinggfont);
		} 
		if (get_theme_option('ktz_ffb') != '') {
			$getbodygfont = '';
			if( get_theme_option('ktz_ffb') ){
				$getbodygfont = get_theme_option('ktz_ffb');
				$getbodygfont = str_replace(' ', '+', $getbodygfont);
			}
		wp_register_style( 'googlefontbody-css', 'http://fonts.googleapis.com/css?family='. $getbodygfont);
		}	
		wp_register_style( 'libs-css',ktz_url . 'css/libs.min.css', array(), '1.0', 'all' );
		wp_register_style( 'main-css',ktz_url . 'style.css',array(), '1.0', 'all' );
		if( get_theme_option('ktz_styledefault') != '' ) { 
			$styledefault = '';
			if( get_theme_option('ktz_styledefault') ){
			$styledefault = get_theme_option('ktz_styledefault');
			}
		wp_register_style( 'defaultstyle-css',ktz_url . 'css/style/' . $styledefault,array(), '1.0', 'all' ); 
		}
	}
}

/*******************************************
# Enqueue CSS on hook system ~ header
*******************************************/
function ktz_enqueue_css()  { 
	if( !is_admin() ) { 
        wp_enqueue_style( 'googlefont-css' ); 
        wp_enqueue_style( 'googlefontbody-css' ); 
        wp_enqueue_style( 'libs-css' );  
        wp_enqueue_style( 'main-css' );  
        wp_enqueue_style( 'defaultstyle-css' ); 
	}
}  

/*******************************************
# Dynamic background on hook system ~ header
*******************************************/
if ( !function_exists('ktz_dynamicbg') ) {
function ktz_dynamicbg(){
if(is_page()) {
global $wp_query;
$postid = $wp_query->post->ID;
	$page_mt = get_post_meta( $postid, 'page_meta', true );
	$bgactivated = empty($page_mt['bgactivated']) ? '' : $page_mt['bgactivated'];
	$bgcolor = empty($page_mt['bgcolor']) ? '' : $page_mt['bgcolor'];
	$bgimage = empty($page_mt['bgimage']) ? '' : $page_mt['bgimage'];
	$bgrepeat = empty($page_mt['bgrepeat']) ? '' : $page_mt['bgrepeat'];
	$bgposition = empty($page_mt['bgposition']) ? '' : $page_mt['bgposition'];
	$bgattach = empty($page_mt['bgattach']) ? '' : $page_mt['bgattach'];
	$page_id = get_the_ID();
	if ($bgactivated != '' ) { echo '<style type="text/css">body.page-id-'.$page_id.'{';
	if ($bgcolor) {	echo 'background-color:'.$bgcolor.';';}
	if ($bgimage) { echo 'background-attachment:fixed;background-image:url('.$bgimage.');';}
	if ($bgrepeat) { echo 'background-repeat:'.$bgrepeat.';';}
	if ($bgposition) { echo 'background-position:'.$bgposition.';';}
	if ($bgattach) { echo 'background-attachment:'.$bgattach.';';}
	echo '}</style>';}
	}	
if(is_single()) { 
global $wp_query;
$postid = $wp_query->post->ID;
	$post_mt = get_post_meta( $postid, 'post_meta', true );
	$bgactivated = empty($post_mt['bgactivated']) ? '' : $post_mt['bgactivated'];
	$bgcolor = empty($post_mt['bgcolor']) ? '' : $post_mt['bgcolor'];
	$bgimage = empty($post_mt['bgimage']) ? '' : $post_mt['bgimage'];
	$bgrepeat = empty($post_mt['bgrepeat']) ? '' : $post_mt['bgrepeat'];
	$bgposition = empty($post_mt['bgposition']) ? '' : $post_mt['bgposition'];
	$bgattach = empty($post_mt['bgattach']) ? '' : $post_mt['bgattach'];
	$post_id = get_the_ID();
	if ($bgactivated != '' ) { echo '<style type="text/css">body.postid-'.$post_id.'{';
	if ($bgcolor) {	echo 'background-color:'.$bgcolor.';';}
	if ($bgimage) { echo 'background-attachment:fixed;background-image:url('.$bgimage.');';}
	if ($bgrepeat) { echo 'background-repeat:'.$bgrepeat.';';}
	if ($bgposition) { echo 'background-position:'.$bgposition.';';}
	if ($bgattach) { echo 'background-attachment:'.$bgattach.';';}
	echo '}</style>';}
	}	
	}  
}  

/*******************************************
# Add favicon on hook system ~ header
*******************************************/
if ( !function_exists('ktz_head_favicon') ) {
function ktz_head_favicon() {
	if(get_theme_option('ktz_favicon') != '') {
		echo '<link rel="shortcut icon" href="'. get_theme_option('ktz_favicon') .' " />';
		} 
	}
}

/*******************************************
# Add head title on hook system ~ header
*******************************************/
/*
* Add SEO dynamic title
*/
function ktz_dynamic_title($title, $sep) {
	global $paged, $page;
	if ( is_feed() ) {
		return $title;
	}
	// Add the site name.
	$title .= get_bloginfo( 'name' );
	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) ) {
		$title = "$title $sep $site_description";
	}
	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 ) {
		$title = "$title $sep " . sprintf( __( 'Page %s', ktz_theme_textdomain ), max( $paged, $page ) );
	}
	return $title;
}
add_filter( 'wp_title', 'ktz_dynamic_title', 10, 2 );

/*******************************************
# Add element on head on hook system ~ header
*******************************************/
if ( !function_exists('ktz_headelement') ) {
function ktz_headelement() { ?>
	<link rel="profile" href="http://gmpg.org/xfn/11" />
	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
	<?php if (get_theme_option('ktz_gplus_sn') != '') { echo '<link href="' . get_theme_option('ktz_gplus_sn') . '" rel="publisher" />'; } ?>
<?php } 
} 

/*******************************************
# Dynamic background on hook system ~ header
*******************************************/
if ( !function_exists('ktz_headlogo') ) :
function ktz_headlogo() { 
	$get_logo_image = get_theme_option('ktz_logo');
	if (get_theme_option('ktz_logo_actived') != '') { 
		echo '<div id="logo" class="span3 pull-left">';
		echo '<a href="' . home_url() . '" id="logo"><img src="' . $get_logo_image . '" alt="' . get_bloginfo('name') . '" title="' . get_bloginfo('name') . '" /></a>';
		echo '<span></span></div>';
		if( (is_single() || is_page() || is_archive()) and !(is_front_page()) ) :
		echo '<h2 style="display:none"><a href="'. home_url() . '">' . get_theme_option('ktz_logo_name') . '</a></h2>';
		echo '<h3 style="display:none">' . get_theme_option('ktz_logo_desc') . '</h3>';
		else : 
		echo '<h1 style="display:none"><a href="'. home_url() . '">' . get_theme_option('ktz_logo_name') . '</a></h1>';
		echo '<h2 style="display:none">' . get_theme_option('ktz_logo_desc') . '</h2>';
		endif;
	} else { 		
		echo '<div id="logo" class="span3 pull-left">';
		if( (is_single() || is_page() || is_archive()) and !(is_front_page()) ) :
		echo '<h2 class="singleblogtit"><a href="'. home_url() . '">' . get_theme_option('ktz_logo_name') . '</a></h2>';
		echo '<h3 class="desc">' . get_theme_option('ktz_logo_desc') . '</h3>';
		else : 
		echo '<h1 class="homeblogtit"><a href="'. home_url() . '">' . get_theme_option('ktz_logo_name') . '</a></h1>';
		echo '<h2 class="desc">' . get_theme_option('ktz_logo_desc') . '</h2>';
		endif;
		echo '<span></span></div>';
		}	
	} 
endif;


/*******************************************
# Top menu on hook system ~ header
*******************************************/
if( !function_exists('ktz_topmenu')) {
	function ktz_topmenu() {
	wp_nav_menu( array(
	'container' =>false,
	'theme_location' =>'ktzmenu_1',
	'echo' => true,
	'depth' =>  3,
	'fallback_cb' => 'ktzmenu_1_default',
	'items_wrap' => '<ul id="topmenu" class="categories"><li class="home"><a href="' . home_url() .'"><span></span></a></li>%3$s</ul>',
	));
	}
}

/*******************************************
# Add default top menu on hook system ~ header
*******************************************/
if ( !function_exists('ktzmenu_1_default') ) {
function ktzmenu_1_default(){  ?>
	<ul id="topmenu" class="categories">
		<li class="home"><a href="<?php echo home_url(); ?>"><span></span></a></li>
        <?php wp_list_categories('depth=1&sort_column=menu_order&title_li=' ); ?>
    </ul>
	<?php }
}

/*******************************************
# Add default fixed menu on hook system ~ footer
*******************************************/
if ( !function_exists('ktzmenu_2_default') ) {
	function ktzmenu_2_default() {  
	?>
		<ul class="categories"><?php wp_list_pages('depth=1&exclude=1&hide_empty=0&orderby=name&show_count=0&use_desc_for_title=1&title_li='); ?></ul>
	<?php }
}

/*******************************************
# Sub footer on hook system ~ post
*******************************************/
if ( !function_exists( 'ktz_secondmenu' ) ) :
function ktz_secondmenu() {
	if(function_exists('wp_nav_menu')) { 
		wp_nav_menu( 'theme_location=ktzmenu_2&depth=1&menu_class=categories&container=&fallback_cb=ktzmenu_2_default'); 
	} else { 
		ktzmenu_2_default();  
	}
	}
endif;

/*******************************************
# Register menu on hook system ~ header
*******************************************/
if ( function_exists( 'register_nav_menus' ) ) {
	register_nav_menus(
	array(
      'ktzmenu_1' => 'Top Menu',
      'ktzmenu_2' => 'Second Menu',
    ) );
}

/*******************************************
# Search hook in header |||||||||||||||||||
*******************************************/
if( !function_exists('ktz_topsearch')) {
	function ktz_topsearch() {
	$search_text = empty($_GET['s']) ? "Search" : get_search_query();
    echo '<form method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '/">'; 
        echo '<div id="search"><input type="text" name="s" id="s" placeholder="Search" />
        <input type="submit" value="Search">
		</div>
    </form>';
	}
}

/*******************************************
# Add Module 1 in magazine homepage ####
*******************************************/
if ( !function_exists('ktz_module_feat') ) :
function ktz_module_feat() {	
	global $post;
		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		$args = array(
			'post_type' => 'post',
			'orderby' => 'comment_count',
			'order' => 'desc',
			'showposts' => 5,
			'post_status' => 'publish',
			'ignore_sticky_posts' => 1
		);
		$ktz_boxfeatquery = new WP_Query($args); 
		if ($ktz_boxfeatquery -> have_posts()) :
		echo'<ul class="listmodule-popular">';
		while ($ktz_boxfeatquery -> have_posts()) : $ktz_boxfeatquery -> the_post(); 
		echo'<li>';
		echo ktz_posted_on();
		echo '<h1 class="entry-title">';
		echo '<a href="' . get_permalink() . '" title="Permalink to ' . get_the_title() . '" rel="bookmark">';
		echo ktz_limittitle('', '', true, '55');
		echo '</a>';
		echo '</h1>';
		echo'</li>';
		endwhile;
		echo'</ul>';
		endif;
		wp_reset_query();
	}
endif;

/*******************************************
# Add Most view in single post ####
*******************************************/
if ( !function_exists('ktz_most_view') ) :
function ktz_most_view() {	
	global $post;
		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		$args = array(
			'post_type' => 'post',
			'meta_key' => 'post_views_count',
			'orderby' => 'meta_value_num',
			'order' => 'desc',
			'showposts' => 5,
			'post_status' => 'publish',
			'ignore_sticky_posts' => 1
		);
		$ktz_boxfeatquery = new WP_Query($args); 
		if ($ktz_boxfeatquery -> have_posts()) :
		echo '<div class="span4">';
		echo '<div class="mostview-single clearfix"><h3>' . __('Most view article', ktz_theme_textdomain) . '</h3></div>';
		echo'<ul class="listmodule-view">';
		while ($ktz_boxfeatquery -> have_posts()) : $ktz_boxfeatquery -> the_post(); 
		echo'<li>';
		echo '<div class="ktz-titlecust">';
		echo '<a href="' . get_permalink() . '" title="Permalink to ' . get_the_title() . '" rel="bookmark">';
		echo ktz_limittitle('', '', true, '55');
		echo ' - ';
		echo ktz_getPostViews(get_the_ID());
		echo '</a>';
		echo '</div>';
		echo'</li>';
		endwhile;
		echo'</ul></div>';
		endif;
		wp_reset_query();
	}
endif;

/*******************************************
# Add NIVO Slider in magazine homepage
*******************************************/
if ( !function_exists('ktz_feat_nivoslider') ) {
function ktz_feat_nivoslider() { 
	if ( is_home() || is_front_page() ) {
	if ( get_theme_option('ktz_nivofeatured_activated') != '' ) :
		$args = array(
		'post_type'=> 'post',
		'post_status'=> 'publish',
		'ignore_sticky_posts'=>1,
		'showposts' => 5,
		'cat' => get_theme_option('ktz_cat_nivoslider'),
		);
		$ktz_sliderquery = new WP_Query($args); 
		echo '<div class="row"><div class="slider-wrapper theme-dark span8">';
        echo '<div id="slider" class="nivoSlider">';
		while ($ktz_sliderquery -> have_posts()) : $ktz_sliderquery -> the_post(); 
		global $post;
		$permalink = get_permalink();
		$title = get_the_title();
		$postid = get_the_ID();
        echo '<a href="' . $permalink . '" title="' . $title . '">';
		echo ktz_featured_img( 640, 320, 'nivo-thumbnail' ); // width, height, class
		echo '</a>';
		endwhile;
        echo '</div></div>';
		echo '<div class="span4">';
		echo ktz_headbanner();
		echo ktz_rightfeat2banner();
		echo '</div></div>';
	endif;
		}
	}
}

/*******************************************
# Add Carousel in magazine homepage
*******************************************/
if ( !function_exists('ktz_feat_fullslider') ) {
function ktz_feat_fullslider() { 
	if ( is_home() || is_front_page() ) {
	if ( get_theme_option('ktz_featured_activated') != '' ) :
		$args = array(
		'post_type'=> 'post',
		'post_status'=> 'publish',
		'ignore_sticky_posts'=>1,
		'showposts' => 5,
		'cat' => get_theme_option('ktz_cat_slider'),
		);
		$ktz_sliderquery = new WP_Query($args); 
		if ($ktz_sliderquery -> have_posts()) :
        echo '<div class="list_carousel"><ul id="newscarausel">';
		while ($ktz_sliderquery -> have_posts()) : $ktz_sliderquery -> the_post(); 
		global $post;
		$permalink = get_permalink();
		$title = get_the_title();
		$postid = get_the_ID();
		echo '<li>';
        echo '<a href="' . $permalink . '" title="' . $title . '">';
		echo ktz_featured_img( 70, 70, 'img-carousel pull-left clearfix' ); // width, height, class
		echo '</a>';
		echo ktz_posted_on();
        echo ktz_posted_title();
		echo '</li>';
		endwhile;
        echo '</ul>';
        echo '<div class="button-carousel">';
		echo '<a id="prev-1" class="prev pull-left clearfix" href="#"><span></span></a>';
		echo '<a id="next-1" class="next pull-right clearfix" href="#"><span></span></a>';
		echo '</div></div>';
		endif;
	endif;
		}
	}
}

/*******************************************
# Breadcrumbs on hook system with rich snipped
*******************************************/
if ( !function_exists('ktz_crumbs') ) {
function ktz_crumbs() {
	if( is_front_page() )
		return;
		echo '<div id="breadcrumbs-wrap"><div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
		if ( get_theme_option('ktz_breadcrumbs') != '' ) :
		echo '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="';
        echo home_url();
        echo '">';
        echo __('Home',ktz_theme_textdomain);
        echo "</a></span>";
        if (is_category() || is_single()) {
		$category = get_the_category();
		foreach($category as $category) {
		echo '<span typeof="v:Breadcrumb"><a href="';
			echo get_category_link($category->term_id);
		echo '" rel="v:url" property="v:title">' . $category->name . '</a></span>';
			}
			if (is_single()) {
                echo '<span property="v:title">' . get_the_title() . '</span>';
			}	
        } elseif (is_page()) {
                echo '<span property="v:title">' . get_the_title() . '</span>';
        }
		elseif (is_tag()) {
                echo '<span property="v:title">';
					echo single_tag_title();
				echo '</span>';
				}
		elseif (is_day()) {echo '<span property="v:title">' . __("Archive for ",ktz_theme_textdomain); get_the_time('F jS, Y') . '</span>';}
		elseif (is_month()) {echo '<span property="v:title">' . __("Archive for ",ktz_theme_textdomain); get_the_time('F, Y') . '</span>';}
		elseif (is_year()) {echo '<span property="v:title">' . __("Archive for ",ktz_theme_textdomain); get_the_time('Y') . '</span>';}
		elseif (is_author()) {echo '<span property="v:title">' . __("Author Archive",ktz_theme_textdomain) . '</span>';}
		elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo '<span property="v:title">' . __("Blog Archives",ktz_theme_textdomain) . '</span>';}
		elseif (is_search()) {echo '<span property="v:title">' . __("Search Results",ktz_theme_textdomain) . '</span>';}
		elseif (is_404()) {echo '<span property="v:title">' . __("Page not found",ktz_theme_textdomain) . '</span>';}
		endif;
		echo '</div></div>';
	}
}