<?php

/* <!-- SAVE  -->
if ( ! function_exists( 'dw_post_format') ) :
function dw_post_format() {
	global $wp_query;
	$post_id = $wp_query->post->ID;
	if ( has_post_format( 'chat', $post_id )) {
	  wp_enqueue_script( 'rainbow', get_template_directory_uri() . '/js/rainbow.min.js', '', '', true );
	}
}
endif;
*/

if ( ! function_exists( 'page_bodyclass') ) :
function page_bodyclass() { 
	global $wp_query;
	$page = '';
	if (is_front_page() ) {
		$page = 'home';
	} elseif (is_page()) {
   	   $page = $wp_query->query_vars["pagename"];
	} elseif (is_single()) {
		$page = 'single';
	} elseif (is_archive()) {
		$page = 'archive';
	} elseif (is_search()) {
		$page = 'search';
	}
	if ($page)
		echo 'class="page '. $page. '"';
}
endif;

// ********* DEPRECATED wp_title in 4.4 ***********
if ( ! function_exists( 'dw_page_title') ) :
function dw_page_title() {
	global $page, $paged; 
	wp_title( '|', true, 'right' ); 
	bloginfo( 'name' ); $site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		echo " | $site_description";
	if ( $paged >= 2 || $page >= 2 )
		echo ' | ' . sprintf( __( 'Page %s', 'dw' ), max( $paged, $page ) );
}
add_filter( 'document_title_separator', function () { return '|'; } );
endif;

/*========= Wait until WP 4.4 ============
function filter_wp_title( $title, $sep ) {
    global $paged, $page;
 
    if ( is_feed() )
        return $title;
 
    $title .= get_bloginfo( 'name' );
 
    $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', 'dw' ), max( $paged, $page ) );
 
    return $title;
}
add_filter( 'wp_title', 'filter_wp_title', 10, 2 );
add_filter( 'document_title_separator', function () { return '|'; } );
*/

if ( ! function_exists( 'dw_meta_desc') ) :
function dw_meta_desc() {
	global $post; 
	$post = get_post( $post );
	setup_postdata( $post );
	if ( is_single() || is_page() ) {
		$meta_desc_value = get_post_meta( $post->ID, 'meta-desc', true );
		if ( $meta_desc_value !== '') {
			echo $meta_desc_value; 
		}
		elseif ( $meta_desc_value == '') {
			$meta_excerpt = dw_good_excerpt(115);
			echo $meta_excerpt;
		}
	}
}
endif;

if ( ! function_exists( 'dw_good_excerpt') ) :
function dw_good_excerpt($length) { 
	global $post;
	$text = $post->post_excerpt;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
	}
	$text = strip_shortcodes($text);
	$text = strip_tags($text);
	$text = substr($text,0,$length);
	if ( strlen($text) < 15 ) {
		$text = the_title('', ' - ', false);
	}
	$allowed_end = array('.', '!', '?', '...');
	$excerpt = reverse_strrchr($text, '.', 2);
	if( $excerpt ) {
		echo apply_filters('get_the_excerpt',$excerpt);
	} else {
		echo apply_filters('get_the_excerpt',$text);
	}
}
function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}
endif;

if ( ! function_exists( 'dw_excerpt') ) :
function dw_excerpt($count){
	$permalink = get_permalink($post->ID);
	$excerpt = get_the_content();
	$excerpt = strip_tags($excerpt);
	$excerpt = substr($excerpt, 0, $count);
	$excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
	return $excerpt;
}
endif;

if ( ! function_exists( 'dw_paging_nav' ) ) :
function dw_paging_nav() {
	if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
		return;
	}

	$paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
	$pagenum_link = html_entity_decode( get_pagenum_link() );
	$query_args   = array();
	$url_parts    = explode( '?', $pagenum_link );

	if ( isset( $url_parts[1] ) ) {
		wp_parse_str( $url_parts[1], $query_args );
	}

	$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
	$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

	$format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
	$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

	$links = paginate_links( array(
		'base'     => $pagenum_link,
		'format'   => $format,
		'total'    => $GLOBALS['wp_query']->max_num_pages,
		'current'  => $paged,
		'mid_size' => 1,
		'add_args' => array_map( 'urlencode', $query_args ),
		'prev_text' => __( '&larr; &nbsp;', 'dw' ),
		'next_text' => __( '&nbsp; &rarr;', 'dw' ),
	) );

	if ( $links ) :

	?>
	<nav class="navigation pagination paging-navigation" role="navigation">
		<div class="pagination loop-pagination">
			<?php echo $links; ?>
		</div>
	</nav>
	<?php
	endif;
}
endif;

if ( ! function_exists( 'dw_posted_at' ) ) :
function dw_posted_at() {
	$categories_list = get_the_category_list( __( ', ', 'dw' ) );	
	if ( in_category( 'fb' )) {
		echo '<div class="categories-fb"><a href="http://facebook.com/windhamdavid" target="_blank">posted on <span class="fb">&nbsp;</span></a></div>';
	} elseif ( in_category( array( 'twitter', 'tumblr' ) )) { 
		
	} else {
		echo '<span class="categories-links">' . $categories_list . '</span>';
	}	
}
endif;

if ( ! function_exists( 'dw_posted_on' ) ) :
function dw_posted_on() {
	return sprintf( __( '<span %1$s></span> %2$s by %3$s', 'daw' ),
		'class="meta-prep meta-prep-author"',
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a> <span class="meta-sep">',
			get_permalink(),
			esc_attr( get_the_time() ),
			get_the_date()
		),
		sprintf( '</span> <span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>', 
			get_author_posts_url( get_the_author_meta( 'ID' ) ),
			sprintf( esc_attr__( 'View all posts by %s', 'daw' ), get_the_author() ),
			get_the_author()
		)
	);
	
}
endif;

if ( ! function_exists( 'dw_posted_in' ) ) :
function dw_posted_in() {
	$tag_list = get_the_tag_list( '', ', ', '' );
	if ( $tag_list ) {
		$utility_text = __( '%1$s and tagged %2$s. <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'dw' );
	} else {
		$utility_text = __( '%1$s. <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'dw' );
	}
	return sprintf(
		$utility_text,
		get_the_category_list( ', ' ),
		$tag_list,
		get_permalink(),
		the_title_attribute( 'echo=0' ),
		get_post_comments_feed_link()
	);	
}
endif;

if ( ! function_exists( 'dw_get_next_attachment_url' ) ) :
function dw_get_next_attachment_url() {
	global $post;
	$post = get_post( $post );
	$attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
 
	foreach ( $attachments as $k => $attachment ) {
		if ( $attachment->ID == $post->ID )
			break;
	}
	$k++;
	if ( isset( $attachments[ $k ] ) )
		return get_attachment_link( $attachments[ $k ]->ID );
	else
		return get_permalink( $post->post_parent );
}
endif;

if ( ! function_exists( 'dw_post_nav' ) ) :
function dw_post_nav() {
	$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
	$next     = get_adjacent_post( false, '', false );

	if ( ! $next && ! $previous ) {
		return;
	}

	?>
	<nav class="navigation post-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'dw' ); ?></h1>
		<div class="nav-links">
			<?php
			if ( is_attachment() ) :
				previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'dw' ) );
			else :
				previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'dw' ) );
				next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'dw' ) );
			endif;
			?>
		</div>
	</nav>
	<?php
}
endif;

if ( ! function_exists( 'dw_cat_list' ) ) :
function dw_cat_list() {
	return daw_term_list( 'category', ', ', __( 'Posted in %s', 'dw' ), __( 'Also posted in %s', 'daw' ) );
}
endif;

if ( ! function_exists( 'dw_tag_list' ) ) :
function dw_tag_list() {
	return daw_term_list( 'post_tag', ', ', __( 'Tagged %s', 'dw' ), __( 'Also tagged %s', 'daw' ) );
}
endif;

if ( ! function_exists( 'dw_term_list' ) ) :
function daw_term_list( $taxonomy, $glue = ', ', $text = '', $also_text = '' ) {
	global $wp_query, $post;
	$current_term = $wp_query->get_queried_object();
	$terms = wp_get_object_terms( $post->ID, $taxonomy );
	if ( isset( $current_term->taxonomy ) && $taxonomy == $current_term->taxonomy ) {
		foreach ( (array) $terms as $key => $term ) {
			if ( $term->term_id == $current_term->term_id ) {
				unset( $terms[$key] );
				break;
			}
		}
		$text = $also_text;
	}
	$tlist = array();
	$rel = 'category' == $taxonomy ? 'rel="category"' : 'rel="tag"';
	foreach ( (array) $terms as $term ) {
		$tlist[] = '<a href="' . get_term_link( $term, $taxonomy ) . '" title="' . esc_attr( sprintf( __( 'View all posts in %s', 'daw' ), $term->name ) ) . '" ' . $rel . '>' . $term->name . '</a>';
	}
	if ( ! empty( $tlist ) )
		return sprintf( $text, join( $glue, $tlist ) );
	return '';
}
endif;

if ( ! function_exists( 'dw_comment' ) ) :
function dw_comment( $comment, $args, $depth ) {
	$GLOBALS ['comment'] = $comment; ?>
	<?php if ( '' == $comment->comment_type ) : ?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
		<div class="comment-author vcard">
			<?php echo get_avatar( $comment, 64 ); ?>
			<?php printf( __( '<cite class="fn">%s</cite><span class="says">:</span>', 'daw' ), get_comment_author_link() ); ?>
		</div>
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em><?php _e( 'Your comment is awaiting moderation.', 'daw' ); ?></em>
			<br />
		<?php endif; ?>

		<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><?php printf( __( '%1$s at %2$s', 'daw' ), get_comment_date(),  get_comment_time() ); ?></a></div>

		<div class="comment-body"><?php comment_text(); ?></div>

		<div class="reply">
			<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
			<?php edit_comment_link( __( '(Edit)', 'daw' ),'  ','' ); ?>
		</div>
	</div>

	<?php else : ?>
	<li class="post pingback">
		<p><?php _e( 'Pingback: ', 'daw' ); ?><?php comment_author_link(); ?><?php edit_comment_link ( __('edit', 'daw'), '&nbsp;&nbsp;', '' ); ?></p>
	<?php endif;
}
endif;

add_filter( 'comment_form_default_fields', 'dw_comment_form_fields' );
function dw_comment_form_fields( $fields ) {
    $commenter = wp_get_current_commenter();
    
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );
    $html5 = current_theme_supports( 'html5', 'comment-form' ) ? 1 : 0;
    
    $fields   =  array(
        'author' => '<div class="form-group comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
                    '<div class="input-group"><div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div><input class="form-control" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /><span class="add-on"><i class="icon-user"></i></span></div></div>',
        'email'  => '<div class="form-group comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
                    '<div class="input-group"><div class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></div><input class="form-control" id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></div></div>',
        'url'    => '<div class="form-group comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
                    '<div class="input-group"><div class="input-group-addon"><span class="glyphicon glyphicon-link" aria-hidden="true"></span></div><input class="form-control" id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></div></div>',
    );
    
    return $fields;
}

add_filter( 'comment_form_defaults', 'dw_comment_form' );
function dw_comment_form( $args ) {
    $args['comment_field'] = '<div class="form-group comment-form-comment">
            <label for="comment">' . _x( 'Comment', 'noun' ) . '</label> 
            <textarea class="form-control" id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>
        </div>';
    return $args;
}

add_action('comment_form', 'dw_comment_button' );
function dw_comment_button() {
    echo '<button class="btn btn-default" type="submit">' . __( 'Submit' ) . '</button>';
}