<?php
/**
 * Custom BlazeBlog template tags
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package WordPress
 * @subpackage blazeblog
 * @since BlazeBlog 1.0
 */

if ( ! function_exists( 'blazeblog_entry_meta' ) ) :
/**
 * Prints HTML with meta information for the categories, tags.
 *
 * Create your own blazeblog_entry_meta() function to override in a child theme.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_entry_meta() {
    if ( is_sticky() && is_home() && ! is_paged() ) {
        printf('<span class="sticky-post">'. __( 'Sticky', 'blazeblog' ) .'</span>');
    }

	if ( 'post' === get_post_type() ) {
		$author_avatar_size = apply_filters( 'blazeblog_author_avatar_size', 49 );
		printf( '<span class="byline"><span class="author vcard">%1$s<span class="screen-reader-text">%2$s </span> <a class="url fn n" href="%3$s">%4$s</a></span></span>',
			get_avatar( get_the_author_meta( 'user_email' ), $author_avatar_size ),
			_x( 'Author', 'Used before post author name.', 'blazeblog' ),
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			get_the_author()
		);
	}

	if ( in_array( get_post_type(), array( 'post', 'attachment' ) ) ) {
		blazeblog_entry_date();
	}

	$format = get_post_format();
	if ( current_theme_supports( 'post-formats', $format ) ) {
		printf( '<span class="entry-format %4$s">%1$s<a href="%2$s">%3$s</a></span>',
			sprintf( '<span class="screen-reader-text">%s </span>', _x( 'Format', 'Used before post format.', 'blazeblog' ) ),
			esc_url( get_post_format_link( $format ) ),
			get_post_format_string( $format ),
            $format
		);
	}

	if ( 'post' === get_post_type() ) {
		blazeblog_entry_taxonomies();
	}

	if ( ! is_singular() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
		comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'blazeblog' ), get_the_title() ) );
		echo '</span>';
	}
}
endif;

if ( ! function_exists( 'blazeblog_entry_date' ) ) :
/**
 * Prints HTML with date information for current post.
 *
 * Create your own blazeblog_entry_date() function to override in a child theme.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_entry_date() {
	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';

	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
	}

	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		get_the_date(),
		esc_attr( get_the_modified_date( 'c' ) ),
		get_the_modified_date()
	);

	printf( '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
		_x( 'Posted on', 'Used before publish date.', 'blazeblog' ),
		esc_url( get_permalink() ),
		$time_string
	);
}
endif;

if ( ! function_exists( 'blazeblog_entry_taxonomies' ) ) :
/**
 * Prints HTML with category and tags for current post.
 *
 * Create your own blazeblog_entry_taxonomies() function to override in a child theme.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_entry_taxonomies() {
	$categories_list = get_the_category_list( _x( ' ', 'Used between list items, there is a space after the comma.', 'blazeblog' ) );
	if ( $categories_list && blazeblog_categorized_blog() ) {
		printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
			_x( 'Categories', 'Used before category names.', 'blazeblog' ),
			$categories_list
		);
	}

	$tags_list = get_the_tag_list( '', _x( ' ', 'Used between list items, there is a space after the comma.', 'blazeblog' ) );
	if ( $tags_list ) {
		printf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
			_x( 'Tags', 'Used before tag names.', 'blazeblog' ),
			$tags_list
		);
	}
}
endif;

if ( ! function_exists( 'blazeblog_post_thumbnail' ) ) :
/**
 * Displays an optional post thumbnail.
 *
 * Wraps the post thumbnail in an anchor element on index views, or a div
 * element when on single views.
 *
 * Create your own blazeblog_post_thumbnail() function to override in a child theme.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_post_thumbnail() {
	if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
		return;
	}

	if ( is_singular() ) :
	?>

	<div class="post-thumbnail col-sm-12">
		<?php the_post_thumbnail( 'post-thumbnail', array( 'alt' => the_title_attribute( 'echo=0' ), 'class' => 'img-responsive' ) ); ?>
	</div><!-- .post-thumbnail -->

	<?php else : ?>
    <div class="post-thumbnail col-sm-12">
	    <a href="<?php the_permalink(); ?>" aria-hidden="true">
		    <?php the_post_thumbnail( 'post-thumbnail', array( 'alt' => the_title_attribute( 'echo=0' ), 'class' => 'img-responsive' ) ); ?>
	    </a>
    </div>
	<?php endif; // End is_singular()
}
endif;

function blazeblog_excerpt_length( $length ) {
	return 55;
}
add_filter( 'excerpt_length', 'blazeblog_excerpt_length', 999 );

if ( ! function_exists( 'blazeblog_excerpt' ) ) :
	/**
	 * Displays the optional excerpt.
	 *
	 * Wraps the excerpt in a div element.
	 *
	 * Create your own blazeblog_excerpt() function to override in a child theme.
	 *
	 * @since BlazeBlog 1.0
	 *
	 * @param string $class Optional. Class string of the div element. Defaults to 'entry-summary'.
	 */
	function blazeblog_excerpt( $class = 'entry-summary' ) {
		$class = esc_attr( $class );

		if ( has_excerpt() || is_search() ) : ?>
			<div class="<?php echo $class; ?>">
				<?php the_excerpt(); ?>
			</div><!-- .<?php echo $class; ?> -->
		<?php endif;
	}
endif;

if ( ! function_exists( 'blazeblog_excerpt_more' ) && ! is_admin() ) :
/**
 * Replaces "[...]" (appended to automatically generated excerpts) with ... 
 *
 * Create your own blazeblog_excerpt_more() function to override in a child theme.
 *
 * @since BlazeBlog 1.0
 *
 * @return string excerpt prepended with an ellipsis.
 */
function blazeblog_excerpt_more() {
	return ' &hellip;';
}
add_filter( 'excerpt_more', 'blazeblog_excerpt_more' );
endif;

function blazeblog_the_content($post = false, $more = true) {
    global $post;
    $string = get_the_excerpt();

    $string = '<p>' . $string . '</p>';
    
    if($more) {
        $string .= sprintf( 
                            '<a class="btn btn-default btn-sm btn-read-more" href="%2$s">'. __( 'Continue reading<span class="screen-reader-text"> "%1$s"</span>', 'blazeblog' ) . ' &raquo;</a>', 
                            $post->post_title,
                            esc_url( get_permalink( $post->ID ) )
                          );
    }

    // Make sure to return the content
    return $string;
}

function blazeblog_link_pages() {
    return wp_link_pages( array(
                'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'blazeblog' ) . '</span>',
                'after'       => '</div>',
                'link_before' => '<span class="page-number">',
                'link_after'  => '</span>',
                'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'blazeblog' ) . ' </span>%',
                'separator'   => '<span class="screen-reader-text">, </span>',
            ) );    
}

/**
 * Determines whether blog/site has more than one category.
 *
 * Create your own blazeblog_categorized_blog() function to override in a child theme.
 *
 * @since BlazeBlog 1.0
 *
 * @return bool True if there is more than one category, false otherwise.
 */
function blazeblog_categorized_blog() {
	if ( false === ( $all_the_cool_cats = get_transient( 'blazeblog_categories' ) ) ) {
		// Create an array of all the categories that are attached to posts.
		$all_the_cool_cats = get_categories( array(
			'fields'     => 'ids',
			// We only need to know if there is more than one category.
			'number'     => 2,
		) );

		// Count the number of categories that are attached to the posts.
		$all_the_cool_cats = count( $all_the_cool_cats );

		set_transient( 'blazeblog_categories', $all_the_cool_cats );
	}

	if ( $all_the_cool_cats > 1 ) {
		// This blog has more than 1 category so blazeblog_categorized_blog should return true.
		return true;
	} else {
		// This blog has only 1 category so blazeblog_categorized_blog should return false.
		return false;
	}
}

/**
 * Flushes out the transients used in blazeblog_categorized_blog().
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_category_transient_flusher() {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	// Like, beat it. Dig?
	delete_transient( 'blazeblog_categories' );
}
add_action( 'edit_category', 'blazeblog_category_transient_flusher' );
add_action( 'save_post',     'blazeblog_category_transient_flusher' );

// modifies html template of categories posts count
if ( ! function_exists( 'categories_postcount_filter' ) ) :
    function categories_postcount_filter ( $links ) {
        $links = str_replace('</a> (', '<span class="post-count">', $links);
        $links = str_replace(')', '</span></a>', $links);
        return $links;
    }
    add_filter('wp_list_categories','categories_postcount_filter');
endif;

// returns HTML template for WordPress Comments Form
if ( ! function_exists( 'blazeblog_comment_form' ) ) :
    function blazeblog_comment_form(){
        $commenter = wp_get_current_commenter();
        $req = get_option( 'require_name_email' );
        $aria_req = ( $req ? " aria-required='true'" : '' );
        $user = wp_get_current_user();
        $user_identity = $user->display_name;
        $comment_form_fields = array(
            'author' => 
            '<div class="form-group comment-form-author">' .
                '<div class="form-group">' .
                    '<input id="author" name="author" type="text"' . 
                    ' class="form-control" placeholder="'. __('Enter Name...', 'blazeblog') .'"' .
                    ' value="' . esc_attr( $commenter['comment_author'] ) .
                    '" size="30"' . $aria_req . ' />' .
                '</div>' .
            '</div>'
            ,

            'email' =>
            '<div class="form-group comment-form-email">' .
                '<div class="form-group">' .
                    '<input id="email" name="email" type="text"' . 
                    ' class="form-control" placeholder="'. __('Enter Email...', 'blazeblog') .'"' .
                    ' value="' . esc_attr(  $commenter['comment_author_email'] ) .
                    '" size="30"' . $aria_req . ' />' .
                '</div>' .
            '</div>'
            ,
        );

        $comment_form_args = array(
			'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
			'title_reply_after'  => '</h2>',

            'title_reply'       =>  __('Leave a Comment', 'blazeblog'),

            'comment_notes_before' => '<p>'. __('All fields are required. Your email address will not be published.', 'blazeblog') .'</p>',

            'label_submit'      => __('Post Comment', 'blazeblog'),

            'fields' => $comment_form_fields,

            'comment_field' =>  
            '<div class="form-group comment-form-comment">' .
                '<div class="form-group">' .
                    '<textarea id="comment" name="comment" rows="8"' .
                    ' class="form-control" placeholder="'. __('Comment Here...', 'blazeblog') .'"' .
                    ' aria-required="true">' .
                    '</textarea>' .
                '</div>' .
            '</div>',

            'comment_notes_after' => '',

            'logged_in_as' =>
            '<p class="logged-in-as">' . sprintf( __('Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>', 'blazeblog'), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>',

            'must_log_in' =>
            '<p class="must-log-in">' .  sprintf( __('You must be <a href="%1$s">logged in</a> to post a comment.', 'blazeblog'), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'

        );

        echo '<div class="row">';
        echo '<div class="col-sm-12">';
        comment_form($comment_form_args); 
        echo '<div class="clearfix"></div>';
        echo '</div>';
        echo '</div>';
    }
endif;

// returns HTML template for comments/discussion threads under the post or page.
if ( ! function_exists( 'blazeblog_comment' ) ) :
    function blazeblog_comment($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;
        extract($args, EXTR_SKIP);

        if ( 'div' == $args['style'] ) {
            $tag = 'div';
            $add_below = 'comment';
        } else {
            $tag = 'li';
            $add_below = 'div-comment';
        }

        ?>
<<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? 'media' : 'media parent') ?> id="comment-<?php comment_ID() ?>">
    <div class="pull-left">
        <span class="media-object">
            <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
        </span>
    </div>
    <?php if ( 'div' != $args['style'] ) : ?>
    <div id="div-comment-<?php comment_ID(); ?>" class="comment-body media-body">
    <?php endif; ?>
        <div class="comment-author vcard">
            <?php printf( '<a href="%1$s" rel="nofollow" target="_blank"><cite class="fn%3$s">%2$s</cite></a>', 
                    get_comment_author_url(),
                    get_comment_author(), 
                    ((get_comment_author() == get_the_author()) ? ' original-author' : '') ); ?>
        </div>
        <?php if ($comment->comment_approved == '0') : ?>
        <small class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'blazeblog'); ?></small>
        <br />
        <?php endif; ?>
        <div class="comment-meta commentmetadata comment-date">
            <?php printf( '<a href="%1$s">%2$s at %3$s</a>', get_comment_link(), get_comment_date(),  get_comment_time()); ?> 
        </div>
        <div class="comment-content">
            <?php comment_text() ?>
        </div>
        <div class="comment-footer">
            <?php edit_comment_link( '<i class="glyphicon glyphicon-edit"></i> ' . __('Edit', 'blazeblog'), '  ', '' ); ?>
            <?php comment_reply_link( 
                    array_merge( $args, 
                        array(
                            'add_below' => $add_below, 
                            'depth' => $depth, 
                            'max_depth' => $args['max_depth'],
                            'reply_text' => '<i class="glyphicon glyphicon-share-alt"></i> ' . __('Reply', 'blazeblog'),
                            'login_text' => __('Log in to Reply', 'blazeblog')
                        )
                     ) 
               ); ?>
        </div>
    <?php if ( 'div' != $args['style'] ) : ?>
    </div>
    <?php endif; ?>
    <div class="clearfix"></div>
<?php
    }
endif;
?>