<?php
/**
 * Update version notice for Forumax
 */

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

/**
 * Display backup warning notice on the plugins page for Forumax update
 *
 * @param array    $plugin_data Plugin metadata.
 * @param stdClass $response   Response metadata.
 */
function forumax_update_version_notice( $plugin_data, $response ) {
	// Plugin current version
	$current_version = defined( 'FORUMAX_VERSION' ) ? FORUMAX_VERSION : '';
	$new_version     = isset( $response->new_version ) ? $response->new_version : '';

	if ( empty( $current_version ) || empty( $new_version ) ) {
		return;
	}

	// Only show notice if current version is less than 2.1.0
	if ( version_compare( $current_version, '2.1.0', '>=' ) ) {
		return;
	}

	$slug           = isset( $response->slug ) ? $response->slug : 'forumax';
	$plugin_file    = isset( $response->plugin ) ? $response->plugin : '';
	$changelog_link = 'https://forumax.spider-themes.net/changelog/';
	$active         = ! empty( $plugin_file ) && is_plugin_active( $plugin_file ) ? 'active' : '';
	?>
	<div class="frmx-update-notice">
		<hr class="frmx-major-update-warning__separator" />
		<div class="frmx-major-update-warning">
			<div class="frmx-major-update-warning__icon">
				<span class="dashicons dashicons-warning"></span>
			</div>
			<div>
				<div class="frmx-major-update-warning__title">
					<?php echo esc_html__( 'Important: Please Backup Before Updating', 'forumax' ); ?>
				</div>
				<div class="frmx-major-update-warning__message">
					<div>
						<?php
						printf(
							esc_html__( 'Forumax v%s brings minor upgrades and enhancements across the plugin. To keep your site safe, make sure you create a complete backup before updating.', 'forumax' ),
							'2.1.0'
						);
						?>
					</div>
					<div>
						<?php esc_html_e( 'We highly recommend updating on a staging environment before applying it to your live site.', 'forumax' ); ?>
					</div>
					<a href="<?php echo esc_url( $changelog_link ); ?>" target="_blank" class="frmx-changelog-btn"><?php esc_html_e( 'View the changelogs', 'forumax' ); ?></a>
				</div>
			</div>
		</div>
	</div>
	<?php
}

if ( defined( 'FORUMAX_FILE' ) ) {
	$basename = plugin_basename( FORUMAX_FILE );
	add_action( 'in_plugin_update_message-' . $basename, 'forumax_update_version_notice', 10, 2 );

	/**
	 * Also hook into bbp-core/forumax.php to handle cases where the plugin is
	 * recognized by the bbp-core slug on WordPress.org.
	 */
	if ( 'bbp-core/forumax.php' !== $basename ) {
		add_action( 'in_plugin_update_message-bbp-core/forumax.php', 'forumax_update_version_notice', 10, 2 );
	}
}