<?php
/**
 * Deactivation Survey AJAX handler.
 *
 * @package automation-web-platform
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


add_action(
	'wp_ajax_wawp_save_deactivation_reason',
	function () {
		check_ajax_referer( 'wawp_deact_survey' );

		$reason = isset( $_POST['reason'] ) ? sanitize_text_field( wp_unslash( $_POST['reason'] ) ) : '';
		$other  = isset( $_POST['other_text'] ) ? sanitize_text_field( wp_unslash( $_POST['other_text'] ) ) : '';

		if ( 'other' === $reason && '' !== $other ) {
			$reason = 'Other: ' . $other;
		}
		update_option(
			'wawp_last_deactivation_reason',
			array(
				'reason' => ! empty( $reason ) ? $reason : '',
				'ts'     => current_time( 'mysql' ),
			),
			false
		);

		wp_send_json_success();
	}
);
