<?php
/**
 * Fired during plugin deactivation.
 *
 * @package RawTypeOE
 */

namespace RawTypeOE\Free;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles plugin deactivation tasks.
 */
class Deactivator {

	/**
	 * Run deactivation tasks.
	 *
	 * @return void
	 */
	public static function deactivate() {
		self::clear_transients();
		File_Manager::unschedule_cleanup();
	}

	/**
	 * Remove plugin transients.
	 *
	 * @return void
	 */
	private static function clear_transients() {
		delete_transient( 'rawtype_oe_settings_cache' );
		delete_transient( 'rawtype_oe_columns_orders' );
		delete_transient( 'rawtype_oe_columns_products' );
		delete_transient( 'rawtype_oe_columns_customers' );
		delete_transient( 'rawtype_oe_custom_fields_orders' );
		delete_transient( 'rawtype_oe_custom_fields_products' );
		delete_transient( 'rawtype_oe_custom_fields_customers' );

		global $wpdb;

		if ( isset( $wpdb ) && method_exists( $wpdb, 'prepare' ) && method_exists( $wpdb, 'query' ) ) {
			$like_transient = $wpdb->esc_like( '_transient_rawtype_oe_' ) . '%';
			$like_timeout   = $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",
					$like_transient,
					$like_timeout
				)
			);
		}
	}
}
