<?php

use FernleafSystems\Wordpress\Plugin\Shield\Modules;
use FernleafSystems\Wordpress\Plugin\Shield\Modules\CommentsFilter;
use FernleafSystems\Wordpress\Services\Services;

/**
 * @deprecated 10.1
 */
class ICWP_WPSF_Processor_CommentsFilter extends Modules\BaseShield\ShieldProcessor {

	public function run() {
	}

	public function onWpInit() {
		/** @var CommentsFilter\ModCon $mod */
		$mod = $this->getMod();
		/** @var CommentsFilter\Options $opts */
		$opts = $this->getOptions();
		$oWpUsers = Services::WpUsers();

		$bLoadComProc = !$oWpUsers->isUserLoggedIn() ||
						!( new CommentsFilter\Scan\IsEmailTrusted() )->trusted(
							$oWpUsers->getCurrentWpUser()->user_email,
							$opts->getApprovedMinimum(),
							$opts->getTrustedRoles()
						);

		if ( $bLoadComProc ) {

			if ( $opts->isEnabledCaptcha() && $mod->getCaptchaCfg()->ready ) {
				$this->getSubPro( 'recaptcha' )->execute();
			}

			if ( Services::Request()->isPost() ) {
				add_filter( 'comment_notification_recipients', [ $this, 'clearCommentNotificationEmail' ], 100, 1 );
			}
			elseif ( $opts->isEnabledGaspCheck() ) {
				$this->getSubPro( 'bot' )->execute();
			}
		}
	}

	public function runHourlyCron() {
		/** @var CommentsFilter\Options $opts */
		$opts = $this->getOptions();
		if ( $opts->isEnabledGaspCheck() && function_exists( 'delete_expired_transients' ) ) {
			delete_expired_transients(); // cleanup unused comment tokens
		}
	}

	/**
	 * @return array
	 */
	protected function getSubProMap() :array {
		return [
			'bot'       => 'ICWP_WPSF_Processor_CommentsFilter_BotSpam',
			'recaptcha' => 'ICWP_WPSF_Processor_CommentsFilter_GoogleRecaptcha',
		];
	}

	/**
	 * When you set a new comment as anything but 'spam' a notification email is sent to the post author.
	 * We suppress this for when we mark as trash by emptying the email notifications list.
	 * @param array $aEmails
	 * @return array
	 */
	public function clearCommentNotificationEmail( $aEmails ) {
		$sStatus = apply_filters( $this->getCon()->prefix( 'cf_status' ), '' );
		if ( in_array( $sStatus, [ 'reject', 'trash' ] ) ) {
			$aEmails = [];
		}
		return $aEmails;
	}
}