<?php
/**
 * Echo header fragments.
 *
 * @package SplendorStudio\Framework\Templates\Fragments
 *
 * @since   1.0.0
 */

class Fancify_Lite_Header_Fragment extends Fancify_Lite_Template_Parts {
	/**
	 * Constructor
	 *
	 * @since    1.0.0
	 */
	function __construct() {
		$this->register_hook_callbacks();
	}

	/**
	 * Register callbacks for actions and filters
	 *
	 * @since    1.0.0
	 */
	protected function register_hook_callbacks() {
		// Check for coming soon
		if ( fancify_lite_is_coming_soon() ) {
			return;
		}
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_head', array( $this, 'head_meta' ), 0 );
		Fancify_Lite_Actions::add_smart_action( 'wp_head', array( $this, 'head_pingback' ) );
		// adds html markups for branding section
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_header_main_left_append_markup', array( $this, 'site_branding' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_site_branding_append_markup', array( $this, 'site_title_tag' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_site_title_link_append_markup', array( $this, 'header_logo' ) );

	}

	/**
	 * Echo head meta.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function head_meta() {
		fancify_lite_selfclose_markup_e(
			'fancify_lite_head_meta[charset]', 
			'meta', 
			array(
				'charset' => esc_attr( get_bloginfo( 'charset' ) )
			)
		);

		fancify_lite_selfclose_markup_e(
			'fancify_lite_head_meta[viewport]',
			'meta',
			array(
				'name'    => 'viewport',
				'content' => 'width=device-width, initial-scale=1',
			)
		);
	}

	/**
	 * Echo head pingback.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function head_pingback() {
		fancify_lite_selfclose_markup_e(
			'fancify_lite_head_link[pingback]',
			'link',
			array(
				'rel'  => 'pingback',
				'href' => esc_url( get_bloginfo( 'pingback_url' ) ),
			)
		);
	}

	/**
	 * Echo header site branding.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function site_branding() {
		fancify_lite_open_markup_e(
			'fancify_lite_site_branding',
			'div',
			array(
				'class' => 'fancify-site-branding fancify-float-left' . ( !get_bloginfo('description') ? ' fancify-margin-small-top' : null ),
			)
		);

		fancify_lite_open_markup_e(
			'fancify_lite_site_title_link',
			'a',
			array(
				'href'      => esc_url( home_url() ),
				'rel'       => 'home',
				'itemprop'  => 'headline',
				'class'     => 'fancify-site-branding-title'
			)
		);

		$logo = $this->get_header_logo();
		
		if ( ! $logo ) {
			fancify_lite_output_e('fancify_lite_site_title_text', esc_html( get_bloginfo('name') ) );
		}

		// phpcs:enable Generic.WhiteSpace.ScopeIndent.IncorrectExact -- Code structure mimics HTML markup.
		fancify_lite_close_markup_e('fancify_lite_site_title_link', 'a');

		fancify_lite_close_markup_e('fancify_lite_site_branding', 'div');
	}

	/**
	 * Echo header site title tag.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function site_title_tag() {
		// Stop here if there isn't a description.
		$description = get_bloginfo('description');

		if ( ! $description) {
			return;
		}

		if( $this->get_header_logo() ){
			$class = 'fancify-site-title-tag fancify-text-small fancify-text-muted fancify-display-block fancify-hidden-text';
		} else {
			$class = 'fancify-site-title-tag fancify-text-small fancify-text-muted fancify-display-block';
		}

		fancify_lite_open_markup_e(
			'fancify_lite_site_title_tag',
			'span',
			array(
				'class'    => esc_attr( $class ),
				'itemprop' => 'description',
			)
		);

		fancify_lite_output_e( 'fancify_lite_site_title_tag_text', esc_html( $description ) );

		fancify_lite_close_markup_e( 'fancify_lite_site_title_tag', 'span' );
	}

	/**
	 * Echo top header  section.
	 *
	 * @since 1.0.9
	 *
	 * @return void
	 */
	function header_logo() {
		$logo = $this->get_header_logo();

		if ( $logo ) {
			fancify_lite_selfclose_markup_e(
				'fancify_lite_logo_image',
				'img',
				array(
					'class' => 'fancify-logo',
					'src'   => esc_url( $logo ),
					'alt'   => esc_attr( get_bloginfo( 'name' ) ),
				)
			);

			fancify_lite_open_markup_e(
				'fancify_lite_site_title_text',
				'h3', 
				array( 
					'class' => 'fancify-site-title fancify-hidden-text'
				)
			);

			fancify_lite_output_e( 
				'fancify_lite_site_title_text', 
				esc_html( get_bloginfo('name') )
			);

			fancify_lite_close_markup_e(
				'fancify_lite_site_title_text', 
				'h3'
			);

		}

	}

	/**
	 * Get header logo.
	 *
	 * @since 1.0.9
	 *
	 * @return void
	 */
	function get_header_logo() {
		$custom_logo_id = get_theme_mod( 'custom_logo' );
		$logo = wp_get_attachment_image_src( $custom_logo_id , 'full' );
		 
		if ( has_custom_logo() ) {
		   $logo =  esc_url( $logo[0] );
		} else {
			$logo = get_theme_mod( 'header_logo', false );
		}

		return $logo;
	}
}

Fancify_Lite_Header_Fragment::get_instance();