feat: content-create and content-update with schema validation
All-or-nothing writes: validation (fields, terms, featured image, parent, template) before any write; failed extras delete the new draft (create) or restore revision, old columns, fields, terms and featured image (update). content-update requires rev; Rev::ofPost covers content, title, excerpt, status and post_modified_gmt. MetaFieldSource::write() now throws on failure. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Integrations\WordPress\Content;
|
||||
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Abilities\ToolResult;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\PostGuard;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* lit/content-create: new entry, always a draft. All or nothing (spec §8.2): input is
|
||||
* validated before the insert; if fields, terms or the featured image cannot be written
|
||||
* afterwards, the brand-new draft is deleted again and the error is rethrown (the
|
||||
* AbilityRegistrar logs it and reports lit_internal_error).
|
||||
*/
|
||||
final class ContentCreateAbility implements Ability
|
||||
{
|
||||
public function __construct(
|
||||
private ContentSchema $schema,
|
||||
private PostWriter $writer,
|
||||
private ChangeLog $log
|
||||
) {
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'lit/content-create';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Inhalt anlegen';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Legt einen neuen Eintrag an – immer als Entwurf (veröffentlichen nur über lit-post-publish). '
|
||||
. 'Kerndaten, Begriffe, Felder (gegen das Schema aus lit-content-types geprüft) und Beitragsbild '
|
||||
. 'in einem Schritt; bei einem Fehler wird nichts angelegt.';
|
||||
}
|
||||
|
||||
public function inputSchema(): array
|
||||
{
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => array_merge([
|
||||
'post_type' => ['type' => 'string'],
|
||||
'title' => ['type' => 'string', 'minLength' => 1],
|
||||
], PostWriter::inputProperties()),
|
||||
'required' => ['post_type', 'title'],
|
||||
'additionalProperties' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function outputSchema(): ?array
|
||||
{
|
||||
return ToolResult::schema();
|
||||
}
|
||||
|
||||
public function annotations(): array
|
||||
{
|
||||
return ['readonly' => false, 'destructive' => false, 'idempotent' => false];
|
||||
}
|
||||
|
||||
public function isDirect(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function permission(array $input): bool|WP_Error
|
||||
{
|
||||
$type = PostGuard::postType($input['post_type'] ?? null, $this->schema, 'create_posts');
|
||||
|
||||
return $type instanceof WP_Error ? $type : true;
|
||||
}
|
||||
|
||||
public function execute(array $input): mixed
|
||||
{
|
||||
$type = PostGuard::postType($input['post_type'] ?? null, $this->schema, 'create_posts');
|
||||
if ($type instanceof WP_Error) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
$postType = (string) $input['post_type'];
|
||||
$prepared = $this->writer->prepare($postType, $input, true);
|
||||
if ($prepared instanceof WP_Error) {
|
||||
return $prepared;
|
||||
}
|
||||
|
||||
$data = array_merge($this->writer->postData($input), [
|
||||
'post_type' => $postType,
|
||||
'post_status' => 'draft',
|
||||
'post_author' => get_current_user_id(),
|
||||
]);
|
||||
$postId = wp_insert_post(wp_slash($data), true);
|
||||
if ($postId instanceof WP_Error) {
|
||||
return Errors::invalidInput('WordPress hat den Eintrag abgelehnt: ' . $postId->get_error_message());
|
||||
}
|
||||
|
||||
$postId = (int) $postId;
|
||||
try {
|
||||
$this->writer->writeExtras($postId, $postType, $prepared, $input);
|
||||
} catch (Throwable $e) {
|
||||
$this->discard($postId, $e);
|
||||
}
|
||||
do_action('lit_mcp_after_save', $postId, $this->name(), $input);
|
||||
|
||||
$post = get_post($postId);
|
||||
$title = $post instanceof WP_Post ? (string) $post->post_title : (string) $input['title'];
|
||||
$summary = sprintf('Entwurf "%s" angelegt', $title);
|
||||
$logId = $this->log->record($this->name(), $postType, (string) $postId, $summary);
|
||||
|
||||
return ToolResult::ok(sprintf('"%s" als Entwurf angelegt (ID %d).', $title, $postId))
|
||||
->changed((string) $postId, $postType, 'created', $title)
|
||||
->rev($post instanceof WP_Post ? Rev::ofPost($post) : null)
|
||||
->logId($logId)
|
||||
->urls((string) get_preview_post_link($postId), (string) get_edit_post_link($postId, 'raw'))
|
||||
->warnings($prepared['warnings'])
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the draft this call just created (it cannot have been used anywhere yet).
|
||||
*
|
||||
* @throws RuntimeException Always – the original failure, noting whether the draft is gone.
|
||||
*/
|
||||
private function discard(int $postId, Throwable $cause): never
|
||||
{
|
||||
$deleted = wp_delete_post($postId, true);
|
||||
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
'Extras of new post %d could not be written (%s); draft %s.',
|
||||
$postId,
|
||||
$cause->getMessage(),
|
||||
$deleted ? 'deleted again' : 'could NOT be deleted'
|
||||
),
|
||||
0,
|
||||
$cause
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user