<?php


if ( ! isset( $content_width ) ) {
	$content_width = 1280;
}


/* theme setup  */
if ( ! function_exists( 'codeillust_setup' ) ) :
function codeillust_setup() {
	register_nav_menus( array(
		'header'   => __( 'Header', 'codeillust' ),
		'footer' => __( 'Footer', 'codeillust' )
	) );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'html5', array( 'search-form' ) );
	add_theme_support( "title-tag" );
	$args = array(
		'width'         => 1600,
		'height'        => 250,
	);
	add_theme_support( "custom-header", $args);
	add_theme_support( "custom-background");
	add_editor_style();
	
	add_theme_support( 'post-formats', array(
		'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery', 'chat',
	) );
	load_theme_textdomain('codeillust', get_template_directory() . '/languages');
	
}
endif; // codeillust_setup
add_action( 'after_setup_theme', 'codeillust_setup' );


/* Theme stylesheet & javascript  */
if ( ! function_exists( 'codeillust_scripts' ) ) :
function codeillust_scripts() {
	wp_enqueue_style( 'codeillust_bootstrap_css', get_template_directory_uri() . '/assets/css/bootstrap.css', array(), '' );
	wp_enqueue_style( 'codeillust_header_common_css', get_template_directory_uri() . '/assets/css/header-common.css', array(), '' );
	wp_enqueue_style( 'codeillust_footer_common_css', get_template_directory_uri() . '/assets/css/footer-common.css', array(), '' );
	wp_enqueue_style( 'codeillust_base_common_css', get_template_directory_uri() . '/assets/css/base-common.css', array(), '' );
	
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
	
	
	wp_enqueue_script( 'codeillust_bootstrap_js', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array('jquery'), '', true );
	wp_enqueue_script( 'codeillust_script_js', get_template_directory_uri() . '/assets/js/script.js', array('jquery'), '', true );

}
endif; // codeillust_scripts
add_action( 'wp_enqueue_scripts', 'codeillust_scripts' );


/* Theme Widget */
if ( ! function_exists( 'codeillust_widgets' ) ) :
function codeillust_widgets() {
	register_sidebar( array(
		'name'          => __('Post','codeillust'),
		'id'            => 'codeillust_post_widget',
		'description'   => __('Post Widget', 'codeillust' ),
		'before_widget' => '<div class="codeillust-widget-area">',
		'after_widget'  => '</div>',
		'before_title'  => '<h5 class="widget-title">',
		'after_title'   => '</h5>',
	) );
	
	register_sidebar( array(
		'name'          => __('Footer Widget','codeillust').'01',
		'id'            => 'codeillust_footer_widget_01',
		'description'   => __('Footer Widget', 'codeillust' ),
		'before_widget' => '<div class="codeillust-widget-area">',
		'after_widget'  => '</div>',
		'before_title'  => '<h5 class="widget-title">',
		'after_title'   => '</h5>',
	) );
	
	register_sidebar( array(
		'name'          => __('Footer Widget','codeillust').'02',
		'id'            => 'codeillust_footer_widget_02',
		'description'   => __('Footer Widget', 'codeillust' ),
		'before_widget' => '<div class="codeillust-widget-area">',
		'after_widget'  => '</div>',
		'before_title'  => '<h5 class="widget-title">',
		'after_title'   => '</h5>',
	) );
	
	register_sidebar( array(
		'name'          => __('Footer Widget','codeillust').'03',
		'id'            => 'codeillust_footer_widget_03',
		'description'   => __('Footer Widget', 'codeillust' ),
		'before_widget' => '<div class="codeillust-widget-area">',
		'after_widget'  => '</div>',
		'before_title'  => '<h5 class="widget-title">',
		'after_title'   => '</h5>',
	) );
	
	register_sidebar( array(
		'name'          => __('Footer Widget','codeillust').'04',
		'id'            => 'codeillust_footer_widget_04',
		'description'   => __('Footer Widget', 'codeillust' ),
		'before_widget' => '<div class="codeillust-widget-area">',
		'after_widget'  => '</div>',
		'before_title'  => '<h5 class="widget-title">',
		'after_title'   => '</h5>',
	) );
	
}
endif; // codeillust_widgets
add_action( 'widgets_init', 'codeillust_widgets' );




/* theme title */
if ( ! function_exists( 'codeillust_wp_title' ) ) :
function codeillust_wp_title( $title, $sep ) {
	global $paged, $page;
	if ( is_feed() ) {return $title;}
	$title .= get_bloginfo( 'name', 'display' );
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) ) {
		$title = "$title $sep $site_description";
	}
	if ( $paged >= 2 || $page >= 2 ) {
		$title = "$title $sep " . sprintf( __( 'Page %s', 'codeillust' ), max( $paged, $page ) );
	}
	return $title;
}
endif; // codeillust_wp_title
add_filter( 'wp_title', 'codeillust_wp_title', 10, 2 );


/* post pagination */
if ( ! function_exists( 'codeillust_pagination' ) ) :
function codeillust_pagination() { 
    global $wp_query;
    $big = 999999999; 
    $pages = paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            'prev_next' => false,
            'type'  => 'array',
            'prev_next'   => TRUE,
			'prev_text'    => '<i class="glyphicon glyphicon-chevron-left"></i>',
			'next_text'    => '<i class="glyphicon glyphicon-chevron-right"></i>',
        ) );
        
        if( is_array( $pages ) ) {
            $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
            echo '<nav class="page-pagination text-center">';
            echo '<ul class="pagination">';
            foreach ( $pages as $page ) {
                echo '<li>'.$page.'</li>'; 
            }
           echo '</ul>';
           echo '</nav>';
        }
}
endif; // codeillust_pagination


// theme excerpt
if ( ! function_exists( 'codeillust_custom_excerpt_length' ) ) :
function codeillust_custom_excerpt_length( $length ) {
	return 300;
}
endif; // codeillust_custom_excerpt_length
add_filter( 'excerpt_length', 'codeillust_custom_excerpt_length', 999 );


//theme excerpt more
if ( ! function_exists( 'codeillust_new_excerpt_more' ) ) :
function codeillust_new_excerpt_more( $more ) {
	return '...';
}
endif; // codeillust_new_excerpt_more
add_filter('excerpt_more', 'codeillust_new_excerpt_more');



//Comments list
if ( ! function_exists( 'codeillust_comment_li' ) ) :
function codeillust_comment_li( $comment, $args, $depth ) {
    global $post;
    $GLOBALS['comment'] = $comment;
    ?>
        <li <?php comment_class(); ?>>
            <article  <?php comment_class(); ?> class="comment">
				<div class="media">
					<div class="media-left">
						<?php echo get_avatar( $comment,40); ?>
					</div>
					<div class="media-body">
						<div class="panel panel-default" id="comment-<?php comment_ID(); ?>">
							<div class="panel-heading">
								<strong class="letters"><?php comment_author(); ?></strong>
								<time <?php comment_time( 'c' ); ?> class="comment-time text-muted small">
									<span class="date">
										<?php comment_date(); ?>
									</span>
									<span class="time">
										<?php comment_time(); ?>
									</span>
								</time>
							</div>
							<div class="panel-body">
								<?php comment_text(); ?>
								<div>
									<?php
										comment_reply_link( array('reply_text' => '<i class="glyphicon glyphicon-share-alt"></i> '.__( 'Reply', 'codeillust' ), 'depth' => $depth, 'max_depth' => $args[ 'max_depth' ] )
								            , $comment->comment_ID
								            , $comment->comment_post_ID
								        );
									?> 
								</div>
							</div>
						</div>
					</div>
				</div>
            </article><!-- #comment-<?php comment_ID(); ?> -->
    <?php // End the default styling of comment
}
endif; // codeillust_comment_li



// theme comment replay button add Class
if ( ! function_exists( 'codeillust_replace_reply_link_class' ) ) :
function codeillust_replace_reply_link_class($class){
    $class = str_replace("class='comment-reply-link", "class='btn btn-info btn-xs", $class);
    return $class;
}
endif; // codeillust_replace_reply_link_class
add_filter('comment_reply_link', 'codeillust_replace_reply_link_class');



// theme comment form
if ( ! function_exists( 'codeillust_comment_form' ) ) :
function codeillust_comment_form(){
	$commenter = wp_get_current_commenter();
	$req = get_option( 'require_name_email' );
	$aria_req = ( $req ? " aria-required='true'" : '' );
	$comments_args = array(
		'title_reply'       => '<i class="glyphicon glyphicon-pencil"></i> '.__( 'Leave a Reply', 'codeillust' ),
		'title_reply_to'    => __( 'Leave a Reply to %s', 'codeillust' ),
		'cancel_reply_link' => '<i class="glyphicon glyphicon-remove"></i> '.__( 'Cancel', 'codeillust' ),
		'label_submit'      => __( 'Post Comment', 'codeillust' ),
		'class_submit' => 'btn btn-primary',
		'comment_field' =>  '<hr class="clear xs" /><p class="comment-form-comment"><textarea class="form-control" placeholder="'.__( 'Comment...', 'codeillust' ).'" id="comment" name="comment" cols="45" rows="3" aria-required="true">' .
		'</textarea></p>',
		'comment_notes_before' => '',
		'fields' => apply_filters( 'comment_form_default_fields', array(
		 	'author' => '<hr class="clear xs" /><div class="row">
		 				 <div class="comment-form-author col-sm-6">
			 				 <div class="input-group">
			 					<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
			 					<input id="author" class="form-control" name="author" placeholder="' . __( 'Name', 'codeillust' ) . ( $req ? ' ('.__( 'required', 'codeillust' ).')' : '' ) . '" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' />
			 				 </div>
		 				 </div>',
		    'email'  => '<div class="comment-form-email col-sm-6">
		    			 	<div class="input-group">
		    			 		<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
		    			 		<input id="email" class="form-control" name="email" placeholder="' . __( 'Email', 'codeillust' ) . ( $req ? ' ('.__( 'required', 'codeillust' ).')' : '' ) .'" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />
		    			 	</div>
		    			 </div>',
		    'url'    => '<div class="comment-form-url col-sm-12">
							<hr class="clear xs" />
		    				<div class="input-group">
		    			 		<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
						 		<input id="url" class="form-control" name="url" placeholder="' . __( 'Website', 'codeillust' ) . '" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" />
						 	</div>
		    			</div>
		    		</div>',
		)),
	);
	
	comment_form($comments_args);
}
endif; // codeillust_comment_form



//theme search form
if ( ! function_exists( 'codeillust_search_form' ) ) :
function codeillust_search_form( $form ) {
	$form = '
	<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url(home_url( '/' )) . '" >
	
		<div class="input-group">
		
			<input type="text" placeholder="' . __( 'SEARCH...', 'codeillust') . '" value="' . get_search_query() . '" name="s" id="s" class="form-control input-lg" />
			<span class="input-group-btn">
            	<button class="btn btn-default btn-lg" type="submit"><i class="glyphicon glyphicon-search"></i></button>
            	<button class="btn btn-danger btn-lg" type="button"><i class="glyphicon glyphicon-remove"></i></button>
            </span>
            
		</div>
	</form>';
	return $form;
}
endif; // codeillust_search_form
add_filter( 'get_search_form', 'codeillust_search_form' );