Every file under src/ exits outside WordPress (defined('ABSPATH') ||
exit; after the use block, so a direct request no longer prints a fatal
error with the server path); divi-convert-fixture.php gets the WP_CLI
guard the other integration scripts have. A unit test keeps both true;
PSR1 side-effects warning excluded for the guard.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
103 lines
2.8 KiB
PHP
103 lines
2.8 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace LIT\MCP\Integrations\Divi\Pages;
|
||
|
||
use LIT\MCP\Integrations\Divi\BlockTree\Block;
|
||
use LIT\MCP\Integrations\Divi\BlockTree\NodeIndex;
|
||
use LIT\MCP\Integrations\Divi\BlockTree\NodeSummary;
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
/**
|
||
* What a Divi write did to the tree: summary, changed nodes (for ToolResult::changed – IDs of
|
||
* surviving nodes are resolved after the change), touched nodes (for the quality warnings)
|
||
* and warnings of its own.
|
||
*/
|
||
final class DiviChange
|
||
{
|
||
/** @var list<array{block: ?Block, id: ?string, type: string, action: string, label: string}> */
|
||
private array $entries = [];
|
||
|
||
/** @var list<Block> */
|
||
private array $touched = [];
|
||
|
||
/** @var list<string> */
|
||
private array $warnings = [];
|
||
|
||
public function __construct(public readonly string $summary)
|
||
{
|
||
}
|
||
|
||
/**
|
||
* A node that still exists after the change: "inserted", "updated", "moved" or "duplicated".
|
||
* Every action but "moved" counts for the quality warnings.
|
||
*/
|
||
public function record(Block $block, string $action): self
|
||
{
|
||
$this->entries[] = [
|
||
'block' => $block,
|
||
'id' => null,
|
||
'type' => $block->name(),
|
||
'action' => $action,
|
||
'label' => NodeSummary::label($block),
|
||
];
|
||
if ($action !== 'moved') {
|
||
$this->touched[] = $block;
|
||
}
|
||
|
||
return $this;
|
||
}
|
||
|
||
/** A node that is gone; $idBefore is its ID before the change. */
|
||
public function removed(string $idBefore, Block $block): self
|
||
{
|
||
$this->entries[] = [
|
||
'block' => null,
|
||
'id' => $idBefore,
|
||
'type' => $block->name(),
|
||
'action' => 'deleted',
|
||
'label' => NodeSummary::label($block),
|
||
];
|
||
|
||
return $this;
|
||
}
|
||
|
||
public function warning(string $warning): self
|
||
{
|
||
$this->warnings[] = $warning;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/** @return list<array{block: ?Block, id: ?string, type: string, action: string, label: string}> */
|
||
public function entries(): array
|
||
{
|
||
return $this->entries;
|
||
}
|
||
|
||
/**
|
||
* Path ID of an entry in the tree after the change: a removed node keeps its ID from before
|
||
* the removal; a surviving node is looked up in $index (null if it is not there).
|
||
*
|
||
* @param array{block: ?Block, id: ?string, type: string, action: string, label: string} $entry
|
||
*/
|
||
public static function idOf(array $entry, NodeIndex $index): ?string
|
||
{
|
||
return $entry['id'] ?? ($entry['block'] !== null ? $index->idOf($entry['block']) : null);
|
||
}
|
||
|
||
/** @return list<Block> */
|
||
public function touched(): array
|
||
{
|
||
return $this->touched;
|
||
}
|
||
|
||
/** @return list<string> */
|
||
public function warnings(): array
|
||
{
|
||
return $this->warnings;
|
||
}
|
||
}
|