'object', 'properties' => array_merge(DiviSchema::write(), [ 'ids' => [ 'type' => 'array', 'minItems' => 1, 'maxItems' => 20, 'items' => ['type' => 'string'], 'description' => 'Pfad-IDs der zu löschenden Knoten (aus lit-divi-outline).', ], ]), 'required' => ['post_id', 'tree_rev', 'ids'], 'additionalProperties' => false, ]; } public function annotations(): array { return Annotations::destructive(false); } /** Like lit/divi-move: TreeEditor::remove() only, no composition of client values. */ protected function baseFeatures(): array { return [DiviFeature::PageRead, DiviFeature::PageWrite, DiviFeature::Catalog, DiviFeature::DesignRead]; } protected function change(DiviPage $page, array $input, DiviComposition $composition): DiviChange|WP_Error { /** @var array $nodes */ $nodes = []; foreach (is_array($input['ids'] ?? null) ? $input['ids'] : [] as $id) { $node = is_string($id) ? $page->index->find($id) : null; if ($node === null) { return DiviErrors::unknownNode((string) $id, $page->post); } $nodes[$id] = $node; } // A node inside another node that is deleted anyway needs no own removal. $nodes = array_filter( $nodes, static fn (Block $node, string $id): bool => array_filter( array_keys($nodes), static fn (string $other): bool => $other !== $id && str_starts_with($id, $other . '.') ) === [], ARRAY_FILTER_USE_BOTH ); if (($input['confirm'] ?? false) !== true) { return Errors::confirmRequired( sprintf('%d Divi-Knoten würden samt Inhalt entfernt: %s.', count($nodes), $this->describe($nodes)), ['nodes' => array_keys($nodes)] ); } $editor = $composition->editor(); $change = new DiviChange(sprintf('%d Divi-Knoten gelöscht', count($nodes))); foreach ($nodes as $id => $node) { $change->removed((string) $id, $node); $editor->remove($page->index, $node); } return $change; } /** @param array $nodes */ private function describe(array $nodes): string { $parts = []; foreach ($nodes as $id => $node) { $label = NodeSummary::label($node); $parts[] = sprintf( '%s %s%s (%d Kind-Knoten)', $id, $node->shortName(), $label === '' ? '' : ' "' . $label . '"', count($node->children()) ); } return implode(', ', $parts); } }