<?php
/**
 * Custom template tags for this theme
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package craftyblog
 */

if ( ! function_exists( 'craftyblog_posted_on' ) ) :
	/**
	 * Prints HTML with meta information for the current post-date/time.
	 */
	function craftyblog_posted_on() {
		$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( DATE_W3C ) ),
			esc_html( get_the_date() ),
			esc_attr( get_the_modified_date( DATE_W3C ) ),
			esc_html( get_the_modified_date() )
		);

		$posted_on = sprintf(
			/* translators: %s: post date. */
			esc_html_x( 'Posted on %s', 'post date', 'craftyblog' ),
			'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
		);

		echo '<span class="posted-on">' . $posted_on . '</span>'; // WPCS: XSS OK.

	}
endif;

if ( ! function_exists( 'craftyblog_posted_by' ) ) :
	/**
	 * Prints HTML with meta information for the current author.
	 */
	function craftyblog_posted_by() {
		$byline = sprintf(
			/* translators: %s: post author. */
			esc_html_x( 'by %s', 'post author', 'craftyblog' ),
			'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
		);

		return '<span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.

	}
endif;

if ( ! function_exists( 'craftyblog_entry_footer' ) ) :
	/**
	 * Prints HTML with meta information for the categories, tags and comments.
	 */
	function craftyblog_entry_footer() {
		// Hide category and tag text for pages.
		if ( 'post' === get_post_type() ) {
			/* translators: used between list items, there is a space after the comma */
			$categories_list = get_the_category_list( esc_html__( ', ', 'craftyblog' ) );
			if ( $categories_list ) {
				/* translators: 1: list of categories. */
				printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s', 'craftyblog' ) . '</span>', $categories_list ); // WPCS: XSS OK.
			}

			/* translators: used between list items, there is a space after the comma */
			$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'craftyblog' ) );
			if ( $tags_list ) {
				/* translators: 1: list of tags. */
				printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', 'craftyblog' ) . '</span>', $tags_list ); // WPCS: XSS OK.
			}
		}

		if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
			echo '<span class="comments-link">';
			comments_popup_link(
				sprintf(
					wp_kses(
						/* translators: %s: post title */
						__( 'Leave a Comment<span class="screen-reader-text"> on %s</span>', 'craftyblog' ),
						array(
							'span' => array(
								'class' => array(),
							),
						)
					),
					get_the_title()
				)
			);
			echo '</span>';
		}

		edit_post_link(
			sprintf(
				wp_kses(
					/* translators: %s: Name of current post. Only visible to screen readers */
					__( 'Edit <span class="screen-reader-text">%s</span>', 'craftyblog' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				get_the_title()
			),
			'<span class="edit-link">',
			'</span>'
		);
	}
endif;

if ( ! function_exists( 'craftyblog_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.
	 */
	function craftyblog_post_thumbnail() {
		if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
			return;
		}

		if ( is_singular() ) :
			?>

			<div class="post-thumbnail">
				<?php the_post_thumbnail(); ?>
			</div><!-- .post-thumbnail -->

		<?php else : ?>

		<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
			<?php
			the_post_thumbnail(
				'post-thumbnail',
				array(
					'alt' => the_title_attribute(
						array(
							'echo' => false,
						)
					),
				)
			);
			?>
		</a>

		<?php
		endif; // End is_singular().
	}
endif;




if ( ! function_exists( 'craftyblog_comment_markup' ) ) :

	/**
	 * Parameter $comment
	 *
	 * Used as a callback by wp_list_comments() for displaying the comments.
	 *
	 * @since Shape 1.0
	 */
	function craftyblog_comment_markup( $comment, $args, $depth ) {

		extract( $args, EXTR_SKIP );

		if ( 'div' == $args['style'] ) {
			$tag = 'div';
			$add_below = 'comment';
		} else {
			$tag = 'li';
			$add_below = 'div-comment';
		}
		?>
  <<?php echo esc_attr( $tag ); ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">
		<?php if ( 'div' == $args['style'] ) : ?>
  <div id="div-comment-<?php comment_ID(); ?>" class="comment-body review-list">
	<?php endif; ?>
	<div class="single-comment clearfix">
		<div class="commenter-image">
			<?php
			if ( 0 != $args['avatar_size'] ) {
				echo get_avatar( $comment, $args['avatar_size'] );}
			?>
		</div>
		<div class="commnenter-details">
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em><?php esc_html_e( 'Your comment is awaiting moderation.', 'craftyblog' ); ?></em>
			<br />
		<?php endif; ?>
			<h4><?php printf( wp_kses_post( '%s', 'craftyblog' ), sprintf( '%s', get_comment_author_link() ) ); ?></h4>
			<div class="comment-time">
				<p><time datetime="<?php comment_time( 'c' ); ?>">
								<?php
									/* translators: 1: comment date, 2: comment time */
									printf( wp_kses_post( '%1$s at %2$s', 'craftyblog' ), get_comment_date( '', $comment ), get_comment_time() );
								?>
							</time></p>
			</div>
				<?php comment_text(); ?>
				<?php
				comment_reply_link(
					array_merge(
						$args,
						array(
							'depth'     => $depth,
							'max_depth' => $args['max_depth'],
							'before'    => '<div class="reply">',
							'after'     => '</div>',
						)
					)
				);
				?>
		</div>
	</div>
		<?php if ( 'div' == $args['style'] ) : ?>
  </div>
			<?php
  endif;

	}
endif; // ends check for craftyblog_comment_markup()


function craftyblog_get_excerpt( $limit, $source = null ) {

	if ( $source == 'content' ? ( $excerpt = get_the_content() ) : ( $excerpt = get_the_excerpt() ) ) {
		$excerpt = $excerpt;
	}
	$excerpt = preg_replace( ' (\[.*?\])', '', $excerpt );
	$excerpt = strip_shortcodes( $excerpt );
	$excerpt = strip_tags( $excerpt );
	$excerpt = substr( $excerpt, 0, $limit );
	$excerpt = substr( $excerpt, 0, strripos( $excerpt, ' ' ) );
	$excerpt = trim( preg_replace( '/\s+/', ' ', $excerpt ) );
	return $excerpt;
}


function craftyblog_social_activity(){
	$facebook = get_theme_mod( 'facebook', '#' );
	$twitter = get_theme_mod('twitter', '#');
	$amazon = get_theme_mod('amazon');
	$pinterest = get_theme_mod('pinterest');
	$youtube = get_theme_mod('youtube', '#');
	$instagram = get_theme_mod('instagram', '#');
	$github = get_theme_mod('github');
	$stumbleupon = get_theme_mod('stumbleupon');
	$tumblr = get_theme_mod('tumblr');
	$whatsapp = get_theme_mod('whatsapp');
	$weixin = get_theme_mod('weixin');
	$snapchat = get_theme_mod('snapchat');
	$qq = get_theme_mod('qq');
	$reddit = get_theme_mod('reddit');
	$linkedin = get_theme_mod('linkedin');
		if(!empty($facebook)) : ?>
		<a href="<?php echo esc_url( $facebook ); ?>" class="fa fa-facebook"></a>
		<?php endif; if(!empty($twitter)): ?>
		<a href="<?php echo esc_url( $twitter ); ?>" class="fa fa-twitter"></a>
		<?php endif; if(!empty($amazon)) :?>
		<a href="<?php echo esc_url( $amazon ); ?>" class="fa fa-amazon"></a>
		<?php endif; if(!empty($pinterest)) : ?>
		<a href="<?php echo esc_url( $pinterest ); ?>" class="fa fa-pinterest"></a>
		<?php endif; if(!empty($youtube)) :?>
		<a href="<?php echo esc_url( $youtube ); ?>" class="fa fa-youtube"></a>
		<?php endif; if(!empty($linkedin)) :?>
		<a href="<?php echo esc_url( $linkedin ); ?>" class="fa fa-linkedin"></a>
		<?php endif; if(!empty($instagram)) :?>
		<a href="<?php echo esc_url( $instagram ); ?>" class="fa fa-instagram"></a>
		<?php endif; if(!empty($github)) :?>
		<a href="<?php echo esc_url( $github ); ?>" class="fa fa-github"></a>
		<?php endif; if(!empty($stumbleupon)) :?>
		<a href="<?php echo esc_url( $stumbleupon ); ?>" class="fa fa-stumbleupon"></a>
		<?php endif; if(!empty($tumblr)) :?>
		<a href="<?php echo esc_url( $tumblr ); ?>" class="fa fa-tumblr"></a>
		<?php endif; if(!empty($whatsapp)) :?>
		<a href="<?php echo esc_url( $whatsapp ); ?>" class="fa fa-whatsapp"></a>
		<?php endif; if(!empty($weixin)) :?>
		<a href="<?php echo esc_url( $weixin ); ?>" class="fa fa-weixin"></a>
		<?php endif; if(!empty($snapchat)) :?>
		<a href="<?php echo esc_url( $snapchat ); ?>" class="fa fa-snapchat"></a>
		<?php endif; if(!empty($qq)) :?>
		<a href="<?php echo esc_url( $qq ); ?>" class="fa fa-qq"></a>
		<?php endif; if(!empty($reddit)) :?>
		<a href="<?php echo esc_url( $reddit ); ?>" class="fa fa-reddit"></a>
		<?php endif;
}
function craftyblog_pagination(){
	$next_icon = '<i class="fa fa-angle-right" aria-hidden="true"></i>';
    $prev_icon = '<i class="fa fa-angle-left" aria-hidden="true"></i>';
	the_posts_navigation(
		array(
			'mid_size'           => 1,
			'prev_text'          => $prev_icon,
			'next_text'          => $next_icon,
		)
	);
}