feat: ColorRestorePlanner und VariableRestorePlanner – Restore-Pläne für Farben und Variablen (V1b, V10)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan-Niclas Loosen
2026-10-19 14:22:23 +02:00
co-authored by Claude Sonnet 5.5
parent 5af188d656
commit bd2043564b
4 changed files with 322 additions and 0 deletions
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\Divi\Design;
use LIT\MCP\Integrations\Divi\Release\DiviFeature;
use WP_Error;
defined('ABSPATH') || exit;
/**
* Restores a snapshot of the global colors (ruling V10): the snapshot replaces every stored color
* (set_global_colors($data, false), ruling V4) – colors created after the snapshot are removed. ColorDiff knows no
* removal, so the planner adds those rows ("removed") itself and warns that pages keep their references.
*/
final class ColorRestorePlanner implements RestorePlanner
{
private const REMOVED = 'Farben, die es im Snapshot nicht gibt, entfallen (%s); Seiten und Presets, die sie '
. 'verwenden, behalten den Verweis ohne Wert.';
public function __construct(private DesignSystemWriter $writer, private GlobalColorsAdapter $colors)
{
}
public function area(): string
{
return 'colors';
}
public function designArea(): DesignArea
{
return new ColorArea($this->colors);
}
public function rights(): ?WP_Error
{
return DesignRights::colors($this->writer);
}
/** Writes the colors (ColorWrite), reading the design system first (DesignRead). */
public function features(): array
{
return [DiviFeature::DesignRead, DiviFeature::ColorWrite];
}
public function plan(array $current, array $snapshot, string $snapshotId): DesignPlan|WP_Error
{
$before = ColorBook::fromDivi($current);
$after = ColorBook::fromDivi($snapshot);
$kept = array_values(array_filter(
ColorDiff::between($before, $after),
static fn (array $row): bool => $after->entry($row['id']) !== null
));
$removed = [];
foreach (array_keys(array_diff_key($before->data(), $after->data())) as $id) {
$removed[] = ['id' => (string) $id, 'name' => $before->label((string) $id), 'action' => 'removed'];
}
$summary = sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, ColorDiff::summary($kept));
$warnings = [];
if ($removed !== []) {
$summary .= sprintf(' Entfernt: %d.', count($removed));
$warnings[] = sprintf(
self::REMOVED,
implode(', ', array_map(static fn (array $row): string => '„' . $row['name'] . '“', $removed))
);
}
return new DesignPlan($after->data(), array_merge($kept, $removed), $summary, null, $warnings, true);
}
}
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\Divi\Design;
use LIT\MCP\Integrations\Divi\Release\DiviFeature;
use WP_Error;
defined('ABSPATH') || exit;
/**
* Restores a snapshot of the global variables (rulings V1b, V10): every variable of the snapshot comes back as it was;
* Divi refuses to drop a stored variable, so variables created after the snapshot stay – archived
* (VariableBook::restoredFrom()) – and the dry run names them as a warning. Nothing is removed.
*/
final class VariableRestorePlanner implements RestorePlanner
{
private const KEPT = 'Variable „%s“ gibt es im Snapshot nicht; sie bleibt archiviert erhalten (Divi löscht keine '
. 'Variablen).';
public function __construct(private DesignSystemWriter $writer, private GlobalVariablesAdapter $variables)
{
}
public function area(): string
{
return 'variables';
}
public function designArea(): DesignArea
{
return new VariableArea($this->variables);
}
public function rights(): ?WP_Error
{
return DesignRights::variables($this->writer, $this->variables);
}
/** Writes the variables (VariableWrite), reading the design system first (DesignRead). */
public function features(): array
{
return [DiviFeature::DesignRead, DiviFeature::VariableWrite];
}
public function plan(array $current, array $snapshot, string $snapshotId): DesignPlan|WP_Error
{
$before = VariableBook::fromDivi($current);
$older = VariableBook::fromDivi($snapshot);
$after = $before->restoredFrom($older);
$diff = VariableDiff::between($before, $after);
return new DesignPlan(
$after->data(),
$diff,
sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, VariableDiff::summary($diff)),
null,
array_map(static fn (array $row): string => sprintf(self::KEPT, $row['label']), $before->onlyHere($older))
);
}
}