<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * When populating this file, consider the following flow
 * of control:
 *
 * - This method should be static
 * - Check if the $_REQUEST content actually is the plugin name
 * - Run an admin referrer check to make sure it goes through authentication
 * - Verify the output of $_GET makes sense
 * - Repeat with other user roles. Best directly by using the links/query string parameters.
 * - Repeat things for multisite. Once for a single site in the network, once sitewide.
 *
 * This file may be updated more in future version of the Boilerplate; however, this is the
 * general skeleton and outline for how the file should work.
 *
 * @since      1.0.0
 * @package    Author_Kit
 */

// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}


global $wpdb;

// Drop the coauthor table upon plugin deactivation.
require_once plugin_dir_path( __FILE__ ) . 'author-kit.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class/class-author-kit-coauthors.php';


/**
 * Delete metadata or options by keys or prefix.
 *
 * @param string $table           'usermeta', 'postmeta', 'options', or 'commentmeta'.
 * @param array  $keys            Meta/option keys to delete.
 * @param string $like_prefix     Optional: delete keys starting with this prefix.
 */
function author_kit_delete_metadata( $table, array $keys = array(), $like_prefix = '' ) {
	global $wpdb;

	$table_map = array(
		'usermeta'    => $wpdb->usermeta,
		'postmeta'    => $wpdb->postmeta,
		'options'     => $wpdb->options,
		'commentmeta' => $wpdb->commentmeta,
	);

		// Special handling for transients.
	if ( 'transients' === $table ) {
		if ( ! empty( $like_prefix ) ) {
			$like         = esc_sql( '%_transient_' . $like_prefix . '%' );
			$timeout_like = esc_sql( '%_transient_timeout_' . $like_prefix . '%' );

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

	if ( ! isset( $table_map[ $table ] ) ) {
		return;
	}

	$table_name = $table_map[ $table ];
	$column     = ( 'options' === $table ) ? 'option_name' : 'meta_key';

	// Delete specific keys.
	if ( ! empty( $keys ) ) {
		foreach ( $keys as $key ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->query( $wpdb->prepare( 'DELETE FROM `' . esc_sql( $table_name ) . '` WHERE `' . esc_sql( $column ) . '` = %s', $key ) );
		}
	}

	// Delete by prefix.
	if ( ! empty( $like_prefix ) ) {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$wpdb->query(
			$wpdb->prepare( 'DELETE FROM `' . esc_sql( $table_name ) . '` WHERE `' . esc_sql( $column ) . '` LIKE %s', $like_prefix . '%' )
		);
	}
}

if ( get_option( 'author_kit_delete_plugin_data', false ) ) {
	// Drop co-author table.
	$author_kit_coauthors = new Author_Kit_Coauthors();
	$author_kit_coauthors->drop_coauthor_table();

	// Just delete everything starting with author_kit_.
	author_kit_delete_metadata( 'usermeta', array(), 'author_kit_' );
	author_kit_delete_metadata( 'postmeta', array(), 'author_kit_' );
	author_kit_delete_metadata( 'options', array(), 'author_kit_' );

	// Delete all transients containing "_author_kit_".
	author_kit_delete_metadata( 'transients', array(), 'author_kit_' );
}
