Files
WP-LIT-MCP/src/Integrations/Divi/Design/PresetRestorePlanner.php
T

75 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\Divi\Design;
use Closure;
use LIT\MCP\Integrations\Divi\Release\DiviFeature;
use WP_Error;
defined('ABSPATH') || exit;
/**
* Restores a snapshot of the presets (Etappe 4a): the snapshot replaces the whole inventory (presets missing in it are
* removed); without unfiltered_html it is refused while the snapshot or the current inventory holds HTML/CSS that
* Divi would strip (final review I-2).
*/
final class PresetRestorePlanner implements RestorePlanner
{
private const MARKUP = 'Der Snapshot oder der aktuelle Bestand enthält Presets mit HTML/CSS, das beim Speichern '
. 'ohne das Recht unfiltered_html verloren ginge; nichts wurde gespeichert.';
/** @param (Closure(): bool)|null $unfiltered Whether the user may save unfiltered HTML (DesignRights). */
public function __construct(private DesignSystemWriter $writer, private ?Closure $unfiltered = null)
{
$this->unfiltered ??= DesignRights::unfilteredHtml();
}
public function area(): string
{
return 'presets';
}
public function designArea(): DesignArea
{
return new PresetArea($this->writer);
}
public function rights(): ?WP_Error
{
return DesignRights::presets($this->writer);
}
/** Writes the presets (PresetWrite), reading them first (DesignRead). */
public function features(): array
{
return [DiviFeature::DesignRead, DiviFeature::PresetWrite];
}
public function plan(array $current, array $snapshot, string $snapshotId): DesignPlan|WP_Error
{
$before = PresetBook::fromDivi($current);
$after = PresetBook::fromDivi($snapshot);
$refusal = DesignRights::markup(
$this->unfiltered,
array_merge($before->markupOutside([]), $after->markupOutside([])),
self::MARKUP
);
if ($refusal !== null) {
return $refusal;
}
$diff = PresetDiff::between($before, $after);
return new DesignPlan(
$after->data(),
$diff,
sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, PresetDiff::summary($diff)),
null,
['Seiten, die Presets verwenden, die es im Snapshot nicht gibt, behalten den Verweis; das Modul fällt '
. 'dann auf das Standard-Preset zurück.'],
true
);
}
}