Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
100 lines
3.1 KiB
PHP
100 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Integrations\Divi\Release;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Divi versions released on this site after a green admin check (spec §5.3, source "database"). An entry holds for
|
|
* the plugin version that ran the check only.
|
|
*
|
|
* Not final so the resolver can be tested with a mock.
|
|
*/
|
|
class ReleaseStore
|
|
{
|
|
public const OPTION = 'lit_mcp_divi_releases';
|
|
|
|
/**
|
|
* @return list<array{divi_version: string, plugin_version: string, user_id: int, released_at: string,
|
|
* report_id: string}>
|
|
*/
|
|
public function entries(): array
|
|
{
|
|
$raw = get_option(self::OPTION, []);
|
|
$entries = [];
|
|
foreach (is_array($raw) ? $raw : [] as $row) {
|
|
if (
|
|
is_array($row)
|
|
&& is_string($row['divi_version'] ?? null)
|
|
&& is_string($row['plugin_version'] ?? null)
|
|
&& is_int($row['user_id'] ?? null)
|
|
&& is_string($row['released_at'] ?? null)
|
|
&& is_string($row['report_id'] ?? null)
|
|
) {
|
|
$entries[] = [
|
|
'divi_version' => $row['divi_version'],
|
|
'plugin_version' => $row['plugin_version'],
|
|
'user_id' => $row['user_id'],
|
|
'released_at' => $row['released_at'],
|
|
'report_id' => $row['report_id'],
|
|
];
|
|
}
|
|
}
|
|
|
|
return $entries;
|
|
}
|
|
|
|
/**
|
|
* @return array{divi_version: string, plugin_version: string, user_id: int, released_at: string,
|
|
* report_id: string}|null
|
|
*/
|
|
public function find(string $diviVersion, string $pluginVersion): ?array
|
|
{
|
|
foreach ($this->entries() as $entry) {
|
|
if ($entry['divi_version'] === $diviVersion && $entry['plugin_version'] === $pluginVersion) {
|
|
return $entry;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function add(string $diviVersion, string $pluginVersion, int $userId, string $reportId): void
|
|
{
|
|
$entries = $this->without($diviVersion, $pluginVersion);
|
|
$entries[] = [
|
|
'divi_version' => $diviVersion,
|
|
'plugin_version' => $pluginVersion,
|
|
'user_id' => $userId,
|
|
'released_at' => gmdate('c'),
|
|
'report_id' => $reportId,
|
|
];
|
|
update_option(self::OPTION, $entries, false);
|
|
}
|
|
|
|
public function withdraw(string $diviVersion, string $pluginVersion): bool
|
|
{
|
|
if ($this->find($diviVersion, $pluginVersion) === null) {
|
|
return false;
|
|
}
|
|
update_option(self::OPTION, $this->without($diviVersion, $pluginVersion), false);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return list<array{divi_version: string, plugin_version: string, user_id: int, released_at: string,
|
|
* report_id: string}>
|
|
*/
|
|
private function without(string $diviVersion, string $pluginVersion): array
|
|
{
|
|
return array_values(array_filter(
|
|
$this->entries(),
|
|
static fn (array $entry): bool => $entry['divi_version'] !== $diviVersion
|
|
|| $entry['plugin_version'] !== $pluginVersion
|
|
));
|
|
}
|
|
}
|