<?php

namespace Waypoint\Agent\Setup;

use Waypoint\Agent\Base\Thing;

class Disable_Admin_Email_Check extends Thing {

	public function attach_hooks(): void {
		add_filter( 'admin_email_check_interval', '__return_false' );

		// Apply admin email changes immediately without email verification.
		// WordPress normally stores the pending address in new_admin_email and
		// waits for a confirmation click before updating admin_email.
		add_action( 'add_option_new_admin_email',    [ $this, 'apply_pending_admin_email' ], 10, 2 );
		add_action( 'update_option_new_admin_email', [ $this, 'apply_pending_admin_email' ], 10, 2 );
	}

	public function apply_pending_admin_email( mixed $old, mixed $new ): void {
		$email = is_string( $new ) ? $new : ( is_string( $old ) ? $old : '' );
		if ( '' === $email || ! is_email( $email ) ) {
			return;
		}
		update_option( 'admin_email', $email );
		delete_option( 'new_admin_email' );
	}
}
