<?php

declare(strict_types=1);

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

global $wpdb;

// Drop all custom tables (order matters for foreign keys)
$tables = [
	$wpdb->prefix . 'sml_string_translations',
	$wpdb->prefix . 'sml_strings',
	$wpdb->prefix . 'sml_translations',
	$wpdb->prefix . 'sml_languages',
];

foreach ($tables as $table) {
	$wpdb->query("DROP TABLE IF EXISTS `{$table}`");
}

// Delete all plugin options
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
		$wpdb->esc_like('sml_') . '%'
	)
);

// Delete all transients
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
		$wpdb->esc_like('_transient_sml_') . '%',
		$wpdb->esc_like('_transient_timeout_sml_') . '%'
	)
);
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
		$wpdb->esc_like('_site_transient_sml_') . '%',
		$wpdb->esc_like('_site_transient_timeout_sml_') . '%'
	)
);

// Delete post meta
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
		$wpdb->esc_like('_sml_') . '%'
	)
);

// Delete term meta
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE %s",
		$wpdb->esc_like('_sml_') . '%'
	)
);

// Delete user meta
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
		$wpdb->esc_like('sml_') . '%'
	)
);
