<?php
/**
 * Uninstall handler for Firevert plugin.
 *
 * Deletes all plugin data from the database.
 *
 * @package Firevert
 */

declare( strict_types=1 );

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

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

delete_option( 'logger_enabled' );

global $wpdb;

$post_types = array(
	'firevert_setting',
	'fv_power_widget',
	'fv_spectator_views',
	'fv_promo_time_widget',
);

foreach ( $post_types as $p_type ) {
	$post_ids = get_posts(
		array(
			'post_type'      => $p_type,
			'post_status'    => 'any',
			'posts_per_page' => - 1,
			'fields'         => 'ids',
		)
	);

	foreach ( $post_ids as $p_id ) {
		wp_delete_post( $p_id, true );
	}
}

delete_option( 'firevert_settings' );
if ( is_multisite() ) {
    delete_site_option( 'firevert_settings' );
}

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%_transient_%firevert%'" );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'firevert_cache%'" );

