<?php

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

/**
 * The main Sifency Addons Notice class.
 *
 * @final
 * @since 1.0.4
 */
final class Sifency_Addons_Notice {

	/**
	 * Instance of the class.
	 *
	 * @var Sifency_Addons_Notice|null
	 */
	private static $instance = null;

	/**
	 * Singleton instance.
	 *
	 * @return Sifency_Addons_Notice
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Checks plugin requirements.
	 *
	 * @return bool
	 */
	public function check_requirements() {

		if ( version_compare( PHP_VERSION, SIFENCY_ADDONS_MIN_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_php_version' ) );
			return false;
		}

		if ( version_compare( get_bloginfo( 'version' ), SIFENCY_ADDONS_MIN_WP_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_wp_version' ) );
			return false;
		}

		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_missing_main_plugin' ) );
			return false;
		}

		if ( defined( 'ELEMENTOR_VERSION' ) && version_compare( ELEMENTOR_VERSION, SIFENCY_MIN_ELEMENTOR_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_elementor_version' ) );
			return false;
		}

		return true;
	}

	/**
	 * Admin notice: Missing Elementor plugin.
	 */
	public function admin_notice_missing_main_plugin() {
		if ( file_exists( WP_PLUGIN_DIR . '/elementor/elementor.php' ) ) {
			$elementor_link   = wp_nonce_url(
				self_admin_url( 'plugins.php?action=activate&plugin=elementor/elementor.php' ),
				'activate-plugin_elementor/elementor.php'
			);
			$elementor_action = esc_html__( 'Activate Elementor', 'sifency-addons' );
		} else {
			$elementor_link   = wp_nonce_url(
				self_admin_url( 'update.php?action=install-plugin&plugin=elementor' ),
				'install-plugin_elementor'
			);
			$elementor_action = esc_html__( 'Install Elementor', 'sifency-addons' );
		}

		$this->render_sifency_admin_notice(
			sprintf(
				'<div class="sifency-addons-notice-content sifency-flex-wrapper">%1$s %2$s</div>',
				sprintf(
					/* translators: 1: Sifency Addons, 2: Elementor */
					esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'sifency-addons' ),
					'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
					'<strong>' . esc_html__( 'Elementor', 'sifency-addons' ) . '</strong>'
				),
				sprintf(
					' <a href="%s" class="sifency-addons-button button-primary sifency-admin-btn">%s</a>',
					esc_url( $elementor_link ),
					esc_html( $elementor_action )
				)
			)
		);
	}


	/**
	 * Admin notice: Minimum Elementor version.
	 */
	public function admin_notice_minimum_elementor_version() {
		$this->render_sifency_admin_notice(
			sprintf(
				/* translators: 1: Sifency Addons, 2: Elementor. */
				esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'sifency-addons' ),
				'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
				'<strong>' . esc_html__( 'Elementor', 'sifency-addons' ) . '</strong>',
				SIFENCY_MIN_ELEMENTOR_VERSION
			)
		);
	}

	/**
	 * Admin notice: Minimum PHP version.
	 */
	public function admin_notice_minimum_php_version() {
		$this->render_sifency_admin_notice(
			sprintf(
				/* translators: 1: Sifency Addons, 2: Elementor. */
				esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'sifency-addons' ),
				'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
				'<strong>' . esc_html__( 'PHP', 'sifency-addons' ) . '</strong>',
				SIFENCY_ADDONS_MIN_PHP_VERSION
			)
		);
	}

	/**
	 * Admin notice: Minimum WordPress version.
	 */
	public function admin_notice_minimum_wp_version() {
		$this->render_sifency_admin_notice(
			sprintf(
				/* translators: 1: Sifency Addons, 2: Elementor. */
				esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'sifency-addons' ),
				'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
				'<strong>' . esc_html__( 'WordPress', 'sifency-addons' ) . '</strong>',
				SIFENCY_ADDONS_MIN_WP_VERSION
			)
		);
	}

	/**
	 * Renders an admin notice message.
	 *
	 * @param string $message Message to display.
	 */
	private function render_sifency_admin_notice( $message ) {
		printf(
			'<div class="notice notice-error sifency-notice-is-elementor-unactive"><p>%s</p></div>',
			wp_kses_post( $message )
		);
	}
}

	/**
	 * Admin Notice for Permalink Structure
	 */
function sifency_addons_permalink_notice() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$permalink_structure = get_option( 'permalink_structure' );

	if ( $permalink_structure !== '/%postname%/' ) {
		?>
			<div class="notice notice-error is-dismissible sifency-permalink-notice">
				<p>
					<strong><?php esc_html_e( 'Plain permalinks are not supported.', 'sifency-addons' ); ?></strong><br>
				<?php esc_html_e( 'We recommend using "Post name" as your permalink structure for compatibility.', 'sifency-addons' ); ?>
					<a class="sifency-admin-btn text_underline_animation" href="<?php echo esc_url( admin_url( 'options-permalink.php' ) ); ?>">
						<span><?php esc_html_e( 'Change Permalink Settings', 'sifency-addons' ); ?></span>
					</a>
				</p>
			</div>
			<?php
	}
}

	/**
	 * Add plugin action link to permalink settings page
	 */
function sifency_add_plugin_page_settings_link( $links ) {
	$links[] = '<a href="' . esc_url( admin_url( 'options-permalink.php' ) ) . '">' . esc_html__( 'Permalink Settings', 'sifency-addons' ) . '</a>';
	return $links;
}
		add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'sifency_add_plugin_page_settings_link' );
		add_action( 'admin_notices', 'sifency_addons_permalink_notice' );
