<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * @package RawTypeOE
 */

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

/**
 * Recursively delete a directory.
 *
 * @param string $directory Absolute directory path.
 * @return void
 */
function rawtype_oe_delete_directory( $directory ) {
	if ( ! is_dir( $directory ) ) {
		return;
	}

	$items = scandir( $directory );

	if ( ! is_array( $items ) ) {
		return;
	}

	foreach ( $items as $item ) {
		if ( '.' === $item || '..' === $item ) {
			continue;
		}

		$path = $directory . DIRECTORY_SEPARATOR . $item;

		if ( is_dir( $path ) ) {
			rawtype_oe_delete_directory( $path );
			continue;
		}

		if ( function_exists( 'wp_delete_file' ) ) {
			wp_delete_file( $path );
		} else {
			unlink( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
		}
	}

	rmdir( $directory ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir
}

// Delete plugin options by prefix.
global $wpdb;

if ( isset( $wpdb ) && method_exists( $wpdb, 'prepare' ) && method_exists( $wpdb, 'query' ) ) {
	$rawtype_oe_option_like = $wpdb->esc_like( 'rawtype_oe_' ) . '%';

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

	// Delete plugin transients. They live in the options table as
	// _transient_rawtype_oe_* / _transient_timeout_rawtype_oe_* and are NOT
	// matched by the rawtype_oe_ LIKE above (they are prefixed with _transient_).
	$rawtype_oe_transient_like         = $wpdb->esc_like( '_transient_rawtype_oe_' ) . '%';
	$rawtype_oe_transient_timeout_like = $wpdb->esc_like( '_transient_timeout_rawtype_oe_' ) . '%';

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

// Delete known options explicitly as a safety net.
delete_option( 'rawtype_oe_settings' );
delete_option( 'rawtype_oe_version' );
delete_option( 'rawtype_oe_activation_error' );

// Delete generated custom post type records.
if ( function_exists( 'get_posts' ) && function_exists( 'wp_delete_post' ) ) {
	$rawtype_oe_post_ids = get_posts(
		array(
			'post_type'      => array( 'rawtype_oe_profile', 'rawtype_oe_export' ),
			'post_status'    => 'any',
			'posts_per_page' => -1,
			'fields'         => 'ids',
		)
	);

	if ( is_array( $rawtype_oe_post_ids ) ) {
		foreach ( $rawtype_oe_post_ids as $rawtype_oe_post_id ) {
			wp_delete_post( absint( $rawtype_oe_post_id ), true );
		}
	}
}

// Clear scheduled jobs.
if ( function_exists( 'wp_clear_scheduled_hook' ) ) {
	wp_clear_scheduled_hook( 'rawtype_oe_cleanup_files' );
}

// Remove generated export files.
if ( function_exists( 'wp_get_upload_dir' ) ) {
	$rawtype_oe_upload_dir = wp_get_upload_dir();

	if ( isset( $rawtype_oe_upload_dir['basedir'] ) ) {
		$rawtype_oe_exports_dir = trailingslashit( $rawtype_oe_upload_dir['basedir'] ) . 'rawtype-order-exporter-woocommerce';
		rawtype_oe_delete_directory( $rawtype_oe_exports_dir );
	}
}
