refactor: ein Helfer für Gruppen-Kurznamen (NodeSummary::groupShortName), ESCAPED_LETTER_REGEX am UsageQuery-Interface, Docblock DiviComposition::presets; Tests für Muster, Kurzname und Gruppen-Überdeckung

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan-Niclas Loosen
2026-10-18 17:40:21 +02:00
co-authored by Claude Sonnet 5.5
parent 6d7c59e3fa
commit e7793fd595
13 changed files with 70 additions and 16 deletions
@@ -23,6 +23,12 @@ final class NodeSummary
/** The admin label is a one-line title in the layers panel, never an unbounded copy. */
private const LABEL_LIMIT = 80;
/** "font-body" for the group name "divi/font-body" – the key "group_presets" of the write tools takes. */
public static function groupShortName(string $groupName): string
{
return str_starts_with($groupName, 'divi/') ? substr($groupName, 5) : $groupName;
}
public static function label(Block $block): string
{
$attrs = $block->name() === 'divi/global-layout' && is_array($block->attrs()['localAttrs'] ?? null)
+1 -2
View File
@@ -174,8 +174,7 @@ final class Outline
{
$lines = [];
foreach (NodeSummary::groupPresets($block) as $group) {
$name = $group['groupName'];
$short = str_starts_with($name, 'divi/') ? substr($name, 5) : $name;
$short = NodeSummary::groupShortName($group['groupName']);
$lines[] = $short . ': ' . implode(', ', $this->namesOf($group['presetIds']));
}
@@ -23,8 +23,6 @@ final class ModuleUsageQuery implements UsageQuery
{
private const NAME = '/^([a-z][a-z0-9_-]*)\/([a-z][a-z0-9_-]*)$/D';
private const ESCAPED_LETTER = '\\\\u00[4-7][0-9a-fA-F]';
/** @throws InvalidArgumentException When $module is no full block name such as "divi/button". */
public function __construct(private string $module)
{
@@ -39,7 +37,7 @@ final class ModuleUsageQuery implements UsageQuery
'(post_content LIKE %s OR post_content REGEXP %s OR post_content REGEXP %s)',
'%' . $db->esc_like($this->module) . '%',
$this->escapedSlash(),
self::ESCAPED_LETTER
self::ESCAPED_LETTER_REGEX
);
}
@@ -6,6 +6,7 @@ namespace LIT\MCP\Integrations\Divi\Design;
use Closure;
use InvalidArgumentException;
use LIT\MCP\Integrations\Divi\BlockTree\NodeSummary;
defined('ABSPATH') || exit;
@@ -280,7 +281,7 @@ final class PresetAssignment
private static function shortNames(array $groups): string
{
$names = array_unique(array_map(
static fn (string $name): string => str_starts_with($name, 'divi/') ? substr($name, 5) : $name,
NodeSummary::groupShortName(...),
array_values($groups)
));
@@ -18,8 +18,6 @@ defined('ABSPATH') || exit;
*/
final class PresetUsageQuery implements UsageQuery
{
private const ESCAPED_LETTER = '\\\\u00[4-7][0-9a-fA-F]';
/** @var list<array{scope: PresetScope, id: string, default: bool}> */
private array $targets;
@@ -61,7 +59,7 @@ final class PresetUsageQuery implements UsageQuery
return (string) $db->prepare(
'(' . str_repeat('post_content LIKE %s OR ', count($likes)) . 'post_content REGEXP %s)',
...array_merge($likes, [self::ESCAPED_LETTER])
...array_merge($likes, [self::ESCAPED_LETTER_REGEX])
);
}
@@ -23,7 +23,7 @@ final class LibraryUsageQuery implements UsageQuery
'(LOWER(post_content) LIKE %s OR LOWER(post_content) LIKE %s OR post_content REGEXP %s)',
'%' . $db->esc_like('globalmodule') . '%',
'%' . $db->esc_like('global_module') . '%',
str_repeat(chr(92), 2) . 'u00[4-7][0-9a-fA-F]'
self::ESCAPED_LETTER_REGEX
);
}
@@ -13,6 +13,12 @@ defined('ABSPATH') || exit;
*/
interface UsageQuery
{
/**
* MySQL REGEXP for a JSON-escaped letter ("\\u004x" … "\\u007x"): a name or ID written that way is not found by
* LIKE, so candidate conditions keep such posts (fail closed, ruling P23).
*/
public const ESCAPED_LETTER_REGEX = '\\\\u00[4-7][0-9a-fA-F]';
/** A condition for "WHERE (…)", already passed through $db->prepare(). */
public function candidateCondition(\wpdb $db): string;
@@ -115,9 +115,8 @@ class DiviComposition
/**
* The one PresetAssignment of this composition (D7, rulings R2, R3): the book comes lazily from Divi's stored
* presets, ids must pass the choke point unchanged.
*
* @internal Public for tests; code uses composer() / warnings().
* presets, ids must pass the choke point unchanged. Public: DiviIntegration::presetAssignment() hands it to
* the site-wide tools (lit/divi-presets-assign), which need the same instance as the write tools.
*/
public function presets(): PresetAssignment
{
@@ -202,10 +202,9 @@ final class DiviNodeGetAbility implements Ability
{
$groups = [];
foreach (NodeSummary::groupPresets($block) as $groupId => $group) {
$name = $group['groupName'];
$groups[] = [
'group_id' => $groupId,
'group' => str_starts_with($name, 'divi/') ? substr($name, 5) : $name,
'group' => NodeSummary::groupShortName($group['groupName']),
'presets' => array_map(
static fn (string $presetId): array => ['id' => $presetId, 'name' => $names[$presetId] ?? ''],
$group['presetIds']
@@ -38,7 +38,8 @@ final class DiviUpdateAbility extends DiviWriteAbility
return 'Ändert vorhandene Divi-Knoten: changes = Liste von {id, set: {Kurzform-Felder}, attrs: {Attributpfad: '
. '{Breakpoint: Wert}}, preset, group_presets, clear_overrides, label}. null entfernt einen Wert. '
. 'preset = Name/ID oder Liste (Stapel, bis ' . PresetAssignment::MAX_STACK . '; bei gleicher Priorität '
. 'gewinnt das letzte Preset im Stapel); group_presets = {Gruppe: Preset}, z. B. {"font-body": "Fließtext"}. Überdecken lokale Werte '
. 'gewinnt das letzte Preset im Stapel); group_presets = {Gruppe: Preset}, z. B. '
. '{"font-body": "Fließtext"}. Überdecken lokale Werte '
. 'ein Preset, meldet die Antwort das; clear_overrides: true entfernt sie (nach set/attrs). Nicht '
. 'genannte Einstellungen bleiben unverändert. Jeder Knoten nur einmal pro Aufruf; denselben Pfad nicht '
. 'zugleich über set und attrs. tree_rev aus lit-divi-outline ist Pflicht; alles oder nichts.';