<?php
/**
 * Header Title.
 *
 * @package marie
 */

if ( ! function_exists( 'marie_get_page_header_title' ) ) {

	/**
	 * Get the title to use in marie-page-header. Mostly the same logic as breadcrumbs.
	 */
	function marie_get_page_header_title() {

		if ( ! is_home() || ! is_front_page() || ! is_paged() ) {

			// Custom Taxonomies eg '/staff/marie', '/location/prospect-hill'.
			$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

			// Check Custom Taxonomy First.
			if ( $term ) {
				// eg 'Galway City'.
				if ( ! empty( $term->name ) && is_string( $term->name ) ) {
					return $term->name;
				}
			} elseif ( is_category() ) {
				/* translators: %s: Category name */
				$txt = sprintf( __( 'Post category "%s"', 'marie' ), single_cat_title( '', false ) );
				return $txt;

			} elseif ( is_day() ) {

				// For posts from 23rd November 2020... 'home/2020/11/23'.
				$y   = get_the_time( 'Y' );
				$m   = get_the_time( 'F' );
				$d   = get_the_time( 'd' );
				$txt = $d . ' ' . $m . ' ' . $y;
				return $txt;
			} elseif ( is_month() ) {

				// For posts from November 2020... 'home/2020/11'.
				$y   = get_the_time( 'Y' );
				$m   = get_the_time( 'F' );
				$txt = $m . ' ' . $y;
				return $txt;

			} elseif ( is_year() ) {
				return get_the_time( 'Y' );

			} elseif ( is_single() && ! is_attachment() ) {

				return get_the_title();

			} elseif ( is_home() ) {
				$blog_page_title = get_the_title( get_option( 'page_for_posts', true ) );
				return $blog_page_title;

			} elseif ( ! is_single() && ! is_page() && get_post_type() !== 'post' && ! is_404() && ! is_search() ) {

				// This one is a post type archive. Eg. '/project(cpt)', '/youthclub(cpt)'.
				$post_type = get_post_type_object( get_post_type() );
				if ( $post_type && is_object( $post_type ) ) {
					return $post_type->labels->plural_name;
				}
			} elseif ( is_attachment() ) {

				return get_the_title();

			} elseif ( is_page() ) {
				// Regular page. Eg. '/resources'.
				if ( get_the_title() ) {
					return get_the_title();
				}
			} elseif ( is_search() ) {

				/* translators: %s: search term. */
				$txt = __( 'Search results', 'marie' );
				// Eg. 'home/search results for whatever' (Page 1 of results).
				return $txt;

			} elseif ( is_tag() ) {

				// Eg. /tag/test-tag.

				/* translators: %s: name of current tag. */
				$txt = sprintf( __( 'Posts tagged "%s"', 'marie' ), single_tag_title( '', false ) );
				return $txt;
			} elseif ( is_author() ) {
				global $author;

				/* translators: %s: author name. */
				$txt = sprintf( __( 'All posts by %s', 'marie' ), get_the_author_meta( 'display_name', $author ) );

				return $txt;
			} elseif ( is_404() ) {
				return 'Page not found';
			}
		}
	}
}
