<?php
/**
 * Header Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Header_Hook as Header_Hook,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Sidebar_Class as Sidebar_Class,
	Errigal_WP_Hook as WP_Hook,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Customiser\Errigal_Custom_Site_Header_Colour as Site_Header_Colour;
use Errigal\Navigation\Errigal_Main_Menu as Main_Menu;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Site_Info as Site_Info,
	Errigal_Utility as Utility,
	Errigal_Validator as Validator,
};
use Errigal\Sidebar\Errigal_Sidebar as Sidebar;

/**
 * This class is in charge of adding header hooks.
 *
 * @since 1.1.0
 */
class Errigal_Header_Hooks extends Errigal_Hooks_Base {
	/**
	 * Adds pingback link.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/wp_head/
	 *
	 * @return void
	 */
	public function add_pingback_link() {
		// Adds pingback url auto-discovery header for single posts, pages, or attachments.
		if ( ! ( is_singular() && pings_open() ) ) {
			return;
		}

		Html_Builder::create()->create_link(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::REL  => 'pingback',
					Html_Attr::HREF => Site_Info::get_pingback_url(),
				),
			)
		)->render_element();
	}

	/**
	 * Renders breadcrumb.
	 *
	 * @since 1.2.0
	 *
	 * @see template-parts\component\navigation\breadcrumb.php
	 *
	 * @return void
	 */
	public function render_breadcrumb() {
		get_template_part( 'template-parts/component/navigation/breadcrumb' );
	}

	/**
	 * Adds body classes.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/body_class/
	 *
	 * @param string[] $classes Body class names.
	 * @return string[]
	 */
	public function add_body_classes( $classes ) {
		if ( Validator::is_string_list( $classes ) ) {
			if ( ! is_singular() ) {
				$classes[] = 'hfeed';
			}

			if ( ! Main_Menu::can_show() ) {
				$classes[] = 'no-navigation-menu';
			}

			if ( wp_is_mobile() ) {
				$classes[] = 'mobile-device';
			}

			if ( ! Sidebar::can_show() || Content::is_no_sidebar_post() ) {
				$classes[] = Sidebar_Class::NO_SIDEBAR;
			}

			return Utility::sanitise_classes( $classes );
		}

		return $classes;
	}

	/**
	 * Adds site header classes.
	 *
	 * @since 1.1.0
	 *
	 * @see Errigal\Header\Errigal_Header::get_site_header_classes()
	 *
	 * @param string[] $classes Site header class names.
	 * @return string[]
	 */
	public function add_site_header_classes( $classes ) {
		if ( Validator::is_string_list( $classes ) ) {
			$classes[] = Site_Header_Colour::get_site_header_colour_class();

			return Utility::sanitise_classes( $classes );
		}

		return $classes;
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		add_action( WP_Hook::WP_HEAD, array( $this, 'add_pingback_link' ) );
		add_action( Header_Hook::BEFORE_MAIN_CONTENT, array( $this, 'render_breadcrumb' ) );

		add_filter( WP_Hook::BODY_CLASS, array( $this, 'add_body_classes' ) );
		add_filter( Header_Hook::SITE_HEADER_CLASSES, array( $this, 'add_site_header_classes' ) );
	}
}
