<?php
/**
 * Helper | Page Title
 *
 * @package Dotspice
 * @version 1.3.0
 */


/**
 * Get Page Title
 *
 * @param  array  $set_location
 * @return string
 */
function dotspice_get_page_title( $set_location = array() ) {
	global $wp_query;

	$page_title = '';

	// Localization
	$default_location = array(
		'home'       => esc_html__( 'Home', 'dotspice' ),
		'category'   => esc_html__( '%s', 'dotspice' ),
		'search'     => esc_html__( 'Search Results for: %s', 'dotspice' ),
		'author'     => esc_html__( 'Author: %s', 'dotspice' ),
		'tag'        => esc_html__( '%s', 'dotspice' ),
		'daily'      => esc_html__( 'Daily Archive for: %s', 'dotspice' ),
		'monthly'    => esc_html__( 'Monthly Archive for: %s', 'dotspice' ),
		'yearly'     => esc_html__( 'Yearly Archive for: %s', 'dotspice' ),
		'attachment' => esc_html__( 'Attachment: %s', 'dotspice' ),
		'error_page' => esc_html__( 'Not Found', 'dotspice' )
	);

	$location = (object) array_merge( $default_location, $set_location );

	// Blog
	if ( isset( $wp_query ) && (bool) $wp_query->is_posts_page ) {
		$post_id    = get_queried_object_id();
		$page_title = get_the_title( $post_id );
	}

	// Home
	else if ( is_home() || is_front_page() ) {
		$page_title = $location->home;
	}

	// Error 404
	else if ( is_404() ) {
		$page_title = $location->error_page;
	}

	// Category Archive
	else if ( is_category() ) {
		$page_title = sprintf( $location->category , single_cat_title( '', false ) );
	}

	// Search Archive
	else if ( is_search() ) {
		$page_title = sprintf( $location->search, get_search_query() );
	}

	// Tag Archive
	else if ( is_tag() ) {
		$page_title = sprintf( $location->tag, single_tag_title( '', false ) );
	}

	// Author Archive
	else if ( is_author() && $author = get_queried_object() ) {
		$page_title = sprintf( $location->author, $author->display_name );
	}

	// Post Type Archive
	else if ( is_post_type_archive() ) {
		$page_title = post_type_archive_title( '', false );

		if ( class_exists( 'woocommerce' ) && is_shop() ) {
			$page_title = get_the_title( wc_get_page_id( 'shop' ) );
		}
	}

	// Tax | Term Archive
	else if ( is_tax() ) {
		$page_title = single_term_title( '', false );
	}

	// Date Archives
	else if ( is_day() ) {
		$page_title = sprintf( $location->daily, get_the_time( 'd F Y' ) );
	}
	else if ( is_month() ) {
		$page_title = sprintf( $location->monthly, get_the_time( 'F, Y' ) );
	}
	else if ( is_year() ) {
		$page_title = sprintf( $location->yearly, get_the_time( 'Y' ) );
	}

	// Attachment
	else if ( is_attachment() ) {
		$attach_id  = get_queried_object_id();
		$page_title = sprintf( $location->attachment, get_the_title( $attach_id ) );
	}

	// Single Post | Page
	else if ( is_single() || is_page() ) {
		$post_id    = get_queried_object_id();
		$page_title = get_the_title( $post_id );
	}

	// Site Title
	else {
		$page_title = get_bloginfo( 'name', 'display' );
	}

	$page_title = wp_kses( $page_title, 'dotspice-tags' );

	return $page_title;
}


/**
 * Admin Title on Theme Editor Page
 *
 * @param  string  $admin_title
 * @param  string  $title
 * @return string
 */
function dotspice_page_title_on_theme_editor( $admin_title, $title ){

	if ( is_admin() && isset( $_GET['file'] ) && $_GET['file'] != '' ) {
		$full_name  = $_GET['file'];
		$name_array = explode( '/', $full_name );

		if ( count( $name_array ) > 1 ) {
			return array_pop( $name_array ) . ' | ' . $full_name;
		} else {
			return $full_name;
		}
	} else {
		return $admin_title;
	}
}
add_filter( 'admin_title', 'dotspice_page_title_on_theme_editor', 10, 2 );