<?php

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

/**
 * Displays a donation message in the WordPress admin dashboard.
 *
 * The message encourages users to support the development of the plugin through donations. 
 * It includes an option to hide the message for a configurable number of months.
 *
 * @return void Exits early if the message is within the hidden duration period.
 */
function eslp_show_donate_message() {
    // Number of months to hide the donation message.
    $months_to_hide = 1; // Please don't change this value. This is my job! Thanks!

    // Calculate the duration in seconds for the hiding period.
    $hide_duration = $months_to_hide * MONTH_IN_SECONDS;

    // Retrieve the timestamp of the last time the message was hidden.
    $eslp_hide_donate_time = get_option( 'eslp_hide_donate_time' );
    $eslp_last_hide = get_option( 'eslp_last_hide' );

    // Check if the message should still be hidden based on the elapsed time.
    if ( $eslp_hide_donate_time && time() - $eslp_last_hide < $hide_duration ) {
        return; // Exit the function without displaying the message.
    }

    // Display the donation message in the WordPress admin dashboard.
    ?>
    <div id="eslp_donate_message_id">
        <h1><?php echo esc_html( __( 'Easy Social Login', 'easy-social-login' ) ); ?></h1>
        <p>
            🎉 <strong>
				<?php echo esc_html( __( "Would you like to help develop the plugin?", "easy-social-login" ) ); ?>
            </strong><br>
				<?php echo esc_html( __( "Your donation makes all the difference in keeping this project going and evolving.", "easy-social-login" ) ); ?>
            <a href="<?php echo esc_url( 'https://www.paypal.com/donate/?hosted_button_id=QC7BKFDAUXLQQ'); ?>" target="_blank">
                <?php echo esc_html( __( "Donate now", "easy-social-login" ) ); ?>
            </a>.
        </p>
        <a href="#" id="eslp_close_time_message_id">
			<?php 
				// translators: %d represents the number of months the message will be hidden.
				echo esc_html( sprintf( __( "Hide it for %d months", "easy-social-login" ), $months_to_hide ) );
			?>
        </a>
        <a href="#" id="eslp_close_message_id">❌</a>
    </div>
	<?php
}
add_action( 'admin_notices', 'eslp_show_donate_message' );

/**
 * Hides the donation message for a specified period.
 *
 * This function updates two WordPress options to control the visibility of the donation message:
 * - `eslp_hide_donate_time`: Sets a flag indicating that the message should be hidden.
 * - `eslp_last_hide`: Stores the current timestamp to calculate the hiding duration.
 *
 * The function is triggered via an AJAX request to the action `eslp_hide_message_time`, 
 * allowing the donation message to be hidden dynamically without a page refresh.
 *
 * @return void This function does not return a value; it terminates the execution using `wp_die()`.
 */
function eslp_hide_message_run() {
    // Set the option to hide the donation message.
    update_option('eslp_hide_donate_time', true);
    
    // Record the current timestamp as the last hide time.
    update_option('eslp_last_hide', time());
    
    // Terminate the execution of the script to respond to the AJAX request.
    wp_die();
}
// Hook the function to handle the AJAX action `eslp_hide_message_time`.
add_action('wp_ajax_eslp_hide_message_time', 'eslp_hide_message_run');
