Your IP : 216.73.216.215


Current Path : /home/flapst5/ekmtoronto.com/wp-content/plugins/wp-defender/src/component/
Upload File :
Current File : /home/flapst5/ekmtoronto.com/wp-content/plugins/wp-defender/src/component/class-notification.php

<?php
/**
 * Handles notifications for various WP Defender components such as Firewall, Malware, and Audit Logging.
 *
 * @package WP_Defender\Component
 */

namespace WP_Defender\Component;

use WP_Defender\Behavior\WPMUDEV;
use WP_Defender\Model\Setting\Audit_Logging;
use WP_Defender\Model\Setting\Scan as Scan_Settings;
use WP_Defender\Model\Notification\Audit_Report;
use WP_Defender\Model\Notification\Malware_Report;
use WP_Defender\Model\Notification\Tweak_Reminder;
use WP_Defender\Model\Notification\Firewall_Report;
use WP_Defender\Model\Notification\Malware_Notification;
use WP_Defender\Model\Notification\Firewall_Notification;

/**
 * Handles notifications for various WP Defender components such as Firewall, Malware, and Audit Logging.
 * Pro implementation of the Notification component.
 */
class Notification extends Notification_Base {

	/**
	 * Indicates if the current installation is a Pro version.
	 *
	 * @var bool
	 */
	private $is_pro;

	/**
	 * Constructs the Notification component and initializes the WPMUDEV behavior.
	 */
	public function __construct() {
		$this->attach_behavior( WPMUDEV::class, WPMUDEV::class );
		$this->is_pro        = ( new WPMUDEV() )->is_pro();
		$this->scan_settings = wd_di()->get( Scan_Settings::class );
	}

	/**
	 * Get all modules as array of arrays.
	 *
	 * @return array
	 */
	public function get_modules(): array {
		$modules = array(
			Malware_Notification::SLUG  => wd_di()->get( Malware_Notification::class )->export(),
			Firewall_Notification::SLUG => wd_di()->get( Firewall_Notification::class )->export(),
		);
		if ( true === $this->is_pro ) {
			$modules[ Tweak_Reminder::SLUG ]  = wd_di()->get( Tweak_Reminder::class )->export();
			$modules[ Malware_Report::SLUG ]  = wd_di()->get( Malware_Report::class )->export();
			$modules[ Firewall_Report::SLUG ] = wd_di()->get( Firewall_Report::class )->export();
			$modules[ Audit_Report::SLUG ]    = wd_di()->get( Audit_Report::class )->export();
		}

		return $modules;
	}

	/**
	 * Get all modules as array of objects.
	 *
	 * @return array
	 */
	public function get_modules_as_objects(): array {
		$modules = array(
			wd_di()->get( Malware_Notification::class ),
			wd_di()->get( Firewall_Notification::class ),
		);
		if ( true === $this->is_pro ) {
			$modules = array_merge(
				$modules,
				array(
					wd_di()->get( Tweak_Reminder::class ),
					wd_di()->get( Malware_Report::class ),
					wd_di()->get( Firewall_Report::class ),
					wd_di()->get( Audit_Report::class ),
				)
			);
		}

		return $modules;
	}

	/**
	 * Build a slug-to-model index for all notification modules.
	 *
	 * @return array<string, \WP_Defender\Model\Notification>
	 */
	public function get_module_map(): array {
		$map = array();
		foreach ( $this->get_modules_as_objects() as $model ) {
			$map[ $model->slug ] = $model;
		}

		return $map;
	}

	/**
	 * Return the time that next report will be triggered.
	 *
	 * @return string|null
	 */
	public function get_next_run() {
		if ( false === $this->is_pro ) {
			return esc_html__( 'Never', 'wpdef' );
		}
		$modules  = $this->get_active_pro_reports_as_objects();
		$next_run = null;
		foreach ( $modules as $module ) {
			if ( \WP_Defender\Model\Notification::STATUS_ACTIVE !== $module->status ) {
				continue;
			}
			if ( is_null( $next_run ) ) {
				$next_run = $module;
			} elseif ( $module->est_timestamp < $next_run->est_timestamp ) {
				$next_run = $module;
			}
		}
		if ( is_null( $next_run ) ) {
			return esc_html__( 'Never', 'wpdef' );
		}

		return $next_run->get_next_run_as_string();
	}

	/**
	 * Get inactive modules.
	 *
	 * @return array
	 * @since 2.7.0 Malware Scanning - Reporting may be inactive.
	 */
	public function get_inactive_modules(): array {
		if ( false === $this->is_pro ) {
			return array();
		}
		$modules = array();
		if ( ! $this->scan_settings->is_enabled_scheduled_scanning() ) {
			$module                          = wd_di()->get( Malware_Report::class )->export();
			$module['link']                  = network_admin_url( 'admin.php?page=wdf-scan&view=settings&enable=scheduled_scanning#setting_scheduled_scanning' );
			$modules[ Malware_Report::SLUG ] = $module;
		}
		if ( false === wd_di()->get( Audit_Logging::class )->is_active() ) {
			$module                        = wd_di()->get( Audit_Report::class )->export();
			$module['link']                = network_admin_url( 'admin.php?page=wdf-logging&view=logs' );
			$modules[ Audit_Report::SLUG ] = $module;
		}

		return $modules;
	}

	/**
	 * Get the alert model (firewall notification).
	 *
	 * @return array
	 */
	public function get_alert_model(): array {
		return array( Firewall_Notification::SLUG => wd_di()->get( Firewall_Notification::class )->export() );
	}

	/**
	 * Get all report models keyed by slug.
	 *
	 * @return array
	 */
	public function get_reports_model(): array {
		$malware_report             = wd_di()->get( Malware_Report::class )->export();
		$malware_report['ui_title'] = esc_html__( 'Issues', 'wpdef' );

		$tweak_reminder             = wd_di()->get( Tweak_Reminder::class )->export();
		$tweak_reminder['ui_title'] = esc_html__( 'Hardening', 'wpdef' );

		$firewall_report             = wd_di()->get( Firewall_Report::class )->export();
		$firewall_report['ui_title'] = esc_html__( 'Firewall', 'wpdef' );

		$audit_report             = wd_di()->get( Audit_Report::class )->export();
		$audit_report['ui_title'] = esc_html__( 'Audit log', 'wpdef' );

		$reports = array(
			Malware_Report::SLUG  => $malware_report,
			Tweak_Reminder::SLUG  => $tweak_reminder,
			Firewall_Report::SLUG => $firewall_report,
			Audit_Report::SLUG    => $audit_report,
		);

		return $reports;
	}

	/**
	 * Get active modules of Pro reports as array of arrays.
	 *
	 * @return array
	 */
	public function get_active_pro_reports(): array {
		$modules = array(
			Tweak_Reminder::SLUG => wd_di()->get( Tweak_Reminder::class )->export(),
		);
		// Malware_Report.
		if ( $this->scan_settings->is_enabled_scheduled_scanning() ) {
			$modules[ Malware_Report::SLUG ] = wd_di()->get( Malware_Report::class )->export();
		}
		// Firewall_Report.
		$modules[ Firewall_Report::SLUG ] = wd_di()->get( Firewall_Report::class )->export();
		// Audit_Report.
		if ( true === wd_di()->get( Audit_Logging::class )->is_active() ) {
			$modules[ Audit_Report::SLUG ] = wd_di()->get( Audit_Report::class )->export();
		}

		return $modules;
	}

	/**
	 * Get active modules of Pro reports as array of objects.
	 *
	 * @return array
	 */
	public function get_active_pro_reports_as_objects(): array {
		$modules = array(
			wd_di()->get( Tweak_Reminder::class ),
		);
		// Malware_Report.
		if ( $this->scan_settings->is_enabled_scheduled_scanning() ) {
			$modules[] = wd_di()->get( Malware_Report::class );
		}
		// Firewall_Report.
		$modules[] = wd_di()->get( Firewall_Report::class );
		// Audit_Report.
		if ( true === wd_di()->get( Audit_Logging::class )->is_active() ) {
			$modules[] = wd_di()->get( Audit_Report::class );
		}

		return $modules;
	}
	/**
	 * Dispatches reports if conditions are met.
	 */
	public function maybe_dispatch_report() {
		$modules = array();
		if ( true === $this->is_pro ) {
			$modules = $this->get_active_pro_reports_as_objects();
		}

		foreach ( $modules as $module ) {
			if ( $module->maybe_send() ) {
				$module->send();
			}
		}
	}
}