refactor: PostChangeResult for the after-save block of the post tools

after_save hook, re-read, change log and ToolResult (rev, log_id, warnings)
were repeated in nine post abilities; they now share one helper. Behaviour
and warning texts are unchanged.

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:
2026-09-26 01:22:34 +02:00
co-authored by Claude Opus 5.5
parent d831cf59ca
commit e57b53d0e5
15 changed files with 231 additions and 191 deletions
@@ -6,11 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Content;
use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use Throwable;
@@ -136,29 +135,15 @@ final class ContentCreateAbility implements Ability
private function result(int $postId, string $postType, array $input, array $warnings): array
{
// The draft is saved – failures from here on are logged and reported as warnings only.
FollowUp::run(
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; der Entwurf ist gespeichert.',
$warnings
);
$post = get_post($postId);
$change = (new PostChangeResult($this->name(), $this->log, $postId, 'der Entwurf', $warnings))
->afterSave($input);
$post = $change->reread();
$title = $post instanceof WP_Post ? (string) $post->post_title : (string) $input['title'];
$summary = sprintf('Entwurf "%s" angelegt', $title);
$logId = FollowUp::run(
fn () => $this->log->record($this->name(), $postType, (string) $postId, $summary),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; der Entwurf ist gespeichert.',
$warnings
);
$change->log($postType, sprintf('Entwurf "%s" angelegt', $title));
return ToolResult::ok(sprintf('"%s" als Entwurf angelegt (ID %d).', $title, $postId))
return $change->result(sprintf('"%s" als Entwurf angelegt (ID %d).', $title, $postId), $post)
->changed((string) $postId, $postType, 'created', $title)
->rev($post instanceof WP_Post ? Rev::ofPost($post) : null)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($postId), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray();
}
@@ -6,11 +6,11 @@ namespace LIT\MCP\Integrations\WordPress\Content;
use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\RevisionPoint;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
@@ -178,31 +178,17 @@ final class ContentUpdateAbility implements Ability
$postType = (string) $post->post_type;
// The change is saved – failures from here on are logged and reported as warnings only.
FollowUp::run(
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId);
$updated = $updated instanceof WP_Post ? $updated : $post;
$change = (new PostChangeResult($this->name(), $this->log, $postId, 'die Änderung', $warnings))
->afterSave($input);
$updated = $change->reread($post) ?? $post;
$changedKeys = array_keys(array_intersect_key($input, array_flip(self::CHANGE_KEYS)));
$summary = sprintf('"%s" geändert: %s', $updated->post_title, implode(', ', $changedKeys));
$logId = FollowUp::run(
fn () => $this->log->record($this->name(), $postType, (string) $postId, $summary, $revisionId),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$change->log($postType, $summary, $revisionId);
return ToolResult::ok(sprintf('%s (Status unverändert: %s).', $summary, $updated->post_status))
return $change->result(sprintf('%s (Status unverändert: %s).', $summary, $updated->post_status), $updated)
->changed((string) $postId, $postType, 'updated', (string) $updated->post_title)
->rev(Rev::ofPost($updated))
->revisionId($revisionId)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray();
}