<?php
/**
 * Archive elements.
 *
 * @package Nevo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! function_exists( 'nevo_archive_title' ) ) {
	/**
	 * Build the archive title
	 *
	 * @since 1.0
	 */
	function nevo_archive_title() {
		$is_blog = Nevo()->is_blog();
		$blog_title = get_bloginfo( 'title' );
		$display_title = Nevo()->get_setting( 'header_logo_name' );
		if ( $is_blog && $blog_title && $display_title === 'no' ) {
			echo '<div class="nevo-archive-title"><h1 class="page-title cv-mb-0">' . esc_html( $blog_title ) . '</h1></div>';
		}
		if ( is_archive() ) {
			?>
			<div class="nevo-archive-title">
				<h1 class="page-title cv-mb-0">
					<?php the_archive_title(); ?>
				</h1>
			</div>
			<?php
		}
	}
}

if ( ! function_exists( 'nevo_filter_the_archive_title' ) ) {
	add_filter( 'get_the_archive_title', 'nevo_filter_the_archive_title', 10, 3 );
	/**
	 * Alter the_archive_title() function to match our original archive title function
	 *
	 * @since 1.0
	 *
	 * @param string $title The archive title.
	 * @return string The altered archive title
	 */
	function nevo_filter_the_archive_title( $title ) {
		if ( is_category() ) {
			$title = single_cat_title( '', false );
		} elseif ( is_tag() ) {
			$title = single_tag_title( '', false );
		} elseif ( is_author() ) {
			/*
			 * Queue the first post, that way we know
			 * what author we're dealing with (if that is the case).
			 */
			the_post();

			$title = sprintf(
				'<div class="author-avatar author-image alignleft cv-me-3">%1$s</div><span class="vcard text-capitalize">%2$s</span>',
				get_avatar( get_the_author_meta( 'ID' ), $size = '45', $default = '', $alt = '', $args = array( 'class' => 'author-img' ) ),
				get_the_author()
			);

			/*
			 * Since we called the_post() above, we need to
			 * rewind the loop back to the beginning that way
			 * we can run the loop properly, in full.
			 */
			rewind_posts();
		} elseif ( is_post_type_archive() ) {
			$title = post_type_archive_title( '', false );
		} elseif ( is_tax() ) {
			$title = single_term_title( '', false );
		}
		
		return $title;
	}
}

/**
 * Output the archive description.
 *
 * @since 1.0
 */
function nevo_do_archive_description() {
	$term_description = get_the_archive_description();
	$is_blog = Nevo()->is_blog();
	$tagline = get_bloginfo( 'description' );
	if ( $is_blog && $tagline ) {
		echo '<div class="tagline">' . esc_html( $tagline ) . '</div>';
	}
	if ( ! empty( $term_description ) ) {
		$class = is_author() ? 'author-info' : 'taxonomy-description';
		printf( '<div class="' . esc_attr( $class ) . '">%s</div>', $term_description ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * nevo_after_archive_description hook.
	 *
	 * @since 1.0
	 */
	do_action( 'nevo_after_archive_description' );
}

/**
 * Add the search results title to the search results page.
 *
 * @since 1.0
 * @param string $template The template we're targeting.
 */
function nevo_do_search_results_title() {
	// Construct the search results title text with translation.
	$search_results_title = sprintf(
		/* translators: 1: Search query name */
		__( 'Search Results for: %s', 'nevo' ),
		'<span>' . esc_html( get_search_query() ) . '</span>'
	);

	// Apply the custom filter to the title text.
	$search_results_title = apply_filters( 'nevo_search_title_output', $search_results_title );

	// Output the title.
	echo '<h1 class="page-title">' . $search_results_title . '</h1>';
}