<?php
/**
 * Custom Footer Class
 *
 * @package errigal
 * @since 1.0.0
 */

namespace Errigal\Customiser;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_Class,
	Errigal_Footer_Hook as Footer_Hook,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
};
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Site_Info as Site_Info,
	Errigal_Theme_Info as Theme_Info,
	Errigal_Validator as Validator,
};

/**
 * This class is in charge of providing site footer settings and functions.
 *
 * @since 1.0.0
 */
class Errigal_Custom_Footer extends Errigal_Custom_Base {
	/**
	 * Setting ID: Copyright text.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const ID_COPYRIGHT_TEXT = 'footer_copyright_text';

	/**
	 * Shortcode: Copyright.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const SHORTCODE_COPYRIGHT = '[copyright]';

	/**
	 * Shortcode: Current year.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const SHORTCODE_CURRENT_YEAR = '[current_year]';

	/**
	 * Shortcode: Site title.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const SHORTCODE_SITE_TITLE = '[site_title]';

	/**
	 * Shortcode: Theme link.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const SHORTCODE_THEME_LINK = '[theme_link]';

	/**
	 * Copyright sign.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const COPYRIGHT_SIGN = '&copy;';

	/**
	 * Gets copyright text.
	 *
	 * @since 1.2.0
	 *
	 * @return string
	 */
	public static function get_copyright_text() {
		$copyright_text = self::get_copyright_text_mod();

		if ( ! Validator::is_non_empty_string( $copyright_text ) ) {
			return '';
		}

		/**
		 * Filters should add screen reader only theme credit.
		 *
		 * @since 1.2.0
		 *
		 * @param bool $should_add_screen_reader_only_theme_credit Whether to add screen reader only theme credit.
		 */
		$should_add_screen_reader_only_theme_credit = apply_filters(
			Footer_Hook::SHOULD_ADD_SCREEN_READER_ONLY_THEME_CREDIT,
			! Validator::string_contains( $copyright_text, self::SHORTCODE_THEME_LINK )
		);
		$copyright_text                             = self::replace_shortcodes( $copyright_text );

		if ( Validator::is_true( $should_add_screen_reader_only_theme_credit ) ) {
			$copyright_text .= self::get_screen_reader_only_theme_credit();
		}

		return wp_kses_post( $copyright_text );
	}

	/**
	 * Gets default copyright text.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_default_copyright_text() {
		return sprintf(
			'%1$s %2$s %3$s | %4$s',
			self::SHORTCODE_COPYRIGHT,
			self::SHORTCODE_CURRENT_YEAR,
			self::SHORTCODE_SITE_TITLE,
			self::SHORTCODE_THEME_LINK
		);
	}

	/**
	 * Gets current year.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_current_year() {
		return esc_html( date_i18n( 'Y' ) );
	}

	/**
	 * Gets theme link.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_theme_link() {
		$theme_uri  = Theme_Info::get_theme_uri();
		$theme_name = Theme_Info::get_theme_name();

		if (
			! Validator::is_non_empty_string( $theme_uri ) ||
			! Validator::is_non_empty_string( $theme_name )
		) {
			return '';
		}

		return sprintf(
			/* translators: %s: Link to errigal theme web site. */
			esc_html__( 'Built with %s', 'errigal' ),
			Html_Builder::create()->create_a(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'site-footer__theme-link',
						Html_Attr::HREF       => $theme_uri,
						Html_Attr::TARGET     => Html_Attr::VALUE_BLANK,
						Html_Attr::REL        => array(
							Html_Attr::VALUE_NOOPENER,
							Html_Attr::VALUE_NOFOLLOW,
						),
					),
				)
			)->get_element(
				esc_html( $theme_name )
			)
		);
	}

	/**
	 * Gets copyright text mod.
	 *
	 * @since 1.2.0
	 *
	 * @return string
	 */
	private static function get_copyright_text_mod() {
		return wp_kses_post(
			self::get_theme_mod_string(
				self::ID_COPYRIGHT_TEXT,
				self::get_default_copyright_text()
			)
		);
	}

	/**
	 * Gets shortcode replacements.
	 *
	 * @since 1.1.0
	 *
	 * @return array
	 */
	private static function get_shortcode_replacements() {
		return array(
			self::SHORTCODE_COPYRIGHT    => self::COPYRIGHT_SIGN,
			self::SHORTCODE_CURRENT_YEAR => self::get_current_year(),
			self::SHORTCODE_SITE_TITLE   => Site_Info::get_title(),
			self::SHORTCODE_THEME_LINK   => self::get_theme_link(),
		);
	}

	/**
	 * Replaces shortcodes.
	 *
	 * @since 1.2.0
	 *
	 * @param string $copyright_text Copyright text.
	 * @return string
	 */
	private static function replace_shortcodes( $copyright_text ) {
		if ( ! Validator::is_non_empty_string( $copyright_text ) ) {
			return '';
		}

		$shortcode_replacements = self::get_shortcode_replacements();

		if ( ! Validator::is_associative_array( $shortcode_replacements ) ) {
			return '';
		}

		foreach ( $shortcode_replacements as $shortcode => $replace ) {
			if (
				! Validator::is_non_empty_string( $shortcode ) ||
				! Validator::is_non_empty_string( $replace )
			) {
				continue;
			}

			$copyright_text = str_replace(
				$shortcode,
				$replace,
				$copyright_text
			);
		}

		return wp_kses_post( $copyright_text );
	}

	/**
	 * Gets screen reader only theme credit.
	 *
	 * @since 1.2.0
	 *
	 * @return string
	 */
	private static function get_screen_reader_only_theme_credit() {
		$theme_link = self::get_theme_link();

		if ( ! Validator::is_non_empty_string( $theme_link ) ) {
			return '';
		}

		return Html_Builder::create()->create_span(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
				),
			)
		)->get_element(
			esc_html(
				wp_strip_all_tags( $theme_link )
			)
		);
	}
}
