<?php
/**
 * Manages the dynamic loading of author bio box templates.
 *
 * @package Author_Kit
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
/**
 * Author Kit Bio Box Manager
 *
 * Handles the display and rendering of author bio boxes within WordPress posts.
 */
class Author_Kit_Bio_Box_Manager {

	/**
	 * Determines whether the author bio box should be automatically displayed on posts.
	 *
	 * Value is retrieved from the `author_kit_bio_display` option, defaulting to true.
	 *
	 * @var bool
	 */
	protected $auto_display;

	/**
	 * Initialize hooks and filters.
	 */
	public function __construct() {
		$this->auto_display = get_option( 'author_kit_bio_display', true ); // Default to true if not set.

		if ( $this->auto_display ) {
			add_filter( 'the_content', array( $this, 'conditionally_display_author_bio' ) );
		}
	}

	/**
	 * Conditionally display the author bio box.
	 *
	 * @param string $content The original post content.
	 * @return string Modified post content with the author bio box.
	 */
	public function conditionally_display_author_bio( $content ) {
		if ( is_singular( 'post' ) ) {
			global $post;
			$author_id = $post->post_author;

			$display_position = get_theme_mod( 'author_kit_bio_box_display_position_default', 'after_content' );

			if ( $this->should_display_author_bio( $author_id, $post->ID ) ) {
				$bio_box = $this->get_author_bio_box( $author_id );

				if ( 'before_content' === $display_position ) {
					return $bio_box . $content;
				}
				return $content . $bio_box;
			}
		}
		return $content;
	}

	/**
	 * Check if the author bio box should be displayed.
	 *
	 * @param int $author_id The author ID.
	 * @param int $post_id The post ID.
	 * @return bool True if the author bio box should be displayed, false otherwise.
	 */
	protected function should_display_author_bio( $author_id, $post_id ) {
		return get_user_meta( $author_id, 'author_kit_bio_widget', true ) == 1 && ! get_post_meta( $post_id, 'hide_author_kit_bio_box', true );
	}


	/**
	 * Generate the author bio box HTML.
	 *
	 * @param int $author_id The author ID.
	 * @return string The author bio box HTML.
	 */
	protected function get_author_bio_box( $author_id ) {
		global $post;
		$post_id              = $post->ID;
		$avatar               = get_avatar_url( $author_id );
		$nicename             = author_kit_display_name( $author_id );
		$display_name         = get_the_author_meta( 'display_name', $author_id );
		$avatar_alt_text      = ! empty( $display_name ) ? esc_attr( $display_name . ' Avatar' ) : 'Author Avatar';
		$description          = get_the_author_meta( 'description', $author_id );
		$selected_information = author_kit_get_selected_sub_info_field( $author_id );

			// Conditionally hide bio box for authors with no bio
	if ( empty( $description ) ) {
		$show_empty_bio_box = get_option( 'author_kit_display_author_bio', true );
		if ( ! $show_empty_bio_box ) {
			return ''; // Skip if option is disabled and bio is empty
		}
	}
		$author_url    = author_kit_get_preferred_url( $author_id );
		$template_path = $this->get_active_template_path();

		if ( file_exists( $template_path ) ) {
			ob_start();
			include $template_path;
			return ob_get_clean();
		}
		return '<p>Author Bio template not found.</p>';
	}





	/**
	 * Get the active template path.
	 *
	 * @return string The active template path.
	 */
	protected function get_active_template_path() {
		$active_template = get_option( 'author_kit_author_bio_template', 'template-default' );
		$template_path   = AUTHOR_KIT_PLUGIN_PATH . 'public/templates/author-bio-box/' . $active_template . '.php';

		return file_exists( $template_path ) ? $template_path : AUTHOR_KIT_PLUGIN_PATH . 'public/templates/author-bio-box/template-default.php';
	}
}

/**
 * Initialize the Author Kit Bio Box Manager.
 */
add_action(
	'plugins_loaded',
	function () {
		new Author_Kit_Bio_Box_Manager();
	}
);
