<?php
/**
 * Dennis SEO Uninstall
 *
 * Removes all plugin data when the plugin is deleted,
 * unless the user opted to keep data.
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

$dennis_geo_opts = get_option( 'dennis_seo_settings', array() );

if ( ! empty( $dennis_geo_opts['keep_data_on_uninstall'] ) ) {
	// User chose to keep data. Only clean up transients and rewrite rules.
	delete_transient( 'dennis_seo_sitemap_index' );
	delete_transient( 'dennis_seo_sitemap_post' );
	delete_transient( 'dennis_seo_sitemap_page' );
	delete_transient( 'dennis_seo_llms_txt' );
	delete_transient( 'dennis_seo_llms_full_txt' );
	flush_rewrite_rules();
	return;
}

// Remove global settings.
delete_option( 'dennis_seo_settings' );
delete_option( 'dennis_seo_last_home_url' );
delete_option( 'dennis_seo_db_version' );

// Remove all post meta.
global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", '_dennis_seo_%' ) );

// Remove transients.
delete_transient( 'dennis_seo_sitemap_index' );
delete_transient( 'dennis_seo_sitemap_post' );
delete_transient( 'dennis_seo_sitemap_page' );
delete_transient( 'dennis_seo_llms_txt' );
delete_transient( 'dennis_seo_llms_full_txt' );

// Flush rewrite rules.
flush_rewrite_rules();
