<?php
/**
 * DESCRIPTION: Handles plugin deactivation tasks including sending a deactivation
 * event to the telemetry server.
 *
 * @package SheetWise
 * @since   2.0.0
 */

namespace SheetWise;

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

/**
 * Handles plugin deactivation tasks.
 *
 * @since 2.0.0
 */
class Deactivator {

	/**
	 * Send a deactivation payload to the telemetry server.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public static function deactivate() {
		if ( ! function_exists( 'wp_remote_post' ) ) {
			return;
		}

		// Use the same token-based URL as the telemetry manager so requests
		// authenticate via the plugin_token middleware, not the api_key middleware.
		$token      = defined( 'SWISE_TELEMETRY_TOKEN' ) ? SWISE_TELEMETRY_TOKEN : '';
		$server_url = apply_filters( 'swise_telemetry_server_url', 'https://feedio.sapayth.com/api/t/' . $token . '/' );
		$endpoint   = rtrim( $server_url, '/' ) . '/plugin-deactivate';

		$body = [
			'url'         => home_url(),
			'plugin_slug' => 'sheet-wise',
		];

		// Send a non-blocking request to the telemetry server.
		wp_remote_post(
			$endpoint,
			[
				'timeout'   => 2,
				'blocking'  => false,
				'sslverify' => false,
				'body'      => $body,
			]
		);
	}
}
