<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File kept as class-donate-notice.php to preserve SVN file history; class follows the ELTMH_ prefix convention.
/**
 * Reusable dismissible donate notice for id7.dev plugins.
 *
 * Drop this file into any plugin's includes/ folder and instantiate once:
 *
 *   new ELTMH_Donate_Notice( __FILE__, array( 'my-screen-id' ) );
 *
 * The donate URL, plugin slug, and version are read automatically from the
 * plugin file headers (Donate link, Text Domain, and Version fields).
 * The rendered link appends UTM params identifying the plugin
 * (?utm_source=<slug>&utm_medium=plugin-notice&utm_campaign=donate&version=<ver>&site=<home_url>).
 *
 * @package Effortless_Taiwan_Mandarin_Helper
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'ELTMH_Donate_Notice' ) ) {

	/**
	 * Shows a dismissible "Support the development" admin notice on specified screens.
	 *
	 * Hooks into current_screen to detect the matching admin page, then registers
	 * admin_notices / network_admin_notices so the notice renders in the correct
	 * position on both single-site and multisite Network Admin pages.
	 */
	class ELTMH_Donate_Notice {

		/**
		 * Absolute path to the main plugin file.
		 *
		 * @var string
		 */
		private string $plugin_file;

		/**
		 * Admin screen IDs on which the notice should appear.
		 *
		 * @var string[]
		 */
		private array $screen_ids;

		/**
		 * Set up the notice and hook into current_screen for reliable screen detection.
		 *
		 * Using current_screen (rather than admin_notices directly) guarantees the
		 * correct screen ID is available and works identically on single-site admin
		 * pages and multisite Network Admin pages.
		 *
		 * @param string   $plugin_file Absolute path to the main plugin file (__FILE__).
		 * @param string[] $screen_ids  Admin screen IDs where the notice is displayed.
		 */
		public function __construct( string $plugin_file, array $screen_ids ) {
			$this->plugin_file = $plugin_file;
			$this->screen_ids  = $screen_ids;
			add_action( 'current_screen', array( $this, 'maybe_register_notice' ) );
		}

		/**
		 * Register the notice hooks when the current screen matches.
		 *
		 * The current screen ID is normalized by stripping any trailing -network or
		 * -user suffix that WordPress appends in those admin contexts. This way
		 * callers only need to specify the base screen ID and the notice works
		 * identically on single-site, Network Admin, and User Admin pages.
		 *
		 * Both admin_notices and network_admin_notices are registered so the notice
		 * fires regardless of whether this is a single-site or Network Admin page.
		 * Only one of the two actions fires per request.
		 *
		 * @param WP_Screen $screen The current admin screen.
		 * @return void
		 */
		public function maybe_register_notice( WP_Screen $screen ): void {
			$normalized_id = (string) preg_replace( '/-(network|user)$/', '', $screen->id );
			if ( ! in_array( $normalized_id, $this->screen_ids, true ) ) {
				return;
			}
			add_action( 'admin_notices', array( $this, 'render' ) );
			add_action( 'network_admin_notices', array( $this, 'render' ) );
		}

		/**
		 * Render the notice HTML.
		 *
		 * @return void
		 */
		public function render(): void {
			$data = get_file_data(
				$this->plugin_file,
				array(
					'DonateURI'  => 'Donate link',
					'TextDomain' => 'Text Domain',
					'Version'    => 'Version',
				)
			);

			$donate_uri = $data['DonateURI'];

			if ( empty( $donate_uri ) ) {
				return;
			}

			$url = add_query_arg(
				array(
					'utm_source'   => $data['TextDomain'],
					'utm_medium'   => 'plugin-notice',
					'utm_campaign' => 'donate',
					'version'      => $data['Version'],
					'site'         => home_url(),
				),
				$donate_uri
			);
			?>
			<div class="notice notice-info is-dismissible">
				<p>
					&#10084;&#65039; Like this plugin?
					<a href="<?php echo esc_url( $url ); ?>" target="_blank">
						Support the development
					</a>!
				</p>
			</div>
			<?php
		}
	}
}
