<?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.' ); }

/*******************************************
# Add post title on hook system ~ SEO TITLE
*******************************************/
if ( !function_exists('ktz_posted_title') ) :
function ktz_posted_title() {
	if( (is_single() || is_page()) and !(is_front_page()) ) :
	printf( __( '<h1 class="entry-title"><a href="%1$s" title="Go to %2$s" target="_blank" rel="bookmark">%3$s</a></h1>', ktz_theme_textdomain ),
		esc_url(get_permalink()),
		esc_attr(get_the_title()),
		esc_attr(get_the_title())
	);
	else :
	printf( __( '<h2 class="entry-title"><a href="%1$s" title="Permalink to %2$s" rel="bookmark">%3$s</a></h2>', ktz_theme_textdomain ),
		esc_url(get_permalink()),
		esc_attr(get_the_title()),
		esc_attr(get_the_title())
	);
	endif;
	}
endif;

/*******************************************
# Add rich snippet star rating ~ post
*******************************************/
if ( !function_exists('ktz_posted_ratting') ) :
function ktz_posted_ratting() {
	global $post,$wp_query;
	$postid = $wp_query->post->ID;
	$post_mt = get_post_meta( $postid, 'post_meta', true );
	$ratedisplay = empty($post_mt['ratedisplay']) ? '' : $post_mt['ratedisplay'];
	$ratevalue = empty($post_mt['ratevalue']) ? '' : $post_mt['ratevalue'];
	$ratenumb = array('0' => '', '0.5' => '05', '1' =>'1', '1.5' => '15','2' => '2','2.5' => '25','3' => '3','3.5' => '35','4' => '4','4.5' => '45','5' => '5');
	echo '<div itemscope itemtype="http://data-vocabulary.org/Review" class="rating-box"';
	if ($ratedisplay == '' ) { echo ' style="display:none;"'; }
	echo '>';
    echo '<span itemprop="itemreviewed" class="rating-title">' . get_the_title() . '</span>';
    echo '<span class="rating-author">' . __('Reviewed by',ktz_theme_textdomain) . ' <span itemprop="reviewer">' . get_the_author() . '</span></span>';
	echo '<span class="rating-date">' . __('on',ktz_theme_textdomain) . '<time itemprop="dtreviewed" datetime="' . get_the_date('F j, Y') . '">' . get_the_time('l, F jS, Y') . '</time>.</span>';
    echo '<span itemprop="summary" class="rating-summery">' . __('This Is Article About',ktz_theme_textdomain);
	echo the_title();
	echo '</span>';
    echo '<span itemprop="description" class="rating-desc">' . get_the_excerpt() . '</span>';
    echo '<span itemprop="rating" class="rating"><span class="rating';
	if (@!empty($ratenumb[$ratevalue])) { echo $ratenumb[$ratevalue];}
	echo '" property="v:rating" content="'.$ratevalue.'" /></span>';
	echo '</div>';
	}
endif;

if ( !function_exists('ktz_feedburner') ) :
function ktz_feedburner() {
	if(get_theme_option('ktz_activated_feedburner') != '') {
	echo '<div class="module_feedburner clearfix">';
	echo '<div class="feedburner_box pull-left">';
	echo '<h2>' . __( 'Subscribe' , ktz_theme_textdomain ) . '</h2>';	
	echo '<p>' . get_theme_option('ktz_text_feedburner') . '</p>';	
	echo '<form class="input-append" action="http://feedburner.google.com/fb/a/mailverify" method="post" target="popupwindow" onsubmit="window.open(\'http://feedburner.google.com/fb/a/mailverify?uri=' . get_theme_option('ktz_id_feedburner') . '\', \'popupwindow\', \'scrollbars=yes,width=550,height=520\');return true">';
	echo '<input type="text" placeholder="Email address" name="email" /> ';
	echo '<input type="hidden" value="' . get_theme_option('ktz_id_feedburner') . '" name="uri"/><input type="hidden" name="loc" value="en_US"/><button class="btn">'. __('Submit', ktz_admin_textdomain) .'</button>';
	echo '</form>';
	echo '</div>';
	echo '<div class="pull-right">';
	if (get_theme_option('ktz_tweet_sn') != '')
		echo '<div class="feed-tweet"><a href="' . get_theme_option('ktz_tweet_sn') . '" class="twitter" title="Twitter">'. __('Twitter',ktz_theme_textdomain) .'</a></div>';
	if (get_theme_option('ktz_fb_sn') != '')
		echo '<div class="feed-fb"><a href="' . get_theme_option('ktz_fb_sn') . '" class="facebook" title="Facebook">'. __('Facebook',ktz_theme_textdomain) .'</a></div>';
	if (get_theme_option('ktz_gplus_sn') != '')
		echo '<div class="feed-gplus"><a href="' . get_theme_option('ktz_gplus_sn') . '" class="gplus" title="GPlus">'. __('Google +',ktz_theme_textdomain) .'</a></div>';
	echo '</div>';	
	echo '</div>';	
	}
	}
endif;

/*******************************************
# Setup meta for view post
*******************************************/
function ktz_getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function ktz_setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

/*******************************************
# For limit title if use limit title ~ post
Use ktz_limittitle('', '...', true, '25');
*******************************************/
function ktz_limittitle($before = '', $after = '', $echo = true, $length = false) { 
	$title = get_the_title();
	if ( $length && is_numeric($length) ) {
		$title = substr( $title, 0, $length );
	}
	if ( strlen($title)> 0 ) {
		$title = apply_filters('ktz_limittitle', $before . $title . $after, $before, $after);
		if ( $echo )
			echo $title;
		else
			return $title;
	}
}

/*******************************************
# Add date on hook system ~ post
*******************************************/
if ( !function_exists('ktz_posted_on') ) :
function ktz_posted_on() {
	printf( __( '<span class="description">On:</span> <span class="entry-date"><a href="%1$s" title="%2$s" rel="bookmark"><time datetime="%3$s" pubdate>%4$s</time></a></span>', ktz_theme_textdomain ),
		esc_url( get_permalink() ),
		esc_attr( get_the_time() ),
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() )
	);
}
endif;

if ( !function_exists('ktz_posted_on_single') ) :
function ktz_posted_on_single() {
	printf( __( '<span class="description">On:</span> <span class="entry-date updated"><a href="%1$s" title="%2$s" rel="bookmark"><time datetime="%3$s" pubdate>%4$s</time></a></span>', ktz_theme_textdomain ),
		esc_url( get_permalink() ),
		esc_attr( get_the_time() ),
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() )
	);
}
endif;

/*******************************************
# Add author on hook system ~ post
*******************************************/
if ( !function_exists('ktz_author_by_single') ) :
function ktz_author_by_single() {
	printf( __( '<span class="description">By:</span> <span class="entry-author"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>', ktz_theme_textdomain ),
		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
		esc_attr( sprintf( __( 'View all posts by %s', ktz_theme_textdomain ), get_the_author() ) ),
		get_the_author()
	);
}
endif;

if ( !function_exists('ktz_author_by') ) :
function ktz_author_by() {
	printf( __( '<span class="description">By:</span> <span class="entry-author"><a href="%1$s" title="%2$s" rel="author">%3$s</a></span>', ktz_theme_textdomain ),
		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
		esc_attr( sprintf( __( 'View all posts by %s', ktz_theme_textdomain ), get_the_author() ) ),
		get_the_author()
	);
}
endif;

/*******************************************
# Add categories on hook system ~ post
*******************************************/
if ( !function_exists('ktz_categories') ) :
function ktz_categories() { 
	$categories_list = get_the_category_list( __( ', ',ktz_theme_textdomain ) );
	if ( $categories_list ):
	printf( __( '<span class="description">Cat:</span> <span class="entry-categories">%2$s</span>',ktz_theme_textdomain ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
	endif; 
}
endif;

/*******************************************
# Add portfolio categories on hook system ~ post
*******************************************/
if ( !function_exists('ktz_portfolio_categories') ) :
function ktz_portfolio_categories() { 
	global $post;
	$post_id = $post->ID;
	$categories_list = get_the_term_list( $post_id, 'portfolio_category', '' );
	if ( $categories_list ):
	printf( __( '<span class="entry-categories">%1$s<i class="icon-folder-open"></i></span>',ktz_theme_textdomain ), $categories_list );
	endif; 
}
endif;

/*******************************************
# Add tags on hook system ~ post
*******************************************/
if ( !function_exists('ktz_tagged') ) :
function ktz_tagged() { 
	$tags_list = get_the_tag_list( '', __( ', ', ktz_theme_textdomain ) );
	if ( $tags_list ):
	printf( __('<span class="description">Tags:</span> <span class="tags">%2$s</span>',ktz_theme_textdomain ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list );
	endif;
}
endif;

/*******************************************
# Add portfolio tags on hook system ~ post
*******************************************/
if ( !function_exists('ktz_portfolio_tagged') ) :
function ktz_portfolio_tagged() { 
	global $post;
	$post_id = $post->ID;
	$tags_list = get_the_term_list( $post_id, 'tagportfolio','', __( ', ', ktz_theme_textdomain ) );
	if ( $tags_list ):
	printf( __('<span class="tags">%1$s<i class="icon-tags"></i></span>',ktz_theme_textdomain ), $tags_list );
	endif;
}
endif;

/*******************************************
# Add comment number on hook system ~ post
*******************************************/
if ( !function_exists('ktz_comment_num') ) :
function ktz_comment_num() {
	if ( comments_open() && ! post_password_required() ) :
	echo '<span class="leave-reply btn btn-small pull-right">';
	comments_popup_link( __( 'Reply', ktz_theme_textdomain ), _x( '1 comments', 'comments number', ktz_theme_textdomain ), _x( '% comments', 'comments number', ktz_theme_textdomain ) );
	echo '</span>';
	endif;
}
endif;

/*******************************************
# Add auto read more @ kasep theme v.1.01
*******************************************/
function ktz_get_excerpt($limit){
	global $post;
  $permalink = get_permalink($post->ID);
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).' <a href="'. $permalink . '">' . __( '<strong>...</strong>', ktz_theme_textdomain ) . '</a>';
  } else {
    $excerpt = implode(" ",$excerpt);
  }	
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;
}

/*******************************************
# Add content on hook system ~ post 
# Call autoread @ kasep theme v.1.01
*******************************************/
if ( !function_exists('ktz_content') ) :
function ktz_content() {
	if ( is_search() ) :
	the_excerpt(); 
	else :
		if ( get_theme_option('ktz_active_automore') != '' && !(is_single()) ) // Set active/deactive via admin
			{ 
				$readmore_count = get_theme_option('ktz_automore_count');
				echo ktz_get_excerpt($readmore_count); 
			} // call function auto readmore
		else
			{ 
				the_content( __( '<strong>...</strong>', ktz_theme_textdomain ) ); 
			}
	endif;
}
endif;

/*******************************************
# Add link pages on hook system ~ post
*******************************************/
if ( !function_exists('ktz_link_pages') ) :
function ktz_link_pages() {	
	wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', ktz_theme_textdomain ) . '</span>', 'after' => '</div>' ) );
	}
endif;

/*******************************************
# Add featured images
# on hook system ~ post
*******************************************/
if ( !function_exists('ktz_bigfeature_post') ) :
function ktz_bigfeature_post() { 
	global $post;
	$permalink = get_permalink();
	if ( has_post_thumbnail()) { 
	$img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '');	
	$img = $img_src[0];	
		echo '<div class="thumbnail btn-box">';
		echo get_the_post_thumbnail($post->ID, 'slider-kas');
		echo '</div>';
		} 
	} 
endif;

if ( !function_exists('ktz_feature_post') ) :
function ktz_feature_post() { 
	global $post;
	$permalink = get_permalink();
	if ( has_post_thumbnail()) { 
	$img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '');	
	$img = $img_src[0];	
		echo '<div class="thumbnail btn-box">';
		echo get_the_post_thumbnail($post->ID, 'col-6');
		echo '</div>';
		} 
	else echo '<div class="thumbnail btn-box"><img src="' . ktz_url . 'img/no-image-m.jpg" alt="No image found"></div>'; 
	} 
endif;

if ( !function_exists('ktz_feature_m') ) :
function ktz_feature_m() { 
	global $post;
	$permalink = get_permalink();
	if ( has_post_thumbnail()) { 
	$img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '');	
	$img = $img_src[0];	
		echo '<div class="thumbnail btn-box">';
		echo get_the_post_thumbnail($post->ID, 'feat-m');
		echo '</div>';
		} 
	else echo '<div class="thumbnail btn-box"><img src="' . ktz_url . 'img/no-image-featm.jpg" alt="No image found"></div>'; 
	} 
endif;

if ( !function_exists('ktz_feature_300') ) :
function ktz_feature_300() { 
	global $post;
	$permalink = get_permalink();
	if ( has_post_thumbnail()) { 
	$img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '');	
	$img = $img_src[0];	
		echo get_the_post_thumbnail($post->ID, 'medium', array('class' => 'thumbnail btn-box'));
		} 
	} 
endif;

if ( !function_exists('ktz_feature_post_single') ) :
function ktz_feature_post_single() { 
	global $post;
	$permalink = get_permalink();
	if ( has_post_thumbnail()) { 
	$img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '');	
	$img = $img_src[0];	
		echo '<div class="thumbnail btn-box pull-right">';
		echo get_the_post_thumbnail($post->ID, 'thumbnail');
		echo '</div>';
		} 
	} 
endif;
function get_first_image_src() {
    $content = get_the_content();
    $image_regex = "/<img [^>]*src=[\"']([^\"^']*)[\"']/";
    preg_match($image_regex, $content, $match);

    if (empty($match))
        return "";
    return $match[1];
}
if ( !function_exists('ktz_featured_just_img') ) :
function ktz_featured_just_img( $width, $height ) { 
	global $post;
	$permalink = get_permalink();
	$title = get_the_title();
	$params = array( 'width' => $width, 'height' => $height, 'crop' => true );
	// Set featured image
	$thumb = get_post_thumbnail_id();
	$img_url = wp_get_attachment_url( $thumb,'full' ); 
	$image = bfi_thumb( $img_url, $params ); //resize & crop featured image
	// Grab from first image, yeahhh
	$fisrtimg_url = get_first_image_src(); 
	$first_image = bfi_thumb( $fisrtimg_url, $params ); 
	// Not image :( So this script will display default image.. :D
	$img_default = ktz_url . 'img/image-blank.jpg'; 
	$default_image = bfi_thumb( $img_default, $params ); 
	if ( $image ) { 
		echo '<img src="' . $image . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} elseif ( $first_image ) {
		echo '<img src="' . $first_image . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} else { 
		echo '<img src="' . $default_image . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} 
} 
endif;