<?php
/**
 * Class RPGAL_Init
 *
 * This file contains functions that play a crucial role in the initial setup
 * of the "Responsive Portfolio Image Gallery" plugin. These functions handle tasks such as
 * text domain setup for translations, adding action links to the plugin settings,
 * and various other essential tasks needed when the plugin is live at the front-end.
 *
 * @package Responsive Portfolio Image Gallery 1.3 - 10 July, 2025
 * @link https://www.realwebcare.com/
 */
if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly
}

if (!class_exists('RPGAL_Init')) {
	class RPGAL_Init
	{

		private static $instance;
		private $rpgalUpdateChecker;

		public function __construct()
		{
			// Load necessary required files.
			$this->required_files();

			// Init admin notice for review
			add_action('admin_init', array($this, 'rpgal_check_i10n_date'));

			// Init to remove admin notice
			add_action('admin_init', array($this, 'rpgal_spare_me'), 5);
		}

		public static function get_instances()
		{
			if (self::$instance) {
				return self::$instance;
			}

			self::$instance = new self();

			return self::$instance;
		}

		/**
		 * Includes required files for the plugin's functionality.
		 *
		 * This method loads the necessary PHP files that provide core functionality for the plugin.
		 * By requiring these files, the plugin ensures that all actions, options, and shortcodes
		 * are properly initialized and available.
		 *
		 * @return void
		 */
		private function required_files()
		{
			// Handles the enqueueing of scripts and styles.
			require_once RPGAL_PLUGIN_PATH . 'includes/rpgal-enqueue.php';

			// Initializes any additional functions required by the plugin.
			require_once RPGAL_PLUGIN_PATH . 'includes/rpgal-functions.php';

            // Help file of the plugin
            require_once RPGAL_PLUGIN_PATH . 'includes/rpgal-guide.php';

			// Sidebar of the plugin
			require_once RPGAL_PLUGIN_PATH . 'includes/rpgal-sidebar.php';

			require_once RPGAL_PLUGIN_PATH . 'init/rpgal-settings.php';

			require_once RPGAL_PLUGIN_PATH . 'init/rpgal-aq-resizer.php';

			require_once RPGAL_PLUGIN_PATH . 'custom-post/rpgal-post.php';

			require_once RPGAL_PLUGIN_PATH . 'custom-post/rpgal-metabox.php';

			// Manages shortcodes for user interactions.
			require_once RPGAL_PLUGIN_PATH . 'init/rpgal-shortcode.php';
		}

		/**
		 * Check date on admin initiation and add to admin notice if it was over 7 days ago.
		 *
		 * @return null
		 */
		public function rpgal_check_i10n_date()
		{
			// Retrieve the 'spare_me' option
			$spare_me = get_option('rpgal_spare_me', false);

			// Proceed only if 'spare_me' is not set
			if (!$spare_me) {
				// Retrieve the 'i10n_date' option and set a default value if not found
				$i10n_date = get_option('rpgal_a08n_time', false);

				// Calculate the past date for the threshold (7 days in seconds: 7 * 24 * 60 * 60)
				$past_date = strtotime('-7 days');

				// Validate the 'i10n_date' and compare it with the threshold
				if ($i10n_date && is_numeric($i10n_date) && $i10n_date < $past_date) {
					// If the condition is met, display the admin notice
					add_action('admin_notices', array($this, 'rpgal_display_admin_notice'));
				} else {
					// Otherwise, store the current timestamp as the activation time
					$current_time = time(); // Current timestamp
					update_option('rpgal_a08n_time', $current_time);
				}
			}
		}

		/**
		 * Display Admin Notice, asking for a review
		 **/
		public function rpgal_display_admin_notice()
		{
			// WordPress global variable
			global $pagenow;

			// Validate current admin page and query parameters
			if (is_admin() && $pagenow === 'admin.php' && isset($_GET['page']) && sanitize_text_field(wp_unslash($_GET['page'])) === 'rpgal-process') {
				// Generate URLs with proper escaping
				$dont_disturb = esc_url_raw(admin_url('admin.php?page=rpgal-process&spare_me=1'));
				$dont_disturb = wp_nonce_url($dont_disturb, 'rpgal_disturb_action', 'rpgal_disturb_nonce');

				// Retrieve plugin data securely
				$plugin_info = get_plugin_data(RPGAL_AUF, true, true);

				// Validate and sanitize plugin data
				$plugin_name = !empty($plugin_info['Name']) ? sanitize_text_field($plugin_info['Name']) : esc_html__('Responsive Portfolio Image Gallery', 'responsive-portfolio-image-gallery');
				$text_domain = !empty($plugin_info['TextDomain']) ? sanitize_text_field($plugin_info['TextDomain']) : 'responsive-portfolio-image-gallery';

				// Construct the review URL securely
				$review_url = 'https://wordpress.org/support/plugin/' . $text_domain . '/reviews/';
				$review_url = wp_nonce_url($review_url, 'rpgal_review_action', 'rpgal_review_nonce');

				// Output the notice with proper escaping
				printf(
					'<div id="rpgal-review" class="notice notice-info is-dismissible">
                        <p>%1$s</p>
                        <p>%2$s</p>
                        <div class="rpgal-review-btn">
                            <a href="%3$s" class="button button-secondary" target="_blank">%4$s</a>
                            <a href="%5$s" class="rpgal-grid-review-done button button-secondary">%6$s</a>
                        </div>
                    </div>',
					esc_html__('It\'s been 7 days since your last update or installation of the latest version of ', 'responsive-portfolio-image-gallery') . '<b>' . esc_html($plugin_name) . '</b>' . esc_html__('! We hope you\'ve had a positive experience so far.', 'responsive-portfolio-image-gallery'),
					esc_html__('Your feedback is important to us and can help us improve. If you find our ', 'responsive-portfolio-image-gallery') . '<b>' . esc_html($plugin_name) . '</b>' . esc_html__(' plugin valuable, could you please take a moment to share your thoughts by leaving a quick review?', 'responsive-portfolio-image-gallery'),
					esc_url($review_url),
					esc_html__('Leave a Review', 'responsive-portfolio-image-gallery'),
					esc_url($dont_disturb),
					esc_html__('Already Left a Review', 'responsive-portfolio-image-gallery')
				);
			}
		}

		/**
		 * remove the notice for the user if review already done or if the user does not want to
		 **/
		public function rpgal_spare_me()
		{
			// Check if 'spare_me' parameter is set and not empty
			if (isset($_GET['spare_me'], $_GET['rpgal_disturb_nonce']) && !empty($_GET['spare_me'])) {
				// Verify nonce
				if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['rpgal_disturb_nonce'])), 'rpgal_disturb_action')) {
					return;
				}
				// Sanitize the input value to ensure it is safe to use
				$spare_me = sanitize_text_field(wp_unslash($_GET['spare_me']));

				// Validate the value to check if it is the expected value
				if ($spare_me === '1') {
					// Add the 'rpgal_spare_me' option with a boolean value
					add_option('rpgal_spare_me', true);
				}
			}
		}
	}
}

RPGAL_Init::get_instances();
