<?php

/**
 * @package CsvImporterPlusforACF
 */

namespace CIPFA\base;

// make sure we don't expose any info if called directly
if (!defined('ABSPATH')) exit; // Exit if accessed directly

class CIPFA_Deactivate
{
   public static function cipfa_deactivate()
   {
      // Remove custom capability from all roles
        self::cipfa_remove_custom_capability();

        // Clean up options
        $options = [
            'cipfa_total_imported_so_far',
            'cipfa_import_completed',
            'cipfa_enable_log',
            'cipfa_flush_cache',
            'cipfa_replace_categories',
            'cipfa_replace_tags',
            'cipfa_replace_custom_taxonomies',
            'cipfa_duplicate_media_path',
            'cipfa_current_log_path',
            'cipfa_imported_categories',
            'cipfa_imported_tags',
            'cipfa_imported_custom_taxonomies',
            'cipfa_imported_user',
            'cipfa_csv_file_path',
        ];

        foreach ($options as $option) {
            if (get_option($option) !== false) {
                delete_option($option);
            }
        }
    }

   public static function cipfa_remove_custom_capability()
   {
      global $wp_roles;
      if (!isset($wp_roles)) {
         $wp_roles = new \WP_Roles();
      }
      foreach ($wp_roles->roles as $role_name => $role_info) {
         $role = get_role($role_name);
         if ($role && $role->has_cap('cipfa_import_data')) {
            $role->remove_cap('cipfa_import_data');
         }
      }
   }
}
