<?php
/**
 * Hooks class.
 *
 * @package DokanKits\Admin
 * @since   3.0.0
 */

namespace DokanKits\Admin;

use DokanKits\Core\Interfaces\Hookable;

/**
 * Hooks Class
 *
 * Handles admin hooks registration and callbacks
 *
 * @since   3.0.0
 * @package DokanKits\Admin
 */
class Hooks implements Hookable {

	/**
	 * Setup hooks
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		/**
		 * Action before admin hooks are registered
		 *
		 * @since 3.0.0
		 * @param Hooks $this Hooks instance.
		 */
		do_action( 'dokan_kits_before_admin_hooks_registration', $this );

		// Plugin action links.
		add_filter( 'plugin_action_links_' . DOKAN_KITS_BASENAME, array( $this, 'plugin_action_links' ) );  // @phpstan-ignore-line

		// Plugin row meta.
		add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );

		/**
		 * Action to register additional admin hooks
		 *
		 * @since 3.0.0
		 * @param Hooks $this Hooks instance.
		 */
		do_action( 'dokan_kits_register_hooks', $this );

		/**
		 * Action after admin hooks are registered
		 *
		 * @since 3.0.0
		 * @param Hooks $this Hooks instance.
		 */
		do_action( 'dokan_kits_after_admin_hooks_registration', $this );
	}

	/**
	 * Add plugin action links
	 *
	 * @since 3.0.0
	 *
	 * @param array $links Plugin action links.
	 *
	 * @return array Modified action links.
	 */
	public function plugin_action_links( array $links ): array {
		/**
		 * Filter the settings URL.
		 *
		 * @since 3.0.0
		 *
		 * @param string $url The settings URL.
		 */
		$settings_url = apply_filters( 'dokan_kits_settings_url', admin_url( 'admin.php?page=dokan-kits' ) );

		/**
		 * Filter the settings text.
		 *
		 * @since 3.0.0
		 *
		 * @param string $text The settings text.
		 */
		$settings_text = apply_filters( 'dokan_kits_settings_text', __( 'Settings', 'dokan-kits' ) );

		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			$settings_url,
			$settings_text
		);

		/**
		 * Filter the settings link.
		 *
		 * @since 3.0.0
		 *
		 * @param string $settings_link The settings link.
		 * @param string $settings_url  The settings URL.
		 * @param string $settings_text The settings text.
		 */
		$settings_link = apply_filters( 'dokan_kits_settings_link', $settings_link, $settings_url, $settings_text );

		/**
		 * Action before plugin action links are modified
		 *
		 * @since 3.0.0
		 * @param array $links Original plugin action links.
		 */
		do_action( 'dokan_kits_before_plugin_action_links', $links );

		array_unshift( $links, $settings_link );

		/**
		 * Filter the plugin action links.
		 *
		 * @since 3.0.0
		 *
		 * @param array $links The plugin action links.
		 */
		return apply_filters( 'dokan_kits_plugin_action_links', $links );
	}

	/**
	 * Add plugin row meta
	 *
	 * @since 3.0.0
	 *
	 * @param array  $links Plugin row meta.
	 * @param string $file  Plugin file.
	 *
	 * @return array Modified row meta.
	 */
	public function plugin_row_meta( array $links, string $file ): array {
		if ( DOKAN_KITS_BASENAME !== $file ) {  // @phpstan-ignore-line
			return $links;
		}

		/**
		 * Action before plugin row meta is modified
		 *
		 * @since 3.0.0
		 * @param array $links Original plugin row meta.
		 * @param string $file Plugin file.
		 */
		do_action( 'dokan_kits_before_plugin_row_meta', $links, $file );

		/**
		 * Filter the documentation URL.
		 *
		 * @since 3.0.0
		 *
		 * @param string $url The documentation URL.
		 */
		$docs_url = apply_filters( 'dokan_kits_docs_url', 'https://wpintegrity.com/knowledgebase/' );

		/**
		 * Filter the support URL.
		 *
		 * @since 3.0.0
		 *
		 * @param string $url The support URL.
		 */
		$support_url = apply_filters( 'dokan_kits_support_url', 'https://wordpress.org/support/plugin/dokan-kits/' );

		/**
		 * Filter the documentation text.
		 *
		 * @since 3.0.0
		 *
		 * @param string $text The documentation text.
		 */
		$docs_text = apply_filters( 'dokan_kits_docs_text', __( 'Documentation', 'dokan-kits' ) );

		/**
		 * Filter the support text.
		 *
		 * @since 3.0.0
		 *
		 * @param string $text The support text.
		 */
		$support_text = apply_filters( 'dokan_kits_support_text', __( 'Support', 'dokan-kits' ) );

		$row_meta = array(
			'docs'    => sprintf(
				'<a href="%s" target="_blank">%s</a>',
				esc_url( $docs_url ),
				$docs_text
			),
			'support' => sprintf(
				'<a href="%s" target="_blank">%s</a>',
				esc_url( $support_url ),
				$support_text
			),
		);

		/**
		 * Filter the plugin row meta.
		 *
		 * @since 3.0.0
		 *
		 * @param array  $row_meta The plugin row meta.
		 * @param string $file     The plugin file.
		 */
		$row_meta = apply_filters( 'dokan_kits_plugin_row_meta', $row_meta, $file );

		/**
		 * Action after plugin row meta is modified
		 *
		 * @since 3.0.0
		 * @param array $row_meta Modified plugin row meta.
		 * @param array $links Original plugin row meta.
		 * @param string $file Plugin file.
		 */
		do_action( 'dokan_kits_after_plugin_row_meta', $row_meta, $links, $file );

		return array_merge( $links, $row_meta );
	}
}
