<?php
/**
 * Abstract base class for Smart Post Show Blocks.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */

namespace SmartPostShow\Blocks;

use SmartPostShow\Blocks\Block_Base;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Section Heading
 */
class Post_Title extends Block_Base {


	/**
	 * Set_block_properties
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name = 'post-title';
		$this->styles     = array( 'sp_smart_post_blocks_css', 'sp_smart_post_blocks_style', 'sp_smart_post_blocks_google_fonts' );
	}

	/**
	 * Render_block
	 *
	 * @param  array  $attributes attributes.
	 * @param  string $content  Content to be rendered.
	 * @param  array  $blocks   Array of block list.
	 * @return string Rendered HTML content.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {

		// Show the block inside the editor normally.
		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return $content;
		}

		// if ( ! ( is_singular() || is_archive() ) ) {
		// return '';
		// }

		// License check.
		$is_license_activated = $this->is_license_activated();
		if ( 'valid' !== $is_license_activated ) {
			return $is_license_activated;
		}

		$unique_id                 = $attributes['uniqueId'] ?? '';
		$html_tag                  = $attributes['htmlTag'] ?? 'h2';
		$advanced_additional_class = $attributes['additionalCssClass'] ?? '';
		$advanced_additional_id    = $attributes['additionalId'] ?? '';
		$layout_style              = $attributes['layoutStyle'] ?? 'layout-one';
		$align_class               = ! empty( $attributes['align'] ) ? 'align' . $attributes['align'] : '';
		$title_global_typography   = $attributes['titleGlobalTypography'] ?? '';
		$title_global_class        = $title_global_typography['class'] ?? '';

		// Actual post title.
		$post_title = get_the_title();

		$post_link = get_permalink();

		// Validate heading tag.
		$allowed_tags = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' );
		if ( ! in_array( strtolower( $html_tag ), $allowed_tags, true ) ) {
			$html_tag = 'h2';
		}

		ob_start();
		?>

		<div class="sp-smart-post-title-header-block <?php echo esc_attr( $align_class ); ?>">
			<div <?php echo $advanced_additional_id ? 'id="' . esc_attr( $advanced_additional_id ) . '"' : ''; ?>>
				<div
					class="sp-smart-post-title-header smart-post-title-wrapper sp-smart-post-wrapper <?php echo esc_attr( $layout_style . ' ' . $advanced_additional_class ); ?>"
					id="<?php echo esc_attr( $unique_id ); ?>"
					>
					<<?php echo esc_attr( $html_tag ); ?> class="smart-post-title <?php echo esc_attr( $title_global_class ); ?> ">
						<a href="<?php echo esc_url( $post_link ); ?>"><?php echo esc_html( $post_title ); ?></a>
					</<?php echo esc_attr( $html_tag ); ?>>
				</div>

			</div>
		</div>

		<?php
		return ob_get_clean();
	}
}
