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

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Author_Kit_Archive_Manager
 * Handles author archive template selection and loading.
 */
class Author_Kit_Archive_Manager {

	/**
	 * Constructor: Hooks the template loading function.
	 */
	public function __construct() {
		add_filter( 'template_include', array( $this, 'load_author_archive_template' ), 999 );
	}

	/**
	 * Loads the correct author archive template.
	 *
	 * @param string $template Default template path.
	 * @return string Template file path.
	 */
	public function load_author_archive_template( $template ) {
		if ( ! is_author() ) {
			return $template;
		}

		$template_path = AUTHOR_KIT_PLUGIN_PATH . 'public/templates/archive-pages/author-kit-archive-template.php';

		if ( file_exists( $template_path ) ) {
			return $template_path;
		}

		return $template;
	}
}

/**
 * Initialize the Author_Kit_Archive_Manager if enabled.
 */
add_action(
	'init',
	function () {
		if ( get_option( 'author_kit_archive_display', true ) ) {
			new Author_Kit_Archive_Manager();
		}
	}
);
