<?php
/**
 * Installation Doctor plugin-warning acknowledgement lifecycle.
 */

if (!defined('WPINC')) {
  die;
}

if (!function_exists('custausu_admin_doctor_plugin_warning_ack_option_name')) {
  function custausu_admin_doctor_plugin_warning_ack_option_name(): string {
    return 'custausu_doctor_plugin_warning_acknowledgements';
  }
}

if (!function_exists('custausu_admin_doctor_plugin_warning_normalize_plugin_file')) {
  function custausu_admin_doctor_plugin_warning_normalize_plugin_file(string $plugin_file): string {
    $plugin_file = wp_normalize_path(trim($plugin_file));
    $plugin_file = ltrim($plugin_file, '/');

    return strtolower($plugin_file);
  }
}

if (!function_exists('custausu_admin_doctor_plugin_warning_acknowledgements')) {
  function custausu_admin_doctor_plugin_warning_acknowledgements(): array {
    $rows = get_option(
      custausu_admin_doctor_plugin_warning_ack_option_name(),
      []
    );
    $rows = is_array($rows) ? $rows : [];
    $clean = [];

    foreach ($rows as $key => $row) {
      $key = strtolower(trim((string)$key));

      if (
        !preg_match('/^[a-f0-9]{64}$/', $key)
        || !is_array($row)
      ) {
        continue;
      }

      $fingerprint = strtolower(
        trim((string)($row['fingerprint'] ?? ''))
      );
      $plugin_file =
        custausu_admin_doctor_plugin_warning_normalize_plugin_file(
          (string)($row['plugin_file'] ?? '')
        );
      $acknowledged_at = trim(
        (string)($row['acknowledged_at'] ?? '')
      );

      if (
        !preg_match('/^[a-f0-9]{64}$/', $fingerprint)
        || $plugin_file === ''
      ) {
        continue;
      }

      $clean[$key] = [
        'fingerprint' => $fingerprint,
        'plugin_file' => $plugin_file,
        'acknowledged_at' => $acknowledged_at,
      ];

      if (count($clean) >= 100) {
        break;
      }
    }

    return $clean;
  }
}

if (!function_exists('custausu_admin_doctor_plugin_warning_invalidate_plugin')) {
  function custausu_admin_doctor_plugin_warning_invalidate_plugin(string $plugin_file): void {
    $plugin_file =
      custausu_admin_doctor_plugin_warning_normalize_plugin_file(
        $plugin_file
      );

    if ($plugin_file === '') {
      return;
    }

    $rows =
      custausu_admin_doctor_plugin_warning_acknowledgements();
    $changed = false;

    foreach ($rows as $key => $row) {
      $stored_plugin_file =
        custausu_admin_doctor_plugin_warning_normalize_plugin_file(
          (string)($row['plugin_file'] ?? '')
        );

      if ($stored_plugin_file !== $plugin_file) {
        continue;
      }

      unset($rows[$key]);
      $changed = true;
    }

    if (!$changed) {
      return;
    }

    if (empty($rows)) {
      delete_option(
        custausu_admin_doctor_plugin_warning_ack_option_name()
      );
      return;
    }

    update_option(
      custausu_admin_doctor_plugin_warning_ack_option_name(),
      $rows,
      false
    );
  }
}

add_action(
  'activated_plugin',
  'custausu_admin_doctor_plugin_warning_invalidate_plugin',
  10,
  1
);

add_action(
  'deactivated_plugin',
  'custausu_admin_doctor_plugin_warning_invalidate_plugin',
  10,
  1
);

add_action(
  'deleted_plugin',
  'custausu_admin_doctor_plugin_warning_invalidate_plugin',
  10,
  1
);
