<?php
/**
 * Activity Guard – Real Time Notifier to Slack Rest Route.
 *
 * This class is used to response and all rest route works.
 *
 * @package WPNTS\Inc.
 */

namespace WPNTS\Inc;

use WPNTS\Inc\wpnts_nts_fs; // phpcs:ignore
use WPNTS\Inc\Activate; // phpcs:ignore
use WPNTS\Inc\Deactivate; // phpcs:ignore
use WPNTS\Inc\SlackAttachment; // phpcs:ignore
use WPNTS\Inc\SlackChannelRouter; // phpcs:ignore

defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');
/**
 * Update used to rest route created.
 *
 * @since 1.0.0
 */
class WPUpdate {

	/**
	 * Construct method.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$schedules_int = get_option( 'wpnts_schedules_interval_site_settings');
		$schedules_interval = json_decode($schedules_int);
		$wpnts_wpnotification = $schedules_interval->wpnotification ?? 'false';

		add_filter( 'cron_schedules', [ $this, 'wpnts_add_cron_interval' ]);

		// Schedule the cron job.
		if ( true === $wpnts_wpnotification ) {
			add_action( 'wpnts_corn_hook', [ $this, 'wpnts_wordpress_core_update' ]);
		}
	}

	/**
	 * Corn setup time interval.
	 *
	 * @since 1.0.0
	 */
	public function wpnts_add_cron_interval() {

		$schedules_int = get_option( 'wpnts_schedules_interval_site_settings');
		$schedules_interval = json_decode($schedules_int);
		$wpnts_time = isset($schedules_interval->interval_plugin_update) ? (int) $schedules_interval->interval_plugin_update : 120;

		$schedules['added_schedules_interval'] = [
			'interval' => isset($wpnts_time) ? $wpnts_time : 30,
			'display'  => esc_html__( 'Assigned Time', 'notifier-to-slack' ),
		];
		return $schedules;
	}

	/**
	 * WordPress core update.
	 *
	 * @since 1.0.0
	 */
	public function wpnts_wordpress_core_update() {

		if ( ! function_exists( 'get_core_updates' ) ) {
			require_once ABSPATH . '/wp-admin/includes/update.php';
		}

		$last_updates = get_option( 'wpnts_last_wordpress_core_updates', 3 );
		$current_time = time();

		$schedules_int = get_option( 'wpnts_schedules_interval_site_settings');
		$schedules_interval = json_decode($schedules_int);

		$wpnts_time = isset($schedules_interval->interval_plugin_update) ? (int) $schedules_interval->interval_plugin_update : 120;

		$wpnotification = $schedules_interval->wpnotification ?? 'false';

		if ( true === $wpnotification && isset($last_updates) && ( $current_time - $last_updates ) >= $wpnts_time ) {

			$schedules_int = get_option( 'wpnts_schedules_interval_site_settings');
			$schedules_interval = json_decode($schedules_int);
			$wpnts_webhook = $schedules_interval->webhook ?? '';

			$slack_webhook_url = $wpnts_webhook;

			$wp_version = get_bloginfo( 'version' );
			$core_updates = get_core_updates();

			// Get the site URL, including child site for multisite.
			$attachmentHandler = new SlackAttachment();
			$site_url = $attachmentHandler->getSiteURLAddOption();

			if ( ! empty( $core_updates ) && version_compare( $wp_version, $core_updates[0]->current, '<' ) ) {

					$message = 'A new WordPress version (' . $core_updates[0]->current . ' -> ' . $core_updates[0]->response . ') is available for update. ' . $site_url;

					// Send notification to Slack with channel routing.
					SlackChannelRouter::send_notification_with_routing(
						'wpnts_schedules_interval_site_settings',
						$message
					);

			}

			update_option( 'wpnts_last_wordpress_core_updates', time() );
		}
	}
}
