<?php

namespace WarrantyCart\Settings;

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

/**
 * Plugin Links Settings Class
 */
class PluginLinks {

	/**
	 * Constructor
	 */
	public function __construct() {
		\add_filter( 'plugin_action_links_' . \plugin_basename( WARRANTY_CART_FILE ), array( $this, 'add_action_links' ) );
		\add_filter( 'plugin_row_meta', array( $this, 'add_row_meta' ), 10, 2 );
	}

	/**
	 * Add action links to plugin listing
	 *
	 * Adds dashboard and payments links to the plugin action links.
	 *
	 * @param array<int, string> $links Existing action links.
	 *
	 * @return array<int, string> Modified action links.
	 */
	public function add_action_links( array $links ): array {
		/**
		 * Filter plugin action links
		 *
		 * @param array $links Existing action links.
		 */
		$links = \apply_filters( 'warranty_cart_plugin_action_links', $links );

		$dashboard_link = \sprintf(
			'<a href="%s">%s</a>',
			\admin_url( 'admin.php?page=warranty-cart' ),
			\__( 'Dashboard', 'warranty-cart' )
		);
		$settings_link  = \sprintf(
			'<a href="%s">%s</a>',
			\admin_url( 'admin.php?page=wc-settings&tab=checkout' ),
			\__( 'Payments', 'warranty-cart' )
		);

		\array_unshift( $links, $dashboard_link, $settings_link );

		return $links;
	}

	/**
	 * Add row meta to plugin listing
	 *
	 * Adds documentation and GitHub links to the plugin row meta.
	 *
	 * @param array<int, string> $links Existing row meta.
	 * @param string             $file  Plugin file.
	 *
	 * @return array<int, string> Modified row meta.
	 */
	public function add_row_meta( array $links, string $file ): array {
		if ( \plugin_basename( WARRANTY_CART_FILE ) !== $file ) {
			return $links;
		}

		/**
		 * Filter plugin row meta links
		 *
		 * @param array $links Existing row meta links.
		 */
		$links = \apply_filters( 'warranty_cart_plugin_row_meta', $links );

		$links[] = \sprintf(
			'<a href="%s" target="_blank">%s</a>',
			'https://github.com/mralaminahamed/warranty-cart#readme',
			\__( 'Documentation', 'warranty-cart' )
		);

		$links[] = \sprintf(
			'<a href="%s" target="_blank">%s</a>',
			'https://github.com/mralaminahamed/warranty-cart',
			\__( 'GitHub', 'warranty-cart' )
		);

		return $links;
	}
}
