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

if ( ! function_exists( 'ndesign_posted_on' ) ) :
	/**
	 * Prints HTML with meta information for the current post-date/time.
	 */
	function ndesign_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', 'ndesign' ),
			'<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( 'ndesign_posted_by' ) ) :
	/**
	 * Prints HTML with meta information for the current author.
	 */
	function ndesign_posted_by() {
		$byline = sprintf(
			/* translators: %s: post author. */
			esc_html_x( 'by %s', 'post author', 'ndesign' ),
			'<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>'
		);
		echo '<span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
	}
endif;
if ( ! function_exists( 'ndesign_posted_category' ) ) :
	/**
	 * Prints HTML with meta information for the current author.
	 */
	function ndesign_posted_category() {
		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__( ', ', 'ndesign' ) );
			if ( $categories_list ) {
				/* translators: 1: list of categories. */
				printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s', 'ndesign' ) . '</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', 'ndesign' ) );
			if ( $tags_list ) {
				/* translators: 1: list of tags. */
				printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', 'ndesign' ) . '</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>', 'ndesign' ),
						array(
							'span' => array(
								'class' => array(),
							),
						)
					),
					get_the_title()
				)
			);
			echo '</span>';
		}
	}
endif;
if ( ! function_exists( 'ndesign_entry_footer' ) ) :
	/**
	 * Prints HTML with meta information for the categories, tags and comments.
	 */
	function ndesign_entry_footer() {
		edit_post_link(
			sprintf(
				wp_kses(
					/* translators: %s: Name of current post. Only visible to screen readers */
					__( 'Edit this post <span class="screen-reader-text">%s</span>', 'ndesign' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				get_the_title()
			),
			'<span class="edit-link">',
			'</span>'
		);
	}
endif;
if ( ! function_exists( 'ndesign_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 ndesign_post_thumbnail() {
		global $post;
		if ( post_password_required() || is_attachment()) {
			return;
		}
		else if(! has_post_thumbnail() && !is_singular() && (ndesign_catch_that_image($post)!=='')){
			$ndesign_feature_image_html='<img src="'.ndesign_catch_that_image($post).'">';
			?>
			<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
				<?php
				echo $ndesign_feature_image_html;
				?>
			</a>
			<?php
		}
		if ( is_singular() ) :
			?>
			<?php if(has_post_thumbnail()){ ?>
				<div class="post-thumbnail">
					<?php the_post_thumbnail(); ?>
				</div><!-- .post-thumbnail -->
			<?php } ?>
			<?php else : ?>
				<?php if(has_post_thumbnail()){ ?>
					<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;
?>
<?php
if ( ! function_exists( 'ndesign_cust_post_navigation' ) ) :
	/**
	 * For Custom next and prev links
	 */
	function ndesign_cust_post_navigation() {
		// FOR PREVIOUS POST.
		$prev_post = get_previous_post();
		if ( ! empty( $prev_post ) ) {
			$id = $prev_post->ID;
			$prev_permalink = get_permalink( $id );
		}
		$next_post = get_next_post();
		if ( ! empty( $next_post ) ) {
			$nid = $next_post->ID;
			$next_permalink = get_permalink( $nid );
		}
		?>
		<div class="cust-post-navigation mb-4">
			<div class="row">
				<div class="previous-timeline col-6">
					<?php if ( ! empty( $prev_post ) ) : ?>
						<?php previous_post_link( '%link', __( '<span class="meta-nav"><i class="fa fa-long-arrow-left"></i></span> Previous', 'ndesign' ) ); ?>
						<h4><a href="<?php echo esc_url( $prev_permalink ); ?>"><?php echo wp_kses_data( $prev_post->post_title ); ?></a></h4>
					<?php endif ?>
				</div>
				<div class="next-timeline col-6 text-right">
					<?php if ( ! empty( $next_post ) ) : ?>
						<?php next_post_link( '%link', __( 'Next <span class="meta-nav"><i class="fa fa-long-arrow-right"></i></span>', 'ndesign' ) ); ?>
						<h4><a href="<?php echo esc_url( $next_permalink ); ?>"><?php echo wp_kses_data( $next_post->post_title ); ?></a></h4>
					<?php endif ?>
				</div>
			</div>
		</div>
		<?php
	}
endif;
?>
