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

/*******************************************
# This is Required from wordpress
*******************************************/
if ( ! isset( $content_width ) ) $content_width = 620;

/*******************************************
# Change avatar CSS on hook system ~ post
*******************************************/
function change_avatar_css($class) {
	$class = str_replace("class='avatar", "class='thumbnail", $class) ;
	return $class;
}

/*******************************************
# Remove auto p on hook system ~ post
*******************************************/
if( !function_exists('ktz_remove_wpautop') ) {
function ktz_remove_wpautop( $content ) { 
	$content = trim( do_shortcode( shortcode_unautop ( $content ) ) ); 
	$content = preg_replace('#^<\/p>|^<br />|<p>$|</p><p>\s*( )?\s*<\/p>#', '', $content);
	return $content;
	}
}

/*******************************************
# WP texturize on hook system ~ post
*******************************************/
function ktz_texturize_SC($content) {
	$content = preg_replace('/\]\[/im', "]\n[", $content);
	return $content;
}

/*******************************************
# Disabling auto format on hook system ~ post
*******************************************/
function ktz_formatter($content) {
	$new_content = '';
	$pattern_full = '{(\[raw\].*?\[/raw\])}is';
	$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
	$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
	foreach ($pieces as $piece) {
		if (preg_match($pattern_contents, $piece, $matches)) {
			$new_content .= $matches[1];
		} else {
			$pattern_full_intl = '{(\<!--start_raw-->.*?\<!--end_raw-->)}is';
			$pattern_contents_intl = '{<!--start_raw-->(.*?)\<!--end_raw-->}is';
			$pieces_intl = preg_split($pattern_full_intl, $piece, -1, PREG_SPLIT_DELIM_CAPTURE);
			
			foreach ($pieces_intl as $piece_intl) {
				if (preg_match($pattern_contents_intl, $piece_intl, $matches_intl)) {
					$new_content .= $matches_intl[1];
				} else {
					$new_content .= wptexturize(wpautop($piece_intl));
				}
			}
		}
	}
	return $new_content;
}

/*******************************************
# Add theme support on hook system ~ post
*******************************************/
if( !function_exists('ktz_setup') ) {
function ktz_setup() {
    add_theme_support('post-thumbnails');
	add_theme_support('automatic-feed-links');
	add_theme_support('custom-background');
	add_theme_support( 'custom-header'); // Just required for WP standart API
	}
}

/*******************************************
# Add editor style on hook system ~ post
*******************************************/
if(function_exists('add_editor_style')) {
	add_editor_style();
}

/*******************************************
# Get limit in the excerpt - use <?php echo ktz_excerpt(number); ?>
*******************************************/
function ktz_excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'...';
  } else {
    $excerpt = implode(" ",$excerpt);
  }	
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;
}

/*******************************************
# Get transient on hook system ~ post
*******************************************/
if( !function_exists('request_url') ) {
	function ktz_get_transient($request_url) {  
        $raw_response = wp_remote_get( $request_url, array( 'timeout' => 1 ) );  
        if ( is_wp_error( $raw_response ) )  
            return $raw_response;  
        $code = (int) wp_remote_retrieve_response_code($raw_response);  
        $response = json_decode( wp_remote_retrieve_body($raw_response) );  
        switch( $code ):  
            case 200:  
                return $response;  
            case 304:  
            case 400:  
            case 401:  
            case 403:  
            case 404:  
            case 406:  
            case 420:  
            case 500:  
            case 502:  
            case 503:  
            case 504:  
                return new WP_Error($code, $response->error);  
            default:  
                return new WP_Error($code, __('Invalid response','twitter'));  
        endswitch;  
    }  
}

/*******************************************
# Set transient on hook system ~ post
*******************************************/
if( !function_exists('ktz_set_transient') ) {
	function ktz_set_transient($key, $data, $expiration) {  
        // Time when transient expires  
        $expire = time() + $expiration;  
        set_transient( $key, array( $expire, $data ) );  
    }  
} 	

/*******************************************
# Override default new comment_form() with own
# better way to override comment form
*******************************************/
function ktz_com_fields($fields) {
	$commenter = wp_get_current_commenter();
	$req = get_option( 'require_name_email' );
	$aria_req = ( $req ? " aria-required='true'" : '' );
	$fields['author'] = '<p class="input-prepend"><span class="add-on"><i class="icon-user"></i></span><input type="text" name="author" class="span2" id="prependedInput" placeholder="' . __( 'Name', ktz_theme_textdomain ) .' ' . ( $req ? __( '*', ktz_theme_textdomain ) : '' ) . '" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" tabindex="1" ' . $aria_req . ' /></p>';
	$fields['email'] = '<p class="input-prepend"><span class="add-on"><i class="icon-envelope"></i></span><input type="text" name="email" class="span2" id="prependedInput" placeholder="' . __( 'Mail (not publish)', ktz_theme_textdomain ) .' ' . ( $req ? __( '*', ktz_theme_textdomain ) : '' ) . '" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" tabindex="2" ' . $aria_req . ' /></p>';
	$fields['url'] =  '<p class="input-prepend"><span class="add-on"><i class="icon-globe"></i></span><input type="text" name="url" class="span2" id="prependedInput" placeholder="' . __( 'Website', ktz_theme_textdomain ) .' ' . ( $req ? __( '*', ktz_theme_textdomain ) : '' ) . '" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" tabindex="3" ' . $aria_req . ' /></p>';
	return $fields;
}

function ktz_com_fields_textarea($fields_com) {
	$fields_com = '<p><textarea name="comment" cols="100%" rows="5" class="span8" tabindex="4" placeholder="' . allowed_tags() . '"></textarea></p>';
	return $fields_com;
}