<?php
/**
 +===========================================================================+
 |    ____         __            ____ _               _               _      |
 |   / ___|  __ _ / _| ___ _ __ / ___| |__   ___  ___| | _____  _   _| |_    |
 |   \___ \ / _` | |_ / _ \ '__| |   | '_ \ / _ \/ __| |/ / _ \| | | | __|   |
 |    ___) | (_| |  _|  __/ |  | |___| | | |  __/ (__|   < (_) | |_| | |_    |
 |   |____/ \__,_|_|  \___|_|   \____|_| |_|\___|\___|_|\_\___/ \__,_|\__|   |
 |                                                                           |
 | (c) NinTechNet Limited ~ https://nintechnet.com/safercheckout/            |
 +===========================================================================+ 2025-01-22
*/

if (! defined('ABSPATH') ) {
	die('Forbidden');
}

// ===========================================================================


class SaferCheckoutLite_scheduler {


	/**
	 * Hooks.
	 */
	public static function init() {

		add_action('safercheckout_lite_scheduler', [ __CLASS__, 'run_scheduler'] );
	}


	/**
	 * Install the scheduled task (daily).
	 */
	public static function install_scheduler() {

		if ( wp_next_scheduled('safercheckout_lite_scheduler') ) {
			wp_clear_scheduled_hook('safercheckout_lite_scheduler');
		}
		/**
		 * Start in 5 minutes.
		 */
		wp_schedule_event( time() + 300, 'daily', 'safercheckout_lite_scheduler');
	}


	/**
	 * Uninstall the scheduled task.
	 */
	public static function uninstall_scheduler() {

		if ( wp_next_scheduled('safercheckout_lite_scheduler') ) {
			wp_clear_scheduled_hook('safercheckout_lite_scheduler');
		}
	}


	/**
	 * Run the scheduled task.
	 */
	public static function run_scheduler() {

		$iso = SAFERCHECKOUT_LITE_UPLOAD_DIR .'/iso3166.json';

		/**
		 * Cancel the download if the file was downloaded less than 22 hours ago.
		 * Used only in the event where the task scheduler is buggy, to prevent
		 * too many connections/downloads (default is daily).
		 */
		if ( is_file( $iso ) ) {
			$now		= time();
			$ftime	= filemtime( $iso );

			if ( $now - $ftime < 60 * 60 * 22 ) {
				return;
			}
			// phpcs:ignore  WordPress.WP.AlternativeFunctions.unlink_unlink
			unlink( $iso );
		}

		global $wp_version;
		$res = wp_remote_get(
			SAFERCHECKOUT_LITE_ISO,
			[
				'timeout'		=> 20,
				'httpversion'	=> '1.1' ,
				'user-agent'	=> 'Mozilla/5.0 (compatible; SaferChekoutLite/'.
										SAFERCHECKOUT_LITE_VERSION .'; WordPress/'. $wp_version .')',
				'sslverify'		=> true
			]
		);
		/**
		 * Save the latest version of iso3166 in json format.
		 */
		if (! is_wp_error( $res ) && wp_remote_retrieve_response_code( $res ) === 200 ) {
			$body = wp_remote_retrieve_body( $res );
			$data = json_decode( $body, true );
			if ( is_array( $data ) && ! empty( $data ) ) {
				// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
				file_put_contents( $iso, wp_json_encode( $data ) );
			}
		}
	}

}

SaferCheckoutLite_scheduler::init();

// ===========================================================================
// EOF
