<?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.' ); }

if( function_exists( 'jetpack_photon_url' ) ) {
    add_filter( 'jetpack_photon_url', 'jetpack_photon_url', 10, 3 );
}

/*******************************************
# 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;

if ( !function_exists('ktz_posted_title_a') ) :
function ktz_posted_title_a() {
	printf( __( '<a href="%1$s" title="Go to %2$s">%3$s</a>', ktz_theme_textdomain ),
		esc_url(get_permalink()),
		esc_attr(get_the_title()),
		esc_attr(get_the_title())
	);
}
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 xmlns:v="http://rdf.data-vocabulary.org/#" typeof="v:Review" class="rating-box"';
	if ($ratedisplay == '' ) { echo ' style="display:none;"'; }
	echo '>';
    echo '<span property="v:itemreviewed" class="rating-title">' . get_the_title() . '</span>';
    echo '<span class="rating-author">' . __('Reviewed by',ktz_theme_textdomain) . ' <span property="v:reviewer">' . get_the_author() . '</span></span>';
	echo '<span class="rating-date">' . __('on',ktz_theme_textdomain) . '<time property="v:dtreviewed" datetime="' . get_the_date('F j, Y') . '">' . get_the_time('l, F jS, Y') . '</time>.</span>';
    echo '<span property="v:summary" class="rating-summery">' . __('This Is Article About',ktz_theme_textdomain);
	echo the_title();
	echo '</span>';
    echo '<span property="v:description" class="rating-desc">' . get_the_excerpt() . '</span>';
    echo '<span 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') != '') {
	if (is_single()) :
	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="buttonfeed">'. __('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>';	
	elseif (is_home() || is_front_page()) :
	echo '<div class="module_feedburner_home clearfix">';
	echo '<div class="feedburner_box_home">';
	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="buttonfeed">'. __('Submit', ktz_admin_textdomain) .'</button>';
	echo '</form>';
	echo '</div>';
	echo '<div class="social-text"><span>' . __( 'Follow us' , ktz_theme_textdomain ) . '</span></div>';
	if (get_theme_option('ktz_tweet_sn') != '')
		echo '<div class="feed-tweet pull-left clearfix"><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 pull-left clearfix"><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 pull-left clearfix"><a href="' . get_theme_option('ktz_gplus_sn') . '" class="gplus" title="GPlus">'. __('Google +',ktz_theme_textdomain) .'</a></div>';
	echo '</div>';	
	endif;
	}
	}
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="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') ) :
function ktz_author_by() {
	printf( __( '<span class="description">Posted by:</span> <span class="entry-author vcard"><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;

/*******************************************
# 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
*******************************************/

/*
* Add featured images * first in kabar themes
* If has_thumb elseif firstimage else no image
* BFI_thumbs https://github.com/bfintal/bfi_thumb
*/
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];
}

function ktz_getpost_images_upload() {
    global $post,$wpdb;
    $postid = $post->ID;
    $results = '';
	$attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = $postid AND post_type = 'attachment' AND post_mime_type LIKE 'image/%%' LIMIT 1 ;" );
    // get the correct image html for the selected size
	$image = wp_get_attachment_image_src($attachment_id, 'full');
	$results = $image[0];
    return $results;
}
if ( !function_exists('ktz_featured_just_img_link') ) :
function ktz_featured_just_img_link( $width, $height ) { 
	global $post;
	$permalink = get_permalink();
	$title = get_the_title();
	$get_imgpost_upload = ktz_getpost_images_upload();
	$params = array( 'width' => $width, 'height' => $height, 'crop' => true );
	$thumb = get_post_thumbnail_id();
	$img_url = wp_get_attachment_url( $thumb,'full' ); 
	$fisrtimg_url = get_first_image_src();
	$img_default = get_template_directory_uri() . '/includes/assets/img/no-image/image-blank.jpg'; 
	
	if (function_exists( 'jetpack_photon_url' )) :
		$image = apply_filters( 'jetpack_photon_url', $img_url, 'resize='. $width . ',' . $height );
	else :
		$image = bfi_thumb( $img_url, $params ); //resize & crop featured image
	endif;
	if (function_exists( 'jetpack_photon_url' )) :
		$image_upload = apply_filters( 'jetpack_photon_url', $get_imgpost_upload, 'resize='. $width . ',' . $height );
	else :
		$image_upload = bfi_thumb( $get_imgpost_upload, $params ); //resize & crop featured image
	endif;
	if (function_exists( 'jetpack_photon_url' )) :
		$first_image = apply_filters( 'jetpack_photon_url', $fisrtimg_url, 'resize='. $width . ',' . $height );
	else :
		$first_image = bfi_thumb( $fisrtimg_url, $params ); 
	endif;
	if (function_exists( 'jetpack_photon_url' )) :
		$default_image = apply_filters( 'jetpack_photon_url', $img_default, 'resize='. $width . ',' . $height );
	else :
		$default_image = bfi_thumb( $img_default, $params ); 
	endif;
	
	if ( $image ) { 
		echo $image;
	} elseif ( $first_image ) {
		echo $first_image;
	} elseif ( $image_upload ) {
		echo $image_upload;
	} else { 
		echo $default_image;
	} 
}
endif;
if ( !function_exists('ktz_featured_img') ) :
function ktz_featured_img( $width, $height, $class ) {
	global $post;
	$permalink = get_permalink();
	$title = get_the_title();
	$params = array( 'width' => $width, 'height' => $height, 'crop' => true );
	$thumb = get_post_thumbnail_id();
	$img_url = wp_get_attachment_url( $thumb,'full' ); 
	$fisrtimg_url = get_first_image_src(); 
	$get_imgpost_upload = ktz_getpost_images_upload();
	$img_default = get_template_directory_uri() . '/img/no-image/image-blank.jpg';

	$classes = (isset($class) ? $class : 'thumbnail btn-box');
	
	if (function_exists( 'jetpack_photon_url' )) :
		$image = apply_filters( 'jetpack_photon_url', $img_url, 'resize='. $width . ',' . $height );
	else :
		$image = bfi_thumb( $img_url, $params ); //resize & crop featured image
	endif;
	if (function_exists( 'jetpack_photon_url' )) :
		$image_upload = apply_filters( 'jetpack_photon_url', $get_imgpost_upload, 'resize='. $width . ',' . $height );
	else :
		$image_upload = bfi_thumb( $get_imgpost_upload, $params ); //resize & crop featured image
	endif;
	if (function_exists( 'jetpack_photon_url' )) :
		$first_image = apply_filters( 'jetpack_photon_url', $fisrtimg_url, 'resize='. $width . ',' . $height );
	else :
		$first_image = bfi_thumb( $fisrtimg_url, $params ); 
	endif;
	if (function_exists( 'jetpack_photon_url' )) :
		$default_image = apply_filters( 'jetpack_photon_url', $img_default, 'resize='. $width . ',' . $height );
	else :
		$default_image = bfi_thumb( $img_default, $params ); 
	endif;
	
	if ( $image ) { 
		echo '<img src="' . $image . '" class="' . $classes . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} elseif ( $first_image ) {
		echo '<img src="' . $first_image . '" class="' . $classes . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} elseif ( $image_upload ) {
		echo '<img src="' . $image_upload . '" class="' . $classes . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} else { 
		echo '<img src="' . $default_image . '" class="' . $classes . '" alt="' . $title . '" width="'.$width.'" height="'.$height.'" title="' . $title . '" />';
	} 
} 
endif;