<?php
/**
 * Admin Notice Suppressor.
 *
 * Prevents third-party admin notices, banners, and promotional messages
 * from appearing on UGC Commerce plugin pages.
 *
 * @package Ugcify\Admin
 */

namespace Ugcify\Admin;

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

/**
 * Admin Notice Suppressor class.
 */
class AdminNoticeSuppressor {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'maybe_suppress_notices' ), 1 );
	}

	/**
	 * Check if we're on a plugin page and suppress third-party notices.
	 *
	 * @param string $hook The current admin page hook.
	 * @return void
	 */
	public function maybe_suppress_notices( $hook ) {
		if ( strpos( $hook, 'ugcify' ) === false ) {
			return;
		}

		// Remove all third-party notice hooks.
		$this->remove_third_party_notices();

		// Re-add our own notice hook so only plugin notices display.
		add_action( 'admin_notices', array( $this, 'render_plugin_notices' ), 999 );

		// Hide any notices that slip through via inline CSS.
		add_action( 'admin_head', array( $this, 'hide_notice_markup' ), 999 );
	}

	/**
	 * Remove all callbacks from notice-related hooks except our own.
	 *
	 * @return void
	 */
	private function remove_third_party_notices() {
		$notice_hooks = array(
			'admin_notices',
			'all_admin_notices',
			'network_admin_notices',
			'user_admin_notices',
		);

		foreach ( $notice_hooks as $hook_name ) {
			remove_all_actions( $hook_name );
		}

		// Remove the WordPress update nag on plugin pages.
		remove_action( 'admin_notices', 'update_nag', 3 );
		remove_action( 'admin_notices', 'maintenance_nag', 10 );
	}

	/**
	 * Render notices that belong to this plugin only.
	 *
	 * Fires at priority 999 on admin_notices after all third-party
	 * callbacks have been removed.
	 *
	 * @return void
	 */
	public function render_plugin_notices() {
		/**
		 * Hook for displaying UGC Commerce admin notices.
		 *
		 * Use this hook to add notices that should appear on plugin pages.
		 *
		 * @since 0.0.3
		 */
		do_action( 'ugcify_admin_notices' );
	}

	/**
	 * Enqueue inline CSS to hide any notices that bypass hook removal.
	 *
	 * Some plugins inject notices via direct output or late-bound hooks.
	 * This CSS hides those while preserving our own notices.
	 *
	 * @return void
	 */
	public function hide_notice_markup() {
		$css = '
			.wrap > .notice:not(.ugcify-notice),
			.wrap > .updated:not(.ugcify-notice),
			.wrap > .error:not(.ugcify-notice),
			.wrap > .update-nag,
			#wpbody-content > .notice:not(.ugcify-notice),
			#wpbody-content > .updated:not(.ugcify-notice),
			#wpbody-content > .error:not(.ugcify-notice),
			#wpbody-content > .update-nag,
			#wpbody-content > div.fs-notice,
			.wrap > div.fs-notice {
				display: none !important;
			}
		';
		wp_register_style( 'ugcify-notice-suppressor', false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters
		wp_enqueue_style( 'ugcify-notice-suppressor' );
		wp_add_inline_style( 'ugcify-notice-suppressor', $css );
	}
}
