<?php
/**
 * PluginActionLinksHook class.
 *
 * Adds a "Settings" link to the plugin actions list.
 *
 * @package Ilabs\Firevert\Core\Hooks\Admin
 */

declare(strict_types=1);

namespace Ilabs\Firevert\Core\Hooks\Admin;

/**
 * Class PluginActionLinksHook
 */
class PluginActionLinksHook {

	/**
	 * Path to main plugin file (e.g., firevert/firevert.php).
	 *
	 * @var string
	 */
	private string $plugin_basename;

	/**
	 * Slug of the settings page (used in admin.php?page=...).
	 *
	 * @var string
	 */
	private string $settings_page_slug;

	/**
	 * Constructor.
	 *
	 * @param string $plugin_basename    Path to the main plugin file (e.g., firevert/firevert.php).
	 * @param string $settings_page_slug Slug of the settings page (used in admin.php?page=...).
	 */
	public function __construct( string $plugin_basename, string $settings_page_slug ) {
		$this->plugin_basename    = $plugin_basename;
		$this->settings_page_slug = $settings_page_slug;
	}

	/**
	 * Register the filter hook.
	 *
	 * @return void
	 */
	public function register(): void {
		add_filter( 'plugin_action_links_' . $this->plugin_basename, array( $this, 'add_settings_link' ) );
	}

	/**
	 * Add a settings link to the plugin actions.
	 *
	 * @param string[] $links Existing plugin action links.
	 *
	 * @return string[] Modified list of plugin action links with "Settings".
	 */
	public function add_settings_link( array $links ): array {
		$settings_url  = admin_url( 'admin.php?page=' . $this->settings_page_slug );
		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			esc_url( $settings_url ),
			esc_html__( 'Settings', 'firevert' )
		);

		array_unshift( $links, $settings_link );
		return $links;
	}
}
