<?php
/**
 * Uninstall cleanup for Papy3D Fact Viewer for Chuck365.
 *
 * @package Papy3DFactViewerForChuck365
 */

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

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

/**
 * Delete only data owned by this plugin on the current site.
 *
 * @return void
 */
function papyfavi_uninstall_cleanup_site(): void {
	foreach (
		array(
			'papyfavi_border_color',
			'papyfavi_bg_color',
			'papyfavi_text_color',
			'papyfavi_text_title',
			'papyfavi_show_copyright',
			'papyfavi_card_style',
			'papyfavi_api_consent',
		) as $papyfavi_option
	) {
		delete_option( $papyfavi_option );
	}

	delete_transient( 'papyfavi_fact' );
	delete_transient( 'papyfavi_fact_date' );
}

if ( ! is_multisite() || ! function_exists( 'get_sites' ) ) {
	papyfavi_uninstall_cleanup_site();
	return;
}

$papyfavi_offset     = 0;
$papyfavi_batch_size = 100;

do {
	$papyfavi_site_ids = get_sites(
		array(
			'fields' => 'ids',
			'number' => $papyfavi_batch_size,
			'offset' => $papyfavi_offset,
		)
	);

	foreach ( $papyfavi_site_ids as $papyfavi_site_id ) {
		switch_to_blog( (int) $papyfavi_site_id );

		try {
			papyfavi_uninstall_cleanup_site();
		} finally {
			restore_current_blog();
		}
	}

	$papyfavi_count   = count( $papyfavi_site_ids );
	$papyfavi_offset += $papyfavi_count;
} while ( $papyfavi_batch_size === $papyfavi_count );
