<?php
/**
 * Archive Title Block class
 *
 * @author Jegstudio
 * @since 1.0.0
 * @package gutenverse\block
 */

namespace Gutenverse\Block;

use Gutenverse\Framework\Block\Block_Abstract;

/**
 * Class Archive Title Block
 *
 * @package gutenverse\block
 */
class Archive_Title extends Block_Abstract {

	/**
	 * Render content
	 *
	 * @param string $archive_title .
	 * @param string $category_url .
	 *
	 * @return string
	 */
	public function render_content( $archive_title, $category_url ) {
		$html_tag     = esc_html( $this->check_tag( $this->attributes['htmlTag'], 'h2' ) );
		$link_target  = ! empty( $this->attributes['archiveLinkTarget'] ) ? '_blank' : '_self';
		$link_rel     = ! empty( $this->attributes['archiveLinkRel'] ) ? esc_attr( $this->attributes['archiveLinkRel'] ) : 'noreferrer';

		$archive_title = esc_html( $archive_title );

		if ( $category_url ) {
			$archive_title = "<a href='" . esc_url( $category_url ) . "' target='{$link_target}' rel='{$link_rel}'>{$archive_title}</a>";
		}

		return "<{$html_tag}>{$archive_title}</{$html_tag}>";
	}

	/**
	 * Render view in editor
	 */
	public function render_gutenberg() {
		return null;
	}

	/**
	 * Render view in frontend
	 *
	 * @return string
	 */
	public function render_frontend() {
		$element_id      = $this->get_element_id();
		$display_classes = $this->set_display_classes();
		$animation_class = $this->set_animation_classes();
		$custom_classes  = $this->get_custom_classes();
		$title           = 'Archive Title';
		$category_url    = '#';

		if ( is_category() || is_tag() ) {
			$title            = single_term_title( '', false );
			$current_category = get_queried_object();
			$category_url     = get_term_link( $current_category );
		}

		if ( is_author() ) {
			$author       = get_queried_object();
			$title        = $author->display_name;
			$category_url = get_author_posts_url( $author->ID );
		}

		$anchor     = isset( $this->attributes['anchor'] ) ? $this->attributes['anchor'] : '';
		$id_attr    = ! empty( $anchor ) ? ' id="' . esc_attr( $anchor ) . '"' : '';
		$class_name = trim( 'guten-element guten-archive-title ' . $element_id . $animation_class . $display_classes . $custom_classes );
		return '<div' . $id_attr . ' class="' . esc_attr( $class_name ) . '">' . $this->render_content( $title, $category_url ) . '</div>';
	}
}
