<?php
/**
 * Template Tags for Eshraq Portfolio
 *
 * Custom template tags and functions used throughout the theme.
 *
 * @package Eshraq_Portfolio
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Print the post meta information.
 */
function eshraq_post_meta() {
	if ( 'post' !== get_post_type() ) {
		return;
	}

	$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 sr-only" 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() )
	);

	printf(
		'<span class="post-meta">%1$s</span>',
		$time_string // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	);
}

/**
 * Print the post category list.
 */
function eshraq_post_categories() {
	if ( 'post' !== get_post_type() ) {
		return;
	}

	$categories = get_the_category();
	if ( empty( $categories ) ) {
		return;
	}

	echo '<span class="post-categories">';
	foreach ( $categories as $index => $category ) {
		if ( $index > 0 ) {
			echo ', ';
		}
		printf(
			'<a href="%s" class="post-category">%s</a>',
			esc_url( get_category_link( $category->term_id ) ),
			esc_html( $category->name )
		);
	}
	echo '</span>';
}

/**
 * Print the post tags.
 */
function eshraq_post_tags() {
	if ( 'post' !== get_post_type() ) {
		return;
	}

	$tags = get_the_tags();
	if ( empty( $tags ) ) {
		return;
	}

	echo '<span class="post-tags">';
	foreach ( $tags as $index => $tag ) {
		if ( $index > 0 ) {
			echo ', ';
		}
		printf(
			'<a href="%s" class="post-tag">%s</a>',
			esc_url( get_tag_link( $tag->term_id ) ),
			esc_html( $tag->name )
		);
	}
	echo '</span>';
}

/**
 * Print the post navigation.
 */
function eshraq_post_navigation() {
	the_post_navigation( array(
		'prev_text' => '<span class="nav-label">' . esc_html__( 'Previous', 'eshraq-portfolio' ) . '</span><span class="nav-title">%title</span>',
		'next_text' => '<span class="nav-label">' . esc_html__( 'Next', 'eshraq-portfolio' ) . '</span><span class="nav-title">%title</span>',
	) );
}

/**
 * Print portfolio item meta.
 */
function eshraq_portfolio_meta() {
	$categories = get_the_terms( get_the_ID(), 'portfolio_category' );
	if ( empty( $categories ) || is_wp_error( $categories ) ) {
		return;
	}

	echo '<span class="portfolio-meta">';
	foreach ( $categories as $index => $category ) {
		if ( $index > 0 ) {
			echo ', ';
		}
		printf(
			'<a href="%s" class="portfolio-category">%s</a>',
			esc_url( get_term_link( $category ) ),
			esc_html( $category->name )
		);
	}
	echo '</span>';
}

/**
 * Custom logo HTML.
 */
function eshraq_custom_logo() {
	if ( has_custom_logo() ) {
		the_custom_logo();
	} else {
		printf(
			'<a href="%s" class="site-logo-text" aria-label="%s">%s</a>',
			esc_url( home_url( '/' ) ),
			esc_attr( get_bloginfo( 'name' ) ),
			esc_html( get_bloginfo( 'name' ) )
		);
	}
}

/**
 * Get the estimated reading time.
 */
function eshraq_reading_time() {
	$content    = get_post_field( 'post_content', get_the_ID() );
	$word_count = str_word_count( strip_tags( $content ) );
	$minutes    = max( 1, ceil( $word_count / 250 ) );

	return sprintf(
		/* translators: %d: number of minutes */
		esc_html( _n( '%d min read', '%d min read', $minutes, 'eshraq-portfolio' ) ),
		$minutes
	);
}

/**
 * Display pagination for archive pages.
 */
function eshraq_pagination() {
	the_posts_pagination( array(
		'mid_size'  => 2,
		'prev_text' => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>',
		'next_text' => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>',
		'class'     => 'eshraq-pagination',
	) );
}

/**
 * Sanitize callback for customizer color settings.
 */
function eshraq_sanitize_color( $color ) {
	return sanitize_hex_color( $color );
}

/**
 * Sanitize callback for customizer text settings.
 */
function eshraq_sanitize_text( $text ) {
	return sanitize_text_field( $text );
}

/**
 * Sanitize callback for customizer textarea settings.
 */
function eshraq_sanitize_textarea( $text ) {
	return sanitize_textarea_field( $text );
}

/**
 * Sanitize callback for customizer select settings.
 */
function eshraq_sanitize_select( $input, $setting ) {
	$choices = $setting->manager->get_control( $setting->id )->choices;
	return ( array_key_exists( $input, $choices ) ) ? $input : $setting->default;
}
